001: /*
002: * Copyright 2004 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.io;
018:
019: import com.sun.syndication.feed.WireFeed;
020: import com.sun.syndication.io.impl.FeedGenerators;
021: import org.jdom.Document;
022: import org.jdom.JDOMException;
023: import org.jdom.output.DOMOutputter;
024: import org.jdom.output.Format;
025: import org.jdom.output.XMLOutputter;
026:
027: import java.io.IOException;
028: import java.io.Writer;
029: import java.io.File;
030: import java.io.FileWriter;
031: import java.util.List;
032:
033: /**
034: * Generates an XML document (String, File, OutputStream, Writer, W3C DOM document or JDOM document)
035: * out of an WireFeed (RSS/Atom).
036: * <p>
037: * It generates all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
038: * Atom 0.3 feeds. Generators are plugable (they must implement the ModuleParser interface).
039: * <p>
040: * @author Alejandro Abdelnur
041: *
042: */
043: public class WireFeedOutput {
044: private final static FeedGenerators GENERATORS = new FeedGenerators();
045:
046: /**
047: * Returns the list of supported output feed types.
048: * <p>
049: * @see WireFeed for details on the format of these strings.
050: * <p>
051: * @return a list of String elements with the supported output feed types.
052: *
053: */
054: public static List getSupportedFeedTypes() {
055: return GENERATORS.getSupportedFeedTypes();
056: }
057:
058: /**
059: * Creates a FeedOuput instance.
060: * <p>
061: *
062: */
063: public WireFeedOutput() {
064: }
065:
066: /**
067: * Creates a String with the XML representation for the given WireFeed.
068: * <p>
069: * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
070: * of the developer to ensure that if the String is written to a character stream the stream charset is the same as
071: * the feed encoding property.
072: * <p>
073: * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
074: * <p>
075: * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
076: * the type given to the FeedOuptut constructor.
077: * @return a String with the XML representation for the given WireFeed.
078: * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
079: * @throws FeedException thrown if the XML representation for the feed could not be created.
080: *
081: */
082: public String outputString(WireFeed feed)
083: throws IllegalArgumentException, FeedException {
084: Document doc = outputJDom(feed);
085: String encoding = feed.getEncoding();
086: Format format = Format.getPrettyFormat();
087: if (encoding != null) {
088: format.setEncoding(encoding);
089: }
090: XMLOutputter outputter = new XMLOutputter(format);
091: return outputter.outputString(doc);
092: }
093:
094: /**
095: * Creates a File containing with the XML representation for the given WireFeed.
096: * <p>
097: * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. The platform
098: * default charset encoding is used to write the feed to the file. It is the responsibility
099: * of the developer to ensure the feed encoding is set to the platform charset encoding.
100: * <p>
101: * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
102: * <p>
103: * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
104: * the type given to the FeedOuptut constructor.
105: * @param file the file where to write the XML representation for the given WireFeed.
106: * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
107: * @throws IOException thrown if there was some problem writing to the File.
108: * @throws FeedException thrown if the XML representation for the feed could not be created.
109: *
110: */
111: public void output(WireFeed feed, File file)
112: throws IllegalArgumentException, IOException, FeedException {
113: Writer writer = new FileWriter(file);
114: output(feed, writer);
115: writer.close();
116: }
117:
118: /**
119: * Writes to an Writer the XML representation for the given WireFeed.
120: * <p>
121: * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
122: * of the developer to ensure the Writer instance is using the same charset encoding.
123: * <p>
124: * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
125: * <p>
126: * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
127: * the type given to the FeedOuptut constructor.
128: * @param writer Writer to write the XML representation for the given WireFeed.
129: * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
130: * @throws IOException thrown if there was some problem writing to the Writer.
131: * @throws FeedException thrown if the XML representation for the feed could not be created.
132: *
133: */
134: public void output(WireFeed feed, Writer writer)
135: throws IllegalArgumentException, IOException, FeedException {
136: Document doc = outputJDom(feed);
137: String encoding = feed.getEncoding();
138: Format format = Format.getPrettyFormat();
139: if (encoding != null) {
140: format.setEncoding(encoding);
141: }
142: XMLOutputter outputter = new XMLOutputter(format);
143: outputter.output(doc, writer);
144: }
145:
146: /**
147: * Creates a W3C DOM document for the given WireFeed.
148: * <p>
149: * This method does not use the feed encoding property.
150: * <p>
151: * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
152: * <p>
153: * @param feed Abstract feed to create W3C DOM document from. The type of the WireFeed must match
154: * the type given to the FeedOuptut constructor.
155: * @return the W3C DOM document for the given WireFeed.
156: * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
157: * @throws FeedException thrown if the W3C DOM document for the feed could not be created.
158: *
159: */
160: public org.w3c.dom.Document outputW3CDom(WireFeed feed)
161: throws IllegalArgumentException, FeedException {
162: Document doc = outputJDom(feed);
163: DOMOutputter outputter = new DOMOutputter();
164: try {
165: return outputter.output(doc);
166: } catch (JDOMException jdomEx) {
167: throw new FeedException("Could not create DOM", jdomEx);
168: }
169: }
170:
171: /**
172: * Creates a JDOM document for the given WireFeed.
173: * <p>
174: * This method does not use the feed encoding property.
175: * <p>
176: * NOTE: All other output methods delegate to this method.
177: * <p>
178: * @param feed Abstract feed to create JDOM document from. The type of the WireFeed must match
179: * the type given to the FeedOuptut constructor.
180: * @return the JDOM document for the given WireFeed.
181: * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
182: * @throws FeedException thrown if the JDOM document for the feed could not be created.
183: *
184: */
185: public Document outputJDom(WireFeed feed)
186: throws IllegalArgumentException, FeedException {
187: String type = feed.getFeedType();
188: WireFeedGenerator generator = GENERATORS.getGenerator(type);
189: if (generator == null) {
190: throw new IllegalArgumentException("Invalid feed type ["
191: + type + "]");
192: }
193:
194: if (!generator.getType().equals(type)) {
195: throw new IllegalArgumentException("WireFeedOutput type["
196: + type + "] and WireFeed type [" + type
197: + "] don't match");
198: }
199: return generator.generate(feed);
200: }
201:
202: }
|