Add AtomLink Module

This commit is contained in:
Dmitry Rygalov 2014-10-25 20:00:32 +07:00
parent 4c40079657
commit 3f37f28b82
8 changed files with 373 additions and 0 deletions

View file

@ -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";
}

View file

@ -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<Namespace> NAMESPACES;
static {
final Set<Namespace> nss = new HashSet<Namespace>();
nss.add(NS);
NAMESPACES = Collections.unmodifiableSet(nss);
}
@Override
public final String getNamespaceUri() {
return AtomLinkModule.URI;
}
@Override
public final Set<Namespace> 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);
}
}

View file

@ -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;
}
}
}

View file

@ -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);
}

View file

@ -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<? extends CopyFrom> 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();
}
}

View file

@ -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 \

View file

@ -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());
}
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Test title</title>
<link>http://test.com</link>
<description>Test description</description>
<atom:link href="http://test.com" type="application/rss+xml" rel="self" />
</channel>
</rss>