summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortkellner <tkellner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2013-12-09 18:28:20 +0000
committertkellner <tkellner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2013-12-09 18:28:20 +0000
commite60190e2c28b66e98864d6b0c5973e2ee2e553aa (patch)
tree70ce5a8bf6719621fc3e2f9f6d2bea666a42a9f2
parentca26d2805186957ecf06ec0a9ff7b1606d49134d (diff)
downloadmocca-e60190e2c28b66e98864d6b0c5973e2ee2e553aa.tar.gz
mocca-e60190e2c28b66e98864d6b0c5973e2ee2e553aa.tar.bz2
mocca-e60190e2c28b66e98864d6b0c5973e2ee2e553aa.zip
Add two utility toString functions to DOMUtils
git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@1244 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4
-rw-r--r--utils/src/main/java/at/gv/egiz/dom/DOMUtils.java56
1 files changed, 49 insertions, 7 deletions
diff --git a/utils/src/main/java/at/gv/egiz/dom/DOMUtils.java b/utils/src/main/java/at/gv/egiz/dom/DOMUtils.java
index 20f4199f..eae8f05e 100644
--- a/utils/src/main/java/at/gv/egiz/dom/DOMUtils.java
+++ b/utils/src/main/java/at/gv/egiz/dom/DOMUtils.java
@@ -29,28 +29,40 @@ import iaik.utils.Base64OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
+import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
-
+
public final class DOMUtils {
/**
* DOM Implementation.
*/
private static String DOM_LS_3_0 = "LS 3.0";
-
+
private static DOMImplementationLS domImplLS;
-
+
+ private final static Logger log = LoggerFactory.getLogger(DOMUtils.class);
+
private DOMUtils() {
}
-
+
private static synchronized void ensureDOMImplementation() {
if (domImplLS == null) {
@@ -79,7 +91,7 @@ public final class DOMUtils {
return domImplLS;
}
-
+
public static Document createDocument() {
// This does not work with the Xerces-J version (2.6.2) included in Java 6
@@ -95,7 +107,37 @@ public final class DOMUtils {
return db.newDocument();
}
-
+
+ public static String documentToString(Document doc) {
+ StringWriter sw = new StringWriter();
+ try {
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer transformer = tf.newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+
+ transformer.transform(new DOMSource(doc), new StreamResult(sw));
+ } catch (TransformerException ex) {
+ log.error("documentToString Transformer Exception");
+ }
+ return sw.toString();
+ }
+
+ public static String nodeToString(Node node) {
+ StringWriter sw = new StringWriter();
+ try {
+ Transformer t = TransformerFactory.newInstance().newTransformer();
+ t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ t.setOutputProperty(OutputKeys.INDENT, "yes");
+ t.transform(new DOMSource(node), new StreamResult(sw));
+ } catch (TransformerException te) {
+ log.error("nodeToString Transformer Exception");
+ }
+ return sw.toString();
+ }
+
public static Text createBase64Text(byte[] bytes, Document doc) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -105,7 +147,7 @@ public final class DOMUtils {
return doc.createTextNode(outputStream.toString("ASCII"));
}
-
+
public static Text createBase64Text(InputStream bytes, Document doc) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();