|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| package nu.xom.tests; |
|
24 |
| |
|
25 |
| import java.io.File; |
|
26 |
| import java.io.IOException; |
|
27 |
| import java.io.StringWriter; |
|
28 |
| |
|
29 |
| import nu.xom.Attribute; |
|
30 |
| import nu.xom.Builder; |
|
31 |
| import nu.xom.Comment; |
|
32 |
| import nu.xom.DocType; |
|
33 |
| import nu.xom.Document; |
|
34 |
| import nu.xom.Element; |
|
35 |
| import nu.xom.ParsingException; |
|
36 |
| import nu.xom.ProcessingInstruction; |
|
37 |
| import nu.xom.converters.SAXConverter; |
|
38 |
| |
|
39 |
| import org.xml.sax.Attributes; |
|
40 |
| import org.xml.sax.ContentHandler; |
|
41 |
| import org.xml.sax.Locator; |
|
42 |
| import org.xml.sax.SAXException; |
|
43 |
| import org.xml.sax.ext.LexicalHandler; |
|
44 |
| import org.xml.sax.helpers.DefaultHandler; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class SAXConverterTest extends XOMTestCase { |
|
57 |
| |
|
58 |
| |
|
59 |
25
| public SAXConverterTest(String name) {
|
|
60 |
25
| super(name);
|
|
61 |
| } |
|
62 |
| |
|
63 |
| |
|
64 |
| private DefaultHandler handler; |
|
65 |
| private SAXConverter converter; |
|
66 |
| private Builder builder = new Builder(); |
|
67 |
| |
|
68 |
| |
|
69 |
25
| protected void setUp() {
|
|
70 |
25
| handler = new DefaultHandler();
|
|
71 |
25
| converter = new SAXConverter(handler);
|
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
1
| public void testGetContentHandler() {
|
|
76 |
1
| assertEquals(handler, converter.getContentHandler());
|
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
1
| public void testSetContentHandler() {
|
|
81 |
| |
|
82 |
1
| handler = new DefaultHandler();
|
|
83 |
1
| converter.setContentHandler(handler);
|
|
84 |
1
| assertEquals(handler, converter.getContentHandler());
|
|
85 |
| |
|
86 |
1
| try {
|
|
87 |
1
| converter.setContentHandler(null);
|
|
88 |
0
| fail("Allowed null ContentHandler");
|
|
89 |
| } |
|
90 |
| catch (NullPointerException success) { |
|
91 |
| |
|
92 |
| } |
|
93 |
| |
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
1
| public void testSetAndGetLexicalHandler() {
|
|
98 |
| |
|
99 |
1
| LexicalHandler handler = new XMLWriter();
|
|
100 |
1
| converter.setLexicalHandler(handler);
|
|
101 |
1
| assertEquals(handler, converter.getLexicalHandler());
|
|
102 |
| |
|
103 |
1
| converter.setLexicalHandler(null);
|
|
104 |
1
| assertNull(converter.getLexicalHandler());
|
|
105 |
| |
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
15
| private void convertAndCompare(Document doc)
|
|
110 |
| throws IOException, SAXException, ParsingException { |
|
111 |
| |
|
112 |
15
| StringWriter result = new StringWriter();
|
|
113 |
15
| XMLWriter handler = new XMLWriter(result);
|
|
114 |
15
| converter.setContentHandler(handler);
|
|
115 |
15
| converter.setLexicalHandler(handler);
|
|
116 |
15
| converter.convert(doc);
|
|
117 |
15
| result.flush();
|
|
118 |
15
| result.close();
|
|
119 |
15
| String convertedDoc = result.toString();
|
|
120 |
15
| Document rebuiltDoc = builder.build(convertedDoc, doc.getBaseURI());
|
|
121 |
15
| assertEquals(doc, rebuiltDoc);
|
|
122 |
| |
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
1
| public void testSimplestDoc()
|
|
127 |
| throws IOException, SAXException, ParsingException { |
|
128 |
1
| Document doc = new Document(new Element("a"));
|
|
129 |
1
| convertAndCompare(doc);
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
1
| public void testXMLBaseAttributesAreThrownAway()
|
|
134 |
| throws SAXException { |
|
135 |
| |
|
136 |
1
| Element root = new Element("root");
|
|
137 |
1
| Element child = new Element("child");
|
|
138 |
1
| Attribute base = new Attribute("xml:base",
|
|
139 |
| "http://www.w3.org/XML/1998/namespace", "data"); |
|
140 |
1
| child.addAttribute(base);
|
|
141 |
1
| root.appendChild(child);
|
|
142 |
1
| Document doc = new Document(root);
|
|
143 |
1
| doc.setBaseURI("http://www.example.com/");
|
|
144 |
1
| converter.setContentHandler(new BaseChecker());
|
|
145 |
1
| converter.convert(doc);
|
|
146 |
| |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| private static class BaseChecker extends DefaultHandler { |
|
151 |
| |
|
152 |
| private Locator locator; |
|
153 |
| |
|
154 |
1
| public void setDocumentLocator(Locator locator) {
|
|
155 |
1
| this.locator = locator;
|
|
156 |
| } |
|
157 |
| |
|
158 |
2
| public void startElement(String localName, String qualifiedName,
|
|
159 |
| String namespaceURI, Attributes attributes) |
|
160 |
| throws SAXException { |
|
161 |
| |
|
162 |
2
| if (localName.equals("root")) {
|
|
163 |
0
| assertEquals("http://www.example.com/", locator.getSystemId());
|
|
164 |
| } |
|
165 |
2
| else if (localName.equals("child")) {
|
|
166 |
0
| assertEquals("http://www.example.com/data", locator.getSystemId());
|
|
167 |
| } |
|
168 |
| |
|
169 |
2
| for (int i=0; i < attributes.getLength(); i++) {
|
|
170 |
0
| String name = attributes.getLocalName(i);
|
|
171 |
0
| String uri = attributes.getURI(i);
|
|
172 |
0
| if ("base".equals(name)
|
|
173 |
| && "http://www.w3.org/XML/1998/namespace".equals(uri)) { |
|
174 |
0
| fail("Passed xml:base attribute into SAXConverter");
|
|
175 |
| } |
|
176 |
| } |
|
177 |
| |
|
178 |
| } |
|
179 |
| |
|
180 |
| } |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
1
| public void testDocType()
|
|
185 |
| throws IOException, SAXException, ParsingException { |
|
186 |
1
| Document doc = new Document(new Element("a"));
|
|
187 |
1
| doc.setDocType(new DocType("root"));
|
|
188 |
1
| convertAndCompare(doc);
|
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
1
| public void testProcessingInstruction()
|
|
193 |
| throws IOException, SAXException, ParsingException { |
|
194 |
| |
|
195 |
1
| Document doc = new Document(new Element("a"));
|
|
196 |
1
| doc.insertChild(new ProcessingInstruction(
|
|
197 |
| "xml-stylesheet", "type=\"application/xml\" href=\"stylesheet.xsl\""), 0); |
|
198 |
1
| convertAndCompare(doc);
|
|
199 |
| |
|
200 |
| } |
|
201 |
| |
|
202 |
| |
|
203 |
1
| public void testComment()
|
|
204 |
| throws IOException, SAXException, ParsingException { |
|
205 |
| |
|
206 |
1
| Element root = new Element("root");
|
|
207 |
1
| root.appendChild(" Lots of random text\n\n\n ");
|
|
208 |
1
| Document doc = new Document(root);
|
|
209 |
1
| doc.insertChild(new Comment("some comment data"), 0);
|
|
210 |
1
| root.insertChild(new Comment("some comment data"), 0);
|
|
211 |
1
| doc.appendChild(new Comment("some comment data"));
|
|
212 |
1
| convertAndCompare(doc);
|
|
213 |
| |
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
1
| public void testDefaultNamespace()
|
|
218 |
| throws IOException, SAXException, ParsingException { |
|
219 |
1
| Document doc = new Document(new Element("a", "http://www.a.com/"));
|
|
220 |
1
| convertAndCompare(doc);
|
|
221 |
| } |
|
222 |
| |
|
223 |
| |
|
224 |
1
| public void testTextContent()
|
|
225 |
| throws IOException, SAXException, ParsingException { |
|
226 |
1
| Element root = new Element("root");
|
|
227 |
1
| root.appendChild(" Lots of random text\n\n\n ");
|
|
228 |
1
| Document doc = new Document(root);
|
|
229 |
1
| convertAndCompare(doc);
|
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
1
| public void testPrefixedNamespace()
|
|
234 |
| throws IOException, SAXException, ParsingException { |
|
235 |
1
| Document doc = new Document(new Element("a:a", "http://www.a.com/"));
|
|
236 |
1
| convertAndCompare(doc);
|
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
1
| public void testAdditionalNamespace()
|
|
241 |
| throws IOException, SAXException, ParsingException { |
|
242 |
| |
|
243 |
1
| Element root = new Element("root");
|
|
244 |
1
| root.addNamespaceDeclaration("xsl", "http://www.w3.org/1999/XSL/Transform");
|
|
245 |
1
| Document doc = new Document(root);
|
|
246 |
1
| convertAndCompare(doc);
|
|
247 |
| |
|
248 |
| } |
|
249 |
| |
|
250 |
| |
|
251 |
1
| public void testPrefixAndAdditionalNamespace()
|
|
252 |
| throws IOException, SAXException, ParsingException { |
|
253 |
| |
|
254 |
1
| Element root = new Element("xsl:root", "http://www.w3.org/1999/XSL/Transform");
|
|
255 |
1
| root.addNamespaceDeclaration("xsl", "http://www.w3.org/1999/XSL/Transform");
|
|
256 |
1
| Document doc = new Document(root);
|
|
257 |
1
| convertAndCompare(doc);
|
|
258 |
| |
|
259 |
| } |
|
260 |
| |
|
261 |
| |
|
262 |
1
| public void testPrefixAndAdditionalNamespaceFromParser()
|
|
263 |
| throws IOException, SAXException, ParsingException { |
|
264 |
1
| Document doc = builder.build(
|
|
265 |
| "<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'/>", |
|
266 |
| null); |
|
267 |
1
| convertAndCompare(doc);
|
|
268 |
| } |
|
269 |
| |
|
270 |
| |
|
271 |
1
| public void testChildElementAddsNamespace()
|
|
272 |
| throws IOException, SAXException, ParsingException { |
|
273 |
| |
|
274 |
1
| Element root = new Element("root");
|
|
275 |
1
| Element child = new Element("pre:child", "http://www.example.org/");
|
|
276 |
1
| child.addAttribute(new Attribute("xlink:type", "http://www.w3.org/1999/xlink", "simple"));
|
|
277 |
1
| root.appendChild(child);
|
|
278 |
1
| Document doc = new Document(root);
|
|
279 |
1
| convertAndCompare(doc);
|
|
280 |
| |
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
1
| public void testAttributesTypes()
|
|
285 |
| throws IOException, SAXException, ParsingException { |
|
286 |
| |
|
287 |
1
| Element root = new Element("root");
|
|
288 |
1
| root.addAttribute(new Attribute("CDATA", "CDATA", Attribute.Type.CDATA));
|
|
289 |
1
| root.addAttribute(new Attribute("ID", "ID", Attribute.Type.ID));
|
|
290 |
1
| root.addAttribute(new Attribute("IDREF", "IDREF", Attribute.Type.IDREF));
|
|
291 |
1
| root.addAttribute(new Attribute("IDRES", "IDREFS", Attribute.Type.IDREFS));
|
|
292 |
1
| root.addAttribute(new Attribute("NMTOKEN", "NMTOKEN", Attribute.Type.NMTOKEN));
|
|
293 |
1
| root.addAttribute(new Attribute("NMTOKENS", "NMTOKENS", Attribute.Type.NMTOKENS));
|
|
294 |
1
| root.addAttribute(new Attribute("UNDECLARED", "UNDECLARED", Attribute.Type.UNDECLARED));
|
|
295 |
1
| root.addAttribute(new Attribute("ENTITY", "ENTITY", Attribute.Type.ENTITY));
|
|
296 |
1
| root.addAttribute(new Attribute("ENTITIES", "ENTITIES", Attribute.Type.ENTITIES));
|
|
297 |
1
| root.addAttribute(new Attribute("NOTATION", "NOTATION", Attribute.Type.NOTATION));
|
|
298 |
1
| root.addAttribute(new Attribute("ENUMERATION", "ENUMERATION", Attribute.Type.ENUMERATION));
|
|
299 |
1
| Document doc = new Document(root);
|
|
300 |
1
| convertAndCompare(doc);
|
|
301 |
| |
|
302 |
| } |
|
303 |
| |
|
304 |
| |
|
305 |
1
| public void testAttributes()
|
|
306 |
| throws IOException, SAXException, ParsingException { |
|
307 |
| |
|
308 |
1
| Element root = new Element("root");
|
|
309 |
1
| root.addAttribute(new Attribute("a", "test"));
|
|
310 |
1
| root.addAttribute(new Attribute("xlink:type",
|
|
311 |
| "http://www.w3.org/1999/xlink", "simple")); |
|
312 |
1
| Document doc = new Document(root);
|
|
313 |
1
| convertAndCompare(doc);
|
|
314 |
| |
|
315 |
| } |
|
316 |
| |
|
317 |
| |
|
318 |
1
| public void testExternalDTDSubset()
|
|
319 |
| throws IOException, SAXException, ParsingException { |
|
320 |
| |
|
321 |
1
| File input = new File("data");
|
|
322 |
1
| input = new File(input, "externalDTDtest.xml");
|
|
323 |
1
| Document doc = builder.build(input);
|
|
324 |
1
| convertAndCompare(doc);
|
|
325 |
| |
|
326 |
| } |
|
327 |
| |
|
328 |
| |
|
329 |
1
| public void testBigDoc()
|
|
330 |
| throws IOException, SAXException, ParsingException { |
|
331 |
1
| Document doc = builder.build("http://www.cafeconleche.org/");
|
|
332 |
1
| convertAndCompare(doc);
|
|
333 |
| } |
|
334 |
| |
|
335 |
| |
|
336 |
1
| public void testNoPrefixMappingEventsForDefaultEmptyNamespace()
|
|
337 |
| throws ParsingException, IOException, SAXException { |
|
338 |
| |
|
339 |
1
| String data = "<root/>";
|
|
340 |
1
| Document doc = builder.build(data, null);
|
|
341 |
1
| ContentHandler handler = new XMLPrefixTester2();
|
|
342 |
1
| SAXConverter converter = new SAXConverter(handler);
|
|
343 |
1
| converter.convert(doc);
|
|
344 |
| |
|
345 |
| } |
|
346 |
| |
|
347 |
| |
|
348 |
1
| public void testNoPrefixMappingEventsForXMLPrefix()
|
|
349 |
| throws ParsingException, IOException, SAXException { |
|
350 |
| |
|
351 |
1
| String data = "<root xml:space='preserve'/>";
|
|
352 |
1
| Document doc = builder.build(data, null);
|
|
353 |
1
| ContentHandler handler = new XMLPrefixTester();
|
|
354 |
1
| SAXConverter converter = new SAXConverter(handler);
|
|
355 |
1
| converter.convert(doc);
|
|
356 |
| |
|
357 |
| } |
|
358 |
| |
|
359 |
| |
|
360 |
1
| public void testNoPrefixMappingEventsForXMLPrefixOnElement()
|
|
361 |
| throws ParsingException, IOException, SAXException { |
|
362 |
| |
|
363 |
1
| String data = "<xml:root/>";
|
|
364 |
1
| Document doc = builder.build(data, null);
|
|
365 |
1
| ContentHandler handler = new XMLPrefixTester();
|
|
366 |
1
| SAXConverter converter = new SAXConverter(handler);
|
|
367 |
1
| converter.convert(doc);
|
|
368 |
| |
|
369 |
| } |
|
370 |
| |
|
371 |
| |
|
372 |
| private static class XMLPrefixTester extends DefaultHandler { |
|
373 |
| |
|
374 |
0
| public void startPrefixMapping(String prefix, String uri) {
|
|
375 |
0
| if ("xml".equals(prefix)) {
|
|
376 |
0
| fail("start mapped prefix xml");
|
|
377 |
| } |
|
378 |
| } |
|
379 |
| |
|
380 |
0
| public void endPrefixMapping(String prefix) {
|
|
381 |
0
| if ("xml".equals(prefix)) {
|
|
382 |
0
| fail("end mapped prefix xml");
|
|
383 |
| } |
|
384 |
| } |
|
385 |
| |
|
386 |
| } |
|
387 |
| |
|
388 |
| |
|
389 |
| private static class XMLPrefixTester2 extends DefaultHandler { |
|
390 |
| |
|
391 |
0
| public void startPrefixMapping(String prefix, String uri) {
|
|
392 |
0
| fail("start mapped prefix " + prefix);
|
|
393 |
| } |
|
394 |
| |
|
395 |
0
| public void endPrefixMapping(String prefix) {
|
|
396 |
0
| fail("end mapped prefix " + prefix);
|
|
397 |
| } |
|
398 |
| |
|
399 |
| } |
|
400 |
| |
|
401 |
| |
|
402 |
1
| public void testNoRedundantPrefixMappingEventsForDefaultNamespace()
|
|
403 |
| throws ParsingException, IOException, SAXException { |
|
404 |
| |
|
405 |
1
| String data = "<root xmlns='http://www.example.org'> <a> <b/> </a> </root>";
|
|
406 |
1
| Document doc = builder.build(data, null);
|
|
407 |
1
| XMLPrefixMapCounter handler = new XMLPrefixMapCounter();
|
|
408 |
1
| SAXConverter converter = new SAXConverter(handler);
|
|
409 |
1
| converter.convert(doc);
|
|
410 |
1
| assertEquals(1, handler.getStarts());
|
|
411 |
1
| assertEquals(1, handler.getEnds());
|
|
412 |
| |
|
413 |
| } |
|
414 |
| |
|
415 |
| |
|
416 |
1
| public void testNoRedundantPrefixMappingEventsForPrefixedNamespace()
|
|
417 |
| throws ParsingException, IOException, SAXException { |
|
418 |
| |
|
419 |
1
| String data = "<a:root xmlns:a='http://www.example.org' />";
|
|
420 |
1
| Document doc = builder.build(data, null);
|
|
421 |
1
| XMLPrefixMapCounter handler = new XMLPrefixMapCounter();
|
|
422 |
1
| SAXConverter converter = new SAXConverter(handler);
|
|
423 |
1
| converter.convert(doc);
|
|
424 |
1
| assertEquals(1, handler.getStarts());
|
|
425 |
1
| assertEquals(1, handler.getEnds());
|
|
426 |
| |
|
427 |
| } |
|
428 |
| |
|
429 |
| |
|
430 |
1
| public void testChildNamespace()
|
|
431 |
| throws ParsingException, IOException, SAXException { |
|
432 |
| |
|
433 |
1
| String data = "<a><pre:b xmlns:pre='http://www.example.com'/></a>";
|
|
434 |
1
| Document doc = builder.build(data, null);
|
|
435 |
1
| XMLPrefixMapCounter handler = new XMLPrefixMapCounter();
|
|
436 |
1
| SAXConverter converter = new SAXConverter(handler);
|
|
437 |
1
| converter.convert(doc);
|
|
438 |
1
| assertEquals(1, handler.getStarts());
|
|
439 |
1
| assertEquals(1, handler.getEnds());
|
|
440 |
| |
|
441 |
| } |
|
442 |
| |
|
443 |
| |
|
444 |
| private static class XMLPrefixMapCounter extends DefaultHandler { |
|
445 |
| |
|
446 |
| private int starts = 0; |
|
447 |
| private int ends = 0; |
|
448 |
| |
|
449 |
3
| public void startPrefixMapping(String prefix, String uri) {
|
|
450 |
3
| starts++;
|
|
451 |
| } |
|
452 |
| |
|
453 |
3
| public void endPrefixMapping(String prefix) {
|
|
454 |
3
| ends++;
|
|
455 |
| } |
|
456 |
| |
|
457 |
3
| int getStarts() {
|
|
458 |
3
| return starts;
|
|
459 |
| } |
|
460 |
| |
|
461 |
3
| int getEnds() {
|
|
462 |
3
| return ends;
|
|
463 |
| } |
|
464 |
| |
|
465 |
| } |
|
466 |
| |
|
467 |
| |
|
468 |
| } |