1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 */
36 public class RSS090Generator extends BaseWireFeedGenerator {
37
38 private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
39 private static final String RSS_URI = "http://my.netscape.com/rdf/simple/0.9/";
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
44 public RSS090Generator() {
45 this("rss_0.9");
46 }
47
48 protected RSS090Generator(String type) {
49 super(type);
50 }
51
52 public Document generate(WireFeed feed) throws FeedException {
53
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 Document createDocument(Element root) {
69 return new Document(root);
70 }
71
72 protected Element createRootElement(Channel channel) {
73 Element root = new Element("RDF",getRDFNamespace());
74 root.addNamespaceDeclaration(getFeedNamespace());
75 root.addNamespaceDeclaration(getRDFNamespace());
76 generateModuleNamespaceDefs(root);
77 return root;
78 }
79
80 protected void populateFeed(Channel channel,Element parent) throws FeedException {
81 addChannel(channel,parent);
82 addImage(channel,parent);
83 addTextInput(channel,parent);
84 addItems(channel,parent);
85 }
86
87 protected void addChannel(Channel channel,Element parent) throws FeedException {
88 Element eChannel = new Element("channel", getFeedNamespace());
89 populateChannel(channel,eChannel);
90 checkChannelConstraints(eChannel);
91 generateFeedModules(channel.getModules(),eChannel);
92 parent.addContent(eChannel);
93 }
94
95 protected void populateChannel(Channel channel,Element eChannel) {
96 String title = channel.getTitle();
97 if (title!=null) {
98 eChannel.addContent(generateSimpleElement("title",title));
99 }
100 String link = channel.getLink();
101 if (link!=null) {
102 eChannel.addContent(generateSimpleElement("link",link));
103 }
104 String description = channel.getDescription();
105 if (description!=null) {
106 eChannel.addContent(generateSimpleElement("description",description));
107 }
108 }
109
110
111 protected void checkNotNullAndLength(Element parent, String childName, int minLen, int maxLen) throws FeedException {
112 Element child = parent.getChild(childName,getFeedNamespace());
113 if (child == null) {
114 throw new FeedException("Invalid "+getType()+" feed, missing "+parent.getName()+" "+childName);
115 }
116 checkLength(parent,childName,minLen,maxLen);
117 }
118
119
120 protected void checkLength(Element parent, String childName, int minLen, int maxLen) throws FeedException {
121 Element child = parent.getChild(childName,getFeedNamespace());
122 if (child != null) {
123 if (minLen>0 && child.getText().length()<minLen) {
124 throw new FeedException("Invalid "+getType()+" feed, "+parent.getName()+" "+childName + "short of "+minLen+" length");
125 }
126 if (maxLen>-1 && child.getText().length()>maxLen) {
127 throw new FeedException("Invalid "+getType()+" feed, "+parent.getName()+" "+childName + "exceeds "+maxLen+" length");
128 }
129 }
130 }
131
132
133 protected void addImage(Channel channel,Element parent) throws FeedException {
134 Image image = channel.getImage();
135 if (image!=null) {
136 Element eImage = new Element("image", getFeedNamespace());
137 populateImage(image,eImage);
138 checkImageConstraints(eImage);
139 parent.addContent(eImage);
140 }
141 }
142
143 protected void populateImage(Image image,Element eImage) {
144 String title = image.getTitle();
145 if (title!=null) {
146 eImage.addContent(generateSimpleElement("title",title));
147 }
148 String url = image.getUrl();
149 if (url!=null) {
150 eImage.addContent(generateSimpleElement("url",url));
151 }
152 String link = image.getLink();
153 if (link!=null) {
154 eImage.addContent(generateSimpleElement("link",link));
155 }
156 }
157
158
159 protected String getTextInputLabel() {
160 return "textInput";
161 }
162
163 protected void addTextInput(Channel channel,Element parent) throws FeedException {
164 TextInput textInput = channel.getTextInput();
165 if (textInput!=null) {
166 Element eTextInput = new Element(getTextInputLabel(), getFeedNamespace());
167 populateTextInput(textInput,eTextInput);
168 checkTextInputConstraints(eTextInput);
169 parent.addContent(eTextInput);
170 }
171 }
172
173 protected void populateTextInput(TextInput textInput,Element eTextInput) {
174 String title = textInput.getTitle();
175 if (title!=null) {
176 eTextInput.addContent(generateSimpleElement("title",title));
177 }
178 String description = textInput.getDescription();
179 if (description!=null) {
180 eTextInput.addContent(generateSimpleElement("description",description));
181 }
182 String name = textInput.getName();
183 if (name!=null) {
184 eTextInput.addContent(generateSimpleElement("name",name));
185 }
186 String link = textInput.getLink();
187 if (link!=null) {
188 eTextInput.addContent(generateSimpleElement("link",link));
189 }
190 }
191
192 protected void addItems(Channel channel,Element parent) throws FeedException {
193 List items = channel.getItems();
194 for (int i=0;i<items.size();i++) {
195 addItem((Item)items.get(i),parent, i);
196 }
197 checkItemsConstraints(parent);
198 }
199
200 protected void addItem(Item item, Element parent, int index) throws FeedException {
201 Element eItem = new Element("item", getFeedNamespace());
202 populateItem(item,eItem, index);
203 checkItemConstraints(eItem);
204 generateItemModules(item.getModules(),eItem);
205 parent.addContent(eItem);
206 }
207
208 protected void populateItem(Item item, Element eItem, int index) {
209 String title = item.getTitle();
210 if (title!=null) {
211 eItem.addContent(generateSimpleElement("title",title));
212 }
213 String link = item.getLink();
214 if (link!=null) {
215 eItem.addContent(generateSimpleElement("link",link));
216 }
217 }
218
219 protected Element generateSimpleElement(String name, String value) {
220 Element element = new Element(name, getFeedNamespace());
221 element.addContent(value);
222 return element;
223 }
224
225 protected void checkChannelConstraints(Element eChannel) throws FeedException {
226 checkNotNullAndLength(eChannel,"title", 0, 40);
227 checkNotNullAndLength(eChannel,"description", 0, 500);
228 checkNotNullAndLength(eChannel,"link", 0, 500);
229 }
230
231 protected void checkImageConstraints(Element eImage) throws FeedException {
232 checkNotNullAndLength(eImage,"title", 0, 40);
233 checkNotNullAndLength(eImage,"url", 0, 500);
234 checkNotNullAndLength(eImage,"link", 0, 500);
235 }
236
237 protected void checkTextInputConstraints(Element eTextInput) throws FeedException {
238 checkNotNullAndLength(eTextInput,"title", 0, 40);
239 checkNotNullAndLength(eTextInput,"description", 0, 100);
240 checkNotNullAndLength(eTextInput,"name", 0, 500);
241 checkNotNullAndLength(eTextInput,"link", 0, 500);
242 }
243
244 protected void checkItemsConstraints(Element parent) throws FeedException {
245 int count = parent.getChildren("item",getFeedNamespace()).size();
246 if (count<1 || count>15) {
247 throw new FeedException("Invalid "+getType()+" feed, item count is "+count+" it must be between 1 an 15");
248 }
249 }
250
251 protected void checkItemConstraints(Element eItem) throws FeedException {
252 checkNotNullAndLength(eItem,"title", 0, 100);
253 checkNotNullAndLength(eItem,"link", 0, 500);
254 }
255
256 }