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