01: /*
02: * Copyright 2005-2007 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.kuali.rice.util;
18:
19: import java.io.IOException;
20: import java.io.StringWriter;
21:
22: import javax.xml.transform.OutputKeys;
23: import javax.xml.transform.Result;
24: import javax.xml.transform.Source;
25: import javax.xml.transform.Transformer;
26: import javax.xml.transform.TransformerException;
27: import javax.xml.transform.TransformerFactory;
28: import javax.xml.transform.dom.DOMSource;
29: import javax.xml.transform.stream.StreamResult;
30:
31: import org.jdom.output.XMLOutputter;
32:
33: /**
34: *
35: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
36: */
37: public class XmlJotter {
38:
39: public static String jotNode(org.jdom.Element element) {
40: XMLOutputter outputer = new XMLOutputter();
41: StringWriter writer = new StringWriter();
42: try {
43: outputer.output(element, writer);
44: } catch (IOException e) {
45: throw new RuntimeException(
46: "Could not write XML data export.", e);
47: }
48: return writer.toString();
49: }
50:
51: public static String jotNode(org.w3c.dom.Node node) {
52: // default to true since this is used mostly for debugging
53: return jotNode(node, true);
54: }
55:
56: public static String jotNode(org.w3c.dom.Node node, boolean indent) {
57: try {
58: return writeNode(node, indent);
59: } catch (TransformerException te) {
60: return RiceUtilities.collectStackTrace(te);
61: }
62: }
63:
64: public static String writeNode(org.w3c.dom.Node node)
65: throws TransformerException {
66: return writeNode(node, false);
67: }
68:
69: public static String writeNode(org.w3c.dom.Node node, boolean indent)
70: throws TransformerException {
71: Source source = new DOMSource(node);
72: StringWriter writer = new StringWriter();
73: Result result = new StreamResult(writer);
74: Transformer transformer = TransformerFactory.newInstance()
75: .newTransformer();
76: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
77: "yes");
78: if (indent) {
79: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
80: }
81: transformer.transform(source, result);
82: return writer.toString();
83: }
84:
85: }
|