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.synd.SyndFeed;
20  import org.jdom.Document;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.Writer;
25  
26  /**
27   * Generates an XML document (String, File, OutputStream, Writer, W3C DOM document or JDOM document)
28   * out of an SyndFeedImpl..
29   * <p>
30   * It delegates to a WireFeedOutput to generate all feed types.
31   * <p>
32   * @author Alejandro Abdelnur
33   *
34   */
35  public class SyndFeedOutput {
36      private WireFeedOutput _feedOutput;
37  
38      /**
39       * Creates a SyndFeedOutput instance.
40       * <p>
41       *
42       */
43      public SyndFeedOutput() {
44          _feedOutput = new WireFeedOutput();
45      }
46  
47      /**
48       * Creates a String with the XML representation for the given SyndFeedImpl.
49       * <p>
50       * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
51       * of the developer to ensure that if the String is written to a character stream the stream charset is the same as
52       * the feed encoding property.
53       * <p>
54       * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl must match
55       *        the type given to the FeedOuptut constructor.
56       * @return a String with the XML representation for the given SyndFeedImpl.
57       * @throws FeedException thrown if the XML representation for the feed could not be created.
58       *
59       */
60      public String outputString(SyndFeed feed) throws FeedException {
61          return _feedOutput.outputString(feed.createWireFeed());
62      }
63  
64      /**
65       * Creates a File containing with the XML representation for the given SyndFeedImpl.
66       * <p>
67       * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. The platform
68       * default charset encoding is used to write the feed to the file. It is the responsibility
69       * of the developer to ensure the feed encoding is set to the platform charset encoding.
70       * <p>
71       * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl must match
72       *        the type given to the FeedOuptut constructor.
73       * @param file the file where to write the XML representation for the given SyndFeedImpl.
74       * @throws IOException thrown if there was some problem writing to the File.
75       * @throws FeedException thrown if the XML representation for the feed could not be created.
76       *
77       */
78      public void output(SyndFeed feed,File file) throws IOException, FeedException {
79          _feedOutput.output(feed.createWireFeed(),file);
80      }
81  
82      /**
83       * Writes to an Writer the XML representation for the given SyndFeedImpl.
84       * <p>
85       * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
86       * of the developer to ensure that if the String is written to a character stream the stream charset is the same as
87       * the feed encoding property.
88       * <p>
89       * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl must match
90       *        the type given to the FeedOuptut constructor.
91       * @param writer Writer to write the XML representation for the given SyndFeedImpl.
92       * @throws IOException thrown if there was some problem writing to the Writer.
93       * @throws FeedException thrown if the XML representation for the feed could not be created.
94       *
95       */
96      public void output(SyndFeed feed,Writer writer) throws IOException, FeedException {
97          _feedOutput.output(feed.createWireFeed(),writer);
98      }
99  
100     /**
101      * Creates a W3C DOM document for the given SyndFeedImpl.
102      * <p>
103      * This method does not use the feed encoding property.
104      * <p>
105      * @param feed Abstract feed to create W3C DOM document from. The type of the SyndFeedImpl must match
106      *        the type given to the FeedOuptut constructor.
107      * @return the W3C DOM document for the given SyndFeedImpl.
108      * @throws FeedException thrown if the W3C DOM document for the feed could not be created.
109      *
110      */
111     public org.w3c.dom.Document outputW3CDom(SyndFeed feed) throws FeedException {
112         return _feedOutput.outputW3CDom(feed.createWireFeed());
113     }
114 
115     /**
116      * Creates a JDOM document for the given SyndFeedImpl.
117      * <p>
118      * This method does not use the feed encoding property.
119      * <p>
120      * @param feed Abstract feed to create JDOM document from. The type of the SyndFeedImpl must match
121      *        the type given to the FeedOuptut constructor.
122      * @return the JDOM document for the given SyndFeedImpl.
123      * @throws FeedException thrown if the JDOM document for the feed could not be created.
124      *
125      */
126     public Document outputJDom(SyndFeed feed) throws FeedException {
127         return _feedOutput.outputJDom(feed.createWireFeed());
128     }
129 
130 }