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.util.Iterator;
21  import java.util.List;
22  
23  import org.jdom.Attribute;
24  import org.jdom.Document;
25  import org.jdom.Element;
26  import org.jdom.Namespace;
27  import org.jdom.input.SAXBuilder;
28  
29  import com.sun.syndication.feed.WireFeed;
30  import com.sun.syndication.feed.atom.Category;
31  import com.sun.syndication.feed.atom.Content;
32  import com.sun.syndication.feed.atom.Entry;
33  import com.sun.syndication.feed.atom.Feed;
34  import com.sun.syndication.feed.atom.Generator;
35  import com.sun.syndication.feed.atom.Link;
36  import com.sun.syndication.feed.atom.Person;
37  import com.sun.syndication.io.FeedException;
38  
39  /***
40   * Feed Generator for Atom
41   * <p/>
42   *
43   * @author Elaine Chien
44   * @author Dave Johnson (updated for Atom 1.0)
45   *
46   */
47  
48  public class Atom10Generator extends BaseWireFeedGenerator {
49      private static final String ATOM_10_URI = "http://www.w3.org/2005/Atom";
50      private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_10_URI);
51  
52      private String _version;
53  
54      public Atom10Generator() {
55          this("atom_1.0","1.0");
56      }
57  
58      protected Atom10Generator(String type,String version) {
59          super(type);
60          _version = version;
61      }
62  
63      protected String getVersion() {
64          return _version;
65      }
66  
67      protected Namespace getFeedNamespace() {
68          return ATOM_NS;
69      }
70  
71      public Document generate(WireFeed wFeed) throws FeedException {
72          Feed feed = (Feed) wFeed;
73          Element root = createRootElement(feed);
74          populateFeed(feed,root);
75          return createDocument(root);
76      }
77  
78      protected Document createDocument(Element root) {
79          return new Document(root);
80      }
81  
82      protected Element createRootElement(Feed feed) {
83          Element root = new Element("feed",getFeedNamespace());
84          root.addNamespaceDeclaration(getFeedNamespace());
85          //Attribute version = new Attribute("version", getVersion());
86          //root.setAttribute(version);
87          if (feed.getXmlBase() != null) {
88              root.setAttribute("base", feed.getXmlBase(), Namespace.XML_NAMESPACE);
89          }
90          generateModuleNamespaceDefs(root);
91          return root;
92      }
93  
94      protected void populateFeed(Feed feed,Element parent) throws FeedException  {
95          addFeed(feed,parent);
96          addEntries(feed,parent);
97      }
98  
99      protected void addFeed(Feed feed,Element parent) throws FeedException {
100         Element eFeed = parent;
101         populateFeedHeader(feed,eFeed);
102         checkFeedHeaderConstraints(eFeed);
103         generateFeedModules(feed.getModules(),eFeed);
104     }
105 
106     protected void addEntries(Feed feed,Element parent) throws FeedException {
107         List items = feed.getEntries();
108         for (int i=0;i<items.size();i++) {
109             addEntry((Entry)items.get(i),parent);
110         }
111         checkEntriesConstraints(parent);
112     }
113 
114     protected void addEntry(Entry entry,Element parent) throws FeedException {
115         Element eEntry = new Element("entry", getFeedNamespace());
116         if (entry.getXmlBase() != null) {
117             eEntry.setAttribute("base", entry.getXmlBase(), Namespace.XML_NAMESPACE);
118         }
119         populateEntry(entry,eEntry);
120         checkEntryConstraints(eEntry);
121         generateItemModules(entry.getModules(),eEntry);
122         parent.addContent(eEntry);
123     }
124 
125     protected void populateFeedHeader(Feed feed,Element eFeed) throws FeedException {
126         if (feed.getTitle() != null) {
127             eFeed.addContent(generateSimpleElement("title", feed.getTitle()));
128         }
129 
130         List links = feed.getAlternateLinks();
131         if (links != null) for (int i = 0; i < links.size(); i++) {
132             eFeed.addContent(generateLinkElement((Link)links.get(i)));
133         }
134         links = feed.getOtherLinks();
135         if (links != null) for (int j = 0; j < links.size(); j++) {
136             eFeed.addContent(generateLinkElement((Link)links.get(j)));
137         }
138 
139         List cats = feed.getCategories();
140         if (cats != null) for (Iterator iter=cats.iterator(); iter.hasNext();) {
141             eFeed.addContent(generateCategoryElement((Category)iter.next()));
142         }
143             
144         List authors = feed.getAuthors();
145         if (authors != null && authors.size() > 0) {
146             for (int i = 0; i < authors.size(); i++) {
147                 Element authorElement = new Element("author", getFeedNamespace());
148                 fillPersonElement(authorElement, (Person)feed.getAuthors().get(i));
149                 eFeed.addContent(authorElement);
150             }
151         }
152 
153         List contributors = feed.getContributors();
154         if (contributors != null && contributors.size() > 0) {
155             for (int i = 0; i < contributors.size(); i++) {
156                 Element contributorElement = new Element("contributor", getFeedNamespace());
157                 fillPersonElement(contributorElement, (Person)contributors.get(i));
158                 eFeed.addContent(contributorElement);
159             }
160         }
161 
162         if (feed.getSubtitle() != null) {
163             eFeed.addContent(
164                 generateSimpleElement("subtitle", feed.getSubtitle().getValue()));
165         }
166 
167         if (feed.getId() != null) {
168             eFeed.addContent(generateSimpleElement("id", feed.getId()));
169         }
170 
171         if (feed.getGenerator() != null) {
172             eFeed.addContent(generateGeneratorElement(feed.getGenerator()));
173         }
174 
175         if (feed.getRights() != null) {
176             eFeed.addContent(generateSimpleElement("rights", feed.getRights()));
177         }
178 
179         if (feed.getUpdated() != null) {
180             Element updatedElement = new Element("updated", getFeedNamespace());
181             updatedElement.addContent(DateParser.formatW3CDateTime(feed.getUpdated()));
182             eFeed.addContent(updatedElement);
183         }
184     }
185 
186     protected void populateEntry(Entry entry, Element eEntry) throws FeedException {
187         if (entry.getTitle() != null) {
188             eEntry.addContent(generateSimpleElement("title", entry.getTitle()));
189         }
190         List links = entry.getAlternateLinks();
191         if (links != null) {
192             for (int i = 0; i < links.size(); i++) {
193                 eEntry.addContent(generateLinkElement((Link)links.get(i)));
194             }
195         }
196         links = entry.getOtherLinks();
197         if (links != null) {
198             for (int i = 0; i < links.size(); i++) {
199                 eEntry.addContent(generateLinkElement((Link)links.get(i)));
200             }
201         }
202 
203         List cats = entry.getCategories();
204         if (cats != null) {
205             for (int i = 0; i < cats.size(); i++) {
206                 eEntry.addContent(generateCategoryElement((Category)cats.get(i)));
207             }
208         }
209         
210         List authors = entry.getAuthors();
211         if (authors != null && authors.size() > 0) {
212             for (int i = 0; i < authors.size(); i++)  {
213                 Element authorElement = new Element("author", getFeedNamespace());
214                 fillPersonElement(authorElement, (Person)entry.getAuthors().get(i));
215                 eEntry.addContent(authorElement);            
216             }
217         }
218 
219         List contributors = entry.getContributors();
220         if (contributors != null && contributors.size() > 0) {
221             for (int i = 0; i < contributors.size(); i++) {
222                 Element contributorElement = new Element("contributor", getFeedNamespace());
223                 fillPersonElement(contributorElement, (Person)contributors.get(i));
224                 eEntry.addContent(contributorElement);
225             }
226         }
227         if (entry.getId() != null) {
228             eEntry.addContent(generateSimpleElement("id", entry.getId()));
229         }
230 
231         if (entry.getUpdated() != null) {
232             Element updatedElement = new Element("updated", getFeedNamespace());
233             updatedElement.addContent(DateParser.formatW3CDateTime(entry.getUpdated()));
234             eEntry.addContent(updatedElement);
235         }
236 
237         if (entry.getPublished() != null) {
238             Element publishedElement = new Element("published", getFeedNamespace());
239             publishedElement.addContent(DateParser.formatW3CDateTime(entry.getPublished()));
240             eEntry.addContent(publishedElement);
241         }
242 
243         if (entry.getContents() != null && entry.getContents().size() > 0) {
244             Element contentElement = new Element("content", getFeedNamespace());
245             Content content = (Content)entry.getContents().get(0);
246             fillContentElement(contentElement, content);
247             eEntry.addContent(contentElement);
248         }
249 
250         if (entry.getSummary() != null) {
251             Element summaryElement = new Element("summary", getFeedNamespace());
252             fillContentElement(summaryElement, entry.getSummary());
253             eEntry.addContent(summaryElement);
254         }
255     }
256 
257     protected void checkFeedHeaderConstraints(Element eFeed) throws FeedException {
258     }
259 
260     protected void checkEntriesConstraints(Element parent) throws FeedException {
261     }
262 
263     protected void checkEntryConstraints(Element eEntry) throws FeedException {
264     }
265 
266 
267     protected Element generateCategoryElement(Category cat) {
268         Element catElement = new Element("category", getFeedNamespace());
269 
270         if (cat.getTerm() != null) {
271             Attribute termAttribute = new Attribute("term", cat.getTerm());
272             catElement.setAttribute(termAttribute);
273         }
274 
275         if (cat.getLabel() != null) {
276             Attribute labelAttribute = new Attribute("label", cat.getLabel());
277             catElement.setAttribute(labelAttribute);
278         }
279 
280         if (cat.getScheme() != null) {
281             Attribute schemeAttribute = new Attribute("scheme", cat.getScheme());
282             catElement.setAttribute(schemeAttribute);
283         }
284         return catElement;
285     }
286 
287     protected Element generateLinkElement(Link link) {
288         Element linkElement = new Element("link", getFeedNamespace());
289 
290         if (link.getRel() != null) {
291             Attribute relAttribute = new Attribute("rel", link.getRel().toString());
292             linkElement.setAttribute(relAttribute);
293         }
294 
295         if (link.getType() != null) {
296             Attribute typeAttribute = new Attribute("type", link.getType());
297             linkElement.setAttribute(typeAttribute);
298         }
299 
300         if (link.getHref() != null) {
301             Attribute hrefAttribute = new Attribute("href", link.getHref());
302             linkElement.setAttribute(hrefAttribute);
303         }
304         
305         if (link.getHreflang() != null) {
306             Attribute hreflangAttribute = new Attribute("hreflang", link.getHreflang());
307             linkElement.setAttribute(hreflangAttribute);
308         }
309         return linkElement;
310     }
311 
312 
313     protected void fillPersonElement(Element element, Person person) {
314         if (person.getName() != null) {
315             element.addContent(generateSimpleElement("name", person.getName()));
316         }
317         if (person.getUri() != null) {
318             element.addContent(generateSimpleElement("uri", person.getUri()));
319         }
320 
321         if (person.getEmail() != null) {
322             element.addContent(generateSimpleElement("email", person.getEmail()));
323         }
324     }
325 
326     protected Element generateTagLineElement(Content tagline) {
327         Element taglineElement = new Element("subtitle", getFeedNamespace());
328 
329         if (tagline.getType() != null) {
330             Attribute typeAttribute = new Attribute("type", tagline.getType());
331             taglineElement.setAttribute(typeAttribute);
332         }
333 
334         if (tagline.getValue() != null) {
335             taglineElement.addContent(tagline.getValue());
336         }
337         return taglineElement;
338     }
339 
340     protected void fillContentElement(Element contentElement, Content content)
341         throws FeedException {
342 
343         String type = content.getType();
344         if (type != null) {
345             Attribute typeAttribute = new Attribute("type", type);
346             contentElement.setAttribute(typeAttribute);
347         }
348         String href = content.getSrc();
349         if (href != null) {
350             Attribute srcAttribute = new Attribute("src", href);
351             contentElement.setAttribute(srcAttribute);
352         }
353 
354         if (content.getValue() != null) {
355 
356             if (type == null || type.equals(Content.TEXT)) {
357                 contentElement.addContent(content.getValue());
358             } else if (type.equals(Content.HTML)) {
359                 contentElement.addContent(content.getValue());
360             } else if (type.equals(Content.XHTML)) {
361 
362                 StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
363                 tmpDocString.append(content.getValue());
364                 tmpDocString.append("</tmpdoc>");
365                 StringReader tmpDocReader = new StringReader(tmpDocString.toString());
366                 Document tmpDoc;
367 
368                 try {
369                     SAXBuilder saxBuilder = new SAXBuilder();
370                     tmpDoc = saxBuilder.build(tmpDocReader);
371                 }
372                 catch (Exception ex) {
373                     throw new FeedException("Invalid XML",ex);
374                 }
375 
376                 List children = tmpDoc.getRootElement().removeContent();
377                 contentElement.addContent(children);
378             }
379         }
380     }
381 
382     protected Element generateGeneratorElement(Generator generator) {
383         Element generatorElement = new Element("generator", getFeedNamespace());
384 
385         if (generator.getUrl() != null) {
386             Attribute urlAttribute = new Attribute("uri", generator.getUrl());
387             generatorElement.setAttribute(urlAttribute);
388         }
389 
390         if (generator.getVersion() != null) {
391             Attribute versionAttribute = new Attribute("version", generator.getVersion());
392             generatorElement.setAttribute(versionAttribute);
393         }
394 
395         if (generator.getValue() != null) {
396             generatorElement.addContent(generator.getValue());
397         }
398 
399         return generatorElement;
400 
401     }
402 
403     protected Element generateSimpleElement(String name, String value) {
404         Element element = new Element(name, getFeedNamespace());
405         element.addContent(value);
406         return element;
407     }
408 
409 }