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.io.FeedException;
21 import com.sun.syndication.io.WireFeedGenerator;
22 import com.sun.syndication.feed.rss.Channel;
23 import com.sun.syndication.feed.rss.Description;
24 import com.sun.syndication.feed.rss.Image;
25 import com.sun.syndication.feed.rss.Item;
26 import com.sun.syndication.feed.rss.TextInput;
27 import org.jdom.Document;
28 import org.jdom.Element;
29 import org.jdom.Namespace;
30
31 import java.util.List;
32
33
34 /***
35 * Feed Generator for RSS 0.90
36 * <p/>
37 *
38 * @author Elaine Chien
39 *
40 */
41 public class RSS090Generator implements WireFeedGenerator {
42
43 private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
44 private static final String RSS_URI = "http://my.netscape.com/rdf/simple/0.9/";
45
46 private static final Namespace RDF_NS = Namespace.getNamespace("rdf", RDF_URI);
47 private static final Namespace RSS_NS = Namespace.getNamespace(RSS_URI);
48
49 private String _type;
50
51 public RSS090Generator() {
52 this("rss_0.9");
53 }
54
55 protected RSS090Generator(String feedType) {
56 _type = feedType;
57 }
58
59 public String getType() {
60 return _type;
61 }
62
63 public Document generate(WireFeed feed) throws FeedException {
64
65 Channel channel = (Channel)feed;
66 Element rootElement = generateRootElement(channel);
67 Document doc = new Document(rootElement);
68
69 return doc;
70 }
71
72 protected Element generateRootElement(Channel channel)
73 throws FeedException {
74
75 Element root = new Element("RDF");
76
77 root.setNamespace(RDF_NS);
78 root.addNamespaceDeclaration(RDF_NS);
79 root.addNamespaceDeclaration(RSS_NS);
80
81 if (channel == null) {
82 throw new FeedException("invalid RSS Channel - missing required channel element");
83 }
84 root.addContent(generateChannelElement(channel));
85
86 if (channel.getImage() != null) {
87 root.addContent(generateImageElement(channel.getImage()));
88 }
89
90 List items = channel.getItems();
91 if (items != null) {
92 for (int i = 0; i < items.size(); i++) {
93 root.addContent(generateItemElement((Item)items.get(i)));
94 }
95 }
96
97 if (channel.getTextInput() != null) {
98 root.addContent(generateTextInputElement(channel.getTextInput()));
99 }
100
101 return root;
102 }
103
104
105 protected Element generateChannelElement(Channel channel)
106 throws FeedException {
107
108 String title = channel.getTitle();
109 String link = channel.getLink();
110 String description = channel.getDescription();
111
112 if (title == null) {
113 throw new FeedException("invalid RSS Channel - missing required title element");
114 }
115 if (link == null) {
116 throw new FeedException("invalid RSS Channel - missing required link element");
117 }
118 if (description == null) {
119 throw new FeedException("invalid RSS Channel - missing required description element");
120 }
121
122 Element channelElement = new Element("channel", RSS_NS);
123 channelElement.addContent(generateSimpleElement("title", title, RSS_NS));
124 channelElement.addContent(generateSimpleElement("link", link, RSS_NS));
125 channelElement.addContent(generateSimpleElement("description", description, RSS_NS));
126
127 return channelElement;
128 }
129
130
131 protected Element generateImageElement(Image image)
132 throws FeedException {
133
134 String title = image.getTitle();
135 String url = image.getUrl();
136 String link = image.getLink();
137
138 if (title == null) {
139 throw new FeedException("invalid RSS Image - missing required title element");
140 }
141 if (url == null) {
142 throw new FeedException("invalid RSS Image - missing required url element");
143 }
144 if (link == null) {
145 throw new FeedException("invalid RSS Image - missing required link element");
146 }
147
148 Element imageElement = new Element("image", RSS_NS);
149 imageElement.addContent(generateSimpleElement("title", title, RSS_NS));
150 imageElement.addContent(generateSimpleElement("url", url, RSS_NS));
151 imageElement.addContent(generateSimpleElement("link", link, RSS_NS));
152
153 return imageElement;
154 }
155
156
157 protected Element generateItemElement(Item item)
158 throws FeedException {
159
160 String title = item.getTitle();
161 String link = item.getLink();
162
163 if (title == null) {
164 throw new FeedException("invalid RSS Item - missing required title element");
165 }
166 if (link == null) {
167 throw new FeedException("invalid RSS Item - missing required link element");
168 }
169
170 Element itemElement = new Element("item", RSS_NS);
171 itemElement.addContent(generateSimpleElement("title", title, RSS_NS));
172 itemElement.addContent(generateSimpleElement("link", link, RSS_NS));
173
174 return itemElement;
175
176 }
177
178 protected Element generateTextInputElement(TextInput textInput)
179 throws FeedException {
180
181 String title = textInput.getTitle();
182 String description = textInput.getDescription();
183 String name = textInput.getName();
184 String link = textInput.getLink();
185
186 if (title == null) {
187 throw new FeedException("invalid RSS TextInput - missing required title element");
188 }
189 if (description == null) {
190 throw new FeedException("invalid RSS TextInput - missing required description element");
191 }
192 if (name == null) {
193 throw new FeedException("invalid RSS TextInput - missing required name element");
194 }
195 if (link == null) {
196 throw new FeedException("invalid RSS TextInput - missing required link element");
197 }
198
199 Element textInputElement = new Element("textInput", RSS_NS);
200 textInputElement.addContent(generateSimpleElement("title", title, RSS_NS));
201 textInputElement.addContent(generateDescriptionElement(textInput.getDescription(), RSS_NS));
202 textInputElement.addContent(generateSimpleElement("name", name, RSS_NS));
203 textInputElement.addContent(generateSimpleElement("link", link, RSS_NS));
204
205 return textInputElement;
206 }
207
208
209 protected Element generateDescriptionElement(Description description, Namespace namespace)
210 throws FeedException {
211
212 return generateDescriptionElement(description.getValue(), namespace);
213 }
214
215
216 protected Element generateDescriptionElement(String description, Namespace namespace)
217 throws FeedException {
218
219 return generateSimpleElement("description", description, namespace);
220 }
221
222 protected Element generateSimpleElement(String name, String value, Namespace namespace)
223 throws FeedException {
224
225 Element element = new Element(name, namespace);
226 element.addContent(value);
227
228 return element;
229 }
230
231 }