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.FeedGenerators;
21  import org.jdom.Document;
22  import org.jdom.JDOMException;
23  import org.jdom.output.DOMOutputter;
24  import org.jdom.output.Format;
25  import org.jdom.output.XMLOutputter;
26  
27  import java.io.IOException;
28  import java.io.OutputStream;
29  import java.io.OutputStreamWriter;
30  import java.io.Writer;
31  import java.io.File;
32  import java.io.FileOutputStream;
33  import java.util.List;
34  
35  /***
36   * Generates an XML document (String, File, OutputStream, Writer, W3C DOM document or JDOM document)
37   * out of an AbstractFeed (RSS/Atom).
38   * <p>
39   * It generates 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. Generators are plugable (they must implement the ModuleParser interface).
41   * <p>
42   * @author Alejandro Abdelnur
43   *
44   */
45  public class FeedOutput {
46      private final static FeedGenerators GENERATORS = new FeedGenerators();
47  
48      /***
49       * Returns the list of supported output feed types.
50       * <p>
51       * @see AbstractFeed for details on the format of these strings.
52       * <p>
53       * @return a list of String elements with the supported output feed types.
54       *
55       */
56      public static List getSupportedFeedTypes() {
57          return GENERATORS.getSupportedFeedTypes();
58      }
59  
60      private FeedGenerator _generator;
61  
62      /***
63       * Creates a FeedOuput instace for a given feed type.
64       * <p>
65       * @param type the type of feed to generate.
66       *
67       */
68      public FeedOutput(String type) {
69          _generator = GENERATORS.getGenerator(type);
70          if (_generator==null) {
71              throw new IllegalArgumentException("Invalid feed type ["+type+"]");
72          }
73      }
74  
75      /***
76       * Returns the type of feed the FeedOutput creates.
77       * <p>
78       * @return the type of feed this FeedOutput creates.
79       *
80       */
81      public String getType() {
82          return _generator.getType();
83      }
84  
85      /***
86       * Creates a String with the XML representation for the given AbstractFeed.
87       * <p>
88       * NOTE: This method delages to the 'Document FeedOutput#outputJDom(AbstractFeed)'.
89       * <p>
90       * @param feed Abstract feed to create XML representation from. The type of the AbstractFeed must match
91       *        the type given to the FeedOuptut constructor.
92       * @return a String with the XML representation for the given AbstractFeed.
93       * @throws IllegalArgumentException thrown if the feed type of the FeedOutput and AbstractFeed don't match.
94       * @throws FeedException thrown if the XML representation for the feed could not be created.
95       *
96       */
97      public String outputString(AbstractFeed feed) throws IllegalArgumentException,FeedException {
98          Document doc = outputJDom(feed);
99          XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
100         return outputter.outputString(doc);
101     }
102 
103     /***
104      * Creates a File containing with the XML representation for the given AbstractFeed.
105      * <p>
106      * NOTE: This method delages to the 'Document FeedOutput#outputJDom(AbstractFeed)'.
107      * <p>
108      * @param feed Abstract feed to create XML representation from. The type of the AbstractFeed must match
109      *        the type given to the FeedOuptut constructor.
110      * @param file the file where to write the XML representation for the given AbstractFeed.
111      * @throws IllegalArgumentException thrown if the feed type of the FeedOutput and AbstractFeed don't match.
112      * @throws IOException thrown if there was some problem writing to the File.
113      * @throws FeedException thrown if the XML representation for the feed could not be created.
114      *
115      */
116     public void output(AbstractFeed feed,File file) throws IllegalArgumentException,IOException,FeedException {
117         FileOutputStream os = new FileOutputStream(file);
118         output(feed,os);
119         os.close();
120     }
121 
122     /***
123      * Writes to an OutputStream the XML representation for the given AbstractFeed.
124      * <p>
125      * NOTE: This method delages to the 'Document FeedOutput#outputJDom(AbstractFeed)'.
126      * <p>
127      * @param feed Abstract feed to create XML representation from. The type of the AbstractFeed must match
128      *        the type given to the FeedOuptut constructor.
129      * @param os OutputStream to write the XML representation for the given AbstractFeed.
130      * @throws IllegalArgumentException thrown if the feed type of the FeedOutput and AbstractFeed don't match.
131      * @throws IOException thrown if there was some problem writing to the OutputStream.
132      * @throws FeedException thrown if the XML representation for the feed could not be created.
133      *
134      */
135     public void output(AbstractFeed feed,OutputStream os) throws IllegalArgumentException,IOException, FeedException {
136         output(feed,new OutputStreamWriter(os));
137     }
138 
139     /***
140      * Writes to an Writer the XML representation for the given AbstractFeed.
141      * <p>
142      * NOTE: This method delages to the 'Document FeedOutput#outputJDom(AbstractFeed)'.
143      * <p>
144      * @param feed Abstract feed to create XML representation from. The type of the AbstractFeed must match
145      *        the type given to the FeedOuptut constructor.
146      * @param writer Writer to write the XML representation for the given AbstractFeed.
147      * @throws IllegalArgumentException thrown if the feed type of the FeedOutput and AbstractFeed don't match.
148      * @throws IOException thrown if there was some problem writing to the Writer.
149      * @throws FeedException thrown if the XML representation for the feed could not be created.
150      *
151      */
152     public void output(AbstractFeed feed,Writer writer) throws IllegalArgumentException,IOException, FeedException {
153         Document doc = outputJDom(feed);
154         XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
155         outputter.output(doc,writer);
156     }
157 
158     /***
159      * Creates a W3C DOM document for the given AbstractFeed.
160      * <p>
161      * NOTE: This method delages to the 'Document FeedOutput#outputJDom(AbstractFeed)'.
162      * <p>
163      * @param feed Abstract feed to create W3C DOM document from. The type of the AbstractFeed must match
164      *        the type given to the FeedOuptut constructor.
165      * @return the W3C DOM document for the given AbstractFeed.
166      * @throws IllegalArgumentException thrown if the feed type of the FeedOutput and AbstractFeed don't match.
167      * @throws FeedException thrown if the W3C DOM document for the feed could not be created.
168      *
169      */
170     public org.w3c.dom.Document ouptutW3CDom(AbstractFeed feed) throws IllegalArgumentException,FeedException {
171         Document doc = outputJDom(feed);
172         DOMOutputter outputter = new DOMOutputter();
173         try {
174             return outputter.output(doc);
175         }
176         catch (JDOMException jdomEx) {
177             throw new FeedException("Could not create DOM",jdomEx);
178         }
179     }
180 
181     /***
182      * Creates a JDOM document for the given AbstractFeed.
183      * <p>
184      * NOTE: All other output methods delegate to this method.
185      * <p>
186      * @param feed Abstract feed to create JDOM document from. The type of the AbstractFeed must match
187      *        the type given to the FeedOuptut constructor.
188      * @return the JDOM document for the given AbstractFeed.
189      * @throws IllegalArgumentException thrown if the feed type of the FeedOutput and AbstractFeed don't match.
190      * @throws FeedException thrown if the JDOM document for the feed could not be created.
191      *
192      */
193     public Document outputJDom(AbstractFeed feed) throws IllegalArgumentException,FeedException {
194         if (!_generator.getType().equals(feed.getType())) {
195             throw new IllegalArgumentException("FeedOutput type["+_generator.getType()+
196                                                "] and AbstractFeed type ["+feed.getType()+"] don't match");
197         }
198         return _generator.generate(feed);
199     }
200 
201 }