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.rss.Channel;
20  import com.sun.syndication.feed.rss.Description;
21  import com.sun.syndication.feed.rss.Image;
22  import com.sun.syndication.feed.rss.Item;
23  import com.sun.syndication.io.FeedException;
24  import org.jdom.Attribute;
25  import org.jdom.Document;
26  import org.jdom.Element;
27  import org.jdom.Namespace;
28  
29  import java.util.List;
30  import java.util.Date;
31  
32  /***
33   * Feed Generator for RSS 0.91
34   * <p/>
35   *
36   * @author Elaine Chien
37   *
38   */
39  public class RSS091UserlandGenerator extends RSS090Generator {
40      private String _version;
41  
42      public RSS091UserlandGenerator() {
43          this("rss_0.91U","0.91");
44      }
45  
46      protected RSS091UserlandGenerator(String type,String version) {
47          super(type);
48          _version = version;
49      }
50  
51      protected String getVersion() {
52          return _version;
53      }
54  
55      protected Namespace getFeedNamespace() {
56          return Namespace.NO_NAMESPACE;
57      }
58  
59      protected Document createDocument(Element root) {
60          return new Document(root);
61      }
62  
63      protected Element createRootElement(Channel channel) {
64          Element root = new Element("rss",getFeedNamespace());
65          Attribute version = new Attribute("version", getVersion());
66          root.setAttribute(version);
67          root.addNamespaceDeclaration(getContentNamespace());
68          generateModuleNamespaceDefs(root);
69          return root;
70      }
71  
72      protected void populateFeed(Channel channel,Element parent) throws FeedException  {
73          addChannel(channel,parent);
74      }
75  
76      protected void addChannel(Channel channel,Element parent) throws FeedException {
77          super.addChannel(channel,parent);
78          Element eChannel = parent.getChild("channel",getFeedNamespace());
79  
80          addImage(channel,eChannel);
81          addTextInput(channel,eChannel);
82          addItems(channel,eChannel);
83      }
84  
85      protected void populateChannel(Channel channel,Element eChannel) {
86          super.populateChannel(channel,eChannel);
87          String language = channel.getLanguage();
88          if (language != null) {
89              eChannel.addContent(generateSimpleElement("language", language));
90          }
91  
92          String rating = channel.getRating();
93          if (rating != null) {
94              eChannel.addContent(generateSimpleElement("rating", rating));
95          }
96  
97          String copyright = channel.getCopyright();
98          if (copyright != null) {
99              eChannel.addContent(generateSimpleElement("copyright", copyright));
100         }
101 
102         Date pubDate = channel.getPubDate();
103         if (pubDate != null) {
104             eChannel.addContent(generateSimpleElement("pubDate", DateParser.formatRFC822(pubDate)));
105         }
106 
107         Date lastBuildDate = channel.getLastBuildDate();
108         if (lastBuildDate != null) {
109             eChannel.addContent(generateSimpleElement("lastBuildDate", DateParser.formatRFC822(lastBuildDate)));
110         }
111 
112         String docs = channel.getDocs();
113         if (docs != null) {
114             eChannel.addContent(generateSimpleElement("docs", docs));
115         }
116 
117         String managingEditor = channel.getManagingEditor();
118         if (managingEditor != null) {
119             eChannel.addContent(generateSimpleElement("managingEditor", managingEditor));
120         }
121 
122         String webMaster = channel.getWebMaster();
123         if (webMaster != null) {
124             eChannel.addContent(generateSimpleElement("webMaster", webMaster));
125         }
126 
127         List skipHours = channel.getSkipHours();
128         if (skipHours != null && skipHours.size()>0) {
129             eChannel.addContent(generateSkipHoursElement(skipHours));
130         }
131 
132         List skipDays = channel.getSkipDays();
133         if (skipDays != null && skipDays.size()>0) {
134             eChannel.addContent(generateSkipDaysElement(skipDays));
135         }
136     }
137 
138     protected Element generateSkipHoursElement(List hours) {
139         Element skipHoursElement = new Element("skipHours",getFeedNamespace());
140         for (int i = 0; i < hours.size(); i++) {
141             skipHoursElement.addContent(generateSimpleElement("hour", hours.get(i).toString()));
142         }
143         return skipHoursElement;
144     }
145 
146     protected Element generateSkipDaysElement(List days) {
147         Element skipDaysElement = new Element("skipDays");
148         for (int i = 0; i < days.size(); i++) {
149             skipDaysElement.addContent(generateSimpleElement("day", days.get(i).toString()));
150         }
151         return skipDaysElement;
152     }
153 
154     protected void populateImage(Image image,Element eImage) {
155         super.populateImage(image,eImage);
156 
157         int width = image.getWidth();
158         if (width>-1) {
159             eImage.addContent(generateSimpleElement("width",String.valueOf(width)));
160         }
161         int height = image.getHeight();
162         if (height>-1) {
163             eImage.addContent(generateSimpleElement("height",String.valueOf(height)));
164         }
165 
166         String description = image.getDescription();
167         if (description!=null) {
168             eImage.addContent(generateSimpleElement("description",description));
169         }
170     }
171 
172     protected void populateItem(Item item, Element eItem, int index) {
173         super.populateItem(item,eItem, index);
174         Description description = item.getDescription();
175         if (description!=null) {
176             eItem.addContent(generateSimpleElement("description",description.getValue()));
177         }
178         if (item.getModule(getContentNamespace().getURI()) == null && item.getContent() != null) {
179             Element elem = new Element("encoded", getContentNamespace());
180             elem.addContent(item.getContent().getValue());
181             eItem.addContent(elem);
182         }
183     }
184 
185     /***
186      * To be overriden by RSS 0.91 Netscape and RSS 0.94
187      */
188     protected boolean isHourFormat24() {
189         return true;
190     }
191 
192     protected void checkChannelConstraints(Element eChannel) throws FeedException {
193         checkNotNullAndLength(eChannel,"title", 1, 100);
194         checkNotNullAndLength(eChannel,"description", 1, 500);
195         checkNotNullAndLength(eChannel,"link", 1, 500);
196         checkNotNullAndLength(eChannel,"language", 2, 5);
197 
198         checkLength(eChannel,"rating", 20, 500);
199         checkLength(eChannel,"copyright", 1, 100);
200         checkLength(eChannel,"pubDate", 1, 100);
201         checkLength(eChannel,"lastBuildDate", 1, 100);
202         checkLength(eChannel,"docs", 1, 500);
203         checkLength(eChannel,"managingEditor", 1, 100);
204         checkLength(eChannel,"webMaster", 1, 100);
205 
206         Element skipHours = eChannel.getChild("skipHours");
207         if (skipHours!=null) {
208             List hours = skipHours.getChildren();
209             for (int i=0;i<hours.size();i++) {
210                 Element hour = (Element) hours.get(i);
211                 int value = Integer.parseInt(hour.getText().trim());
212                 if (isHourFormat24()) {
213                     if (value<1 || value>24) {
214                         throw new FeedException("Invalid hour value "+value+", it must be between 1 and 24");
215                     }
216                 }
217                 else {
218                     if (value<0 || value>23) {
219                         throw new FeedException("Invalid hour value "+value+", it must be between 0 and 23");
220                     }
221                 }
222             }
223         }
224     }
225 
226     protected void checkImageConstraints(Element eImage) throws FeedException {
227         checkNotNullAndLength(eImage,"title", 1, 100);
228         checkNotNullAndLength(eImage,"url", 1, 500);
229 
230         checkLength(eImage,"link", 1, 500);
231         checkLength(eImage,"width", 1, 3);
232         checkLength(eImage,"width", 1, 3);
233         checkLength(eImage,"description", 1, 100);
234     }
235 
236 
237     protected void checkTextInputConstraints(Element eTextInput) throws FeedException {
238         checkNotNullAndLength(eTextInput,"title", 1, 100);
239         checkNotNullAndLength(eTextInput,"description", 1, 500);
240         checkNotNullAndLength(eTextInput,"name", 1, 20);
241         checkNotNullAndLength(eTextInput,"link", 1, 500);
242     }
243 
244     protected void checkItemConstraints(Element eItem) throws FeedException {
245         checkNotNullAndLength(eItem,"title", 1, 100);
246         checkNotNullAndLength(eItem,"link", 1, 500);
247 
248         checkLength(eItem,"description", 1, 500);
249     }
250 
251 }