001: //$Id: RSSParser.java,v 1.9 2004/03/28 13:07:16 taganaka Exp $
002: package org.gnu.stealthp.rsslib;
003:
004: import org.xml.sax.*;
005: import javax.xml.parsers.*;
006: import org.xml.sax.helpers.*;
007: import java.io.*;
008: import java.net.*;
009:
010: /**
011: * RSS Parser.
012: *
013: * <blockquote>
014: * <em>This module, both source code and documentation, is in the
015: * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
016: * </blockquote>
017: *
018: * @since RSSLIB4J 0.1
019: * @author Francesco aka 'Stealthp' stealthp[@]stealthp.org
020: * @version 0.2
021: */
022:
023: public class RSSParser {
024:
025: private SAXParserFactory factory = RSSFactory.getInstance();
026: private DefaultHandler hnd;
027: private File f;
028: private URL u;
029: private InputStream in;
030: private boolean validate;
031:
032: public RSSParser() {
033: validate = false;
034: }
035:
036: /**
037: * Set the event handler
038: * @param h the DefaultHandler
039: *
040: */
041: public void setHandler(DefaultHandler h) {
042: hnd = h;
043: }
044:
045: /**
046: * Set rss resource by local file name
047: * @param file_name loca file name
048: * @throws RSSException
049: */
050: public void setXmlResource(String file_name) throws RSSException {
051: f = new File(file_name);
052: try {
053: in = new FileInputStream(f);
054: } catch (Exception e) {
055: throw new RSSException("RSSParser::setXmlResource fails: "
056: + e.getMessage());
057: }
058:
059: }
060:
061: /**
062: * Set rss resource by URL
063: * @param ur the remote url
064: * @throws RSSException
065: */
066: public void setXmlResource(URL ur) throws RSSException {
067: u = ur;
068: File ft = null;
069: try {
070: URLConnection con = u.openConnection();
071: in = u.openStream();
072: if (con.getContentLength() == -1) {
073: this .fixZeroLength();
074: }
075:
076: } catch (IOException e) {
077: throw new RSSException("RSSParser::setXmlResource fails: "
078: + e.getMessage());
079: }
080: }
081:
082: /**
083: * set true if parse have to validate the document
084: * defoult is false
085: * @param b true or false
086: */
087: public void setValidate(boolean b) {
088: validate = b;
089: }
090:
091: /**
092: * Parse rss file
093: * @param filename local file name
094: * @param handler the handler
095: * @param validating validate document??
096: * @throws RSSException
097: */
098: public static void parseXmlFile(String filename,
099: DefaultHandler handler, boolean validating)
100: throws RSSException {
101: RSSParser p = new RSSParser();
102: p.setXmlResource(filename);
103: p.setHandler(handler);
104: p.setValidate(validating);
105: p.parse();
106: }
107:
108: /**
109: * Parse rss file from a url
110: * @param remote_url remote rss file
111: * @param handler the handler
112: * @param validating validate document??
113: * @throws RSSException
114: */
115: public static void parseXmlFile(URL remote_url,
116: DefaultHandler handler, boolean validating)
117: throws RSSException {
118: RSSParser p = new RSSParser();
119: p.setXmlResource(remote_url);
120: p.setHandler(handler);
121: p.setValidate(validating);
122: p.parse();
123: }
124:
125: /**
126: * Try to fix null length bug
127: * @throws IOException
128: * @throws RSSException
129: */
130: private void fixZeroLength() throws IOException, RSSException {
131:
132: File ft = File.createTempFile(".rsslib4jbugfix", ".tmp");
133: ft.deleteOnExit();
134: FileWriter fw = new FileWriter(ft);
135: BufferedReader reader = new BufferedReader(
136: new InputStreamReader(in));
137: BufferedWriter out = new BufferedWriter(fw);
138: String line = "";
139: while ((line = reader.readLine()) != null) {
140: out.write(line + "\n");
141: }
142: out.flush();
143: out.close();
144: reader.close();
145: fw.close();
146: setXmlResource(ft.getAbsolutePath());
147:
148: }
149:
150: /**
151: * Call it at the end of the work to preserve memory
152: */
153: public void free() {
154: this .factory = null;
155: this .f = null;
156: this .in = null;
157: this .hnd = null;
158: System.gc();
159: }
160:
161: /**
162: * Parse the documen
163: * @throws RSSException
164: */
165: public void parse() throws RSSException {
166: try {
167: factory.setValidating(validate);
168: // Create the builder and parse the file
169: factory.newSAXParser().parse(in, hnd);
170: } catch (SAXException e) {
171: throw new RSSException("RSSParser::parse fails: "
172: + e.getMessage());
173: } catch (ParserConfigurationException e) {
174: throw new RSSException("RSSParser::parse fails: "
175: + e.getMessage());
176: } catch (IOException e) {
177: throw new RSSException("RSSParser::parse fails: "
178: + e.getMessage());
179: }
180:
181: }
182:
183: }
|