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