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: TestSAXReader.java,v 1.11 2004/08/07 18:58:47 maartenc Exp $
8    */
9   
10  package org.dom4j.io;
11  
12  import java.io.ByteArrayOutputStream;
13  import java.io.File;
14  import java.io.FileOutputStream;
15  import java.io.StringWriter;
16  import java.net.URL;
17  import java.util.List;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  import org.dom4j.Document;
24  import org.dom4j.DocumentHelper;
25  import org.dom4j.Element;
26  import org.dom4j.io.XMLWriter;
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.InputSource;
29  
30  /*** A test harness to test the content API in DOM4J
31    *
32    * @author <a href="mailto:maartenc@sourceforge.net">Maarten Coene</a>
33    */
34  public class TestSAXReader extends TestCase {
35  
36      public static void main( String[] args ) {
37          TestRunner.run( suite() );
38      }
39      
40      public static Test suite() {
41          return new TestSuite( TestSAXReader.class );
42      }
43      
44      public TestSAXReader(String name) {
45          super(name);
46      }
47  
48      // Test case(s)
49      //-------------------------------------------------------------------------
50      /***
51       * Test bug reported by Christian Oetterli
52       * http://sourceforge.net/tracker/index.php?func=detail&aid=681658&group_id=16035&atid=116035
53       */
54      public void testReadFile() {
55          try {
56              URL location = TestSAXReader.class.getResource("/xml/#.xml");
57              String fileName = location.getPath();
58              if (fileName.endsWith("%23.xml")) {
59                  // since JDK 1.5 beta2 the path contains the #.xml file as "%23.xml"
60                  fileName = fileName.substring(0, fileName.indexOf("%23.xml"));
61              }
62              
63              if (!fileName.endsWith("#.xml")) {
64                  fileName += "/#.xml";
65              }
66              File file = new File(fileName);
67              new SAXReader().read(file);
68          } catch (Exception e) {
69              e.printStackTrace();
70              fail(e.getMessage());
71          }
72      }
73      
74      public void testRussian() {
75          try {
76              URL location = TestSAXReader.class.getResource("/xml/russArticle.xml");
77              File file = new File(location.toString()); 
78              SAXReader xmlReader = new SAXReader(); 
79              Document doc = xmlReader.read( location ); 
80              Element el = doc.getRootElement();
81              
82              StringWriter writer = new StringWriter();
83              XMLWriter xmlWriter = new XMLWriter(writer);
84              OutputFormat format = OutputFormat.createPrettyPrint();
85              format.setEncoding("koi8-r");
86              xmlWriter.write(doc);
87              System.out.println(writer.toString());
88          } catch (Exception e) {
89              e.printStackTrace();
90              fail(e.getMessage());
91          }
92      }
93      
94      public void testRussian2() {
95          try {
96              URL location = TestSAXReader.class.getResource("/xml/russArticle.xml");
97              File file = new File(location.toString()); 
98              SAXReader xmlReader = new SAXReader();
99              Document doc = xmlReader.read( location );
100             XMLWriter xmlWriter = new XMLWriter( new OutputFormat ( "", false, "koi8-r" ) );
101             ByteArrayOutputStream out = new ByteArrayOutputStream();
102             xmlWriter.setOutputStream(out);
103             xmlWriter.write( doc );
104             xmlWriter.flush();
105             xmlWriter.close();
106             System.out.println(out.toString());
107         } catch (Exception e) {
108             e.printStackTrace();
109             fail(e.getMessage());
110         }
111     }
112     
113     public void testBug833765() {
114         try {
115             URL location = TestSAXReader.class.getResource("/xml/dtd/external.xml");
116             File file = new File(location.getPath()); 
117             SAXReader xmlReader = new SAXReader("org.dom4j.io.aelfred2.SAXDriver");
118             xmlReader.setIncludeExternalDTDDeclarations(true);
119             Document doc = xmlReader.read(file);
120         } catch (Exception e) {
121             e.printStackTrace();
122             fail(e.getMessage());
123         }
124     }
125     
126     public void testBug527062() throws Exception {
127         SAXReader reader = new SAXReader();
128         Document doc = reader.read(TestSAXReader.class.getResource("/xml/test/test.xml"));
129         List l = doc.selectNodes("//broked/junk");
130         for (int i = 0; i < l.size(); i++) {
131             System.out.println("Found node: " + ((Element)l.get(i)).getStringValue());
132         }
133         
134         assertEquals("hi there", ((Element)l.get(0)).getStringValue());
135         assertEquals("hello world", ((Element)l.get(1)).getStringValue());
136     }
137     
138     public void testEscapedComment() throws Exception {
139     	Document doc = DocumentHelper.parseText("<eg>&lt;!-- declarations for &lt;head> &amp; &lt;body> --&gt;</eg>");
140     	Element eg = doc.getRootElement();
141     	System.out.println(doc.asXML());
142     	assertEquals("<!-- declarations for <head> & <body> -->", eg.getText());
143     }
144 
145     
146 }
147 
148 
149 
150 
151 /*
152  * Redistribution and use of this software and associated documentation
153  * ("Software"), with or without modification, are permitted provided
154  * that the following conditions are met:
155  *
156  * 1. Redistributions of source code must retain copyright
157  *    statements and notices.  Redistributions must also contain a
158  *    copy of this document.
159  *
160  * 2. Redistributions in binary form must reproduce the
161  *    above copyright notice, this list of conditions and the
162  *    following disclaimer in the documentation and/or other
163  *    materials provided with the distribution.
164  *
165  * 3. The name "DOM4J" must not be used to endorse or promote
166  *    products derived from this Software without prior written
167  *    permission of MetaStuff, Ltd.  For written permission,
168  *    please contact dom4j-info@metastuff.com.
169  *
170  * 4. Products derived from this Software may not be called "DOM4J"
171  *    nor may "DOM4J" appear in their names without prior written
172  *    permission of MetaStuff, Ltd. DOM4J is a registered
173  *    trademark of MetaStuff, Ltd.
174  *
175  * 5. Due credit should be given to the DOM4J Project - 
176  *    http://www.dom4j.org
177  *
178  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
179  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
180  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
181  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
182  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
183  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
184  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
185  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
186  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
187  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
188  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
189  * OF THE POSSIBILITY OF SUCH DAMAGE.
190  *
191  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
192  *
193  * $Id: TestSAXReader.java,v 1.11 2004/08/07 18:58:47 maartenc Exp $
194  */