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.AbstractFeed;
20  import com.sun.syndication.io.FeedException;
21  import com.sun.syndication.feed.rss.Category;
22  import com.sun.syndication.feed.rss.Channel;
23  import com.sun.syndication.feed.rss.Cloud;
24  import com.sun.syndication.feed.rss.Enclosure;
25  import com.sun.syndication.feed.rss.Item;
26  import com.sun.syndication.feed.rss.Source;
27  import org.jdom.Attribute;
28  import org.jdom.Document;
29  import org.jdom.Element;
30  
31  import java.util.List;
32  
33  
34  /***
35   * Feed Generator for RSS 0.92
36   * <p/>
37   *
38   * @author Elaine Chien
39   *
40   */
41  
42  public class RSS092Generator extends RSS091Generator {
43  
44      private static final String VERSION = "0.92";
45  
46      public RSS092Generator() {
47          super("rss_0.92");
48      }
49  
50      protected RSS092Generator(String feedType) {
51          super(feedType);
52      }
53  
54      protected String getVersion() {
55          return VERSION;
56      }
57  
58      public Document generate(AbstractFeed feed) throws FeedException {
59  
60          Channel channel = (Channel)feed;
61          Element rootElement = generateRootElement(channel);
62          Document doc = new Document(rootElement);
63  
64          return doc;
65      }
66  
67      protected Element generateChannelElement(Channel channel)
68          throws FeedException {
69  
70          Element channelElement = super.generateChannelElement(channel);
71  
72          if (channel.getCloud() != null) {
73              channelElement.addContent(generateCloudElement(channel.getCloud()));
74          }
75  
76          return channelElement;
77      }
78  
79      protected Element generateLanguageElement(String language)
80          throws FeedException {
81  
82          if (language == null) {
83              return generateSimpleElement("language", "");
84          } else {
85              return generateSimpleElement("language", language);
86          }
87      }
88  
89      protected Element generateCloudElement(Cloud cloud)
90          throws FeedException {
91  
92          Element cloudElement = new Element("cloud");
93  
94          if (cloud.getDomain() != null) {
95              cloudElement.setAttribute(new Attribute("domain", cloud.getDomain()));
96          }
97  
98          if (cloud.getPort() != 0) {
99              cloudElement.setAttribute(new Attribute("port", String.valueOf(cloud.getPort())));
100         }
101 
102         if (cloud.getRegisterProcedure() != null) {
103             cloudElement.setAttribute(new Attribute("registerProcedure", cloud.getRegisterProcedure()));
104         }
105 
106         if (cloud.getProtocol() != null) {
107             cloudElement.setAttribute(new Attribute("protocol", cloud.getProtocol()));
108         }
109 
110         return cloudElement;
111     }
112 
113     protected Element generateItemElement(Item item)
114         throws FeedException {
115 
116         Element itemElement = new Element("item");
117         if (item.getTitle() != null) {
118             itemElement.addContent(generateSimpleElement("title", item.getTitle()));
119         }
120         if (item.getLink() != null) {
121             itemElement.addContent(generateSimpleElement("link", item.getLink()));
122         }
123         if (item.getDescription() != null) {
124             itemElement.addContent(generateDescriptionElement(item.getDescription()));
125         }
126 
127         if (item.getSource() != null) {
128             itemElement.addContent(generateSourceElement(item.getSource()));
129         }
130 
131         List enclosures = item.getEnclosures();
132         for(int i = 0; i < enclosures.size(); i++) {
133             itemElement.addContent(generateEnclosure((Enclosure)enclosures.get(i)));
134         }
135 
136         List categories = item.getCategories();
137         for(int i = 0; i < categories.size(); i++) {
138             itemElement.addContent(generateCategoryElement((Category)categories.get(i)));
139         }
140 
141         return itemElement;
142     }
143 
144     protected Element generateSourceElement(Source source)
145         throws FeedException {
146 
147         Element sourceElement = new Element("source");
148         if (source.getUrl() != null) {
149             sourceElement.setAttribute(new Attribute("url", source.getUrl()));
150         }
151         sourceElement.addContent(source.getValue());
152 
153         return sourceElement;
154     }
155 
156     protected Element generateEnclosure(Enclosure enclosure)
157         throws FeedException {
158 
159         Element enclosureElement = new Element("enclosure");
160         if (enclosure.getUrl() != null) {
161             enclosureElement.setAttribute("url", enclosure.getUrl());
162         }
163         if (enclosure.getLength() != 0) {
164             enclosureElement.setAttribute("length", String.valueOf(enclosure.getLength()));
165         }
166         if (enclosure.getType() != null) {
167             enclosureElement.setAttribute("type", enclosure.getType());
168         }
169         return enclosureElement;
170     }
171 
172     protected Element generateCategoryElement(Category category)
173         throws FeedException {
174 
175         Element categoryElement = new Element("category");
176         if (category.getDomain() != null) {
177             categoryElement.setAttribute("domain", category.getDomain());
178         }
179         categoryElement.addContent(category.getValue());
180 
181         return categoryElement;
182     }
183 
184 
185 }