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: TestXSLT.java,v 1.4 2002/05/20 08:14:08 jstrachan Exp $
8 */
9
10 package org.dom4j;
11
12 import java.io.File;
13 import java.net.URL;
14 import java.util.List;
15
16 import javax.xml.parsers.SAXParser;
17 import javax.xml.parsers.SAXParserFactory;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 import org.dom4j.io.SAXContentHandler;
24 import org.dom4j.io.SAXReader;
25 import org.dom4j.io.aelfred2.SAXDriver;
26 import org.xml.sax.XMLReader;
27
28 public class TestSAXContentHandler extends AbstractTestCase {
29 private XMLReader xmlReader;
30
31 protected String[] testDocuments = {
32 "/xml/test/test_schema.xml",
33 //"xml/test/encode.xml",
34 "/xml/fibo.xml",
35 "/xml/test/schema/personal-prefix.xsd",
36 //"xml/test/soap2.xml",
37 };
38
39 public static void main( String[] args ) {
40 TestRunner.run( suite() );
41 }
42
43 public static Test suite() {
44 return new TestSuite( TestSAXContentHandler.class );
45 }
46
47 public TestSAXContentHandler(String name) {
48 super(name);
49 }
50
51 protected void setUp() throws Exception {
52 // SAXParserFactory spf = SAXParserFactory.newInstance();
53 // spf.setNamespaceAware(true);
54 // SAXParser parser = spf.newSAXParser();
55 // xmlReader = parser.getXMLReader();
56 xmlReader = new SAXDriver();
57
58 System.out.println("Using XMLReader class: "+xmlReader.getClass().getName());
59 }
60
61 public void testSAXContentHandler() throws Exception {
62
63 SAXContentHandler contentHandler = new SAXContentHandler();
64 xmlReader.setContentHandler(contentHandler);
65 xmlReader.setDTDHandler(contentHandler);
66 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", contentHandler);
67
68 for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
69 SAXReader reader = new SAXReader("org.dom4j.io.aelfred2.SAXDriver");
70 File file = new File(testDocuments[i]);
71 URL url = getClass().getResource(testDocuments[i]);
72 Document docFromSAXReader = reader.read(url);
73
74 xmlReader.parse(url.toString());
75 Document docFromSAXContentHandler = contentHandler.getDocument();
76
77 //System.out.println("docFromSAXReader = " + docFromSAXReader.asXML());
78 //System.out.println("docFromSAXContentHandler = " + docFromSAXContentHandler.asXML());
79
80 docFromSAXContentHandler.setName(docFromSAXReader.getName());
81
82 assertDocumentsEqual(docFromSAXReader, docFromSAXContentHandler);
83 assertEquals(docFromSAXReader.asXML(), docFromSAXContentHandler.asXML());
84 }
85 }
86
87 public void testBug926713() throws Exception {
88 URL url = getClass().getResource("/xml/test/cdata.xml");
89 SAXReader reader = new SAXReader();
90
91 Document doc = reader.read(url);
92 Element foo = doc.getRootElement();
93 Element bar = foo.element("bar");
94 List content = bar.content();
95 assertEquals(3, content.size());
96 assertEquals(Node.TEXT_NODE, ((Node) content.get(0)).getNodeType());
97 assertEquals(Node.CDATA_SECTION_NODE, ((Node) content.get(1)).getNodeType());
98 assertEquals(Node.TEXT_NODE, ((Node) content.get(2)).getNodeType());
99 }
100
101 }
102
103
104
105
106 /*
107 * Redistribution and use of this software and associated documentation
108 * ("Software"), with or without modification, are permitted provided
109 * that the following conditions are met:
110 *
111 * 1. Redistributions of source code must retain copyright
112 * statements and notices. Redistributions must also contain a
113 * copy of this document.
114 *
115 * 2. Redistributions in binary form must reproduce the
116 * above copyright notice, this list of conditions and the
117 * following disclaimer in the documentation and/or other
118 * materials provided with the distribution.
119 *
120 * 3. The name "DOM4J" must not be used to endorse or promote
121 * products derived from this Software without prior written
122 * permission of MetaStuff, Ltd. For written permission,
123 * please contact dom4j-info@metastuff.com.
124 *
125 * 4. Products derived from this Software may not be called "DOM4J"
126 * nor may "DOM4J" appear in their names without prior written
127 * permission of MetaStuff, Ltd. DOM4J is a registered
128 * trademark of MetaStuff, Ltd.
129 *
130 * 5. Due credit should be given to the DOM4J Project -
131 * http://www.dom4j.org
132 *
133 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
134 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
135 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
136 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
137 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
138 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
139 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
140 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
141 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
142 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
143 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
144 * OF THE POSSIBILITY OF SUCH DAMAGE.
145 *
146 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
147 *
148 * $Id: TestXSLT.java,v
149 */