From 3f37f28b822927e4c0be26d6d54644ae203067d5 Mon Sep 17 00:00:00 2001 From: Dmitry Rygalov Date: Sat, 25 Oct 2014 20:00:32 +0700 Subject: [PATCH] Add AtomLink Module --- .../modules/atom/io/AtomLinkAttribute.java | 10 +++ .../modules/atom/io/AtomModuleGenerator.java | 82 +++++++++++++++++ .../modules/atom/io/AtomModuleParser.java | 90 +++++++++++++++++++ .../modules/atom/modules/AtomLinkModule.java | 13 +++ .../atom/modules/AtomLinkModuleImpl.java | 83 +++++++++++++++++ src/main/resources/rome.properties | 4 + .../rometools/modules/atom/AtomLinkTest.java | 82 +++++++++++++++++ src/test/resources/atom/atom.xml | 9 ++ 8 files changed, 373 insertions(+) create mode 100644 src/main/java/com/rometools/modules/atom/io/AtomLinkAttribute.java create mode 100644 src/main/java/com/rometools/modules/atom/io/AtomModuleGenerator.java create mode 100644 src/main/java/com/rometools/modules/atom/io/AtomModuleParser.java create mode 100644 src/main/java/com/rometools/modules/atom/modules/AtomLinkModule.java create mode 100644 src/main/java/com/rometools/modules/atom/modules/AtomLinkModuleImpl.java create mode 100644 src/test/java/com/rometools/modules/atom/AtomLinkTest.java create mode 100644 src/test/resources/atom/atom.xml diff --git a/src/main/java/com/rometools/modules/atom/io/AtomLinkAttribute.java b/src/main/java/com/rometools/modules/atom/io/AtomLinkAttribute.java new file mode 100644 index 0000000..1ccfbfb --- /dev/null +++ b/src/main/java/com/rometools/modules/atom/io/AtomLinkAttribute.java @@ -0,0 +1,10 @@ +package com.rometools.modules.atom.io; + +public interface AtomLinkAttribute { + String REL = "rel"; + String TYPE = "type"; + String HREF = "href"; + String TITLE = "title"; + String HREF_LANG = "hreflang"; + String LENGTH = "length"; +} diff --git a/src/main/java/com/rometools/modules/atom/io/AtomModuleGenerator.java b/src/main/java/com/rometools/modules/atom/io/AtomModuleGenerator.java new file mode 100644 index 0000000..a683a6c --- /dev/null +++ b/src/main/java/com/rometools/modules/atom/io/AtomModuleGenerator.java @@ -0,0 +1,82 @@ +package com.rometools.modules.atom.io; + +import com.rometools.modules.atom.modules.AtomLinkModule; +import com.rometools.rome.feed.atom.Link; +import com.rometools.rome.feed.module.Module; +import com.rometools.rome.io.ModuleGenerator; +import org.jdom2.Attribute; +import org.jdom2.Element; +import org.jdom2.Namespace; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class AtomModuleGenerator implements ModuleGenerator { + + static final Namespace NS = Namespace.getNamespace("atom", AtomLinkModule.URI); + private static final Set NAMESPACES; + + static { + final Set nss = new HashSet(); + nss.add(NS); + NAMESPACES = Collections.unmodifiableSet(nss); + } + + @Override + public final String getNamespaceUri() { + return AtomLinkModule.URI; + } + + @Override + public final Set getNamespaces() { + return NAMESPACES; + } + + @Override + public void generate(Module module, Element element) { + if (module instanceof AtomLinkModule) { + AtomLinkModule m = (AtomLinkModule) module; + generateLinc(m.getLink(), element); + } + } + + private void generateLinc(Link link, Element element) { + if (link == null) { + return; + } + + Element linkElement = new Element("link", NS); + + if (link.getHref() != null) { + Attribute href = new Attribute(AtomLinkAttribute.HREF, link.getHref()); + linkElement.setAttribute(href); + } + if (link.getType() != null) { + Attribute type = new Attribute(AtomLinkAttribute.TYPE, link.getType()); + linkElement.setAttribute(type); + } + if (link.getRel() != null) { + Attribute rel = new Attribute(AtomLinkAttribute.REL, link.getRel()); + linkElement.setAttribute(rel); + } + + if (link.getHreflang() != null) { + final Attribute hreflangAttribute = new Attribute(AtomLinkAttribute.HREF_LANG, link.getHreflang()); + linkElement.setAttribute(hreflangAttribute); + } + + if (link.getTitle() != null) { + final Attribute title = new Attribute(AtomLinkAttribute.TITLE, link.getTitle()); + linkElement.setAttribute(title); + } + + if (link.getLength() != 0) { + final Attribute lenght = new Attribute(AtomLinkAttribute.LENGTH, Long.toString(link.getLength())); + linkElement.setAttribute(lenght); + } + + element.addContent(linkElement); + } + +} diff --git a/src/main/java/com/rometools/modules/atom/io/AtomModuleParser.java b/src/main/java/com/rometools/modules/atom/io/AtomModuleParser.java new file mode 100644 index 0000000..36cdfba --- /dev/null +++ b/src/main/java/com/rometools/modules/atom/io/AtomModuleParser.java @@ -0,0 +1,90 @@ +package com.rometools.modules.atom.io; + +import com.rometools.modules.atom.modules.AtomLinkModule; +import com.rometools.modules.atom.modules.AtomLinkModuleImpl; +import com.rometools.rome.feed.atom.Link; +import com.rometools.rome.feed.module.Module; +import com.rometools.rome.io.ModuleParser; +import com.rometools.rome.io.impl.NumberParser; +import org.jdom2.Attribute; +import org.jdom2.Element; +import org.jdom2.Namespace; + +import java.util.Locale; + +public class AtomModuleParser implements ModuleParser { + + private static final Namespace NS = Namespace.getNamespace(AtomLinkModule.URI); + + @Override + public String getNamespaceUri() { + return null; + } + + @Override + public Module parse(Element element, Locale locale) { + AtomLinkModuleImpl mod = new AtomLinkModuleImpl(); + if (element.getName().equals("channel") || element.getName().equals("item")) { + Element link = element.getChild("link", NS); + if (link != null) { + Link l = parseLink(link); + mod.setLink(l); + return mod; + } + } + return null; + } + + private Link parseLink(final Element eLink) { + + final Link link = new Link(); + + final String rel = getAttributeValue(eLink, AtomLinkAttribute.REL); + if (rel != null) { + link.setRel(rel); + } + + final String type = getAttributeValue(eLink, AtomLinkAttribute.TYPE); + if (type != null) { + link.setType(type); + } + + final String href = getAttributeValue(eLink, AtomLinkAttribute.HREF); + if (href != null) { + link.setHref(href); + } + + final String title = getAttributeValue(eLink, AtomLinkAttribute.TITLE); + if (title != null) { + link.setTitle(title); + } + + final String hrefLang = getAttributeValue(eLink, AtomLinkAttribute.HREF_LANG); + if (hrefLang != null) { + link.setHreflang(hrefLang); + } + + final String length = getAttributeValue(eLink, AtomLinkAttribute.LENGTH); + if (length != null) { + final Long val = NumberParser.parseLong(length); + if (val != null) { + link.setLength(val.longValue()); + } + } + + return link; + + } + + protected String getAttributeValue(final Element e, final String attributeName) { + Attribute attr = e.getAttribute(attributeName); + if (attr == null) { + attr = e.getAttribute(attributeName, NS); + } + if (attr != null) { + return attr.getValue(); + } else { + return null; + } + } +} diff --git a/src/main/java/com/rometools/modules/atom/modules/AtomLinkModule.java b/src/main/java/com/rometools/modules/atom/modules/AtomLinkModule.java new file mode 100644 index 0000000..22e3f6d --- /dev/null +++ b/src/main/java/com/rometools/modules/atom/modules/AtomLinkModule.java @@ -0,0 +1,13 @@ +package com.rometools.modules.atom.modules; + +import com.rometools.rome.feed.atom.Link; +import com.rometools.rome.feed.module.Module; + +public interface AtomLinkModule extends Module { + + public static final String URI = "http://www.w3.org/2005/Atom"; + + public Link getLink(); + + public void setLink(Link link); +} diff --git a/src/main/java/com/rometools/modules/atom/modules/AtomLinkModuleImpl.java b/src/main/java/com/rometools/modules/atom/modules/AtomLinkModuleImpl.java new file mode 100644 index 0000000..ec5e9e2 --- /dev/null +++ b/src/main/java/com/rometools/modules/atom/modules/AtomLinkModuleImpl.java @@ -0,0 +1,83 @@ +package com.rometools.modules.atom.modules; + +import com.rometools.rome.feed.CopyFrom; +import com.rometools.rome.feed.atom.Link; +import com.rometools.rome.feed.impl.EqualsBean; +import com.rometools.rome.feed.impl.ToStringBean; + +import java.io.Serializable; + +public class AtomLinkModuleImpl implements AtomLinkModule, Cloneable, Serializable { + + private Link link; + + @Override + public Link getLink() { + return link; + } + + @Override + public void setLink(Link link) { + this.link = link; + } + + @Override + public String getUri() { + return URI; + } + + @Override + public Class getInterface() { + return AtomLinkModule.class; + } + + @Override + public void copyFrom(CopyFrom obj) { + AtomLinkModule other = (AtomLinkModule) obj; + Link link = other.getLink(); + if (link != null) { + Link l = new Link(); + l.setHref(link.getHref()); + l.setType(link.getType()); + l.setRel(link.getRel()); + l.setHreflang(link.getHreflang()); + l.setTitle(link.getTitle()); + l.setLength(link.getLength()); + setLink(l); + } + } + + @Override + public Object clone() { + final AtomLinkModuleImpl m = new AtomLinkModuleImpl(); + if (link != null) { + Link l = new Link(); + l.setHref(link.getHref()); + l.setType(link.getType()); + l.setRel(link.getRel()); + l.setHreflang(link.getHreflang()); + l.setTitle(link.getTitle()); + l.setLength(link.getLength()); + m.setLink(l); + } + return m; + } + + @Override + public boolean equals(final Object obj) { + final EqualsBean eBean = new EqualsBean(AtomLinkModuleImpl.class, this); + return eBean.beanEquals(obj); + } + + @Override + public int hashCode() { + final EqualsBean equals = new EqualsBean(AtomLinkModuleImpl.class, this); + return equals.beanHashCode(); + } + + @Override + public String toString() { + final ToStringBean tsBean = new ToStringBean(AtomLinkModuleImpl.class, this); + return tsBean.toString(); + } +} diff --git a/src/main/resources/rome.properties b/src/main/resources/rome.properties index c05fe38..0fb85df 100644 --- a/src/main/resources/rome.properties +++ b/src/main/resources/rome.properties @@ -9,6 +9,7 @@ rss_2.0.feed.ModuleParser.classes=com.rometools.modules.cc.io.ModuleParserRSS2 \ com.rometools.modules.georss.W3CGeoParser \ com.rometools.modules.photocast.io.Parser \ com.rometools.modules.mediarss.io.MediaModuleParser \ + com.rometools.modules.atom.io.AtomModuleParser \ com.rometools.modules.itunes.io.ITunesParserOldNamespace \ com.rometools.modules.mediarss.io.AlternateMediaModuleParser \ com.rometools.modules.sle.io.ModuleParser \ @@ -39,6 +40,7 @@ rss_2.0.item.ModuleParser.classes=com.rometools.modules.cc.io.ModuleParserRSS2 \ com.rometools.modules.slash.io.SlashModuleParser \ com.rometools.modules.itunes.io.ITunesParser \ com.rometools.modules.mediarss.io.MediaModuleParser \ + com.rometools.modules.atom.io.AtomModuleParser \ com.rometools.modules.opensearch.impl.OpenSearchModuleParser \ com.rometools.modules.georss.SimpleParser \ com.rometools.modules.georss.W3CGeoParser \ @@ -84,6 +86,7 @@ rss_2.0.feed.ModuleGenerator.classes=com.rometools.modules.cc.io.CCModuleGenerat com.rometools.modules.georss.W3CGeoGenerator \ com.rometools.modules.photocast.io.Generator \ com.rometools.modules.mediarss.io.MediaModuleGenerator \ + com.rometools.modules.atom.io.AtomModuleGenerator \ com.rometools.modules.sle.io.ModuleGenerator \ com.rometools.modules.yahooweather.io.WeatherModuleGenerator @@ -114,6 +117,7 @@ rss_2.0.item.ModuleGenerator.classes=com.rometools.modules.cc.io.CCModuleGenerat com.rometools.modules.georss.W3CGeoGenerator \ com.rometools.modules.photocast.io.Generator \ com.rometools.modules.mediarss.io.MediaModuleGenerator \ + com.rometools.modules.atom.io.AtomModuleGenerator \ com.rometools.modules.yahooweather.io.WeatherModuleGenerator rss_1.0.item.ModuleGenerator.classes=com.rometools.modules.base.io.GoogleBaseGenerator \ diff --git a/src/test/java/com/rometools/modules/atom/AtomLinkTest.java b/src/test/java/com/rometools/modules/atom/AtomLinkTest.java new file mode 100644 index 0000000..508646e --- /dev/null +++ b/src/test/java/com/rometools/modules/atom/AtomLinkTest.java @@ -0,0 +1,82 @@ +/* + * SSEParserTest.java + * JUnit based test + * + * Created on August 2, 2005, 1:30 PM + */ +package com.rometools.modules.atom; + +import com.rometools.modules.AbstractTestCase; +import com.rometools.modules.atom.io.AtomModuleGenerator; +import com.rometools.modules.atom.modules.AtomLinkModule; +import com.rometools.modules.sle.SimpleListExtension; +import com.rometools.modules.sse.SSE091Generator; +import com.rometools.modules.sse.modules.Conflict; +import com.rometools.modules.sse.modules.History; +import com.rometools.modules.sse.modules.SSEModule; +import com.rometools.modules.sse.modules.Sync; +import com.rometools.rome.feed.atom.Link; +import com.rometools.rome.feed.rss.Item; +import com.rometools.rome.feed.synd.SyndEntry; +import com.rometools.rome.feed.synd.SyndFeed; +import com.rometools.rome.io.SyndFeedInput; +import com.rometools.rome.io.SyndFeedOutput; +import com.rometools.rome.io.XmlReader; +import com.rometools.rome.io.impl.DateParser; +import junit.framework.Test; +import junit.framework.TestSuite; +import org.jdom2.Attribute; +import org.jdom2.Content; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.input.SAXBuilder; + +import java.io.File; +import java.net.URL; +import java.util.*; + +/** + * Test to verify correctness of SSE subproject. + */ +public class AtomLinkTest extends AbstractTestCase { + public static final String href = "http://test.com"; + public static final String type = "application/rss+xml"; + public static final String rel = "self"; + + public AtomLinkTest(final String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + } + + @Override + protected void tearDown() throws Exception { + } + + public static Test suite() { + return new TestSuite(AtomLinkTest.class); + } + + /** + * Test of getNamespaceUri method, of class com.rometools.rome.feed.module.sse.SSE091 + */ + public void testGetNamespaceUri() { + assertEquals("Namespace", AtomLinkModule.URI, new AtomModuleGenerator().getNamespaceUri()); + } + + public void test() throws Exception { + + final File testdata = new File(super.getTestFile("atom/atom.xml")); + final SyndFeed feed = new SyndFeedInput().build(testdata); + + final AtomLinkModule atomLinkModule = (AtomLinkModule) feed.getModule(AtomLinkModule.URI); + Link link = atomLinkModule.getLink(); + + assertEquals(href, link.getHref()); + assertEquals(rel, link.getRel()); + assertEquals(type, link.getType()); + + } +} diff --git a/src/test/resources/atom/atom.xml b/src/test/resources/atom/atom.xml new file mode 100644 index 0000000..678d281 --- /dev/null +++ b/src/test/resources/atom/atom.xml @@ -0,0 +1,9 @@ + + + + Test title + http://test.com + Test description + + +