| 1 | |
package org.kuali.rice.core.api.util.xml; |
| 2 | |
|
| 3 | |
import org.jdom.output.Format; |
| 4 | |
import org.jdom.output.XMLOutputter; |
| 5 | |
|
| 6 | |
import javax.xml.transform.OutputKeys; |
| 7 | |
import javax.xml.transform.Result; |
| 8 | |
import javax.xml.transform.Source; |
| 9 | |
import javax.xml.transform.Transformer; |
| 10 | |
import javax.xml.transform.TransformerException; |
| 11 | |
import javax.xml.transform.TransformerFactory; |
| 12 | |
import javax.xml.transform.dom.DOMSource; |
| 13 | |
import javax.xml.transform.stream.StreamResult; |
| 14 | |
import java.io.IOException; |
| 15 | |
import java.io.StringWriter; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
public final class XmlJotter { |
| 22 | |
|
| 23 | 0 | private XmlJotter() { |
| 24 | 0 | throw new UnsupportedOperationException("do not call"); |
| 25 | |
} |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public static String jotDocument(org.jdom.Document document) { |
| 34 | 0 | return jotDocument(document, true); |
| 35 | |
} |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public static String jotDocument(org.jdom.Document document, boolean indent) { |
| 45 | 0 | XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent)); |
| 46 | 0 | StringWriter writer = new StringWriter(); |
| 47 | |
try { |
| 48 | 0 | outputer.output(document, writer); |
| 49 | 0 | } catch (IOException e) { |
| 50 | 0 | throw new XmlException("Could not write XML data export.", e); |
| 51 | 0 | } |
| 52 | 0 | return writer.toString(); |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public static String jotDocument(org.w3c.dom.Document document) { |
| 62 | 0 | return jotNode(document.getDocumentElement()); |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public static String jotDocument(org.w3c.dom.Document document, boolean indent) { |
| 73 | 0 | return jotNode(document.getDocumentElement(), indent); |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public static String jotNode(org.jdom.Element node) { |
| 83 | 0 | return jotNode(node, true); |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public static String jotNode(org.jdom.Element node, boolean indent) { |
| 94 | 0 | XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent)); |
| 95 | 0 | StringWriter writer = new StringWriter(); |
| 96 | |
try { |
| 97 | 0 | outputer.output(node, writer); |
| 98 | 0 | } catch (IOException e) { |
| 99 | 0 | throw new XmlException("Could not write XML data export.", e); |
| 100 | 0 | } |
| 101 | 0 | return writer.toString(); |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public static String jotNode(org.w3c.dom.Node node) { |
| 111 | 0 | return jotNode(node, true); |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public static String jotNode(org.w3c.dom.Node node, boolean indent) { |
| 122 | |
try { |
| 123 | 0 | return nodeToString(node, indent); |
| 124 | 0 | } catch (TransformerException te) { |
| 125 | 0 | throw new XmlException(te); |
| 126 | |
} |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
private static String nodeToString(org.w3c.dom.Node node, boolean indent) throws TransformerException { |
| 138 | 0 | Source source = new DOMSource(node); |
| 139 | 0 | StringWriter writer = new StringWriter(); |
| 140 | 0 | Result result = new StreamResult(writer); |
| 141 | 0 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
| 142 | 0 | transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 143 | 0 | if (indent) { |
| 144 | 0 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 145 | |
} |
| 146 | 0 | transformer.transform(source, result); |
| 147 | 0 | return writer.toString(); |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
private static Format getJdomFormat(boolean indent) { |
| 157 | 0 | return indent ? Format.getPrettyFormat() : Format.getRawFormat(); |
| 158 | |
} |
| 159 | |
} |