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.WireFeed;
24  import com.sun.syndication.io.FeedException;
25  import com.sun.syndication.io.WireFeedGenerator;
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 WireFeedGenerator {
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(WireFeed 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                 contentElement.addContent(Base64.encode(content.getValue()));
244             } else if (mode.equals(Content.XML)) {
245 
246                 StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
247                 tmpDocString.append(content.getValue());
248                 tmpDocString.append("</tmpdoc>");
249                 StringReader tmpDocReader = new StringReader(tmpDocString.toString());
250                 Document tmpDoc = null;
251 
252                 try {
253                     SAXBuilder saxBuilder = new SAXBuilder();
254                     tmpDoc = saxBuilder.build(tmpDocReader);
255                 }
256                 catch (Exception ex) {
257                     throw new FeedException("Invalid XML",ex);
258                 }
259 
260                 List children = tmpDoc.getRootElement().removeContent();
261                 contentElement.addContent(children);
262             }
263         }
264     }
265 
266     protected Element generateGeneratorElement(Generator generator)
267         throws FeedException  {
268 
269         Element generatorElement = new Element("generator", ATOM_NS);
270 
271         if (generator.getUrl() != null) {
272             Attribute urlAttribute = new Attribute("url", generator.getUrl());
273             generatorElement.setAttribute(urlAttribute);
274         }
275 
276         if (generator.getVersion() != null) {
277             Attribute versionAttribute = new Attribute("version", generator.getVersion());
278             generatorElement.setAttribute(versionAttribute);
279         }
280 
281         if (generator.getValue() != null) {
282             generatorElement.addContent(generator.getValue());
283         }
284 
285         return generatorElement;
286 
287     }
288 
289     protected Element generateEntryElement(Entry entry)
290         throws FeedException  {
291 
292         Element entryElement = new Element("entry", ATOM_NS);
293 
294         if (entry.getTitle() != null) {
295             entryElement.addContent(generateSimpleElement("title", entry.getTitle()));
296         }
297         List links = entry.getAlternateLinks();
298         for (int i = 0; i < links.size(); i++) {
299             entryElement.addContent(generateLinkElement((Link)links.get(i)));
300         }
301 
302         links = entry.getOtherLinks();
303         for (int i = 0; i < links.size(); i++) {
304             entryElement.addContent(generateLinkElement((Link)links.get(i)));
305         }
306 
307         if (entry.getAuthor() != null) {
308             Element authorElement = new Element("author", ATOM_NS);
309             fillPersonElement(authorElement, entry.getAuthor());
310             entryElement.addContent(authorElement);
311         }
312 
313         List contributors = entry.getContributors();
314         for (int i = 0; i < contributors.size(); i++) {
315             Element contributorElement = new Element("contributor", ATOM_NS);
316             fillPersonElement(contributorElement, (Person)contributors.get(i));
317             entryElement.addContent(contributorElement);
318         }
319         if (entry.getId() != null) {
320             entryElement.addContent(generateSimpleElement("id", entry.getId()));
321         }
322 
323         if (entry.getModified() != null) {
324             Element modifiedElement = new Element("modified", ATOM_NS);
325             modifiedElement.addContent(DateParser.parseW3CDateTime(entry.getModified()));
326             entryElement.addContent(modifiedElement);
327         }
328 
329         if (entry.getIssued() != null) {
330             Element issuedElement = new Element("issued", ATOM_NS);
331             issuedElement.addContent(DateParser.parseW3CDateTime(entry.getIssued()));
332             entryElement.addContent(issuedElement);
333         }
334 
335         if (entry.getCreated() != null) {
336             Element createdElement = new Element("created", ATOM_NS);
337             createdElement.addContent(DateParser.parseW3CDateTime(entry.getCreated()));
338             entryElement.addContent(createdElement);
339         }
340 
341         if (entry.getSummary() != null) {
342             Element summaryElement = new Element("summary", ATOM_NS);
343             fillContentElement(summaryElement, entry.getSummary());
344             entryElement.addContent(summaryElement);
345         }
346 
347         List contents = entry.getContents();
348         for (int i = 0; i < contents.size(); i++) {
349             Element contentElement = new Element("content", ATOM_NS);
350             fillContentElement(contentElement, (Content)contents.get(i));
351             entryElement.addContent(contentElement);
352         }
353 
354         List modules = entry.getModules();
355         if (modules!=null) {
356             ModulesGenerator modulesGenerator = new ModulesGenerator();
357             modulesGenerator.generateFeedModules(modules, entryElement);
358         }
359 
360         return entryElement;
361 
362     }
363 
364 
365     protected Element generateSimpleElement(String name, String value)
366         throws FeedException  {
367 
368         Element element = new Element(name, ATOM_NS);
369         element.addContent(value);
370 
371         return element;
372     }
373 }