KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > io > impl > RSS091UserlandGenerator


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 JavaDoc;
30 import java.util.Date JavaDoc;
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 JavaDoc _version;
41
42     public RSS091UserlandGenerator() {
43         this("rss_0.91U","0.91");
44     }
45
46     protected RSS091UserlandGenerator(String JavaDoc type,String JavaDoc version) {
47         super(type);
48         _version = version;
49     }
50
51     protected String JavaDoc 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         generateModuleNamespaceDefs(root);
68         return root;
69     }
70
71     protected void populateFeed(Channel channel,Element parent) throws FeedException {
72         addChannel(channel,parent);
73     }
74
75     protected void addChannel(Channel channel,Element parent) throws FeedException {
76         super.addChannel(channel,parent);
77         Element eChannel = parent.getChild("channel",getFeedNamespace());
78
79         addImage(channel,eChannel);
80         addTextInput(channel,eChannel);
81         addItems(channel,eChannel);
82     }
83
84     protected void populateChannel(Channel channel,Element eChannel) {
85         super.populateChannel(channel,eChannel);
86         String JavaDoc language = channel.getLanguage();
87         if (language != null) {
88             eChannel.addContent(generateSimpleElement("language", language));
89         }
90
91         String JavaDoc rating = channel.getRating();
92         if (rating != null) {
93             eChannel.addContent(generateSimpleElement("rating", rating));
94         }
95
96         String JavaDoc copyright = channel.getCopyright();
97         if (copyright != null) {
98             eChannel.addContent(generateSimpleElement("copyright", copyright));
99         }
100
101         Date JavaDoc pubDate = channel.getPubDate();
102         if (pubDate != null) {
103             eChannel.addContent(generateSimpleElement("pubDate", DateParser.formatRFC822(pubDate)));
104         }
105
106         Date JavaDoc lastBuildDate = channel.getLastBuildDate();
107         if (lastBuildDate != null) {
108             eChannel.addContent(generateSimpleElement("lastBuildDate", DateParser.formatRFC822(lastBuildDate)));
109         }
110
111         String JavaDoc docs = channel.getDocs();
112         if (docs != null) {
113             eChannel.addContent(generateSimpleElement("docs", docs));
114         }
115
116         String JavaDoc managingEditor = channel.getManagingEditor();
117         if (managingEditor != null) {
118             eChannel.addContent(generateSimpleElement("managingEditor", managingEditor));
119         }
120
121         String JavaDoc webMaster = channel.getWebMaster();
122         if (webMaster != null) {
123             eChannel.addContent(generateSimpleElement("webMaster", webMaster));
124         }
125
126         List JavaDoc skipHours = channel.getSkipHours();
127         if (skipHours != null && skipHours.size()>0) {
128             eChannel.addContent(generateSkipHoursElement(skipHours));
129         }
130
131         List JavaDoc skipDays = channel.getSkipDays();
132         if (skipDays != null && skipDays.size()>0) {
133             eChannel.addContent(generateSkipDaysElement(skipDays));
134         }
135     }
136
137     protected Element generateSkipHoursElement(List JavaDoc hours) {
138         Element skipHoursElement = new Element("skipHours",getFeedNamespace());
139         for (int i = 0; i < hours.size(); i++) {
140             skipHoursElement.addContent(generateSimpleElement("hour", hours.get(i).toString()));
141         }
142         return skipHoursElement;
143     }
144
145     protected Element generateSkipDaysElement(List JavaDoc days) {
146         Element skipDaysElement = new Element("skipDays");
147         for (int i = 0; i < days.size(); i++) {
148             skipDaysElement.addContent(generateSimpleElement("day", days.get(i).toString()));
149         }
150         return skipDaysElement;
151     }
152
153     protected void populateImage(Image image,Element eImage) {
154         super.populateImage(image,eImage);
155
156         int width = image.getWidth();
157         if (width>-1) {
158             eImage.addContent(generateSimpleElement("width",String.valueOf(width)));
159         }
160         int height = image.getHeight();
161         if (height>-1) {
162             eImage.addContent(generateSimpleElement("height",String.valueOf(height)));
163         }
164
165         String JavaDoc description = image.getDescription();
166         if (description!=null) {
167             eImage.addContent(generateSimpleElement("description",description));
168         }
169     }
170
171     protected void populateItem(Item item, Element eItem, int index) {
172         super.populateItem(item,eItem, index);
173         Description description = item.getDescription();
174         if (description!=null) {
175             eItem.addContent(generateSimpleElement("description",description.getValue()));
176         }
177     }
178
179     /**
180      * To be overriden by RSS 0.91 Netscape and RSS 0.94
181      */

182     protected boolean isHourFormat24() {
183         return true;
184     }
185
186     protected void checkChannelConstraints(Element eChannel) throws FeedException {
187         checkNotNullAndLength(eChannel,"title", 1, 100);
188         checkNotNullAndLength(eChannel,"description", 1, 500);
189         checkNotNullAndLength(eChannel,"link", 1, 500);
190         checkNotNullAndLength(eChannel,"language", 2, 5);
191
192         checkLength(eChannel,"rating", 20, 500);
193         checkLength(eChannel,"copyright", 1, 100);
194         checkLength(eChannel,"pubDate", 1, 100);
195         checkLength(eChannel,"lastBuildDate", 1, 100);
196         checkLength(eChannel,"docs", 1, 500);
197         checkLength(eChannel,"managingEditor", 1, 100);
198         checkLength(eChannel,"webMaster", 1, 100);
199
200         Element skipHours = eChannel.getChild("skipHours");
201         if (skipHours!=null) {
202             List JavaDoc hours = skipHours.getChildren();
203             for (int i=0;i<hours.size();i++) {
204                 Element hour = (Element) hours.get(i);
205                 int value = Integer.parseInt(hour.getText());
206                 if (isHourFormat24()) {
207                     if (value<1 || value>24) {
208                         throw new FeedException("Invalid hour value "+value+", it must be between 1 and 24");
209                     }
210                 }
211                 else {
212                     if (value<0 || value>23) {
213                         throw new FeedException("Invalid hour value "+value+", it must be between 0 and 23");
214                     }
215                 }
216             }
217         }
218     }
219
220     protected void checkImageConstraints(Element eImage) throws FeedException {
221         checkNotNullAndLength(eImage,"title", 1, 100);
222         checkNotNullAndLength(eImage,"url", 1, 500);
223
224         checkLength(eImage,"link", 1, 500);
225         checkLength(eImage,"width", 1, 3);
226         checkLength(eImage,"width", 1, 3);
227         checkLength(eImage,"description", 1, 100);
228     }
229
230
231     protected void checkTextInputConstraints(Element eTextInput) throws FeedException {
232         checkNotNullAndLength(eTextInput,"title", 1, 100);
233         checkNotNullAndLength(eTextInput,"description", 1, 500);
234         checkNotNullAndLength(eTextInput,"name", 1, 20);
235         checkNotNullAndLength(eTextInput,"link", 1, 500);
236     }
237
238     protected void checkItemConstraints(Element eItem) throws FeedException {
239         checkNotNullAndLength(eItem,"title", 1, 100);
240         checkNotNullAndLength(eItem,"link", 1, 500);
241
242         checkLength(eItem,"description", 1, 500);
243     }
244
245 }
246
Popular Tags