1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.io;
18
19 import com.sun.syndication.feed.WireFeed;
20 import com.sun.syndication.io.impl.FeedParsers;
21 import org.jdom.Document;
22 import org.jdom.input.DOMBuilder;
23 import org.jdom.input.SAXBuilder;
24 import org.xml.sax.InputSource;
25
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.FileReader;
29 import java.io.Reader;
30 import java.io.IOException;
31 import java.util.List;
32
33 /***
34 * Parses an XML document (File, InputStream, Reader, W3C SAX InputSource, W3C DOM Document or JDom DOcument)
35 * into an WireFeed (RSS/Atom).
36 * <p>
37 * It accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
38 * Atom 0.3 feeds. Parsers are plugable (they must implement the WireFeedParser interface).
39 * <p>
40 * The WireFeedInput useds liberal parsers.
41 * <p>
42 * @author Alejandro Abdelnur
43 *
44 */
45 public class WireFeedInput {
46 private static FeedParsers FEED_PARSERS = new FeedParsers();
47
48 private boolean _validate;
49
50 /***
51 * Returns the list of supported input feed types.
52 * <p>
53 * @see WireFeed for details on the format of these strings.
54 * <p>
55 * @return a list of String elements with the supported input feed types.
56 *
57 */
58 public static List getSupportedFeedTypes() {
59 return FEED_PARSERS.getSupportedFeedTypes();
60 }
61
62 /***
63 * Creates a WireFeedInput instance with input validation turned off.
64 * <p>
65 *
66 */
67 public WireFeedInput() {
68 this (false);
69 }
70
71 /***
72 * Creates a WireFeedInput instance.
73 * <p>
74 * @param validate indicates if the input should be validated. NOT IMPLEMENTED YET (validation does not happen)
75 *
76 */
77 public WireFeedInput(boolean validate) {
78 _validate = false;
79 }
80
81 /***
82 * Builds an WireFeed (RSS or Atom) from a file.
83 * <p>
84 * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
85 * <p>
86 * @param file file to read to create the WireFeed.
87 * @return the WireFeed read from the file.
88 * @throws FileNotFoundException thrown if the file could not be found.
89 * @throws IOException thrown if there is problem reading the file.
90 * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
91 * @throws FeedException if the feed could not be parsed
92 *
93 */
94 public WireFeed build(File file) throws FileNotFoundException,IOException,IllegalArgumentException,FeedException {
95 WireFeed feed;
96 Reader reader = new FileReader(file);
97 feed = build(reader);
98 reader.close();
99 return feed;
100 }
101
102 /***
103 * Builds an WireFeed (RSS or Atom) from an Reader.
104 * <p>
105 * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
106 * <p>
107 * @param reader Reader to read to create the WireFeed.
108 * @return the WireFeed read from the Reader.
109 * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
110 * @throws FeedException if the feed could not be parsed
111 *
112 */
113 public WireFeed build(Reader reader) throws IllegalArgumentException,FeedException {
114 SAXBuilder saxBuilder = new SAXBuilder(_validate);
115
116
117
118
119 saxBuilder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
120 try {
121 Document document = saxBuilder.build(reader);
122 return build(document);
123 }
124 catch (Exception ex) {
125 throw new FeedException("Invalid XML",ex);
126 }
127 }
128
129 /***
130 * Builds an WireFeed (RSS or Atom) from an W3C SAX InputSource.
131 * <p>
132 * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
133 * <p>
134 * @param is W3C SAX InputSource to read to create the WireFeed.
135 * @return the WireFeed read from the W3C SAX InputSource.
136 * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
137 * @throws FeedException if the feed could not be parsed
138 *
139 */
140 public WireFeed build(InputSource is) throws IllegalArgumentException,FeedException {
141 SAXBuilder saxBuilder = new SAXBuilder(_validate);
142 try {
143 Document document = saxBuilder.build(is);
144 return build(document);
145 }
146 catch (Exception ex) {
147 throw new FeedException("Invalid XML",ex);
148 }
149 }
150
151 /***
152 * Builds an WireFeed (RSS or Atom) from an W3C DOM document.
153 * <p>
154 * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
155 * <p>
156 * @param document W3C DOM document to read to create the WireFeed.
157 * @return the WireFeed read from the W3C DOM document.
158 * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
159 * @throws FeedException if the feed could not be parsed
160 *
161 */
162 public WireFeed build(org.w3c.dom.Document document) throws IllegalArgumentException,FeedException {
163 DOMBuilder domBuilder = new DOMBuilder();
164 try {
165 Document jdomDoc = domBuilder.build(document);
166 return build(jdomDoc);
167 }
168 catch (Exception ex) {
169 throw new FeedException("Invalid XML",ex);
170 }
171 }
172
173 /***
174 * Builds an WireFeed (RSS or Atom) from an JDOM document.
175 * <p>
176 * NOTE: All other build methods delegate to this method.
177 * <p>
178 * @param document JDOM document to read to create the WireFeed.
179 * @return the WireFeed read from the JDOM document.
180 * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
181 * @throws FeedException if the feed could not be parsed
182 *
183 */
184 public WireFeed build(Document document) throws IllegalArgumentException,FeedException {
185 WireFeedParser parser = FEED_PARSERS.getParserFor(document);
186 if (parser==null) {
187 throw new IllegalArgumentException("Invalid document");
188 }
189 return parser.parse(document, _validate);
190 }
191
192 }