001: /*
002: * Copyright 2004 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.io.impl;
018:
019: import com.sun.syndication.feed.WireFeed;
020: import com.sun.syndication.feed.atom.*;
021: import com.sun.syndication.feed.synd.SyndPerson;
022: import com.sun.syndication.io.FeedException;
023: import org.jdom.Attribute;
024: import org.jdom.Document;
025: import org.jdom.Element;
026: import org.jdom.Namespace;
027: import org.jdom.input.SAXBuilder;
028:
029: import java.io.StringReader;
030: import java.util.List;
031:
032: /**
033: * Feed Generator for Atom
034: * <p/>
035: *
036: * @author Elaine Chien
037: *
038: */
039:
040: public class Atom03Generator extends BaseWireFeedGenerator {
041: private static final String ATOM_03_URI = "http://purl.org/atom/ns#";
042: private static final Namespace ATOM_NS = Namespace
043: .getNamespace(ATOM_03_URI);
044:
045: private String _version;
046:
047: public Atom03Generator() {
048: this ("atom_0.3", "0.3");
049: }
050:
051: protected Atom03Generator(String type, String version) {
052: super (type);
053: _version = version;
054: }
055:
056: protected String getVersion() {
057: return _version;
058: }
059:
060: protected Namespace getFeedNamespace() {
061: return ATOM_NS;
062: }
063:
064: public Document generate(WireFeed wFeed) throws FeedException {
065: Feed feed = (Feed) wFeed;
066: Element root = createRootElement(feed);
067: populateFeed(feed, root);
068: return createDocument(root);
069: }
070:
071: protected Document createDocument(Element root) {
072: return new Document(root);
073: }
074:
075: protected Element createRootElement(Feed feed) {
076: Element root = new Element("feed", getFeedNamespace());
077: root.addNamespaceDeclaration(getFeedNamespace());
078: Attribute version = new Attribute("version", getVersion());
079: root.setAttribute(version);
080: generateModuleNamespaceDefs(root);
081: return root;
082: }
083:
084: protected void populateFeed(Feed feed, Element parent)
085: throws FeedException {
086: addFeed(feed, parent);
087: addEntries(feed, parent);
088: }
089:
090: protected void addFeed(Feed feed, Element parent)
091: throws FeedException {
092: Element eFeed = parent;
093: populateFeedHeader(feed, eFeed);
094: checkFeedHeaderConstraints(eFeed);
095: generateFeedModules(feed.getModules(), eFeed);
096: generateForeignMarkup(eFeed, (List) feed.getForeignMarkup());
097: }
098:
099: protected void addEntries(Feed feed, Element parent)
100: throws FeedException {
101: List items = feed.getEntries();
102: for (int i = 0; i < items.size(); i++) {
103: addEntry((Entry) items.get(i), parent);
104: }
105: checkEntriesConstraints(parent);
106: }
107:
108: protected void addEntry(Entry entry, Element parent)
109: throws FeedException {
110: Element eEntry = new Element("entry", getFeedNamespace());
111: populateEntry(entry, eEntry);
112: checkEntryConstraints(eEntry);
113: generateItemModules(entry.getModules(), eEntry);
114: parent.addContent(eEntry);
115: }
116:
117: protected void populateFeedHeader(Feed feed, Element eFeed)
118: throws FeedException {
119: if (feed.getTitle() != null) {
120: eFeed.addContent(generateSimpleElement("title", feed
121: .getTitle()));
122: }
123:
124: List links = feed.getAlternateLinks();
125: for (int i = 0; i < links.size(); i++) {
126: eFeed.addContent(generateLinkElement((Link) links.get(i)));
127: }
128:
129: links = feed.getOtherLinks();
130: for (int i = 0; i < links.size(); i++) {
131: eFeed.addContent(generateLinkElement((Link) links.get(i)));
132: }
133: if (feed.getAuthors() != null && feed.getAuthors().size() > 0) {
134: Element authorElement = new Element("author",
135: getFeedNamespace());
136: fillPersonElement(authorElement, (Person) feed.getAuthors()
137: .get(0));
138: eFeed.addContent(authorElement);
139: }
140:
141: List contributors = feed.getContributors();
142: for (int i = 0; i < contributors.size(); i++) {
143: Element contributorElement = new Element("contributor",
144: getFeedNamespace());
145: fillPersonElement(contributorElement, (Person) contributors
146: .get(i));
147: eFeed.addContent(contributorElement);
148: }
149:
150: if (feed.getTagline() != null) {
151: Element taglineElement = new Element("tagline",
152: getFeedNamespace());
153: fillContentElement(taglineElement, feed.getTagline());
154: eFeed.addContent(taglineElement);
155: }
156:
157: if (feed.getId() != null) {
158: eFeed.addContent(generateSimpleElement("id", feed.getId()));
159: }
160:
161: if (feed.getGenerator() != null) {
162: eFeed.addContent(generateGeneratorElement(feed
163: .getGenerator()));
164: }
165:
166: if (feed.getCopyright() != null) {
167: eFeed.addContent(generateSimpleElement("copyright", feed
168: .getCopyright()));
169: }
170:
171: if (feed.getInfo() != null) {
172: Element infoElement = new Element("info",
173: getFeedNamespace());
174: fillContentElement(infoElement, feed.getInfo());
175: eFeed.addContent(infoElement);
176: }
177:
178: if (feed.getModified() != null) {
179: Element modifiedElement = new Element("modified",
180: getFeedNamespace());
181: modifiedElement.addContent(DateParser
182: .formatW3CDateTime(feed.getModified()));
183: eFeed.addContent(modifiedElement);
184: }
185: }
186:
187: protected void populateEntry(Entry entry, Element eEntry)
188: throws FeedException {
189: if (entry.getTitle() != null) {
190: eEntry.addContent(generateSimpleElement("title", entry
191: .getTitle()));
192: }
193: List links = entry.getAlternateLinks();
194: for (int i = 0; i < links.size(); i++) {
195: eEntry.addContent(generateLinkElement((Link) links.get(i)));
196: }
197:
198: links = entry.getOtherLinks();
199: for (int i = 0; i < links.size(); i++) {
200: eEntry.addContent(generateLinkElement((Link) links.get(i)));
201: }
202:
203: if (entry.getAuthors() != null && entry.getAuthors().size() > 0) {
204: Element authorElement = new Element("author",
205: getFeedNamespace());
206: fillPersonElement(authorElement, (Person) entry
207: .getAuthors().get(0));
208: eEntry.addContent(authorElement);
209: }
210:
211: List contributors = entry.getContributors();
212: for (int i = 0; i < contributors.size(); i++) {
213: Element contributorElement = new Element("contributor",
214: getFeedNamespace());
215: fillPersonElement(contributorElement, (Person) contributors
216: .get(i));
217: eEntry.addContent(contributorElement);
218: }
219: if (entry.getId() != null) {
220: eEntry
221: .addContent(generateSimpleElement("id", entry
222: .getId()));
223: }
224:
225: if (entry.getModified() != null) {
226: Element modifiedElement = new Element("modified",
227: getFeedNamespace());
228: modifiedElement.addContent(DateParser
229: .formatW3CDateTime(entry.getModified()));
230: eEntry.addContent(modifiedElement);
231: }
232:
233: if (entry.getIssued() != null) {
234: Element issuedElement = new Element("issued",
235: getFeedNamespace());
236: issuedElement.addContent(DateParser.formatW3CDateTime(entry
237: .getIssued()));
238: eEntry.addContent(issuedElement);
239: }
240:
241: if (entry.getCreated() != null) {
242: Element createdElement = new Element("created",
243: getFeedNamespace());
244: createdElement.addContent(DateParser
245: .formatW3CDateTime(entry.getCreated()));
246: eEntry.addContent(createdElement);
247: }
248:
249: if (entry.getSummary() != null) {
250: Element summaryElement = new Element("summary",
251: getFeedNamespace());
252: fillContentElement(summaryElement, entry.getSummary());
253: eEntry.addContent(summaryElement);
254: }
255:
256: List contents = entry.getContents();
257: for (int i = 0; i < contents.size(); i++) {
258: Element contentElement = new Element("content",
259: getFeedNamespace());
260: fillContentElement(contentElement, (Content) contents
261: .get(i));
262: eEntry.addContent(contentElement);
263: }
264:
265: generateForeignMarkup(eEntry, (List) entry.getForeignMarkup());
266: }
267:
268: protected void checkFeedHeaderConstraints(Element eFeed)
269: throws FeedException {
270: }
271:
272: protected void checkEntriesConstraints(Element parent)
273: throws FeedException {
274: }
275:
276: protected void checkEntryConstraints(Element eEntry)
277: throws FeedException {
278: }
279:
280: protected Element generateLinkElement(Link link) {
281: Element linkElement = new Element("link", getFeedNamespace());
282:
283: if (link.getRel() != null) {
284: Attribute relAttribute = new Attribute("rel", link.getRel()
285: .toString());
286: linkElement.setAttribute(relAttribute);
287: }
288:
289: if (link.getType() != null) {
290: Attribute typeAttribute = new Attribute("type", link
291: .getType());
292: linkElement.setAttribute(typeAttribute);
293: }
294:
295: if (link.getHref() != null) {
296: Attribute hrefAttribute = new Attribute("href", link
297: .getHref());
298: linkElement.setAttribute(hrefAttribute);
299: }
300: return linkElement;
301: }
302:
303: protected void fillPersonElement(Element element, Person person) {
304: if (person.getName() != null) {
305: element.addContent(generateSimpleElement("name", person
306: .getName()));
307: }
308: if (person.getUrl() != null) {
309: element.addContent(generateSimpleElement("url", person
310: .getUrl()));
311: }
312:
313: if (person.getEmail() != null) {
314: element.addContent(generateSimpleElement("email", person
315: .getEmail()));
316: }
317: }
318:
319: protected Element generateTagLineElement(Content tagline) {
320: Element taglineElement = new Element("tagline",
321: getFeedNamespace());
322:
323: if (tagline.getType() != null) {
324: Attribute typeAttribute = new Attribute("type", tagline
325: .getType());
326: taglineElement.setAttribute(typeAttribute);
327: }
328:
329: if (tagline.getValue() != null) {
330: taglineElement.addContent(tagline.getValue());
331: }
332: return taglineElement;
333: }
334:
335: protected void fillContentElement(Element contentElement,
336: Content content) throws FeedException {
337:
338: if (content.getType() != null) {
339: Attribute typeAttribute = new Attribute("type", content
340: .getType());
341: contentElement.setAttribute(typeAttribute);
342: }
343:
344: String mode = content.getMode();
345: if (mode != null) {
346: Attribute modeAttribute = new Attribute("mode", content
347: .getMode().toString());
348: contentElement.setAttribute(modeAttribute);
349: }
350:
351: if (content.getValue() != null) {
352:
353: if (mode == null || mode.equals(Content.ESCAPED)) {
354: contentElement.addContent(content.getValue());
355: } else if (mode.equals(Content.BASE64)) {
356: contentElement.addContent(Base64.encode(content
357: .getValue()));
358: } else if (mode.equals(Content.XML)) {
359:
360: StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
361: tmpDocString.append(content.getValue());
362: tmpDocString.append("</tmpdoc>");
363: StringReader tmpDocReader = new StringReader(
364: tmpDocString.toString());
365: Document tmpDoc;
366:
367: try {
368: SAXBuilder saxBuilder = new SAXBuilder();
369: tmpDoc = saxBuilder.build(tmpDocReader);
370: } catch (Exception ex) {
371: throw new FeedException("Invalid XML", ex);
372: }
373:
374: List children = tmpDoc.getRootElement().removeContent();
375: contentElement.addContent(children);
376: }
377: }
378: }
379:
380: protected Element generateGeneratorElement(Generator generator) {
381: Element generatorElement = new Element("generator",
382: getFeedNamespace());
383:
384: if (generator.getUrl() != null) {
385: Attribute urlAttribute = new Attribute("url", generator
386: .getUrl());
387: generatorElement.setAttribute(urlAttribute);
388: }
389:
390: if (generator.getVersion() != null) {
391: Attribute versionAttribute = new Attribute("version",
392: generator.getVersion());
393: generatorElement.setAttribute(versionAttribute);
394: }
395:
396: if (generator.getValue() != null) {
397: generatorElement.addContent(generator.getValue());
398: }
399:
400: return generatorElement;
401:
402: }
403:
404: protected Element generateSimpleElement(String name, String value) {
405: Element element = new Element(name, getFeedNamespace());
406: element.addContent(value);
407: return element;
408: }
409:
410: }
|