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.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.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 WireFeed (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 WireFeedParser interface).
41   * <p>
42   * The WireFeedInput useds liberal parsers.
43   * <p>
44   * @author Alejandro Abdelnur
45   *
46   */
47  public class WireFeedInput {
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 WireFeed 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 WireFeedInput instance with input validation turned off.
66       * <p>
67       *
68       */
69      public WireFeedInput() {
70          this (false);
71      }
72  
73      /***
74       * Creates a WireFeedInput instance.
75       * <p>
76       * @param validate indicates if the input should be validated. NOT IMPLEMENTED YET (validation does not happen)
77       *
78       */
79      public WireFeedInput(boolean validate) {
80          _validate = false; // TODO FIX THIS THINGY
81      }
82  
83      /***
84       * Builds an WireFeed (RSS or Atom) from a file.
85       * <p>
86       * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
87       * <p>
88       * @param file file to read to create the WireFeed.
89       * @return the WireFeed read from the file.
90       * @throws FileNotFoundException thrown if the file could not be found.
91       * @throws IOException thrown if there is problem reading the file.
92       * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
93       * @throws FeedException if the feed could not be parsed
94       *
95       */
96      public WireFeed build(File file) throws FileNotFoundException,IOException,IllegalArgumentException,FeedException {
97          WireFeed feed;
98          Reader reader = new FileReader(file);
99          feed = build(reader);
100         reader.close();
101         return feed;
102     }
103 
104     /***
105      * Builds an WireFeed (RSS or Atom) from an InputStream.
106      * <p>
107      * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
108      * <p>
109      * @param is InputStream to read to create the WireFeed.
110      * @return the WireFeed read from the InputStream.
111      * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
112      * @throws FeedException if the feed could not be parsed
113      *
114      */
115     public WireFeed build(InputStream is) throws IllegalArgumentException,FeedException {
116         return build(new InputStreamReader(is));
117     }
118 
119     /***
120      * Builds an WireFeed (RSS or Atom) from an Reader.
121      * <p>
122      * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
123      * <p>
124      * @param reader Reader to read to create the WireFeed.
125      * @return the WireFeed read from the Reader.
126      * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
127      * @throws FeedException if the feed could not be parsed
128      *
129      */
130     public WireFeed build(Reader reader) throws IllegalArgumentException,FeedException {
131         SAXBuilder saxBuilder = new SAXBuilder(_validate);
132         try {
133             Document document = saxBuilder.build(reader);
134             return build(document);
135         }
136         catch (Exception ex) {
137             throw new FeedException("Invalid XML",ex);
138         }
139     }
140 
141     /***
142      * Builds an WireFeed (RSS or Atom) from an W3C SAX InputSource.
143      * <p>
144      * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
145      * <p>
146      * @param is W3C SAX InputSource to read to create the WireFeed.
147      * @return the WireFeed read from the W3C SAX InputSource.
148      * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
149      * @throws FeedException if the feed could not be parsed
150      *
151      */
152     public WireFeed build(InputSource is) throws IllegalArgumentException,FeedException {
153         SAXBuilder saxBuilder = new SAXBuilder(_validate);
154         try {
155             Document document = saxBuilder.build(is);
156             return build(document);
157         }
158         catch (Exception ex) {
159             throw new FeedException("Invalid XML",ex);
160         }
161     }
162 
163     /***
164      * Builds an WireFeed (RSS or Atom) from an W3C DOM document.
165      * <p>
166      * NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom.Document)'.
167      * <p>
168      * @param document W3C DOM document to read to create the WireFeed.
169      * @return the WireFeed read from the W3C DOM document.
170      * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
171      * @throws FeedException if the feed could not be parsed
172      *
173      */
174     public WireFeed build(org.w3c.dom.Document document) throws IllegalArgumentException,FeedException {
175         DOMBuilder domBuilder = new DOMBuilder();
176         try {
177             Document jdomDoc = domBuilder.build(document);
178             return build(jdomDoc);
179         }
180         catch (Exception ex) {
181             throw new FeedException("Invalid XML",ex);
182         }
183     }
184 
185     /***
186      * Builds an WireFeed (RSS or Atom) from an JDOM document.
187      * <p>
188      * NOTE: All other build methods delegate to this method.
189      * <p>
190      * @param document JDOM document to read to create the WireFeed.
191      * @return the WireFeed read from the JDOM document.
192      * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
193      * @throws FeedException if the feed could not be parsed
194      *
195      */
196     public WireFeed build(Document document) throws IllegalArgumentException,FeedException {
197         WireFeedParser parser = FEED_PARSERS.getParserFor(document);
198         if (parser==null) {
199             throw new IllegalArgumentException("Invalid document");
200         }
201         return parser.parse(document, _validate);
202     }
203 
204 }