001: /**********************************************************************************
002:
003: Feedzeo!
004: A free and open source RSS/Atom/RDF feed aggregator
005:
006: Copyright (C) 2005-2006 Anand Rao (anandrao@users.sourceforge.net)
007:
008: This library is free software; you can redistribute it and/or
009: modify it under the terms of the GNU Lesser General Public
010: License as published by the Free Software Foundation; either
011: version 2.1 of the License, or (at your option) any later version.
012:
013: This library is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
021:
022: ************************************************************************************/package layout;
023:
024: import java.io.*;
025:
026: import util.*;
027: import ui.*;
028: import data.DataElement;
029: import app.SysObjects;
030:
031: /**
032: *
033: * @author Anand Rao
034: */
035: public class PageGenerator {
036: private HTMLPage page;
037: private PageRequest req;
038: private String TableInfo; /* holds the raw HTML table data for the page */
039:
040: private void populateCell(TableLayout t, CellElement c, int row,
041: int col, int NumCols) {
042: int index = 0;
043: String data = null;
044: if (c.IsTable()) {
045: if (t.IsTableListType()) {
046: // table is a list of table items
047: // sample a cell of the table and depending on its type
048: // set the appropriate index
049: String CellDataType = c.getTable().getCell(0, 0)
050: .getDataType();
051: if (DataElement.IsItemData(CellDataType))
052: req.ItemIndex = row * NumCols + col;
053: }
054: boolean cellhasData = populateTable(c.getTable());
055: if (cellhasData)
056: t.setCellStatus(row, col, true);
057: } else { // normal cell with datatype given by user
058: if (DataElement.IsCategoryData(c.getDataType())) {
059: data = SysObjects.getDataManager().getCategoryData(
060: c.getDataType(), req.getFeedCategory());
061: c.setData(data);
062: } else if (DataElement.IsChannelData(c.getDataType())) {
063: data = SysObjects.getDataManager().getChannelData(
064: c.getDataType(), req.getFeedCategory(),
065: req.getFeedSourceURL());
066: c.setData(data);
067: } else if (DataElement.IsItemData(c.getDataType())) {
068: if (t.IsTableListType()) {
069: index = row * NumCols + col;
070: data = SysObjects.getDataManager().getItemData(
071: c.getDataType(), req.getFeedCategory(),
072: req.getFeedSourceURL(), index);
073: c.setData(data);
074: } else {
075: index = req.ItemIndex;
076: data = SysObjects.getDataManager().getItemData(
077: c.getDataType(), req.getFeedCategory(),
078: req.getFeedSourceURL(), index);
079: c.setData(data);
080: }
081: }
082:
083: if (data.length() > 0) // cell with non-empty data
084: t.setCellStatus(row, col, true);
085: }
086: }
087:
088: /*
089: * returns true if atleast one of the table's cell contains
090: * non-empty data; else returns false
091: */
092: private boolean populateTable(TableLayout t) {
093: //System.out.println("POPULATE TABLE "+t.getName());
094: // set table content to empty
095: t.setTableEmpty();
096: int NumRows = t.getNumRows();
097: int NumCols = t.getNumCols();
098: for (int i = 0; i < NumRows; i++)
099: for (int j = 0; j < NumCols; j++)
100: populateCell(t, t.getCell(i, j), i, j, NumCols);
101: boolean tableEmpty = t.IsTableEmpty();
102: if (tableEmpty)
103: return false;
104: return true;
105: }
106:
107: private String generateTableInfo() {
108: String PageName = req.getPageLayoutName();
109: PageLayout Page = SysObjects.getLayoutManager().getPageLayout(
110: PageName);
111: populateTable(Page.getMainTable());
112: return Page.getMainTable().toHTML();
113: }
114:
115: /** Creates a new instance of PageGenerator */
116: public PageGenerator(PageRequest req) {
117: this .req = req;
118: }
119:
120: /*
121: * Sets the link to the HTML page just generated into the appropriate
122: * Data object; Currently this is only set in ChannelData object
123: */
124: private void setHTMLPageLinkInfo() {
125: SysObjects.getDataManager().setChannelHTMLPageLink(
126: req.getFeedCategory(), req.getFeedSourceURL(),
127: req.getHTMLFileName());
128: }
129:
130: private void setHTMLTableDataLinkInfo() {
131: SysObjects.getDataManager().setChannelHTMLTableDataPageLink(
132: req.getFeedCategory(), req.getFeedSourceURL(),
133: req.getHTMLTableDataFileName());
134: }
135:
136: /* compiles the HTML tabledata and keeps it ready to be used later
137: while generating HTML page
138: */
139: public void preparePageInfo() {
140: TableInfo = generateTableInfo();
141: }
142:
143: public void generateHTMLPage() {
144: if (TableInfo == null) {
145: System.out
146: .println("TableInfo not found! unable to generate "
147: + req.getFullHTMLFileName());
148: return;
149: }
150: page = new HTMLPage(req.getFullHTMLFileName());
151: page.setTableData(TableInfo);
152: // TODO: set css file in pagelayout object
153: page.addStyleSheetFile("style.css");
154: page.writePage();
155: page.closePage();
156: // update the datamanager with the link of the page
157: // we just generated
158: setHTMLPageLinkInfo();
159: }
160:
161: public void generateHTMLTableDataPage() {
162: if (TableInfo == null) {
163: System.out
164: .println("TableInfo not found! unable to generate "
165: + req.getFullHTMLTableDataFileName());
166: return;
167: }
168: String outputFile = req.getFullHTMLTableDataFileName();
169: PrintWriter pw = null;
170: try {
171: pw = new PrintWriter(new BufferedWriter(new FileWriter(
172: outputFile, false)));
173: } catch (Exception e) {
174: ExceptionUtil.reportException(e);
175: return;
176: }
177: pw.println(TableInfo);
178: pw.close();
179: //update datamanager about this info
180: setHTMLTableDataLinkInfo();
181: }
182: }
|