Merge pull request #213 from mishako/spaces-in-rss-version

Trim RSS version string
This commit is contained in:
Patrick Gotthard 2015-12-15 07:37:02 +01:00
commit b0f3908956
2 changed files with 60 additions and 7 deletions

View file

@ -14,14 +14,15 @@
* limitations under the License.
*
*/
package com.rometools.rome.io.impl;
import com.rometools.rome.feed.rss.Description;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import com.rometools.rome.feed.rss.Description;
public class RSS20Parser extends RSS094Parser {
public RSS20Parser() {
@ -50,11 +51,16 @@ public class RSS20Parser extends RSS094Parser {
@Override
public boolean isMyType(final Document document) {
final Element rssRoot = document.getRootElement();
final Attribute version = rssRoot.getAttribute("version");
// as far ROME is concerned RSS 2.0, 2.00 and 2.0.X are all the same, so let's use
// startsWith for leniency.
return rssRoot.getName().equals("rss") && version != null && version.getValue().startsWith(getRSSVersion());
return rootElementMatches(document) && versionMatches(document);
}
private boolean rootElementMatches(final Document document) {
return document.getRootElement().getName().equals("rss");
}
private boolean versionMatches(final Document document) {
final Attribute version = document.getRootElement().getAttribute("version");
return (version != null)
&& version.getValue().trim().startsWith(getRSSVersion());
}
}

View file

@ -0,0 +1,47 @@
package com.rometools.rome.io.impl;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.rometools.rome.io.WireFeedParser;
import org.jdom2.Document;
import org.jdom2.Element;
import org.junit.Before;
import org.junit.Test;
public class RSS20ParserTest {
private WireFeedParser parser;
private Document document;
@Before
public void setUp() throws Exception {
parser = new RSS20Parser();
document = new Document();
}
@Test
public void testIsMyType() {
document.setRootElement(new Element("rss").setAttribute("version", "2.0"));
assertTrue(parser.isMyType(document));
}
@Test
public void testIsMyTypeNotMyType() {
document.setRootElement(new Element("rss").setAttribute("version", "1.0"));
assertFalse(parser.isMyType(document));
}
@Test
public void testIsMyTypeVersionWithSpaces() {
document.setRootElement(new Element("rss").setAttribute("version", " 2.0 "));
assertTrue(parser.isMyType(document));
}
@Test
public void testIsMyTypeVersionWithTrailingText() {
document.setRootElement(new Element("rss").setAttribute("version", "2.0test"));
assertTrue(parser.isMyType(document));
}
}