Skip to content

Commit

Permalink
Add support for stylesheets (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickGotthard authored Dec 28, 2021
1 parent 49c3c7a commit f7d6c85
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 42 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
</dependency>
<!-- AssertJ -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.21.0</version>
</dependency>
<!-- JavaX -->
<dependency>
<groupId>javax.servlet</groupId>
Expand Down
5 changes: 5 additions & 0 deletions rome/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
Expand Down
23 changes: 22 additions & 1 deletion rome/src/main/java/com/rometools/rome/io/WireFeedOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.ProcessingInstruction;
import org.jdom2.output.DOMOutputter;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
Expand Down Expand Up @@ -119,6 +121,7 @@ public String outputString(final WireFeed feed) throws IllegalArgumentException,
*
*/
public String outputString(final WireFeed feed, final boolean prettyPrint) throws IllegalArgumentException, FeedException {

final Document doc = outputJDom(feed);
final String encoding = feed.getEncoding();
Format format;
Expand All @@ -130,6 +133,7 @@ public String outputString(final WireFeed feed, final boolean prettyPrint) throw
if (encoding != null) {
format.setEncoding(encoding);
}

final XMLOutputter outputter = new XMLOutputter(format);
return outputter.outputString(doc);
}
Expand Down Expand Up @@ -232,6 +236,7 @@ public void output(final WireFeed feed, final Writer writer) throws IllegalArgum
*
*/
public void output(final WireFeed feed, final Writer writer, final boolean prettyPrint) throws IllegalArgumentException, IOException, FeedException {

final Document doc = outputJDom(feed);
final String encoding = feed.getEncoding();
Format format;
Expand All @@ -243,8 +248,10 @@ public void output(final WireFeed feed, final Writer writer, final boolean prett
if (encoding != null) {
format.setEncoding(encoding);
}

final XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc, writer);

}

/**
Expand Down Expand Up @@ -290,7 +297,9 @@ public org.w3c.dom.Document outputW3CDom(final WireFeed feed) throws IllegalArgu
*
*/
public Document outputJDom(final WireFeed feed) throws IllegalArgumentException, FeedException {

final String type = feed.getFeedType();

final WireFeedGenerator generator = getFeedGenerators().getGenerator(type);
if (generator == null) {
throw new IllegalArgumentException("Invalid feed type [" + type + "]");
Expand All @@ -299,7 +308,19 @@ public Document outputJDom(final WireFeed feed) throws IllegalArgumentException,
if (!generator.getType().equals(type)) {
throw new IllegalArgumentException("WireFeedOutput type[" + type + "] and WireFeed type [" + type + "] don't match");
}
return generator.generate(feed);

final Document doc = generator.generate(feed);

final String styleSheet = feed.getStyleSheet();
if (styleSheet != null) {
final Map<String, String> data = new LinkedHashMap<String, String>();
data.put("type", "text/xsl");
data.put("href", styleSheet);
doc.addContent(0, new ProcessingInstruction("xml-stylesheet", data));
}

return doc;

}

}
18 changes: 17 additions & 1 deletion rome/src/test/java/com/rometools/rome/unittest/FeedOpsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.rometools.rome.unittest;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
Expand All @@ -23,13 +25,14 @@
import com.rometools.rome.feed.WireFeed;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.feed.synd.SyndFeedImpl;
import com.rometools.rome.io.SyndFeedOutput;

public abstract class FeedOpsTest extends FeedTest {

protected FeedOpsTest(final String feedType) {
super(feedType + ".xml");
}

protected FeedOpsTest(final String feedType, boolean allowDoctypes) {
super(feedType + ".xml", allowDoctypes);
}
Expand Down Expand Up @@ -123,4 +126,17 @@ public void testSyndFeedSerialization() throws Exception {
assertTrue(feed1.equals(feed2));
}

public void testStylesheet() throws Exception {

// verify parser
assertThat(this.getCachedSyndFeed().getStyleSheet()).isEqualTo("style.xsl");
assertThat(this.getCachedWireFeed().getStyleSheet()).isEqualTo("style.xsl");

// verify output
final SyndFeed feed = this.getCachedSyndFeed();
final String xml = new SyndFeedOutput().outputString(feed);
assertThat(xml).contains("<?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?>");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*/
package com.rometools.rome.unittest;

public class TestOpsRSS091N extends FeedOpsTest {
import static org.assertj.core.api.Assertions.assertThat;

public static void main(final String[] args) throws Exception {
final FeedOpsTest test = new TestOpsRSS091N();
test.testWireFeedSyndFeedConversion();
}
public class TestOpsRSS091N extends FeedOpsTest {

public TestOpsRSS091N() {
super("rss_0.91N", true);
}

@Override
public void testStylesheet() throws Exception {
assertThat(this.getCachedSyndFeed().getStyleSheet()).isEqualTo("style.xsl");
assertThat(this.getCachedWireFeed().getStyleSheet()).isEqualTo("style.xsl");
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.rometools.rome.unittest;

import static org.assertj.core.api.Assertions.assertThat;

public class TestOpsRSS091U extends FeedOpsTest {

public TestOpsRSS091U() {
super("rss_0.91U");
}

@Override
public void testStylesheet() throws Exception {
assertThat(this.getCachedSyndFeed().getStyleSheet()).isEqualTo("style.xsl");
assertThat(this.getCachedWireFeed().getStyleSheet()).isEqualTo("style.xsl");
}

}

This file was deleted.

1 change: 1 addition & 0 deletions rome/src/test/resources/atom_0.3.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<feed version="0.3"
xmlns="http://purl.org/atom/ns#"
xmlns:rometest='http://rome.dev.java.net/namespacetest' >
Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/atom_1.0.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:rometest='http://rome.dev.java.net/namespacetest'
xml:lang='en-us'>
Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/rss_0.9.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://my.netscape.com/rdf/simple/0.9/">
<channel>
Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/rss_0.91N.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
"http://my.netscape.com/publish/formats/rss-0.91.dtd">

Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/rss_0.91U.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<rss version="0.91">

<channel>
Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/rss_0.92.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<rss version="0.92">
<channel>
<title>rss_0.92.channel.title</title>
Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/rss_0.93.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<rss version="0.93">
<channel>
<title>rss_0.93.channel.title</title>
Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/rss_0.94.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<rss version="0.94">
<channel>
<title>rss_0.94.channel.title</title>
Expand Down
1 change: 1 addition & 0 deletions rome/src/test/resources/rss_1.0.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rometest='http://rome.dev.java.net/namespacetest'
xmlns:content='http://purl.org/rss/1.0/modules/content/'
Expand Down
2 changes: 1 addition & 1 deletion rome/src/test/resources/rss_2.0.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="http://test.example/test.xslt"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<rss version="2.0"
xmlns:rometest='http://rome.dev.java.net/namespacetest'
xmlns:content='http://purl.org/rss/1.0/modules/content/'>
Expand Down

0 comments on commit f7d6c85

Please sign in to comment.