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 java.io.StringReader;
20 import java.util.List;
21
22 import com.sun.syndication.feed.WireFeed;
23 import com.sun.syndication.io.FeedException;
24 import com.sun.syndication.io.WireFeedGenerator;
25
26 import org.jdom.Attribute;
27 import org.jdom.Document;
28 import org.jdom.Element;
29 import org.jdom.Namespace;
30 import org.jdom.input.SAXBuilder;
31
32 import com.sun.syndication.feed.atom.*;
33
34 /***
35 * Feed Generator for Atom
36 * <p/>
37 *
38 * @author Elaine Chien
39 *
40 */
41
42 public class Atom03Generator implements WireFeedGenerator {
43
44 /***
45 * atom_0.3.feed.ModuleGenerator.classes= [className] ...
46 *
47 */
48 public static final String FEED_MODULE_GENERATORS_KEY = "atom_0.3.feed.ModuleGenerator.classes";
49
50 /***
51 * atom_0.3.item.ModuleGenerator.classes= [className] ...
52 *
53 */
54 public static final String ITEM_MODULE_GENERATORS_KEY = "atom_0.3.item.ModuleGenerator.classes";
55
56 private static ModuleGenerators FEED_MODULES_GENERATOR = new ModuleGenerators(FEED_MODULE_GENERATORS_KEY);
57 private static ModuleGenerators ITEM_MODULES_GENERATOR = new ModuleGenerators(ITEM_MODULE_GENERATORS_KEY);
58
59 private String _type;
60
61 private static final String VERSION = "0.3";
62 private static final String ATOM_URI = "http://purl.org/atom/ns#";
63 private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_URI);
64
65 private static final String DC_URI = "http://purl.org/dc/elements/1.1/";
66 private static final String SY_URI = "http://purl.org/rss/1.0/modules/syndication/";
67 private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
68 private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
69
70 private static final Namespace DC_NS = Namespace.getNamespace("dc", DC_URI);
71 private static final Namespace SY_NS = Namespace.getNamespace("sy", SY_URI);
72 private static final Namespace TAXO_NS = Namespace.getNamespace("taxo", TAXO_URI);
73 private static final Namespace RDF_NS = Namespace.getNamespace("rdf", RDF_URI);
74
75 public Atom03Generator() {
76 _type = "atom_0.3";
77 }
78
79 public String getType() {
80 return _type;
81 }
82
83 public Document generate(WireFeed feed) throws FeedException {
84 Element rootElement = generateRootElement((Feed)feed);
85 Document doc = new Document(rootElement);
86 return doc;
87 }
88
89 protected Element generateRootElement(Feed feed)
90 throws FeedException {
91
92 Element root = new Element("feed");
93 root.setNamespace(ATOM_NS);
94 root.addNamespaceDeclaration(ATOM_NS);
95
96
97 root.addNamespaceDeclaration(DC_NS);
98 root.addNamespaceDeclaration(TAXO_NS);
99 root.addNamespaceDeclaration(RDF_NS);
100 root.addNamespaceDeclaration(SY_NS);
101
102 Attribute version = new Attribute("version", VERSION);
103 root.setAttribute(version);
104
105 if (feed == null) {
106 throw new FeedException("invalid Atom Feed - missing required feed element");
107 }
108
109 if (feed.getTitle() != null) {
110 root.addContent(generateSimpleElement("title", feed.getTitle()));
111 }
112
113 List links = feed.getAlternateLinks();
114 for (int i = 0; i < links.size(); i++) {
115 root.addContent(generateLinkElement((Link)links.get(i)));
116 }
117
118 links = feed.getOtherLinks();
119 for (int i = 0; i < links.size(); i++) {
120 root.addContent(generateLinkElement((Link)links.get(i)));
121 }
122
123 if (feed.getAuthor() != null) {
124 Element authorElement = new Element("author", ATOM_NS);
125 fillPersonElement(authorElement, feed.getAuthor());
126 root.addContent(authorElement);
127 }
128
129 List contributors = feed.getContributors();
130 for (int i = 0; i < contributors.size(); i++) {
131 Element contributorElement = new Element("contributor", ATOM_NS);
132 fillPersonElement(contributorElement, (Person)contributors.get(i));
133 root.addContent(contributorElement);
134 }
135
136 if (feed.getTagline() != null) {
137 Element taglineElement = new Element("tagline", ATOM_NS);
138 fillContentElement(taglineElement, feed.getTagline());
139 root.addContent(taglineElement);
140 }
141
142 if (feed.getId() != null) {
143 root.addContent(generateSimpleElement("id", feed.getId()));
144 }
145
146 if (feed.getGenerator() != null) {
147 root.addContent(generateGeneratorElement(feed.getGenerator()));
148 }
149
150 if (feed.getCopyright() != null) {
151 root.addContent(generateSimpleElement("copyright", feed.getCopyright()));
152 }
153
154 if (feed.getInfo() != null) {
155 Element infoElement = new Element("info", ATOM_NS);
156 fillContentElement(infoElement, feed.getInfo());
157 root.addContent(infoElement);
158 }
159
160 if (feed.getModified() != null) {
161 Element modifiedElement = new Element("modified", ATOM_NS);
162 modifiedElement.addContent(DateParser.parseW3CDateTime(feed.getModified()));
163 root.addContent(modifiedElement);
164 }
165
166 List entries = feed.getEntries();
167 for (int i = 0; i < entries.size(); i++) {
168 root.addContent(generateEntryElement((Entry)entries.get(i)));
169 }
170
171 List modules = feed.getModules();
172 if (modules!=null) {
173 FEED_MODULES_GENERATOR.generateModules(modules, root);
174 }
175
176 return root;
177 }
178
179 protected Element generateLinkElement(Link link)
180 throws FeedException {
181
182 Element linkElement = new Element("link", ATOM_NS);
183
184 if (link.getRel() != null) {
185 Attribute relAttribute = new Attribute("rel", link.getRel().toString());
186 linkElement.setAttribute(relAttribute);
187 }
188
189 if (link.getType() != null) {
190 Attribute typeAttribute = new Attribute("type", link.getType());
191 linkElement.setAttribute(typeAttribute);
192 }
193
194 if (link.getHref() != null) {
195 Attribute hrefAttribute = new Attribute("href", link.getHref());
196 linkElement.setAttribute(hrefAttribute);
197 }
198 return linkElement;
199 }
200
201
202 protected void fillPersonElement(Element element, Person person)
203 throws FeedException {
204
205 if (person.getName() != null) {
206 element.addContent(generateSimpleElement("name", person.getName()));
207 }
208 if (person.getUrl() != null) {
209 element.addContent(generateSimpleElement("url", person.getUrl()));
210 }
211
212 if (person.getEmail() != null) {
213 element.addContent(generateSimpleElement("email", person.getEmail()));
214 }
215 }
216
217
218 protected Element generateTagLineElement(Content tagline)
219 throws FeedException {
220
221 Element taglineElement = new Element("tagline", ATOM_NS);
222
223 if (tagline.getType() != null) {
224 Attribute typeAttribute = new Attribute("type", tagline.getType());
225 taglineElement.setAttribute(typeAttribute);
226 }
227
228 if (tagline.getValue() != null) {
229 taglineElement.addContent(tagline.getValue());
230 }
231
232 return taglineElement;
233
234 }
235
236 protected void fillContentElement(Element contentElement, Content content)
237 throws FeedException {
238
239 if (content.getType() != null) {
240 Attribute typeAttribute = new Attribute("type", content.getType());
241 contentElement.setAttribute(typeAttribute);
242 }
243
244 Content.Mode mode = content.getMode();
245 if (mode != null) {
246 Attribute modeAttribute = new Attribute("mode", content.getMode().toString());
247 contentElement.setAttribute(modeAttribute);
248 }
249
250 if (content.getValue() != null) {
251
252 if (mode == null || mode.equals(Content.ESCAPED)) {
253 contentElement.addContent(content.getValue());
254 } else if (mode.equals(Content.BASE64)) {
255 contentElement.addContent(Base64.encode(content.getValue()));
256 } else if (mode.equals(Content.XML)) {
257
258 StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
259 tmpDocString.append(content.getValue());
260 tmpDocString.append("</tmpdoc>");
261 StringReader tmpDocReader = new StringReader(tmpDocString.toString());
262 Document tmpDoc = null;
263
264 try {
265 SAXBuilder saxBuilder = new SAXBuilder();
266 tmpDoc = saxBuilder.build(tmpDocReader);
267 }
268 catch (Exception ex) {
269 throw new FeedException("Invalid XML",ex);
270 }
271
272 List children = tmpDoc.getRootElement().removeContent();
273 contentElement.addContent(children);
274 }
275 }
276 }
277
278 protected Element generateGeneratorElement(Generator generator)
279 throws FeedException {
280
281 Element generatorElement = new Element("generator", ATOM_NS);
282
283 if (generator.getUrl() != null) {
284 Attribute urlAttribute = new Attribute("url", generator.getUrl());
285 generatorElement.setAttribute(urlAttribute);
286 }
287
288 if (generator.getVersion() != null) {
289 Attribute versionAttribute = new Attribute("version", generator.getVersion());
290 generatorElement.setAttribute(versionAttribute);
291 }
292
293 if (generator.getValue() != null) {
294 generatorElement.addContent(generator.getValue());
295 }
296
297 return generatorElement;
298
299 }
300
301 protected Element generateEntryElement(Entry entry)
302 throws FeedException {
303
304 Element entryElement = new Element("entry", ATOM_NS);
305
306 if (entry.getTitle() != null) {
307 entryElement.addContent(generateSimpleElement("title", entry.getTitle()));
308 }
309 List links = entry.getAlternateLinks();
310 for (int i = 0; i < links.size(); i++) {
311 entryElement.addContent(generateLinkElement((Link)links.get(i)));
312 }
313
314 links = entry.getOtherLinks();
315 for (int i = 0; i < links.size(); i++) {
316 entryElement.addContent(generateLinkElement((Link)links.get(i)));
317 }
318
319 if (entry.getAuthor() != null) {
320 Element authorElement = new Element("author", ATOM_NS);
321 fillPersonElement(authorElement, entry.getAuthor());
322 entryElement.addContent(authorElement);
323 }
324
325 List contributors = entry.getContributors();
326 for (int i = 0; i < contributors.size(); i++) {
327 Element contributorElement = new Element("contributor", ATOM_NS);
328 fillPersonElement(contributorElement, (Person)contributors.get(i));
329 entryElement.addContent(contributorElement);
330 }
331 if (entry.getId() != null) {
332 entryElement.addContent(generateSimpleElement("id", entry.getId()));
333 }
334
335 if (entry.getModified() != null) {
336 Element modifiedElement = new Element("modified", ATOM_NS);
337 modifiedElement.addContent(DateParser.parseW3CDateTime(entry.getModified()));
338 entryElement.addContent(modifiedElement);
339 }
340
341 if (entry.getIssued() != null) {
342 Element issuedElement = new Element("issued", ATOM_NS);
343 issuedElement.addContent(DateParser.parseW3CDateTime(entry.getIssued()));
344 entryElement.addContent(issuedElement);
345 }
346
347 if (entry.getCreated() != null) {
348 Element createdElement = new Element("created", ATOM_NS);
349 createdElement.addContent(DateParser.parseW3CDateTime(entry.getCreated()));
350 entryElement.addContent(createdElement);
351 }
352
353 if (entry.getSummary() != null) {
354 Element summaryElement = new Element("summary", ATOM_NS);
355 fillContentElement(summaryElement, entry.getSummary());
356 entryElement.addContent(summaryElement);
357 }
358
359 List contents = entry.getContents();
360 for (int i = 0; i < contents.size(); i++) {
361 Element contentElement = new Element("content", ATOM_NS);
362 fillContentElement(contentElement, (Content)contents.get(i));
363 entryElement.addContent(contentElement);
364 }
365
366 List modules = entry.getModules();
367 if (modules!=null) {
368 ITEM_MODULES_GENERATOR.generateModules(modules, entryElement);
369 }
370
371 return entryElement;
372 }
373
374
375 protected Element generateSimpleElement(String name, String value)
376 throws FeedException {
377
378 Element element = new Element(name, ATOM_NS);
379 element.addContent(value);
380
381 return element;
382 }
383 }