1
2
3
4
5
6
7
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
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
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><!-- declarations for <head> & <body> --></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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194