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