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.impl;
18  
19  import com.sun.syndication.feed.WireFeed;
20  import com.sun.syndication.io.FeedException;
21  import com.sun.syndication.io.WireFeedGenerator;
22  import com.sun.syndication.feed.rss.Channel;
23  import com.sun.syndication.feed.rss.Image;
24  import com.sun.syndication.feed.rss.Item;
25  import com.sun.syndication.feed.rss.TextInput;
26  import com.sun.syndication.feed.rss.Description;
27  import org.jdom.Attribute;
28  import org.jdom.DocType;
29  import org.jdom.Document;
30  import org.jdom.Element;
31  
32  import java.util.List;
33  
34  /***
35   * Feed Generator for RSS 0.91
36   * <p/>
37   *
38   * @author Elaine Chien
39   *
40   */
41  public class RSS091Generator implements WireFeedGenerator {
42  
43      private static final String VERSION = "0.91";
44      private static final String RSS_NAME = "rss";
45  
46      private String _type;
47  
48      public RSS091Generator() {
49          this("rss_0.91");
50      }
51  
52      protected RSS091Generator(String feedType) {
53          _type = feedType;
54      }
55  
56      protected String getVersion() {
57          return VERSION;
58      }
59  
60      public String getType() {
61          return _type;
62      }
63  
64      public Document generate(WireFeed feed) throws FeedException {
65  
66          Channel channel = (Channel)feed;
67          Element rootElement = generateRootElement(channel);
68          Document doc = new Document(rootElement);
69          //doc.setDocType(DOC_TYPE);
70  
71          return doc;
72      }
73  
74      protected Element generateRootElement(Channel channel)
75          throws FeedException {
76  
77          Element root = new Element("rss");
78          Attribute version = new Attribute("version", getVersion());
79          root.setAttribute(version);
80  
81          if (channel == null) {
82              throw new FeedException("invalid RSS Channel - missing required channel element");
83          }
84          root.addContent(generateChannelElement(channel));
85          return root;
86  
87      }
88  
89      protected Element generateChannelElement(Channel channel)
90          throws FeedException {
91  
92          String title = channel.getTitle();
93          String link = channel.getLink();
94          String description = channel.getDescription();
95  
96          if (title == null) {
97              throw new FeedException("invalid RSS Channel - missing required title element");
98          }
99          if (link == null) {
100             throw new FeedException("invalid RSS Channel - missing required link element");
101         }
102         if (description == null) {
103             throw new FeedException("invalid RSS Channel - missing required description element");
104         }
105 
106         Element channelElement = new Element("channel");
107         channelElement.addContent(generateSimpleElement("title", title));
108         channelElement.addContent(generateSimpleElement("link", link));
109         channelElement.addContent(generateSimpleElement("description", description));
110         channelElement.addContent(generateLanguageElement(channel.getLanguage()));
111 
112         if (channel.getRating() != null) {
113             channelElement.addContent(generateSimpleElement("rating", channel.getRating()));
114         }
115 
116         if (channel.getCopyright() != null) {
117             channelElement.addContent(generateSimpleElement("copyright", channel.getCopyright()));
118         }
119 
120         if (channel.getPubDate() != null) {
121             channelElement.addContent(generateSimpleElement("pubDate", channel.getPubDate().toString()));
122         }
123 
124         if (channel.getLastBuildDate() != null) {
125             channelElement.addContent(generateSimpleElement("lastBuildDate", channel.getLastBuildDate().toString()));
126         }
127 
128         if (channel.getDocs() != null) {
129             channelElement.addContent(generateSimpleElement("docs", channel.getDocs()));
130         }
131 
132         if (channel.getManagingEditor() != null) {
133             channelElement.addContent(generateSimpleElement("managingEditor", channel.getManagingEditor()));
134         }
135         if (channel.getWebMaster() != null) {
136             channelElement.addContent(generateSimpleElement("webMaster", channel.getWebMaster()));
137         }
138 
139         if (channel.getImage() != null) {
140             channelElement.addContent(generateImageElement(channel.getImage()));
141         }
142 
143         if (channel.getTextInput() != null) {
144             channelElement.addContent(generateTextInputElement(channel.getTextInput()));
145         }
146 
147         if (channel.getSkipHours() != null) {
148             channelElement.addContent(generateSkipHoursElement(channel.getSkipHours()));
149         }
150         if (channel.getSkipDays() != null && channel.getSkipDays().size() != 0) {
151             channelElement.addContent(generateSkipDaysElement(channel.getSkipDays()));
152         }
153 
154         List items = channel.getItems();
155         if (items != null) {
156             for (int i = 0; i < items.size(); i++) {
157                 channelElement.addContent(generateItemElement((Item)items.get(i)));
158             }
159         }
160 
161         return channelElement;
162     }
163 
164     protected Element generateLanguageElement(String language)
165         throws FeedException {
166 
167         if (language == null) {
168             throw new FeedException("invalid RSS Channel - missing required language element");
169         } else {
170             return generateSimpleElement("language", language);
171         }
172     }
173 
174     protected Element generateSkipHoursElement(List hours)
175         throws FeedException {
176 
177         Element skipHoursElement = new Element("skipHours");
178         for (int i = 0; i < hours.size(); i++) {
179             skipHoursElement.addContent(generateSimpleElement("hour", hours.get(i).toString()));
180         }
181         return skipHoursElement;
182     }
183 
184     protected Element generateSkipDaysElement(List days)
185         throws FeedException {
186 
187         Element skipDaysElement = new Element("skipDays");
188         for (int i = 0; i < days.size(); i++) {
189             skipDaysElement.addContent(generateSimpleElement("day", days.get(i).toString()));
190         }
191         return skipDaysElement;
192     }
193 
194 
195     protected Element generateImageElement(Image image)
196         throws FeedException {
197 
198         String title = image.getTitle();
199         String url = image.getUrl();
200 
201         if (title == null) {
202             throw new FeedException("invalid RSS Image - missing required title element");
203         }
204         if (url == null) {
205             throw new FeedException("invalid RSS Image - missing required url element");
206         }
207 
208         Element imageElement = new Element("image");
209         imageElement.addContent(generateSimpleElement("title", title));
210         imageElement.addContent(generateSimpleElement("url", url));
211 
212         if (image.getLink() != null) {
213             imageElement.addContent(generateSimpleElement("link", image.getLink()));
214         }
215         if (image.getWidth() != -1) {
216             imageElement.addContent(generateSimpleElement("width", String.valueOf(image.getWidth())));
217         }
218         if (image.getHeight() != -1) {
219             imageElement.addContent(generateSimpleElement("height", String.valueOf(image.getHeight())));
220         }
221         if (image.getDescription() != null) {
222             imageElement.addContent(generateSimpleElement("description", image.getDescription()));
223         }
224         return imageElement;
225     }
226 
227     protected Element generateItemElement(Item item)
228         throws FeedException  {
229 
230         String title = item.getTitle();
231         String link = item.getLink();
232 
233         if (title == null) {
234             throw new FeedException("invalid RSS Item - missing required title element");
235         }
236         if (link == null) {
237             throw new FeedException("invalid RSS Item - missing required link element");
238         }
239 
240         Element itemElement = new Element("item");
241         itemElement.addContent(generateSimpleElement("title", title));
242         itemElement.addContent(generateSimpleElement("link", link));
243 
244         if (item.getDescription() != null) {
245             itemElement.addContent(generateDescriptionElement(item.getDescription()));
246         }
247 
248         return itemElement;
249 
250     }
251 
252     protected Element generateTextInputElement(TextInput textInput)
253         throws FeedException  {
254 
255         String title = textInput.getTitle();
256         String description = textInput.getDescription();
257         String name = textInput.getName();
258         String link = textInput.getLink();
259 
260         if (title == null) {
261             throw new FeedException("invalid RSS TextInput - missing required title element");
262         }
263         if (description == null) {
264             throw new FeedException("invalid RSS TextInput - missing required description element");
265         }
266         if (name == null) {
267             throw new FeedException("invalid RSS TextInput - missing required name element");
268         }
269         if (link == null) {
270             throw new FeedException("invalid RSS TextInput - missing required link element");
271         }
272 
273         Element textInputElement = new Element("textInput");
274         textInputElement.addContent(generateSimpleElement("title", title));
275         textInputElement.addContent(generateDescriptionElement(textInput.getDescription()));
276         textInputElement.addContent(generateSimpleElement("name", name));
277         textInputElement.addContent(generateSimpleElement("link", link));
278 
279         return textInputElement;
280     }
281 
282     protected Element generateDescriptionElement(String description)
283         throws FeedException {
284 
285         return generateSimpleElement("description", description);
286     }
287 
288     protected Element generateDescriptionElement(Description description)
289         throws FeedException {
290 
291         return generateDescriptionElement(description.getValue());
292     }
293 
294     protected Element generateSimpleElement(String name, String value)
295         throws FeedException  {
296 
297         Element element = new Element(name);
298         element.addContent(value);
299 
300         return element;
301     }
302 
303 }