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 util;
023:
024: import java.io.*;
025:
026: /*
027: * This class is used to parse layout objects like tables and pages;
028: * It uses the basic config file parser for this;
029: * Since both types of objects are present in the same file, the table
030: * and pages are differentiated using type property of the object
031: * eg:
032: * t1
033: * {
034: * type=table;
035: * rows=10;
036: * cols=10;
037: * cell(0,0)=....
038: * cell(0,1)=....
039: * }
040: *
041: * p1
042: * {
043: * type=page;
044: * table=t1;
045: * }
046: *
047: */
048:
049: /**
050: *
051: * @author Anand Rao
052: */
053: public class LayoutInputFileParser extends ConfigFileParser {
054:
055: private final boolean DEBUG_CLASS = false;
056:
057: private int NumTables = 0, NumPages = 0;
058: private int TableStartIndex = -1, PageStartIndex = -1; /*
059: * Index from which Tables or page
060: * starts; table and page objects
061: * cannot be mixed;
062: * TableStartIndex points to first
063: * Table object;
064: * PageStartIndex points to first
065: * page object;
066: */
067:
068: private boolean IsTable(String objname) {
069: String line;
070: initGroupCursor(objname);
071: while ((line = getNextGroupItem(objname)) != null) {
072: NameValueListPair nvp = new NameValueListPair(line);
073:
074: if (DEBUG_CLASS)
075: System.out.println("line:" + line + "Name:"
076: + nvp.getName() + " Value:"
077: + nvp.getValueList());
078:
079: if (nvp.getName().equalsIgnoreCase("type")) {
080: // we have a type property
081: if (nvp.getValueList().equalsIgnoreCase("table"))
082: return true; // we found a table
083: else if (nvp.getValueList().equalsIgnoreCase("page"))
084: return false;
085: }
086: }
087: ExceptionUtil.reportException(new Exception(
088: "Error parsing table/page element"));
089: return false;
090: }
091:
092: /** Creates a new instance of LayoutInputFileParser */
093: public LayoutInputFileParser(String Path)
094: throws FileNotFoundException {
095: super (Path);
096: for (int i = 0; i < getNumGroups(); i++) {
097: String Grpname = getGroup(i);
098: if (DEBUG_CLASS)
099: System.out.println("Layout Group:" + Grpname);
100: if (IsTable(Grpname)) {
101: // set the TableStartIndex if we have not set yet
102: if (TableStartIndex == -1)
103: TableStartIndex = i;
104: NumTables++;
105: } else { // must be a page
106: // set the PageStartIndex if we have not set yet
107: if (PageStartIndex == -1)
108: PageStartIndex = i;
109: NumPages++;
110: }
111: }
112: }
113:
114: // TableLayout related APIS
115: public void initTableCursor(String Tablename) {
116: initGroupCursor(Tablename);
117: }
118:
119: public String getNextTableElement(String Tablename) {
120: try {
121: String NextItem = getNextGroupItem(Tablename);
122: return NextItem;
123: } catch (Exception e) {
124: ExceptionUtil.reportException(e);
125: return null;
126: }
127: }
128:
129: public int getNumTables() {
130: return NumTables;
131: }
132:
133: public String getTable(int index) {
134: return getGroup(index + TableStartIndex);
135: }
136:
137: // PageLayout related APIS
138: public void initPageCursor(String Pagename) {
139: initGroupCursor(Pagename);
140: }
141:
142: public String getNextPageElement(String Pagename) {
143: try {
144: String nextItem = getNextGroupItem(Pagename);
145: return nextItem;
146: } catch (Exception e) {
147: ExceptionUtil.reportException(e);
148: return null;
149: }
150: }
151:
152: public int getNumPages() {
153: return NumPages;
154: }
155:
156: public String getPage(int index) {
157: return getGroup(index + PageStartIndex);
158: }
159:
160: public void closeParser() {
161: CloseFile();
162: }
163:
164: }
|