KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestSAXContentHandler


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: TestSAXContentHandler.java,v 1.1 2003/07/07 10:30:29 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.SAXContentHandler;
17 import org.dom4j.io.SAXReader;
18 import org.xml.sax.XMLReader JavaDoc;
19
20 import javax.xml.parsers.SAXParser JavaDoc;
21 import javax.xml.parsers.SAXParserFactory JavaDoc;
22 import java.io.File JavaDoc;
23
24 public class TestSAXContentHandler extends AbstractTestCase {
25     private XMLReader JavaDoc xmlReader;
26
27     protected String JavaDoc[] testDocuments = {
28         "xml/test/test_schema.xml",
29         //"xml/test/encode.xml",
30
"xml/fibo.xml",
31         "xml/test/schema/personal-prefix.xsd",
32         //"xml/test/soap2.xml",
33
};
34
35     public static void main( String JavaDoc[] args ) {
36         TestRunner.run( suite() );
37     }
38
39     public static Test suite() {
40         return new TestSuite( TestSAXContentHandler.class );
41     }
42
43     public TestSAXContentHandler(String JavaDoc name) {
44         super(name);
45     }
46
47     protected void setUp() throws Exception JavaDoc {
48         SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
49         spf.setNamespaceAware(true);
50         SAXParser JavaDoc parser = spf.newSAXParser();
51         xmlReader = parser.getXMLReader();
52
53         System.out.println("Using XMLReader class: "+xmlReader.getClass().getName());
54     }
55
56     public void testSAXContentHandler() throws Exception JavaDoc {
57
58         SAXContentHandler contentHandler = new SAXContentHandler();
59         xmlReader.setContentHandler(contentHandler);
60         xmlReader.setDTDHandler(contentHandler);
61         xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", contentHandler);
62
63         for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
64             SAXReader reader = new SAXReader();
65             File JavaDoc file = new File JavaDoc(testDocuments[i]);
66             Document docFromSAXReader = reader.read( file );
67
68             xmlReader.parse(file.toURL().toString());
69             Document docFromSAXContentHandler = contentHandler.getDocument();
70
71             //System.out.println("docFromSAXReader = " + docFromSAXReader.asXML());
72
//System.out.println("docFromSAXContentHandler = " + docFromSAXContentHandler.asXML());
73

74             docFromSAXContentHandler.setName(docFromSAXReader.getName());
75
76             assertDocumentsEqual(docFromSAXReader, docFromSAXContentHandler);
77             assertEquals(docFromSAXReader.asXML(), docFromSAXContentHandler.asXML());
78         }
79     }
80
81 }
82
83
84
85
86 /*
87  * Redistribution and use of this software and associated documentation
88  * ("Software"), with or without modification, are permitted provided
89  * that the following conditions are met:
90  *
91  * 1. Redistributions of source code must retain copyright
92  * statements and notices. Redistributions must also contain a
93  * copy of this document.
94  *
95  * 2. Redistributions in binary form must reproduce the
96  * above copyright notice, this list of conditions and the
97  * following disclaimer in the documentation and/or other
98  * materials provided with the distribution.
99  *
100  * 3. The name "DOM4J" must not be used to endorse or promote
101  * products derived from this Software without prior written
102  * permission of MetaStuff, Ltd. For written permission,
103  * please contact dom4j-info@metastuff.com.
104  *
105  * 4. Products derived from this Software may not be called "DOM4J"
106  * nor may "DOM4J" appear in their names without prior written
107  * permission of MetaStuff, Ltd. DOM4J is a registered
108  * trademark of MetaStuff, Ltd.
109  *
110  * 5. Due credit should be given to the DOM4J Project
111  * (http://dom4j.org/).
112  *
113  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
114  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
115  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
116  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
117  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
118  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
119  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
120  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
121  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
122  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
123  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
124  * OF THE POSSIBILITY OF SUCH DAMAGE.
125  *
126  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
127  *
128  * $Id: TestSAXContentHandler.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
129  */

130
Popular Tags