KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > util > XmlUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.util;
18
19 import java.io.IOException JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import javax.xml.transform.Transformer JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23
24 import org.dom4j.Document;
25 import org.dom4j.DocumentException;
26 import org.dom4j.DocumentHelper;
27 import org.dom4j.Element;
28 import org.dom4j.io.DocumentResult;
29 import org.dom4j.io.DocumentSource;
30 import org.dom4j.io.OutputFormat;
31 import org.dom4j.io.XMLWriter;
32
33 /**
34  * Utilities to parse strings into XML DOM Documents and vice versa
35  */

36 public final class XmlUtils {
37     
38     /**
39      * uses Dom4j to neatly format a given XML string
40      * by adding indents, newlines etc.
41      */

42     public static String JavaDoc getAsPrettyXml(String JavaDoc xmlString) {
43         return getAsPrettyXml(parse(xmlString));
44     }
45     
46     /**
47      * Override that accepts an XML DOM Document
48      * @param document XML as DOM Document
49      */

50     public static String JavaDoc getAsPrettyXml(Document document) {
51         OutputFormat format = new OutputFormat(" ", true);
52         format.setSuppressDeclaration(true);
53         StringWriter JavaDoc out = new StringWriter JavaDoc();
54         XMLWriter writer = new XMLWriter(out, format);
55         try {
56             try {
57                 writer.write(document);
58             } finally {
59                 writer.close();
60             }
61         } catch (IOException JavaDoc ioe) {
62             throw new RuntimeException JavaDoc(ioe);
63         }
64         return out.toString().trim();
65     }
66     
67     /**
68      * Converts a String into XML by parsing into a DOM Document
69      * uses Dom4j
70      */

71     public static Document parse(String JavaDoc xmlString) {
72         try {
73             return DocumentHelper.parseText(xmlString);
74         } catch (DocumentException de) {
75             throw new RuntimeException JavaDoc(de);
76         }
77     }
78     
79     public static Element getNewElement(String JavaDoc name) {
80         return DocumentHelper.createElement(name);
81     }
82     
83     public static Document getNewDocument(String JavaDoc rootElementName) {
84         Document d = DocumentHelper.createDocument();
85         d.addElement(rootElementName);
86         return d;
87     }
88     
89     public static Document transform(Document source, Document stylesheet) {
90         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
91         try {
92             Transformer JavaDoc transformer = factory.newTransformer(new DocumentSource(stylesheet));
93             DocumentResult result = new DocumentResult();
94             transformer.transform(new DocumentSource(source), result );
95             return result.getDocument();
96         } catch (Exception JavaDoc e) {
97             throw new RuntimeException JavaDoc(e);
98         }
99     }
100     
101 }
102
Popular Tags