001: /*
002: * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Sun Microsystems, Inc. has intellectual property rights relating to
005: * technology embodied in the product that is described in this document.
006: * In particular, and without limitation, these intellectual property
007: * rights may include one or more of the U.S. patents listed at
008: * http://www.sun.com/patents and one or more additional patents or
009: * pending patent applications in the U.S. and in other countries.
010: *
011: * U.S. Government Rights - Commercial software. Government users are subject
012: * to the Sun Microsystems, Inc. standard license agreement and applicable
013: * provisions of the FAR and its supplements. Use is subject to license terms.
014: * This distribution may include materials developed by third parties.
015: * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
016: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
017: */
018: package com.sun.portal.app.blog;
019:
020: import com.sun.syndication.feed.atom.Entry;
021: import com.sun.syndication.feed.atom.Feed;
022: import com.sun.syndication.io.FeedException;
023: import com.sun.syndication.io.WireFeedOutput;
024: import com.sun.syndication.io.WireFeedInput;
025: import java.io.ByteArrayInputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.StringWriter;
029: import java.util.Collections;
030: import java.util.List;
031: import org.jdom.input.SAXBuilder;
032: import org.jdom.Document;
033: import org.jdom.Element;
034: import org.jdom.output.Format;
035: import org.jdom.output.XMLOutputter;
036: import org.jdom.JDOMException;
037:
038: public class EntrySerializer {
039: private static final String FEED_TYPE = "atom_1.0";
040:
041: /**
042: * Serialize a ROME feed.
043: * Utility method to make up for ROME shortcoming:
044: * ROME can only serialize entire feeds, not individual elements
045: *
046: * We'd be better off if the APP protocol just used feeds, always, and not
047: * individual entries is some cases.
048: */
049: public static String toString(Entry entry)
050: throws IllegalArgumentException, FeedException, IOException {
051: // Build a feed containing only the entry
052: List entries = Collections.singletonList(entry);
053: Feed feed = new Feed();
054: feed.setFeedType("atom_1.0");
055: feed.setEntries(entries);
056:
057: // Get Rome to output feed as a JDOM document
058: WireFeedOutput wireFeedOutput = new WireFeedOutput();
059: Document feedDoc = wireFeedOutput.outputJDom(feed);
060:
061: // Grab entry element from feed and get JDOM to serialize it
062: Element entryElement = (Element) feedDoc.getRootElement()
063: .getChildren().get(0);
064: XMLOutputter outputter = new XMLOutputter();
065: outputter.setFormat(Format.getPrettyFormat());
066:
067: StringWriter sw = new StringWriter();
068: outputter.output(entryElement, sw);
069: String serializedForm = sw.toString();
070:
071: return serializedForm;
072: }
073:
074: public static InputStream toInputStream(Entry entry)
075: throws IllegalArgumentException, FeedException, IOException {
076: String serializedForm = toString(entry);
077: InputStream is = new ByteArrayInputStream(serializedForm
078: .getBytes("UTF-8"));
079:
080: return is;
081: }
082:
083: public static Entry toEntry(InputStream is) throws JDOMException,
084: IOException, IllegalArgumentException, FeedException {
085: // Parse entry into JDOM tree
086: SAXBuilder builder = new SAXBuilder();
087: Document entryDoc = builder.build(is);
088: Element fetchedEntryElement = entryDoc.getRootElement();
089: fetchedEntryElement.detach();
090:
091: // Put entry into a JDOM document with 'feed' root so that Rome can handle it
092: Feed feed = new Feed();
093: feed.setFeedType(FEED_TYPE);
094: WireFeedOutput wireFeedOutput = new WireFeedOutput();
095: Document feedDoc = wireFeedOutput.outputJDom(feed);
096: feedDoc.getRootElement().addContent(fetchedEntryElement);
097:
098: WireFeedInput input = new WireFeedInput();
099: Feed parsedFeed = (Feed) input.build(feedDoc);
100:
101: return (Entry) parsedFeed.getEntries().get(0);
102: }
103: }
|