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 css;
023:
024: import util.*;
025: import java.io.*;
026: import java.util.*;
027:
028: /**
029: *
030: * @author Anand Rao
031: */
032: public class StyleManager {
033:
034: private String StyleInfoFile;
035:
036: /** Creates a new instance of StyleManager */
037: public StyleManager(String filename) {
038: StyleInfoFile = filename;
039: }
040:
041: public synchronized void generateCSSFile(String CSSFile) {
042:
043: try {
044:
045: StyleInputFileParser styleParser = new StyleInputFileParser(
046: StyleInfoFile);
047: CSSWriter cw = new CSSWriter(CSSFile);
048:
049: for (int i = 0; i < styleParser.getNumElements(); i++) {
050: String prop;
051: String StyleElem = styleParser.getElement(i);
052: CSSSelector selector;
053: selector = new CSSSelector("." + StyleElem);
054: styleParser.initPropertyListCursor(StyleElem);
055: while ((prop = styleParser.getNextProperty(StyleElem)) != null) {
056: NameValueListPair nvp = new NameValueListPair(prop);
057: CSSProperty p = new CSSProperty(nvp.getName(), nvp
058: .getValueList());
059: selector.addProperty(p);
060: }
061: cw.addSelector(selector);
062: }
063: cw.writePage();
064: cw.closePage();
065: styleParser.closeParser();
066:
067: } catch (Exception e) {
068: ExceptionUtil.reportException(e);
069: }
070: }
071:
072: /*
073: * API reads the Styleinfo file contents and returns it in a
074: * String object
075: */
076: public synchronized String getStyleInfo() {
077: StringBuffer sb = new StringBuffer();
078: String line;
079: try {
080: BufferedReader freader = new BufferedReader(new FileReader(
081: StyleInfoFile));
082: while ((line = freader.readLine()) != null) {
083: sb.append(line);
084: sb.append("\r\n");
085: }
086: freader.close();
087: return sb.toString();
088: } catch (Exception e) {
089: ExceptionUtil.reportException(e);
090: return null;
091: }
092: }
093:
094: /*
095: * API to save Styleinfo data;
096: * NOTE: TODO - validate the data passed
097: */
098: public synchronized int saveStyleInfo(String data) {
099: try {
100: RandomAccessFile file = new RandomAccessFile(StyleInfoFile,
101: "rwd");
102: file.writeBytes(data);
103: file.setLength(file.getFilePointer());
104: file.close();
105: return 0;
106: } catch (Exception e) {
107: ExceptionUtil.reportException(e);
108: return -1;
109: }
110: }
111: }
|