[core] Refactor XMLRenderer to use XMLStreamWriter
In order to properly support different encodings, a OutputStream is needed. Then Java will take care of unmappaple characters and encode them as entities for XML. For backwards compatibility, a writer is still created and exposed.
This commit is contained in:
1 parent
bb97b693f2
commit
2e006697e0
4 files changed
+200
-173
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -45,6 +45,7 @@ public class XSLTRenderer extends XMLRenderer {
|
||||
private Transformer transformer;
|
||||
private String xsltFilename = "/pmd-nicerhtml.xsl";
|
||||
private Writer outputWriter;
|
||||
private StringWriter stringWriter;
|
||||
|
||||
public XSLTRenderer() {
|
||||
super();
|
||||
@@ -71,8 +72,8 @@ public class XSLTRenderer extends XMLRenderer {
|
||||
// We keep the inital writer to put the final html output
|
||||
this.outputWriter = getWriter();
|
||||
// We use a new one to store the XML...
|
||||
Writer w = new StringWriter();
|
||||
setWriter(w);
|
||||
this.stringWriter = new StringWriter();
|
||||
setWriter(stringWriter);
|
||||
// If don't find the xsl no need to bother doing the all report,
|
||||
// so we check this here...
|
||||
InputStream xslt = null;
|
||||
@@ -100,16 +101,14 @@ public class XSLTRenderer extends XMLRenderer {
|
||||
* The stylesheet provided as an InputStream
|
||||
*/
|
||||
private void prepareTransformer(InputStream xslt) {
|
||||
if (xslt != null) {
|
||||
try {
|
||||
// Get a TransformerFactory object
|
||||
TransformerFactory factory = TransformerFactory.newInstance();
|
||||
StreamSource src = new StreamSource(xslt);
|
||||
// Get an XSL Transformer object
|
||||
this.transformer = factory.newTransformer(src);
|
||||
} catch (TransformerConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
// Get a TransformerFactory object
|
||||
TransformerFactory factory = TransformerFactory.newInstance();
|
||||
StreamSource src = new StreamSource(xslt);
|
||||
// Get an XSL Transformer object
|
||||
this.transformer = factory.newTransformer(src);
|
||||
} catch (TransformerConfigurationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,25 +117,17 @@ public class XSLTRenderer extends XMLRenderer {
|
||||
// First we finish the XML report
|
||||
super.end();
|
||||
// Now we transform it using XSLT
|
||||
if (writer instanceof StringWriter) {
|
||||
StringWriter w = (StringWriter) writer;
|
||||
Document doc = this.getDocument(w.toString());
|
||||
this.transform(doc);
|
||||
} else {
|
||||
// Should not happen !
|
||||
throw new RuntimeException("Wrong writer");
|
||||
}
|
||||
|
||||
Document doc = this.getDocument(stringWriter.toString());
|
||||
this.transform(doc);
|
||||
}
|
||||
|
||||
private void transform(Document doc) {
|
||||
DOMSource source = new DOMSource(doc);
|
||||
this.setWriter(new StringWriter());
|
||||
StreamResult result = new StreamResult(this.outputWriter);
|
||||
try {
|
||||
transformer.transform(source, result);
|
||||
} catch (TransformerException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,8 +136,7 @@ public class XSLTRenderer extends XMLRenderer {
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
return parser.parse(new InputSource(new StringReader(xml)));
|
||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -125,10 +125,10 @@ public class XMLRendererTest extends AbstractRendererTest {
|
||||
|
||||
public String getHeader() {
|
||||
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + PMD.EOL
|
||||
+ "<pmd xmlns=\"http://pmd.sourceforge.net/report/2.0.0\"" + PMD.EOL
|
||||
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + PMD.EOL
|
||||
+ " xsi:schemaLocation=\"http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd\"" + PMD.EOL
|
||||
+ " version=\"" + PMDVersion.VERSION + "\" timestamp=\"2014-10-06T19:30:51.262\">" + PMD.EOL;
|
||||
+ "<pmd xmlns=\"http://pmd.sourceforge.net/report/2.0.0\""
|
||||
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
|
||||
+ " xsi:schemaLocation=\"http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd\""
|
||||
+ " version=\"" + PMDVersion.VERSION + "\" timestamp=\"2014-10-06T19:30:51.262\">" + PMD.EOL;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -8,10 +8,8 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Rule;
|
||||
@@ -112,17 +110,8 @@ public class PMDTaskTest extends AbstractAntTestHelper {
|
||||
}
|
||||
};
|
||||
|
||||
// See http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding and http://stackoverflow.com/a/14987992/1169968
|
||||
private static void setDefaultCharset(String charsetName) {
|
||||
try {
|
||||
System.setProperty("file.encoding", charsetName);
|
||||
Field charset = Charset.class.getDeclaredField("defaultCharset");
|
||||
charset.setAccessible(true);
|
||||
charset.set(null, null);
|
||||
Objects.requireNonNull(Charset.defaultCharset());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.setProperty("file.encoding", charsetName);
|
||||
}
|
||||
|
||||
@Rule
|
||||
|
||||
Reference in new issue
Block a user