Improved Atom Module to work with several links (#320)
* Improved AtomLink Module to work with several links. * Added getLink & setLink for backwards compability * Improved cloning of AtomLinkModule
This commit is contained in:
parent
e77ed9fd17
commit
998e2060eb
7 changed files with 75 additions and 51 deletions
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package com.rometools.modules.atom.io;
|
package com.rometools.modules.atom.io;
|
||||||
|
|
||||||
public interface AtomLinkAttribute {
|
interface AtomLinkAttribute {
|
||||||
String REL = "rel";
|
String REL = "rel";
|
||||||
String TYPE = "type";
|
String TYPE = "type";
|
||||||
String HREF = "href";
|
String HREF = "href";
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.rometools.modules.atom.io;
|
package com.rometools.modules.atom.io;
|
||||||
|
|
||||||
import com.rometools.modules.atom.modules.AtomLinkModule;
|
import com.rometools.modules.atom.modules.AtomLinkModule;
|
||||||
|
@ -25,6 +26,7 @@ import org.jdom2.Namespace;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class AtomModuleGenerator implements ModuleGenerator {
|
public class AtomModuleGenerator implements ModuleGenerator {
|
||||||
|
@ -52,15 +54,11 @@ public class AtomModuleGenerator implements ModuleGenerator {
|
||||||
public void generate(Module module, Element element) {
|
public void generate(Module module, Element element) {
|
||||||
if (module instanceof AtomLinkModule) {
|
if (module instanceof AtomLinkModule) {
|
||||||
AtomLinkModule m = (AtomLinkModule) module;
|
AtomLinkModule m = (AtomLinkModule) module;
|
||||||
generateLinc(m.getLink(), element);
|
generateLinks(m.getLinks(), element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateLinc(Link link, Element element) {
|
private Element generateLink(Link link) {
|
||||||
if (link == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Element linkElement = new Element("link", NS);
|
Element linkElement = new Element("link", NS);
|
||||||
|
|
||||||
if (link.getHref() != null) {
|
if (link.getHref() != null) {
|
||||||
|
@ -87,11 +85,18 @@ public class AtomModuleGenerator implements ModuleGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (link.getLength() != 0) {
|
if (link.getLength() != 0) {
|
||||||
final Attribute lenght = new Attribute(AtomLinkAttribute.LENGTH, Long.toString(link.getLength()));
|
final Attribute length = new Attribute(AtomLinkAttribute.LENGTH, Long.toString(link.getLength()));
|
||||||
linkElement.setAttribute(lenght);
|
linkElement.setAttribute(length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return linkElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateLinks(List<Link> links, Element element) {
|
||||||
|
for (Link link : links) {
|
||||||
|
element.addContent(generateLink(link));
|
||||||
}
|
}
|
||||||
|
|
||||||
element.addContent(linkElement);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.rometools.modules.atom.io;
|
package com.rometools.modules.atom.io;
|
||||||
|
|
||||||
import com.rometools.modules.atom.modules.AtomLinkModule;
|
import com.rometools.modules.atom.modules.AtomLinkModule;
|
||||||
|
@ -25,6 +26,8 @@ import org.jdom2.Attribute;
|
||||||
import org.jdom2.Element;
|
import org.jdom2.Element;
|
||||||
import org.jdom2.Namespace;
|
import org.jdom2.Namespace;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class AtomModuleParser implements ModuleParser {
|
public class AtomModuleParser implements ModuleParser {
|
||||||
|
@ -40,12 +43,14 @@ public class AtomModuleParser implements ModuleParser {
|
||||||
public Module parse(Element element, Locale locale) {
|
public Module parse(Element element, Locale locale) {
|
||||||
AtomLinkModuleImpl mod = new AtomLinkModuleImpl();
|
AtomLinkModuleImpl mod = new AtomLinkModuleImpl();
|
||||||
if (element.getName().equals("channel") || element.getName().equals("item")) {
|
if (element.getName().equals("channel") || element.getName().equals("item")) {
|
||||||
Element link = element.getChild("link", NS);
|
List<Element> links = element.getChildren("link", NS);
|
||||||
if (link != null) {
|
List<Link> result = new LinkedList<Link>();
|
||||||
|
for (Element link : links) {
|
||||||
Link l = parseLink(link);
|
Link l = parseLink(link);
|
||||||
mod.setLink(l);
|
result.add(l);
|
||||||
return mod;
|
|
||||||
}
|
}
|
||||||
|
mod.setLinks(result);
|
||||||
|
return mod;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,16 +13,23 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.rometools.modules.atom.modules;
|
package com.rometools.modules.atom.modules;
|
||||||
|
|
||||||
import com.rometools.rome.feed.atom.Link;
|
import com.rometools.rome.feed.atom.Link;
|
||||||
import com.rometools.rome.feed.module.Module;
|
import com.rometools.rome.feed.module.Module;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface AtomLinkModule extends Module {
|
public interface AtomLinkModule extends Module {
|
||||||
|
|
||||||
public static final String URI = "http://www.w3.org/2005/Atom";
|
String URI = "http://www.w3.org/2005/Atom";
|
||||||
|
|
||||||
public Link getLink();
|
List<Link> getLinks();
|
||||||
|
@Deprecated
|
||||||
|
Link getLink();
|
||||||
|
|
||||||
public void setLink(Link link);
|
void setLinks(List<Link> links);
|
||||||
|
@Deprecated
|
||||||
|
void setLink(Link link);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.rometools.modules.atom.modules;
|
package com.rometools.modules.atom.modules;
|
||||||
|
|
||||||
import com.rometools.rome.feed.CopyFrom;
|
import com.rometools.rome.feed.CopyFrom;
|
||||||
|
@ -21,19 +22,34 @@ import com.rometools.rome.feed.impl.EqualsBean;
|
||||||
import com.rometools.rome.feed.impl.ToStringBean;
|
import com.rometools.rome.feed.impl.ToStringBean;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class AtomLinkModuleImpl implements AtomLinkModule, Cloneable, Serializable {
|
public class AtomLinkModuleImpl implements AtomLinkModule, Cloneable, Serializable {
|
||||||
|
|
||||||
private Link link;
|
private List<Link> links = new LinkedList<Link>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Link> getLinks() {
|
||||||
|
return links;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Link getLink() {
|
public Link getLink() {
|
||||||
return link;
|
if(links.size() > 0) {
|
||||||
|
return links.get(0);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLinks(List<Link> links) {
|
||||||
|
this.links = links;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLink(Link link) {
|
public void setLink(Link link) {
|
||||||
this.link = link;
|
links.set(0, link);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -49,8 +65,8 @@ public class AtomLinkModuleImpl implements AtomLinkModule, Cloneable, Serializab
|
||||||
@Override
|
@Override
|
||||||
public void copyFrom(CopyFrom obj) {
|
public void copyFrom(CopyFrom obj) {
|
||||||
AtomLinkModule other = (AtomLinkModule) obj;
|
AtomLinkModule other = (AtomLinkModule) obj;
|
||||||
Link link = other.getLink();
|
List<Link> links = other.getLinks();
|
||||||
if (link != null) {
|
for (Link link : links) {
|
||||||
Link l = new Link();
|
Link l = new Link();
|
||||||
l.setHref(link.getHref());
|
l.setHref(link.getHref());
|
||||||
l.setType(link.getType());
|
l.setType(link.getType());
|
||||||
|
@ -58,14 +74,15 @@ public class AtomLinkModuleImpl implements AtomLinkModule, Cloneable, Serializab
|
||||||
l.setHreflang(link.getHreflang());
|
l.setHreflang(link.getHreflang());
|
||||||
l.setTitle(link.getTitle());
|
l.setTitle(link.getTitle());
|
||||||
l.setLength(link.getLength());
|
l.setLength(link.getLength());
|
||||||
setLink(l);
|
this.links.add(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object clone() {
|
public Object clone() {
|
||||||
final AtomLinkModuleImpl m = new AtomLinkModuleImpl();
|
final AtomLinkModuleImpl m = new AtomLinkModuleImpl();
|
||||||
if (link != null) {
|
List<Link> result = new LinkedList<Link>();
|
||||||
|
for(Link link : this.getLinks()) {
|
||||||
Link l = new Link();
|
Link l = new Link();
|
||||||
l.setHref(link.getHref());
|
l.setHref(link.getHref());
|
||||||
l.setType(link.getType());
|
l.setType(link.getType());
|
||||||
|
@ -73,8 +90,10 @@ public class AtomLinkModuleImpl implements AtomLinkModule, Cloneable, Serializab
|
||||||
l.setHreflang(link.getHreflang());
|
l.setHreflang(link.getHreflang());
|
||||||
l.setTitle(link.getTitle());
|
l.setTitle(link.getTitle());
|
||||||
l.setLength(link.getLength());
|
l.setLength(link.getLength());
|
||||||
m.setLink(l);
|
result.add(l);
|
||||||
}
|
}
|
||||||
|
links.subList(0, links.size());
|
||||||
|
m.setLinks(result);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,39 +22,22 @@ package com.rometools.modules.atom;
|
||||||
import com.rometools.modules.AbstractTestCase;
|
import com.rometools.modules.AbstractTestCase;
|
||||||
import com.rometools.modules.atom.io.AtomModuleGenerator;
|
import com.rometools.modules.atom.io.AtomModuleGenerator;
|
||||||
import com.rometools.modules.atom.modules.AtomLinkModule;
|
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.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.feed.synd.SyndFeed;
|
||||||
import com.rometools.rome.io.SyndFeedInput;
|
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.Test;
|
||||||
import junit.framework.TestSuite;
|
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.io.File;
|
||||||
import java.net.URL;
|
import java.util.List;
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test to verify correctness of SSE subproject.
|
* Test to verify correctness of SSE subproject.
|
||||||
*/
|
*/
|
||||||
public class AtomLinkTest extends AbstractTestCase {
|
public class AtomLinkTest extends AbstractTestCase {
|
||||||
public static final String href = "http://test.com";
|
public static final String[] href = {"http://test.com", "http://test.com/alt"};
|
||||||
public static final String type = "application/rss+xml";
|
public static final String[] type = {"application/rss+xml", "application/rss+xml"};
|
||||||
public static final String rel = "self";
|
public static final String[] rel = {"self", "alternate"};
|
||||||
|
|
||||||
public AtomLinkTest(final String testName) {
|
public AtomLinkTest(final String testName) {
|
||||||
super(testName);
|
super(testName);
|
||||||
|
@ -85,11 +68,15 @@ public class AtomLinkTest extends AbstractTestCase {
|
||||||
final SyndFeed feed = new SyndFeedInput().build(testdata);
|
final SyndFeed feed = new SyndFeedInput().build(testdata);
|
||||||
|
|
||||||
final AtomLinkModule atomLinkModule = (AtomLinkModule) feed.getModule(AtomLinkModule.URI);
|
final AtomLinkModule atomLinkModule = (AtomLinkModule) feed.getModule(AtomLinkModule.URI);
|
||||||
Link link = atomLinkModule.getLink();
|
List<Link> links = atomLinkModule.getLinks();
|
||||||
|
for (int i = 0; i < links.size(); i++) {
|
||||||
assertEquals(href, link.getHref());
|
Link link = links.get(i);
|
||||||
assertEquals(rel, link.getRel());
|
assertEquals(href[i], link.getHref());
|
||||||
assertEquals(type, link.getType());
|
assertEquals(rel[i], link.getRel());
|
||||||
|
assertEquals(type[i], link.getType());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,5 +5,6 @@
|
||||||
<link>http://test.com</link>
|
<link>http://test.com</link>
|
||||||
<description>Test description</description>
|
<description>Test description</description>
|
||||||
<atom:link href="http://test.com" type="application/rss+xml" rel="self" />
|
<atom:link href="http://test.com" type="application/rss+xml" rel="self" />
|
||||||
|
<atom:link href="http://test.com/alt" type="application/rss+xml" rel="alternate" />
|
||||||
</channel>
|
</channel>
|
||||||
</rss>
|
</rss>
|
||||||
|
|
Loading…
Reference in a new issue