View Javadoc

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