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.feed.rss.*;
21  import com.sun.syndication.io.FeedException;
22  import org.jdom.Document;
23  import org.jdom.Element;
24  import org.jdom.Namespace;
25  
26  import java.util.List;
27  
28  
29  /**
30   * Feed Generator for RSS 0.90
31   * <p/>
32   *
33   * @author Elaine Chien
34   */
35  public class RSS090Generator extends BaseWireFeedGenerator {
36  
37      private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
38      private static final String RSS_URI = "http://my.netscape.com/rdf/simple/0.9/";
39      private static final String CONTENT_URI = "http://purl.org/rss/1.0/modules/content/";
40  
41      private static final Namespace RDF_NS = Namespace.getNamespace("rdf", RDF_URI);
42      private static final Namespace RSS_NS = Namespace.getNamespace(RSS_URI);
43      private static final Namespace CONTENT_NS = Namespace.getNamespace("content", CONTENT_URI);
44  
45      public RSS090Generator() {
46          this("rss_0.9");
47      }
48  
49      protected RSS090Generator(String type) {
50          super(type);
51      }
52  
53      public Document generate(WireFeed feed) throws FeedException {
54          Channel channel = (Channel)feed;
55          Element root = createRootElement(channel);
56          populateFeed(channel,root);
57          return createDocument(root);
58      }
59  
60      protected Namespace getFeedNamespace() {
61          return RSS_NS;
62      }
63  
64      protected Namespace getRDFNamespace() {
65          return RDF_NS;
66      }
67  
68      protected Namespace getContentNamespace() {
69          return CONTENT_NS;
70      }
71  
72      protected Document createDocument(Element root) {
73          return new Document(root);
74      }
75  
76      protected Element createRootElement(Channel channel) {
77          Element root = new Element("RDF",getRDFNamespace());
78          root.addNamespaceDeclaration(getFeedNamespace());
79          root.addNamespaceDeclaration(getRDFNamespace());
80          root.addNamespaceDeclaration(getContentNamespace());
81          generateModuleNamespaceDefs(root);
82          return root;
83      }
84  
85      protected void populateFeed(Channel channel, Element parent) throws FeedException  {
86          addChannel(channel,parent);
87          addImage(channel,parent);
88          addTextInput(channel,parent);
89          addItems(channel,parent);
90          generateForeignMarkup(parent, (List)channel.getForeignMarkup());
91      }
92  
93      protected void addChannel(Channel channel,Element parent) throws FeedException {
94          Element eChannel = new Element("channel", getFeedNamespace());
95          populateChannel(channel,eChannel);
96          checkChannelConstraints(eChannel);
97          parent.addContent(eChannel);
98          generateFeedModules(channel.getModules(),eChannel);
99      }
100 
101     /**
102      * Populates the given channel with parsed data from the ROME element that holds the
103      * channel data.
104      *
105      * @param channel the channel into which parsed data will be added.
106      * @param eChannel the XML element that holds the data for the channel.
107      */
108     protected void populateChannel(Channel channel,Element eChannel) {
109         String title = channel.getTitle();
110         if (title!=null) {
111             eChannel.addContent(generateSimpleElement("title",title));
112         }
113         String link = channel.getLink();
114         if (link!=null) {
115             eChannel.addContent(generateSimpleElement("link",link));
116         }
117         String description = channel.getDescription();
118         if (description!=null) {
119             eChannel.addContent(generateSimpleElement("description",description));
120         }
121     }
122 
123     // maxLen == -1 means unlimited.
124     protected void checkNotNullAndLength(Element parent, String childName, int minLen, int maxLen) throws FeedException {
125         Element  child = parent.getChild(childName,getFeedNamespace());
126         if (child == null) {
127             throw new FeedException("Invalid "+getType()+" feed, missing "+parent.getName()+" "+childName);
128         }
129         checkLength(parent,childName,minLen,maxLen);
130     }
131 
132     // maxLen == -1 means unlimited.
133     protected void checkLength(Element parent, String childName, int minLen, int maxLen) throws FeedException {
134         Element  child = parent.getChild(childName,getFeedNamespace());
135         if (child != null) {
136             if (minLen>0 && child.getText().length()<minLen) {
137                 throw new FeedException("Invalid "+getType()+" feed, "+parent.getName()+" "+childName + "short of "+minLen+" length");
138             }
139             if (maxLen>-1 && child.getText().length()>maxLen) {
140                 throw new FeedException("Invalid "+getType()+" feed, "+parent.getName()+" "+childName + "exceeds "+maxLen+" length");
141             }
142         }
143     }
144 
145 
146     protected void addImage(Channel channel,Element parent) throws FeedException {
147         Image image = channel.getImage();
148         if (image!=null) {
149             Element eImage = new Element("image", getFeedNamespace());
150             populateImage(image,eImage);
151             checkImageConstraints(eImage);
152             parent.addContent(eImage);
153         }
154     }
155 
156     protected void populateImage(Image image,Element eImage) {
157         String title = image.getTitle();
158         if (title!=null) {
159             eImage.addContent(generateSimpleElement("title",title));
160         }
161         String url = image.getUrl();
162         if (url!=null) {
163             eImage.addContent(generateSimpleElement("url",url));
164         }
165         String link = image.getLink();
166         if (link!=null) {
167             eImage.addContent(generateSimpleElement("link",link));
168         }
169     }
170 
171     // Thxs DW for this one
172     protected String getTextInputLabel() {
173         return "textInput";
174     }
175 
176     protected void addTextInput(Channel channel,Element parent) throws FeedException {
177         TextInput textInput = channel.getTextInput();
178         if (textInput!=null) {
179             Element eTextInput = new Element(getTextInputLabel(), getFeedNamespace());
180             populateTextInput(textInput,eTextInput);
181             checkTextInputConstraints(eTextInput);
182             parent.addContent(eTextInput);
183         }
184     }
185 
186     protected void populateTextInput(TextInput textInput,Element eTextInput) {
187         String title = textInput.getTitle();
188         if (title!=null) {
189             eTextInput.addContent(generateSimpleElement("title",title));
190         }
191         String description = textInput.getDescription();
192         if (description!=null) {
193             eTextInput.addContent(generateSimpleElement("description",description));
194         }
195         String name = textInput.getName();
196         if (name!=null) {
197             eTextInput.addContent(generateSimpleElement("name",name));
198         }
199         String link = textInput.getLink();
200         if (link!=null) {
201             eTextInput.addContent(generateSimpleElement("link",link));
202         }
203     }
204 
205     protected void addItems(Channel channel,Element parent) throws FeedException {
206         List items = channel.getItems();
207         for (int i=0;i<items.size();i++) {
208             addItem((Item)items.get(i),parent, i);
209         }
210         checkItemsConstraints(parent);
211     }
212 
213     protected void addItem(Item item, Element parent, int index) throws FeedException {
214         Element eItem = new Element("item", getFeedNamespace());
215         populateItem(item,eItem, index);
216         checkItemConstraints(eItem);
217         generateItemModules(item.getModules(),eItem);
218         parent.addContent(eItem);
219     }
220 
221     protected void populateItem(Item item, Element eItem, int index) {
222         String title = item.getTitle();
223         if (title!=null) {
224             eItem.addContent(generateSimpleElement("title",title));
225         }
226         String link = item.getLink();
227         if (link!=null) {
228             eItem.addContent(generateSimpleElement("link",link));
229         }
230         generateForeignMarkup(eItem, (List)item.getForeignMarkup());
231     }
232 
233     protected Element generateSimpleElement(String name, String value) {
234         Element element = new Element(name, getFeedNamespace());
235         element.addContent(value);
236         return element;
237     }
238 
239     protected void checkChannelConstraints(Element eChannel) throws FeedException {
240         checkNotNullAndLength(eChannel,"title", 0, 40);
241         checkNotNullAndLength(eChannel,"description", 0, 500);
242         checkNotNullAndLength(eChannel,"link", 0, 500);
243     }
244 
245     protected void checkImageConstraints(Element eImage) throws FeedException {
246         checkNotNullAndLength(eImage,"title", 0, 40);
247         checkNotNullAndLength(eImage,"url", 0, 500);
248         checkNotNullAndLength(eImage,"link", 0, 500);
249     }
250 
251     protected void checkTextInputConstraints(Element eTextInput) throws FeedException {
252         checkNotNullAndLength(eTextInput,"title", 0, 40);
253         checkNotNullAndLength(eTextInput,"description", 0, 100);
254         checkNotNullAndLength(eTextInput,"name", 0, 500);
255         checkNotNullAndLength(eTextInput,"link", 0, 500);
256     }
257 
258     protected void checkItemsConstraints(Element parent) throws FeedException {
259         int count = parent.getChildren("item",getFeedNamespace()).size();
260         if (count<1 || count>15) {
261             throw new FeedException("Invalid "+getType()+" feed, item count is "+count+" it must be between 1 an 15");
262         }
263     }
264 
265     protected void checkItemConstraints(Element eItem) throws FeedException {
266         checkNotNullAndLength(eItem,"title", 0, 100);
267         checkNotNullAndLength(eItem,"link", 0, 500);
268     }
269 
270 }