pmd (build): Add a commodity method to printout a DOMSource

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7529 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse
2011-12-06 18:15:39 +00:00
parent 4b8caa5bfa
commit 6ecd260d25

View File

@ -3,6 +3,7 @@
*/ */
package net.sourceforge.pmd.build.util; package net.sourceforge.pmd.build.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -11,8 +12,10 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import net.sourceforge.pmd.build.PmdBuildException; import net.sourceforge.pmd.build.PmdBuildException;
@ -30,7 +33,7 @@ import org.xml.sax.SAXException;
public final class XmlUtil { public final class XmlUtil {
private XmlUtil() {}; private XmlUtil() {};
public static Transformer createTransformer(String xsl) throws PmdBuildException { public static Transformer createTransformer(String xsl) throws PmdBuildException {
try { try {
TransformerFactory factory = TransformerFactory.newInstance(); TransformerFactory factory = TransformerFactory.newInstance();
@ -40,7 +43,7 @@ public final class XmlUtil {
throw new PmdBuildException(e); throw new PmdBuildException(e);
} }
} }
public static DOMSource createDomSourceFrom(InputStream inputStream) { public static DOMSource createDomSourceFrom(InputStream inputStream) {
try { try {
return new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream)); return new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream));
@ -52,7 +55,7 @@ public final class XmlUtil {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
} }
public static DOMSource createXmlBackbone(XmlFileTemplater templater) { public static DOMSource createXmlBackbone(XmlFileTemplater templater) {
Document doc = null; Document doc = null;
try { try {
@ -67,4 +70,21 @@ public final class XmlUtil {
doc.appendChild(root); doc.appendChild(root);
return new DOMSource(doc); return new DOMSource(doc);
} }
public static String transformDOMToString(DOMSource source) {
try {
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
ByteArrayOutputStream sos = new ByteArrayOutputStream();
StreamResult result = new StreamResult(sos);
transformer.transform(source, result);
return sos.toString();
} catch (TransformerException e) {
throw new IllegalArgumentException(e);
}
}
} }