From f8f17874ed6e748d85ae8972d9b0e812b15216a8 Mon Sep 17 00:00:00 2001 From: Antonio Santos Izaguirre Date: Sat, 18 Mar 2023 17:50:01 +0100 Subject: [PATCH] create new module based on CommentAPI namespace, created by Joe Gregorio Signed-off-by: Antonio Santos Izaguirre --- .../modules/commentapi/CommentAPI.java | 17 ++++ .../modules/commentapi/CommentAPIImpl.java | 77 +++++++++++++++++++ .../io/CommentAPIModuleGenerator.java | 59 ++++++++++++++ .../commentapi/io/CommentAPIModuleParser.java | 37 +++++++++ .../src/main/resources/rome.properties | 9 ++- .../commentapi/CommentAPIGeneratorTest.java | 45 +++++++++++ .../commentapi/CommentAPIParserTest.java | 47 +++++++++++ .../src/test/resources/commentapi/rss.xml | 33 ++++++++ 8 files changed, 321 insertions(+), 3 deletions(-) create mode 100644 rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPI.java create mode 100644 rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPIImpl.java create mode 100644 rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleGenerator.java create mode 100644 rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleParser.java create mode 100644 rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIGeneratorTest.java create mode 100644 rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIParserTest.java create mode 100644 rome-modules/src/test/resources/commentapi/rss.xml diff --git a/rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPI.java b/rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPI.java new file mode 100644 index 000000000..e9a1d6ed6 --- /dev/null +++ b/rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPI.java @@ -0,0 +1,17 @@ +package com.rometools.modules.commentapi; + +import com.rometools.rome.feed.module.Module; + +public interface CommentAPI extends Module { + + static final String URI = "http://wellformedweb.org/CommentAPI/"; + + String getComment(); + + void setComment(final String comment); + + String getCommentRss(); + + void setCommentRss(final String commentRss); + +} diff --git a/rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPIImpl.java b/rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPIImpl.java new file mode 100644 index 000000000..de9423ef6 --- /dev/null +++ b/rome-modules/src/main/java/com/rometools/modules/commentapi/CommentAPIImpl.java @@ -0,0 +1,77 @@ +package com.rometools.modules.commentapi; + +import com.rometools.rome.feed.CopyFrom; +import com.rometools.rome.feed.impl.EqualsBean; +import com.rometools.rome.feed.impl.ToStringBean; +import com.rometools.rome.feed.module.ModuleImpl; + +public class CommentAPIImpl extends ModuleImpl implements CommentAPI { + + public CommentAPIImpl() { + super(CommentAPI.class, CommentAPI.URI); + } + + private String comment; + private String commentRss; + + @Override + public String getComment() { + return this.comment; + } + + @Override + public void setComment(final String comment) { + this.comment = comment; + } + + @Override + public String getCommentRss() { + return this.commentRss; + } + + @Override + public void setCommentRss(final String commentRss) { + this.commentRss = commentRss; + } + + @Override + public String getUri() { + return CommentAPI.URI; + } + + @Override + public Class getInterface() { + return CommentAPI.class; + } + + @Override + public Object clone() { + final CommentAPIImpl clone = new CommentAPIImpl(); + clone.setComment(this.getComment()); + clone.setCommentRss(this.getCommentRss()); + return clone; + } + + @Override + public void copyFrom(CopyFrom obj) { + final CommentAPI source = (CommentAPI) obj; + this.setComment(source.getComment()); + this.setCommentRss(source.getCommentRss()); + } + + @Override + public boolean equals(final Object obj) { + return EqualsBean.beanEquals(CommentAPIImpl.class, this, obj); + } + + @Override + public int hashCode() { + return EqualsBean.beanHashCode(this); + } + + @Override + public String toString() { + return ToStringBean.toString(CommentAPIImpl.class, this); + } + +} diff --git a/rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleGenerator.java b/rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleGenerator.java new file mode 100644 index 000000000..51a7424f3 --- /dev/null +++ b/rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleGenerator.java @@ -0,0 +1,59 @@ +package com.rometools.modules.commentapi.io; + +import java.util.HashSet; +import java.util.Set; + +import org.jdom2.Element; +import org.jdom2.Namespace; + +import com.rometools.modules.commentapi.CommentAPI; +import com.rometools.rome.feed.module.Module; +import com.rometools.rome.io.ModuleGenerator; + +public class CommentAPIModuleGenerator implements ModuleGenerator { + + private static final Namespace NS = Namespace.getNamespace("wfw", CommentAPI.URI); + private static final Set NAMESPACES = new HashSet(); + + static { + NAMESPACES.add(NS); + } + + public CommentAPIModuleGenerator() { + super(); + } + + @Override + public String getNamespaceUri() { + return CommentAPI.URI; + } + + @Override + public Set getNamespaces() { + return NAMESPACES; + } + + @Override + public void generate(Module module, Element element) { + if (!(module instanceof CommentAPI)) { + return; + } + + final CommentAPI commentAPI = (CommentAPI) module; + + if (null != commentAPI.getComment()) { + element.addContent(generateSimpleElement("comment", commentAPI.getCommentRss())); + } + + if (null != commentAPI.getCommentRss()) { + element.addContent(generateSimpleElement("commentRss", commentAPI.getCommentRss())); + } + } + + protected Element generateSimpleElement(final String name, final String value) { + final Element element = new Element(name, CommentAPIModuleGenerator.NS); + element.addContent(value); + + return element; + } +} diff --git a/rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleParser.java b/rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleParser.java new file mode 100644 index 000000000..1ad10c67d --- /dev/null +++ b/rome-modules/src/main/java/com/rometools/modules/commentapi/io/CommentAPIModuleParser.java @@ -0,0 +1,37 @@ +package com.rometools.modules.commentapi.io; + +import java.util.Locale; + +import org.jdom2.Element; +import org.jdom2.Namespace; + +import com.rometools.modules.commentapi.CommentAPI; +import com.rometools.modules.commentapi.CommentAPIImpl; +import com.rometools.rome.feed.module.Module; +import com.rometools.rome.io.ModuleParser; + +public class CommentAPIModuleParser implements ModuleParser { + private static final Namespace NS = Namespace.getNamespace(CommentAPI.URI); + + @Override + public String getNamespaceUri() { + return CommentAPI.URI; + } + + @Override + public Module parse(Element element, Locale locale) { + CommentAPIImpl cai = new CommentAPIImpl(); + + Element tag = element.getChild("comment", CommentAPIModuleParser.NS); + if (tag != null) { + cai.setComment(tag.getText().trim()); + } + + tag = element.getChild("commentRss", CommentAPIModuleParser.NS); + if (tag != null) { + cai.setCommentRss(tag.getText().trim()); + } + return cai; + } + +} diff --git a/rome-modules/src/main/resources/rome.properties b/rome-modules/src/main/resources/rome.properties index c08fea495..348157d88 100644 --- a/rome-modules/src/main/resources/rome.properties +++ b/rome-modules/src/main/resources/rome.properties @@ -53,13 +53,15 @@ rss_2.0.item.ModuleParser.classes=com.rometools.modules.cc.io.ModuleParserRSS2 \ com.rometools.modules.mediarss.io.AlternateMediaModuleParser \ com.rometools.modules.sle.io.ItemParser \ com.rometools.modules.yahooweather.io.WeatherModuleParser \ - com.rometools.modules.psc.io.PodloveSimpleChapterParser + com.rometools.modules.psc.io.PodloveSimpleChapterParser \ + com.rometools.modules.commentapi.io.CommentAPIModuleParser rss_1.0.item.ModuleParser.classes=com.rometools.modules.cc.io.ModuleParserRSS1 \ com.rometools.modules.base.io.GoogleBaseParser \ com.rometools.modules.base.io.CustomTagParser \ com.rometools.modules.content.io.ContentModuleParser \ - com.rometools.modules.slash.io.SlashModuleParser + com.rometools.modules.slash.io.SlashModuleParser \ + com.rometools.modules.commentapi.io.CommentAPIModuleParser atom_0.3.item.ModuleParser.classes=com.rometools.modules.cc.io.ModuleParserRSS2 \ com.rometools.modules.base.io.GoogleBaseParser \ @@ -130,7 +132,8 @@ rss_2.0.item.ModuleGenerator.classes=com.rometools.modules.cc.io.CCModuleGenerat com.rometools.modules.mediarss.io.MediaModuleGenerator \ com.rometools.modules.atom.io.AtomModuleGenerator \ com.rometools.modules.yahooweather.io.WeatherModuleGenerator \ - com.rometools.modules.psc.io.PodloveSimpleChapterGenerator + com.rometools.modules.psc.io.PodloveSimpleChapterGenerator \ + com.rometools.modules.commentapi.io.CommentAPIModuleGenerator rss_1.0.item.ModuleGenerator.classes=com.rometools.modules.base.io.GoogleBaseGenerator \ com.rometools.modules.content.io.ContentModuleGenerator \ diff --git a/rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIGeneratorTest.java b/rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIGeneratorTest.java new file mode 100644 index 000000000..7b1c96c87 --- /dev/null +++ b/rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIGeneratorTest.java @@ -0,0 +1,45 @@ +package com.rometools.modules.commentapi; + +import java.io.File; +import java.io.StringWriter; + +import com.rometools.modules.AbstractTestCase; +import com.rometools.modules.commentapi.io.CommentAPIModuleGenerator; +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 junit.framework.TestSuite; + +public class CommentAPIGeneratorTest extends AbstractTestCase { + + public CommentAPIGeneratorTest(String testName) { + super(testName); + } + + @Override + protected void setUp() { + } + + @Override + protected void tearDown() { + } + + public static junit.framework.Test suite() { + return new TestSuite(CommentAPIGeneratorTest.class); + } + + public void testGenerateRss() throws Exception { + final SyndFeedInput input = new SyndFeedInput(); + final SyndFeed feed = input.build(new XmlReader(new File(getTestFile("commentapi/rss.xml")))); + final SyndFeedOutput output = new SyndFeedOutput(); + final StringWriter writer = new StringWriter(); + output.output(feed, writer); + } + + public void testGetNamespaceUri() { + assertEquals("Namespace", CommentAPI.URI, new CommentAPIModuleGenerator().getNamespaceUri()); + } + +} diff --git a/rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIParserTest.java b/rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIParserTest.java new file mode 100644 index 000000000..e1b8952db --- /dev/null +++ b/rome-modules/src/test/java/com/rometools/modules/commentapi/CommentAPIParserTest.java @@ -0,0 +1,47 @@ +package com.rometools.modules.commentapi; + +import java.io.File; +import java.util.Iterator; +import java.util.List; + +import com.rometools.modules.AbstractTestCase; +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.XmlReader; + +import junit.framework.TestSuite; + +public class CommentAPIParserTest extends AbstractTestCase { + + public CommentAPIParserTest(String testName) { + super(testName); + } + + @Override + protected void setUp() { + } + + @Override + protected void tearDown() { + } + + public static junit.framework.Test suite() { + return new TestSuite(CommentAPIParserTest.class); + } + + public void testParseRss() throws Exception { + final SyndFeedInput input = new SyndFeedInput(); + final SyndFeed feed = input.build(new XmlReader(new File(getTestFile("commentapi/rss.xml")))); + List entries = feed.getEntries(); + int i = 0; + for (Iterator it = entries.iterator(); it.hasNext(); ++i) { + final SyndEntry entry = it.next(); + final CommentAPI entryInfo = (CommentAPI) entry.getModule(CommentAPI.URI); + assertNotNull(entryInfo); + assertEquals(String.format("http://www.example.com/feed/rss/5432%d/comments", i), entryInfo.getComment()); + assertEquals(String.format("http://www.example.com/feed/rss/5432%d/commentsRss", i), entryInfo.getCommentRss()); + } + } + +} diff --git a/rome-modules/src/test/resources/commentapi/rss.xml b/rome-modules/src/test/resources/commentapi/rss.xml new file mode 100644 index 000000000..99407087b --- /dev/null +++ b/rome-modules/src/test/resources/commentapi/rss.xml @@ -0,0 +1,33 @@ + + + + Example + An RSS Example with WFW + Sun, 15 May 2005 13:02:08 -0500 + http://www.example.com + + I like Root Beer + d77d2e80-0487-4e8c-a35d-a93f12a0ff7d:article:54320 + Sun, 15 May 2005 13:02:08 -0500 + http://www.example.com/article/54320 + http://www.example.com/feed/rss/54320/comments + http://www.example.com/feed/rss/54320/commentsRss + + + Rain is Wet + d77d2e80-0487-4e8c-a35d-a93f12a0ff7d:article:54321 + Sun, 15 May 2005 10:55:12 -0500 + http://www.example.com/article/54321 + http://www.example.com/feed/rss/54321/comments + http://www.example.com/feed/rss/54321/commentsRss + + + Huh?! + d77d2e80-0487-4e8c-a35d-a93f12a0ff7d:article:54322 + Sun, 15 May 2005 08:14:11 -0500 + http://www.example.com/article/54322 + http://www.example.com/feed/rss/54322/comments + http://www.example.com/feed/rss/54322/commentsRss + + + \ No newline at end of file