KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestRoundTrip


1 /*
2  * Copyright 2001 (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: TestRoundTrip.java,v 1.2 2003/07/12 12:13:06 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Document;
16 import org.dom4j.io.*;
17 import org.w3c.tidy.Tidy;
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 import java.io.*;
24 import java.net.URL JavaDoc;
25
26 /** A test harness to test the the round trips of Documents.
27   *
28   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 1.2 $
30   */

31 public class TestRoundTrip extends AbstractTestCase {
32
33     protected String JavaDoc[] testDocuments = {
34         //"xml/test/encode.xml",
35
"xml/fibo.xml",
36         "xml/test/schema/personal-prefix.xsd",
37         //"xml/test/soap2.xml",
38
"xml/test/test_schema.xml",
39     };
40
41     public static void main( String JavaDoc[] args ) {
42         TestRunner.run( suite() );
43     }
44
45     public static Test suite() {
46         return new TestSuite( TestRoundTrip.class );
47     }
48
49     public TestRoundTrip(String JavaDoc name) {
50         super(name);
51     }
52
53     // Test case(s)
54
//-------------------------------------------------------------------------
55
public void testTextRoundTrip() throws Exception JavaDoc {
56         for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
57             Document doc = parseDocument( testDocuments[i] );
58             roundTripText( doc );
59         }
60     }
61
62     public void testSAXRoundTrip() throws Exception JavaDoc {
63         for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
64             Document doc = parseDocument( testDocuments[i] );
65             roundTripSAX( doc );
66         }
67     }
68
69     public void testDOMRoundTrip() throws Exception JavaDoc {
70         for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
71             Document doc = parseDocument( testDocuments[i] );
72             roundTripDOM( doc );
73         }
74     }
75
76     public void testJAXPRoundTrip() throws Exception JavaDoc {
77         for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
78             Document doc = parseDocument( testDocuments[i] );
79             roundTripJAXP( doc );
80         }
81     }
82
83     public void testFullRoundTrip() throws Exception JavaDoc {
84         for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
85             Document doc = parseDocument( testDocuments[i] );
86             roundTripFull( doc );
87         }
88     }
89
90     public void testJTidyRoundTrip() throws Exception JavaDoc {
91         Document document = loadHTML( "xml/readme.html" );
92
93         //Document doc1 = roundTripText( document );
94
Document doc1 = roundTripSAX( document );
95         Document doc2 = roundTripDOM( doc1);
96         Document doc3 = roundTripSAX( doc2 );
97         //Document doc4 = roundTripText( doc3 );
98
//Document doc5 = roundTripDOM( doc4 );
99
Document doc5 = roundTripDOM( doc3 );
100
101         assertDocumentsEqual( document, doc5 );
102     }
103
104     // Implementation methods
105
//-------------------------------------------------------------------------
106
protected void setUp() throws Exception JavaDoc {
107     }
108
109     protected Document parseDocument(String JavaDoc file) throws Exception JavaDoc {
110         SAXReader reader = new SAXReader();
111         return reader.read( file );
112     }
113
114     protected Document loadHTML( String JavaDoc xmlFile ) throws Exception JavaDoc {
115         InputStream in = openStream( xmlFile );
116         Tidy tidy = new Tidy();
117         tidy.setXHTML(true);
118         tidy.setDocType("omit");
119         org.w3c.dom.Document JavaDoc domDocument = tidy.parseDOM( in, null );
120
121         DOMReader domReader = new DOMReader();
122         return domReader.read( domDocument );
123     }
124
125     protected InputStream openStream(String JavaDoc xmlFile) throws Exception JavaDoc {
126         File file = new File( xmlFile );
127         if ( file.exists() ) {
128             return new BufferedInputStream( new FileInputStream( file ) );
129         }
130         try {
131             return new URL JavaDoc( xmlFile ).openStream();
132         } catch (IOException e) {
133             throw new FileNotFoundException("resource does not exist " + file.getAbsolutePath() + " and is not a URL");
134         }
135     }
136
137     protected Document roundTripDOM(Document document) throws Exception JavaDoc {
138         // now lets make a DOM object
139
DOMWriter domWriter = new DOMWriter();
140         org.w3c.dom.Document JavaDoc domDocument = domWriter.write(document);
141
142         // now lets read it back as a DOM4J object
143
DOMReader domReader = new DOMReader();
144         Document newDocument = domReader.read( domDocument );
145
146         // lets ensure names are same
147
newDocument.setName( document.getName() );
148
149         assertDocumentsEqual( document, newDocument );
150
151         return newDocument;
152     }
153
154     protected Document roundTripJAXP(Document document) throws Exception JavaDoc {
155         // output the document to a text buffer via JAXP
156
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
157         Transformer JavaDoc transformer = factory.newTransformer();
158
159         StringWriter buffer = new StringWriter();
160         StreamResult JavaDoc streamResult = new StreamResult JavaDoc(buffer);
161         DocumentSource documentSource = new DocumentSource(document);
162
163         transformer.transform(documentSource, streamResult);
164
165         // now lets parse it back again via JAXP
166
DocumentResult documentResult = new DocumentResult();
167         StreamSource JavaDoc streamSource = new StreamSource JavaDoc( new StringReader( buffer.toString() ) );
168
169         transformer.transform(streamSource, documentResult);
170
171         Document newDocument = documentResult.getDocument();
172
173         // lets ensure names are same
174
newDocument.setName( document.getName() );
175
176         assertDocumentsEqual( document, newDocument );
177
178         return newDocument;
179     }
180
181     protected Document roundTripSAX(Document document) throws Exception JavaDoc {
182
183         // now lets write it back as SAX events to
184
// a SAX ContentHandler which should build up a new document
185
SAXContentHandler contentHandler = new SAXContentHandler();
186         SAXWriter saxWriter = new SAXWriter( contentHandler, null, contentHandler );
187
188         saxWriter.write( document );
189         Document newDocument = contentHandler.getDocument();
190
191         // lets ensure names are same
192
newDocument.setName( document.getName() );
193
194         assertDocumentsEqual( document, newDocument );
195
196         return newDocument;
197     }
198
199     protected Document roundTripText(Document document) throws Exception JavaDoc {
200         StringWriter out = new StringWriter();
201         XMLWriter xmlWriter = new XMLWriter(out);
202
203         xmlWriter.write( document );
204
205         // now lets read it back
206
String JavaDoc xml = out.toString();
207
208         StringReader in = new StringReader( xml );
209         SAXReader reader = new SAXReader();
210         Document newDocument = reader.read(in);
211
212         // lets ensure names are same
213
newDocument.setName( document.getName() );
214
215         assertDocumentsEqual( document, newDocument );
216
217         return newDocument;
218     }
219
220     protected Document roundTripFull(Document document) throws Exception JavaDoc {
221         Document doc2 = roundTripDOM( document );
222         Document doc3 = roundTripSAX( doc2 );
223         Document doc4 = roundTripText( doc3 );
224
225         assertDocumentsEqual( document, doc4 );
226
227         return doc4;
228     }
229 }
230
231
232
233
234 /*
235  * Redistribution and use of this software and associated documentation
236  * ("Software"), with or without modification, are permitted provided
237  * that the following conditions are met:
238  *
239  * 1. Redistributions of source code must retain copyright
240  * statements and notices. Redistributions must also contain a
241  * copy of this document.
242  *
243  * 2. Redistributions in binary form must reproduce the
244  * above copyright notice, this list of conditions and the
245  * following disclaimer in the documentation and/or other
246  * materials provided with the distribution.
247  *
248  * 3. The name "DOM4J" must not be used to endorse or promote
249  * products derived from this Software without prior written
250  * permission of MetaStuff, Ltd. For written permission,
251  * please contact dom4j-info@metastuff.com.
252  *
253  * 4. Products derived from this Software may not be called "DOM4J"
254  * nor may "DOM4J" appear in their names without prior written
255  * permission of MetaStuff, Ltd. DOM4J is a registered
256  * trademark of MetaStuff, Ltd.
257  *
258  * 5. Due credit should be given to the DOM4J Project
259  * (http://dom4j.org/).
260  *
261  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
262  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
263  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
264  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
265  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
266  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
267  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
268  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
270  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
271  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
272  * OF THE POSSIBILITY OF SUCH DAMAGE.
273  *
274  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
275  *
276  * $Id: TestRoundTrip.java,v 1.2 2003/07/12 12:13:06 per_nyfelt Exp $
277  */

278
Popular Tags