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