KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > jaxp > RoundTripDemo


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: RoundTripDemo.java,v 1.4 2005/01/29 14:53:13 maartenc Exp $
8  */

9
10 package org.dom4j.samples.jaxp;
11
12 import org.dom4j.samples.SAXDemo;
13
14 import java.io.Reader JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.io.Writer JavaDoc;
18
19 import javax.xml.transform.Transformer JavaDoc;
20 import javax.xml.transform.TransformerFactory JavaDoc;
21 import javax.xml.transform.stream.StreamResult JavaDoc;
22 import javax.xml.transform.stream.StreamSource JavaDoc;
23
24 import org.dom4j.Document;
25 import org.dom4j.io.DocumentResult;
26 import org.dom4j.io.DocumentSource;
27 import org.dom4j.io.XMLWriter;
28
29 /**
30  * A program demonstrating a round trip from XML to dom4j to text to dom4j again
31  * using JAXP to convert the XML.
32  *
33  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
34  * @version $Revision: 1.4 $
35  */

36 public class RoundTripDemo extends SAXDemo {
37
38     public static void main(String JavaDoc[] args) {
39         run(new RoundTripDemo(), args);
40     }
41
42     public RoundTripDemo() {
43     }
44
45     protected void outputDocument(Document document, Writer JavaDoc out) {
46         try {
47             TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
48
49             Transformer JavaDoc transformer = factory.newTransformer();
50
51             StreamResult JavaDoc result = new StreamResult JavaDoc(out);
52             DocumentSource source = new DocumentSource(document);
53
54             transformer.transform(source, result);
55         } catch (Exception JavaDoc ex) {
56             ex.printStackTrace();
57         }
58
59     }
60
61     protected Document parseDocument(Reader JavaDoc in) {
62         try {
63             TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
64
65             Transformer JavaDoc transformer = factory.newTransformer();
66
67             DocumentResult result = new DocumentResult();
68             StreamSource JavaDoc source = new StreamSource JavaDoc(in);
69
70             transformer.transform(source, result);
71
72             return result.getDocument();
73         } catch (Exception JavaDoc ex) {
74             ex.printStackTrace();
75             return null;
76         }
77
78     }
79
80     /** Outputs the document to a buffer, parse it back again then output it */
81     protected void process(Document document) throws Exception JavaDoc {
82
83         System.out.println("about to output: " + document);
84
85         StringWriter JavaDoc out = new StringWriter JavaDoc();
86         outputDocument(document, out);
87
88         Document doc2 = parseDocument(new StringReader JavaDoc(out.toString()));
89
90         System.out.println("parsed back again: " + doc2);
91
92         System.out.println("Writing it out...");
93
94         XMLWriter writer = new XMLWriter(System.out);
95         writer.write(doc2);
96
97     }
98 }
99
100 /*
101  * Redistribution and use of this software and associated documentation
102  * ("Software"), with or without modification, are permitted provided that the
103  * following conditions are met:
104  *
105  * 1. Redistributions of source code must retain copyright statements and
106  * notices. Redistributions must also contain a copy of this document.
107  *
108  * 2. Redistributions in binary form must reproduce the above copyright notice,
109  * this list of conditions and the following disclaimer in the documentation
110  * and/or other materials provided with the distribution.
111  *
112  * 3. The name "DOM4J" must not be used to endorse or promote products derived
113  * from this Software without prior written permission of MetaStuff, Ltd. For
114  * written permission, please contact dom4j-info@metastuff.com.
115  *
116  * 4. Products derived from this Software may not be called "DOM4J" nor may
117  * "DOM4J" appear in their names without prior written permission of MetaStuff,
118  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
119  *
120  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
121  *
122  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
123  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
124  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
125  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
126  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
127  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
128  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
129  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
130  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
131  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
132  * POSSIBILITY OF SUCH DAMAGE.
133  *
134  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
135  *
136  * $Id: RoundTripDemo.java,v 1.4 2005/01/29 14:53:13 maartenc Exp $
137  */

138
Popular Tags