View Javadoc

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