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