From 42a9f870dc364f546b7de94165bfe875a5fef32d Mon Sep 17 00:00:00 2001 From: Martin Kurz Date: Wed, 9 Oct 2013 17:07:43 +0200 Subject: [PATCH] parsing of description for RSS 0.92+ enhanced, so some invalid html content is handled (fixes #137) --- .../sun/syndication/io/impl/RSS092Parser.java | 31 ++++++- .../sun/syndication/io/impl/RSS094Parser.java | 12 --- .../sun/syndication/io/impl/RSS20Parser.java | 2 - .../unittest/issues/Issue137Test.java | 39 +++++++++ src/test/resources/issue137-rss.xml | 80 +++++++++++++++++++ 5 files changed, 148 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/sun/syndication/unittest/issues/Issue137Test.java create mode 100644 src/test/resources/issue137-rss.xml diff --git a/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java index b90acaf..8a223d7 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java @@ -19,7 +19,9 @@ package com.sun.syndication.io.impl; import java.util.ArrayList; import java.util.List; +import org.jdom2.Content; import org.jdom2.Element; +import org.jdom2.output.XMLOutputter; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.rss.Category; @@ -166,8 +168,33 @@ public class RSS092Parser extends RSS091UserlandParser { @Override protected Description parseItemDescription(final Element rssRoot, final Element eDesc) { - final Description desc = super.parseItemDescription(rssRoot, eDesc); - desc.setType("text/html"); + final Description desc = new Description(); + final StringBuilder sb = new StringBuilder(); + final XMLOutputter xmlOut = new XMLOutputter(); + for (final Content c : eDesc.getContent()) { + switch (c.getCType()) { + case Text: + case CDATA: + sb.append(c.getValue()); + break; + case EntityRef: + System.out.println("entity: " + c.getValue()); + sb.append(c.getValue()); + break; + case Element: + sb.append(xmlOut.outputString((Element) c)); + break; + default: + // ignore + break; + } + } + desc.setValue(sb.toString()); + String att = eDesc.getAttributeValue("type"); + if (att == null) { + att = "text/html"; + } + desc.setType(att); return desc; } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java index 3a68c63..72aad41 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java @@ -98,16 +98,4 @@ public class RSS094Parser extends RSS093Parser { return item; } - @Override - protected Description parseItemDescription(final Element rssRoot, final Element eDesc) { - final Description desc = super.parseItemDescription(rssRoot, eDesc); - String att = eDesc.getAttributeValue("type");// getRSSNamespace()); DONT - // KNOW WHY DOESN'T WORK - if (att == null) { - att = "text/html"; - } - desc.setType(att); - return desc; - } - } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java index 9051c13..51fcf60 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java @@ -47,8 +47,6 @@ public class RSS20Parser extends RSS094Parser { @Override protected Description parseItemDescription(final Element rssRoot, final Element eDesc) { final Description desc = super.parseItemDescription(rssRoot, eDesc); - desc.setType("text/html"); // change as per - // https://rome.dev.java.net/issues/show_bug.cgi?id=26 return desc; } diff --git a/src/test/java/com/sun/syndication/unittest/issues/Issue137Test.java b/src/test/java/com/sun/syndication/unittest/issues/Issue137Test.java new file mode 100644 index 0000000..6e5fd35 --- /dev/null +++ b/src/test/java/com/sun/syndication/unittest/issues/Issue137Test.java @@ -0,0 +1,39 @@ +package com.sun.syndication.unittest.issues; + +import com.sun.syndication.feed.synd.SyndFeed; +import com.sun.syndication.unittest.FeedTest; + +/** + * Test for #137: RSS item description is not complete. + * @author Martin Kurz + * + */ +public class Issue137Test extends FeedTest { + + public Issue137Test() { + super("issue137-rss.xml"); + } + + public void testDescription() throws Exception { + final SyndFeed feed = this.getCachedSyndFeed(); + assertEquals("\"Teacher

MOE will be building 11 new primary\n"+ + " schools and upgrading another 28 existing schools from November 2009. This is the first phase of\n"+ + " MOE's plans to enhance primary school infrastructure to facilitate the transition of all primary schools\n"+ + " to a single-session model to support a higher quality primary education.

", feed.getEntries().get(0).getDescription().getValue()); + assertEquals("\"Minister

At the Work Plan Seminar 2009, Minister for Education\n"+ + " Dr Ng Eng Hen talked\n"+ + " about the importance of language & communication skills and growing a world-class education service\n"+ + ". He spoke of strengthening the teaching track and of a teacher-driven culture of professional excellence.", feed.getEntries().get(1).getDescription().getValue()); + assertEquals("\"Students\n

A total of 275 schools will receive 580 Special and Level\n"+ + " Two Awards this year under the MOE Masterplan of Awards. These awards will be presented at the 2009\n"+ + " MOE Work Plan Seminar on 17 Sep 09 by the Minister for Education, Dr Ng Eng Hen.

", feed.getEntries().get(2).getDescription().getValue()); + } + +} diff --git a/src/test/resources/issue137-rss.xml b/src/test/resources/issue137-rss.xml new file mode 100644 index 0000000..a4b541b --- /dev/null +++ b/src/test/resources/issue137-rss.xml @@ -0,0 +1,80 @@ + + + + Spotlight + http://www.moe.gov.sg/media/spotlight/ + + en + Copyright 2009 + Fri, 25 Sep 2009 10:00:00 +0800 + http://www.rssboard.org/rss-specification + + + Single-Session Primary Schools + Teacher and his students tossing frisbees into the airMOE will be building 11 new primary + schools and upgrading another 28 existing schools from November 2009. This is the first phase of + MOE's plans to enhance primary school infrastructure to facilitate the transition of all primary schools + to a single-session model to support a higher quality primary education.

]]>
+ http://feedproxy.google.com/~r/singapore-education/spotlight/~3/vnNsA4Hdk6U/ + http://www.moe.gov.sg/media/spotlight/ + + + Fri, 25 Sep 2009 10:00:00 +0800 + http://www.moe.gov.sg/media/spotlight/
+ + + Work Plan Seminar 2009 + ]]>At the Work Plan Seminar 2009, Minister for Education + Dr Ng Eng Hen talked + about the importance of language & communication skills and growing a world-class education service +. He spoke of strengthening the teaching track and of a teacher-driven culture of professional excellence.

]]>
+ http://feedproxy.google.com/~r/singapore-education/spotlight/~3/vnNsA4Hdk6U/ + http://www.moe.gov.sg/media/spotlight/ + + + Thu, 17 Sep 2009 15:33:15 +0800 + http://www.moe.gov.sg/media/spotlight/
+ + + Recognising School Achievements in 2009 + <img src="http://www.moe.gov.sg/media/spotlight/assets_c/2009/09/spotlight-moa-2009-thumb-120x80-278.jpg" alt="Students at a podcast session" /> + A total of 275 schools will receive 580 Special and Level + Two Awards this year under the MOE Masterplan of Awards. These awards will be presented at the 2009 + MOE Work Plan Seminar on 17 Sep 09 by the Minister for Education, Dr Ng Eng Hen.

]]>
+ http://feedproxy.google.com/~r/singapore-education/spotlight/~3/vnNsA4Hdk6U/ + http://www.moe.gov.sg/media/spotlight/ + + + Thu, 10 Sep 2009 17:00:00 +0800 + http://www.moe.gov.sg/media/spotlight/
+ + + New Programmes in Special Assistance Plan (SAP) Schools + Students engaged in discussionAt the Special Assistance Plan (SAP) Seminar + 2009 on 4 Sep 09, Ms Grace Fu, +Senior Minister of State for Education, shared on the new initiatives +rolled out in SAP schools.

]]>
+ http://feedproxy.google.com/~r/singapore-education/spotlight/~3/vnNsA4Hdk6U/ + http://www.moe.gov.sg/media/spotlight/ + + + Fri, 04 Sep 2009 09:30:00 +0800 + http://www.moe.gov.sg/media/spotlight/
+ +
+