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.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 WireFeed (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 WireFeedOutput {
46 private final static FeedGenerators GENERATORS = new FeedGenerators();
47
48 /***
49 * Returns the list of supported output feed types.
50 * <p>
51 * @see WireFeed 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 /***
61 * Creates a FeedOuput instance.
62 * <p>
63 *
64 */
65 public WireFeedOutput() {
66 }
67
68 /***
69 * Creates a String with the XML representation for the given WireFeed.
70 * <p>
71 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
72 * <p>
73 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
74 * the type given to the FeedOuptut constructor.
75 * @return a String with the XML representation for the given WireFeed.
76 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
77 * @throws FeedException thrown if the XML representation for the feed could not be created.
78 *
79 */
80 public String outputString(WireFeed feed) throws IllegalArgumentException,FeedException {
81 Document doc = outputJDom(feed);
82 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
83 return outputter.outputString(doc);
84 }
85
86 /***
87 * Creates a File containing with the XML representation for the given WireFeed.
88 * <p>
89 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
90 * <p>
91 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
92 * the type given to the FeedOuptut constructor.
93 * @param file the file where to write the XML representation for the given WireFeed.
94 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
95 * @throws IOException thrown if there was some problem writing to the File.
96 * @throws FeedException thrown if the XML representation for the feed could not be created.
97 *
98 */
99 public void output(WireFeed feed,File file) throws IllegalArgumentException,IOException,FeedException {
100 FileOutputStream os = new FileOutputStream(file);
101 output(feed,os);
102 os.close();
103 }
104
105 /***
106 * Writes to an OutputStream the XML representation for the given WireFeed.
107 * <p>
108 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
109 * <p>
110 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
111 * the type given to the FeedOuptut constructor.
112 * @param os OutputStream to write the XML representation for the given WireFeed.
113 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
114 * @throws IOException thrown if there was some problem writing to the OutputStream.
115 * @throws FeedException thrown if the XML representation for the feed could not be created.
116 *
117 */
118 public void output(WireFeed feed,OutputStream os) throws IllegalArgumentException,IOException, FeedException {
119 output(feed,new OutputStreamWriter(os));
120 }
121
122 /***
123 * Writes to an Writer the XML representation for the given WireFeed.
124 * <p>
125 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
126 * <p>
127 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
128 * the type given to the FeedOuptut constructor.
129 * @param writer Writer to write the XML representation for the given WireFeed.
130 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
131 * @throws IOException thrown if there was some problem writing to the Writer.
132 * @throws FeedException thrown if the XML representation for the feed could not be created.
133 *
134 */
135 public void output(WireFeed feed,Writer writer) throws IllegalArgumentException,IOException, FeedException {
136 Document doc = outputJDom(feed);
137 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
138 outputter.output(doc,writer);
139 }
140
141 /***
142 * Creates a W3C DOM document for the given WireFeed.
143 * <p>
144 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
145 * <p>
146 * @param feed Abstract feed to create W3C DOM document from. The type of the WireFeed must match
147 * the type given to the FeedOuptut constructor.
148 * @return the W3C DOM document for the given WireFeed.
149 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
150 * @throws FeedException thrown if the W3C DOM document for the feed could not be created.
151 *
152 */
153 public org.w3c.dom.Document outputW3CDom(WireFeed feed) throws IllegalArgumentException,FeedException {
154 Document doc = outputJDom(feed);
155 DOMOutputter outputter = new DOMOutputter();
156 try {
157 return outputter.output(doc);
158 }
159 catch (JDOMException jdomEx) {
160 throw new FeedException("Could not create DOM",jdomEx);
161 }
162 }
163
164 /***
165 * Creates a JDOM document for the given WireFeed.
166 * <p>
167 * NOTE: All other output methods delegate to this method.
168 * <p>
169 * @param feed Abstract feed to create JDOM document from. The type of the WireFeed must match
170 * the type given to the FeedOuptut constructor.
171 * @return the JDOM document for the given WireFeed.
172 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
173 * @throws FeedException thrown if the JDOM document for the feed could not be created.
174 *
175 */
176 public Document outputJDom(WireFeed feed) throws IllegalArgumentException,FeedException {
177 String type = feed.getFeedType();
178 WireFeedGenerator generator = GENERATORS.getGenerator(type);
179 if (generator==null) {
180 throw new IllegalArgumentException("Invalid feed type ["+type+"]");
181 }
182
183 if (!generator.getType().equals(type)) {
184 throw new IllegalArgumentException("WireFeedOutput type["+type+"] and WireFeed type ["+
185 type+"] don't match");
186 }
187 return generator.generate(feed);
188 }
189
190 }