linkAttrs.add(feedDoc.buildAttribute("hreflang", "en-US"));
List<Link> links = new LinkedList<Link>();
Link link = feedDoc.buildLink(linkAttrs, null);
links.add(link);
Attribute extAttr = feedDoc.buildAttribute("xmlns:xhtml",
"http://www.w3.org/1999/xhtml");
List<Attribute> extAttrs = new LinkedList<Attribute>();
extAttrs.add(extAttr);
// the base feed attributes.
List<Attribute> feedAttrs = new LinkedList<Attribute>();
feedAttrs.add(feedDoc.getAtomBase());
feedAttrs.add(feedDoc.getLangEn());
feedAttrs.addAll(extAttrs);
List<Extension> extensions = new LinkedList<Extension>();
Extension extension = feedDoc.buildExtension("xhtml:div", null,
"<span style='color:red;'>hello there</span>");
extensions.add(extension);
List<Author> authors = new LinkedList<Author>();
authors.add(feedDoc.buildAuthor(feedDoc.buildName("Bill Brown"),
null, null, null, null));
Entry entry = feedDoc
.buildEntry(
feedDoc
.buildId(null,
"http://www.colorfulsoftware.com/atom.xml#entry1"),
feedDoc.buildTitle("an example atom entry", null),
feedDoc.buildUpdated(null, Calendar.getInstance()
.getTime().toString()),
null,
feedDoc
.buildContent(
"Hello World. Welcome to the atomsphere feed builder for atom 1.0 builds. I hope it is useful for you.",
null), authors, null, null, null,
null, null, null, null, null);
List<Entry> entries = new LinkedList<Entry>();
entries.add(entry);
Feed feed = feedDoc.buildFeed(id, title, updated, rights, authors,
categories, contributors, links, feedAttrs, extensions,
generator, null, icon, logo, entries);
assertNotNull(feed);
assertNotNull(feed.getAttribute("xml:lang"));
assertNull(feed.getAttribute("bunk"));
// read and write a full feed.
feed = feedDoc.readFeedToBean(mega);
assertNotNull(feedDoc.getLibVersion());
BufferedWriter out = new BufferedWriter(new FileWriter(
"target/mega.xml"));
out.write(feed.toString());
out.flush();
out.close();
feedDoc.writeFeedDoc(new FileOutputStream(new File(
"target/mega2.xml")), feed, feedDoc.getEncoding(), feedDoc
.getXmlVersion());
assertNotNull(feed.getId());
assertNull(feed.getGenerator().getAttribute("notHere"));
assertNotNull(feed.getAuthor("Bill Brown"));
assertNotNull(feed.getContributor("Bill Brown"));
assertNotNull(feed.getLink("self"));
assertNull(feed.getLink("self").getContent());
assertNull(feed.getLink(null));
assertNotNull(feed.getId().getAttribute("local:something"));
assertNull(feed.getId().getAttribute("bunk"));
assertNull(feed.getCategory("math").getAttribute("anythingWrong"));
assertNull(feed.getCategory("math").getContent());
assertNotNull(feed.getIcon().getAttribute("local:testAttr"));
assertNotNull(feed.getLogo().getAttribute("local:testAttr"));
assertNotNull(feed.getRights().getAttribute("xmlns"));
assertNull(feed.getRights().getAttribute("sayWhat"));
assertNotNull(feed.getUpdated().getDateTime());
assertNull(feed.getSubtitle().getAttribute("sayWhat"));
// sort the entries:
feed = feedDoc.sortEntries(feed, Summary.class, "asc");
feed = feedDoc.sortEntries(feed, Title.class, "desc");
feed = feedDoc.sortEntries(feed, Updated.class, "asc");
// this wont sort anything.
try {
feed = feedDoc.sortEntries(feed, Author.class, "asc");
fail("should not get here.");
} catch (AtomSpecException e) {
assertEquals(e.getMessage(),
"The feed entries cannot be sorted by an invalid type 'Author'.");
}
XMLStreamWriter writer = XMLOutputFactory.newInstance()
.createXMLStreamWriter(
new FileOutputStream("target/dump1.xml"));
feedDoc.writeFeedDoc(writer, feed, null, null);
int fileName = 1;
for (Entry ent : feed.getEntries()) {
out = new BufferedWriter(new FileWriter("target/"
+ (fileName++) + ".xml"));
out.write(ent.toString());
out.flush();
out.close();
if (ent
.getId()
.getAtomUri()
.equals(
"http://colorfulsoftware.localhost/colorfulsoftware/projects/atomsphere/atom.xml#About")) {
Author auth = ent.getAuthor("Bill Brown");
assertNotNull(ent.getAttribute("xmlns"));
assertNotNull(auth);
assertNotNull(auth.getAttribute("local:testAttr"));
Attribute attr1 = auth.getAttribute("local:testAttr");
assertFalse(attr1.equals(auth.getAttribute("local:blank")));
assertNull(auth.getAttribute("local:blank"));
assertNotNull(auth.getExtension("test:test"));
assertNull(auth.getExtension("local:bunky"));
assertNull(ent.getAuthor("some other dude"));
assertNotNull(ent.getContributor("Bill Brown"));
assertNull(ent.getContributor("some other dude"));
assertNotNull(ent.getCategory("science"));
assertNull(ent.getCategory("nothing"));
assertNotNull(ent.getLink("alternate"));
assertNull(ent.getLink("http://www.fakeness.net"));
assertNotNull(ent.getExtension("local:element"));
assertNull(ent.getExtension("local:notthere"));
assertNotNull(ent.getPublished().getAttribute("xmlns"));
assertNotNull(ent.getPublished().getDateTime());
assertNull(ent.getSummary().getAttribute("sayWhat"));
Contributor cont = ent.getContributor("Bill Brown");
assertNotNull(cont);
assertNotNull(cont.getExtension("test:test"));
assertNotNull(cont.getAttribute("local:testAttr"));
attr1 = cont.getAttribute("local:testAttr");
assertFalse(attr1.equals(auth.getAttribute("local:blank")));
assertNull(auth.getAttribute("local:blank"));
assertNotNull(auth.getExtension("test:test"));
assertNull(auth.getExtension("local:bunky"));
assertNull(ent.getAuthor("some other dude"));
assertNotNull(ent.getContributor("Bill Brown"));