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 * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
70 * of the developer to ensure that if the String is written to a character stream the stream charset is the same as
71 * the feed encoding property.
72 * <p>
73 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
74 * <p>
75 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
76 * the type given to the FeedOuptut constructor.
77 * @return a String with the XML representation for the given WireFeed.
78 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
79 * @throws FeedException thrown if the XML representation for the feed could not be created.
80 *
81 */
82 public String outputString(WireFeed feed) throws IllegalArgumentException,FeedException {
83 Document doc = outputJDom(feed);
84 String encoding = feed.getEncoding();
85 Format format = Format.getPrettyFormat();
86 if (encoding!=null) {
87 format.setEncoding(encoding);
88 }
89 XMLOutputter outputter = new XMLOutputter(format);
90 return outputter.outputString(doc);
91 }
92
93 /***
94 * Creates a File containing with the XML representation for the given WireFeed.
95 * <p>
96 * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. The platform
97 * default charset encoding is used to write the feed to the file. It is the responsibility
98 * of the developer to ensure the feed encoding is set to the platform charset encoding.
99 * <p>
100 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
101 * <p>
102 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
103 * the type given to the FeedOuptut constructor.
104 * @param file the file where to write the XML representation for the given WireFeed.
105 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
106 * @throws IOException thrown if there was some problem writing to the File.
107 * @throws FeedException thrown if the XML representation for the feed could not be created.
108 *
109 */
110 public void output(WireFeed feed,File file) throws IllegalArgumentException,IOException,FeedException {
111 Writer writer = new FileWriter(file);
112 output(feed,writer);
113 writer.close();
114 }
115
116 /***
117 * Writes to an Writer the XML representation for the given WireFeed.
118 * <p>
119 * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
120 * of the developer to ensure the Writer instance is using the same charset encoding.
121 * <p>
122 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
123 * <p>
124 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
125 * the type given to the FeedOuptut constructor.
126 * @param writer Writer to write the XML representation for the given WireFeed.
127 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
128 * @throws IOException thrown if there was some problem writing to the Writer.
129 * @throws FeedException thrown if the XML representation for the feed could not be created.
130 *
131 */
132 public void output(WireFeed feed,Writer writer) throws IllegalArgumentException,IOException, FeedException {
133 Document doc = outputJDom(feed);
134 String encoding = feed.getEncoding();
135 Format format = Format.getPrettyFormat();
136 if (encoding!=null) {
137 format.setEncoding(encoding);
138 }
139 XMLOutputter outputter = new XMLOutputter(format);
140 outputter.output(doc,writer);
141 }
142
143 /***
144 * Creates a W3C DOM document for the given WireFeed.
145 * <p>
146 * This method does not use the feed encoding property.
147 * <p>
148 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
149 * <p>
150 * @param feed Abstract feed to create W3C DOM document from. The type of the WireFeed must match
151 * the type given to the FeedOuptut constructor.
152 * @return the W3C DOM 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 W3C DOM document for the feed could not be created.
155 *
156 */
157 public org.w3c.dom.Document outputW3CDom(WireFeed feed) throws IllegalArgumentException,FeedException {
158 Document doc = outputJDom(feed);
159 DOMOutputter outputter = new DOMOutputter();
160 try {
161 return outputter.output(doc);
162 }
163 catch (JDOMException jdomEx) {
164 throw new FeedException("Could not create DOM",jdomEx);
165 }
166 }
167
168 /***
169 * Creates a JDOM document for the given WireFeed.
170 * <p>
171 * This method does not use the feed encoding property.
172 * <p>
173 * NOTE: All other output methods delegate to this method.
174 * <p>
175 * @param feed Abstract feed to create JDOM document from. The type of the WireFeed must match
176 * the type given to the FeedOuptut constructor.
177 * @return the JDOM document for the given WireFeed.
178 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
179 * @throws FeedException thrown if the JDOM document for the feed could not be created.
180 *
181 */
182 public Document outputJDom(WireFeed feed) throws IllegalArgumentException,FeedException {
183 String type = feed.getFeedType();
184 WireFeedGenerator generator = GENERATORS.getGenerator(type);
185 if (generator==null) {
186 throw new IllegalArgumentException("Invalid feed type ["+type+"]");
187 }
188
189 if (!generator.getType().equals(type)) {
190 throw new IllegalArgumentException("WireFeedOutput type["+type+"] and WireFeed type ["+
191 type+"] don't match");
192 }
193 return generator.generate(feed);
194 }
195
196 }