1 21 22 package nu.xom.tests; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.Reader ; 28 import java.io.StringReader ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 34 import nu.xom.Attribute; 35 import nu.xom.Builder; 36 import nu.xom.Comment; 37 import nu.xom.DocType; 38 import nu.xom.Document; 39 import nu.xom.Element; 40 import nu.xom.Nodes; 41 import nu.xom.ParsingException; 42 import nu.xom.ProcessingInstruction; 43 import nu.xom.Text; 44 import nu.xom.XMLException; 45 import nu.xom.converters.DOMConverter; 46 47 import org.w3c.dom.CDATASection ; 48 import org.w3c.dom.DOMImplementation ; 49 import org.w3c.dom.DocumentFragment ; 50 import org.xml.sax.InputSource ; 51 import org.xml.sax.SAXException ; 52 53 54 64 public class DOMConverterTest extends XOMTestCase { 65 66 67 public DOMConverterTest(String name) { 68 super(name); 69 } 70 71 72 private String source = "<!DOCTYPE test [ ]>\r\n" 73 + "<?xml-stylesheet HREF=\"file.css\" type=\"text/css\"?>" 74 + "<!-- test -->" 75 + "<test xmlns:xlink='http://www.w3.org/TR/1999/xlink'>Hello dear" 76 + "\r\n<em id=\"p1\" " 77 + "xmlns:none=\"http://www.example.com\">very important</em>" 78 + "<span xlink:type='simple'>here's the link</span>\r\n" 79 + "<svg:svg xmlns:svg='http://www.w3.org/TR/2000/svg'><svg:text>" 80 + "text in a namespace</svg:text></svg:svg>\r\n" 81 + "<svg xmlns='http://www.w3.org/TR/2000/svg'><text>text in a " 82 + "namespace</text></svg>" 83 + "</test>\r\n" 84 + "<!--epilog-->"; 85 86 87 private Document xomDocument; 88 private org.w3c.dom.Document domDocument; 89 private DOMImplementation impl; 90 private DocumentBuilder builder; 91 92 93 protected void setUp() throws ParserConfigurationException { 94 95 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 96 factory.setNamespaceAware(true); 97 builder = factory.newDocumentBuilder(); 98 impl = builder.getDOMImplementation(); 99 100 DocType type = new DocType("test"); 101 Element root = new Element("test"); 102 xomDocument = new Document(root); 103 xomDocument.insertChild(type, 0); 104 xomDocument.insertChild(new ProcessingInstruction( 105 "xml-stylesheet", "href=\"file.css\" type=\"text/css\""), 1); 106 xomDocument.insertChild(new Comment(" test "), 2); 107 xomDocument.appendChild(new Comment("epilog")); 108 root.addNamespaceDeclaration("xlink", 109 "http://www.w3.org/TR/1999/xlink"); 110 root.appendChild("Hello dear\r\n"); 111 Element em = new Element("em"); 112 root.appendChild(em); 113 em.addAttribute(new Attribute("id", "p1")); 114 em.addNamespaceDeclaration("none", "http://www.example.com"); 115 em.appendChild("very important"); 116 Element span = new Element("span"); 117 root.appendChild(span); 118 span.addAttribute(new Attribute("xlink:type", 119 "http://www.w3.org/TR/1999/xlink", "simple")); 120 span.appendChild("here's the link"); 121 root.appendChild("\r\n"); 122 123 Element empty = new Element("empty"); 124 root.appendChild(empty); 125 empty.addAttribute(new Attribute("xom:temp", 126 "http://xom.nu/", 127 "Just to see if this can handle namespaced attributes")); 128 129 root.appendChild("\r\n"); 130 Element svg = new Element("svg:svg", 131 "http://www.w3.org/TR/2000/svg"); 132 root.appendChild(svg); 133 Element text = new Element("svg:text", "http://www.w3.org/TR/2000/svg"); 134 svg.appendChild(text); 135 text.appendChild("text in a namespace"); 136 root.appendChild("\r\n"); 137 138 svg = new Element("svg", "http://www.w3.org/TR/2000/svg"); 139 root.appendChild(svg); 140 text = new Element("text", "http://www.w3.org/TR/2000/svg"); 141 svg.appendChild(text); 142 text.appendChild("text in a namespace"); 143 144 Reader reader = new StringReader (source); 145 InputSource inso = new InputSource (reader); 146 147 try { 148 domDocument = builder.parse(inso); 149 } 150 catch (Exception ex) { 151 throw new RuntimeException ("Oops!"); 153 } 154 155 } 156 157 158 public void testToDOM() { 159 160 org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocument, impl); 161 org.w3c.dom.DocumentType doctype = domDoc.getDoctype(); 162 163 assertEquals("test", domDoc.getDocumentElement().getNodeName()); 164 assertTrue(doctype != null); 165 assertEquals(org.w3c.dom.Node.DOCUMENT_TYPE_NODE, 166 domDoc.getFirstChild().getNodeType()); 167 assertEquals(org.w3c.dom.Node.COMMENT_NODE, 168 domDoc.getLastChild().getNodeType()); 169 170 } 171 172 173 public void testToXOM() { 174 175 Document xomDoc = DOMConverter.convert(domDocument); 176 DocType doctype = xomDoc.getDocType(); 177 Element root = xomDoc.getRootElement(); 178 179 assertEquals("test", root.getQualifiedName()); 180 assertEquals("test", root.getLocalName()); 181 assertEquals("", root.getNamespaceURI()); 182 183 assertTrue(doctype != null); 184 assertTrue(xomDoc.getChild(0) instanceof DocType); 185 assertTrue(xomDoc.getChild(4) instanceof nu.xom.Comment); 186 assertTrue(xomDoc.getChild(2) instanceof nu.xom.Comment); 187 assertEquals(" test ", xomDoc.getChild(2).getValue()); 188 assertEquals("epilog", xomDoc.getChild(4).getValue()); 189 assertTrue( 190 xomDoc.getChild(1) instanceof nu.xom.ProcessingInstruction); 191 assertEquals("test", doctype.getRootElementName()); 192 assertNull(doctype.getPublicID()); 193 assertNull(doctype.getSystemID()); 194 195 } 196 197 198 public void testDefaultNamespacedElement() 199 throws SAXException , IOException , ParserConfigurationException { 200 201 byte[] data = "<root xmlns=\"http://www.example.com\"/>".getBytes(); 202 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 203 Document xomDoc = DOMConverter.convert(doc); 204 205 Element root = xomDoc.getRootElement(); 206 assertEquals("root", root.getQualifiedName()); 207 assertEquals("http://www.example.com", root.getNamespaceURI()); 208 209 } 210 211 212 public void testPrefixedElement() 213 throws SAXException , IOException , ParserConfigurationException { 214 215 byte[] data = "<pre:root xmlns:pre=\"http://www.example.com\"/>".getBytes(); 216 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 217 Document xomDoc = DOMConverter.convert(doc); 218 219 Element root = xomDoc.getRootElement(); 220 assertEquals("pre:root", root.getQualifiedName()); 221 assertEquals("http://www.example.com", root.getNamespaceURI()); 222 223 } 224 225 226 public void testConvertAttr() 227 throws SAXException , IOException , ParserConfigurationException { 228 229 byte[] data = ("<element name='value' " + 230 "xmlns='http://example.com/' " + 231 "xmlns:pre='http://example.net'/>").getBytes(); 232 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 233 234 org.w3c.dom.Element root = doc.getDocumentElement(); 235 Attribute attribute = DOMConverter.convert(root.getAttributeNode("name")); 236 assertEquals("name", attribute.getQualifiedName()); 237 assertEquals("", attribute.getNamespacePrefix()); 238 assertEquals("", attribute.getNamespaceURI()); 239 assertEquals("value", attribute.getValue()); 240 241 try { 242 DOMConverter.convert(root.getAttributeNode("xmlns")); 243 fail("Converted xmlns attribute"); 244 } 245 catch (XMLException ex) { 246 assertNotNull(ex.getMessage()); 247 } 248 try { 249 DOMConverter.convert(root.getAttributeNode("xmlns:pre")); 250 fail("Converted xmlns:pre attribute"); 251 } 252 catch (XMLException ex) { 253 assertNotNull(ex.getMessage()); 254 } 255 256 } 257 258 259 public void testConvertElement() 260 throws SAXException , IOException , ParserConfigurationException { 261 262 byte[] data = ("<element name='value' " + 263 "xmlns='http://example.com/' " + 264 "xmlns:pre='http://example.net'/>").getBytes(); 265 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 266 267 org.w3c.dom.Element root = doc.getDocumentElement(); 268 Element xomRoot = DOMConverter.convert(root); 269 assertEquals("name", xomRoot.getAttribute("name").getQualifiedName()); 270 assertEquals("", xomRoot.getAttribute("name").getNamespacePrefix()); 271 assertEquals("", xomRoot.getAttribute("name").getNamespaceURI()); 272 assertEquals("value", xomRoot.getAttribute("name").getValue()); 273 assertEquals("element", xomRoot.getQualifiedName()); 274 assertEquals("", xomRoot.getValue()); 275 assertEquals(0, xomRoot.getChildCount()); 276 assertEquals("http://example.com/", xomRoot.getNamespaceURI()); 277 assertEquals("http://example.net", xomRoot.getNamespaceURI("pre")); 278 279 } 280 281 282 public void testConvertComment() 283 throws SAXException , IOException , ParserConfigurationException { 284 285 byte[] data = "<element><!--data--></element>".getBytes(); 286 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 287 288 org.w3c.dom.Element root = doc.getDocumentElement(); 289 org.w3c.dom.Comment comment = (org.w3c.dom.Comment ) (root.getChildNodes().item(0)); 290 Comment xomComment = DOMConverter.convert(comment); 291 assertEquals(comment.getNodeValue(), xomComment.getValue()); 292 293 } 294 295 296 public void testConvertText() 297 throws SAXException , IOException , ParserConfigurationException { 298 299 byte[] data = "<element> here's the text </element>".getBytes(); 300 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 301 302 org.w3c.dom.Element root = doc.getDocumentElement(); 303 org.w3c.dom.Text node = (org.w3c.dom.Text ) (root.getChildNodes().item(0)); 304 Text text = DOMConverter.convert(node); 305 assertEquals(node.getNodeValue(), text.getValue()); 306 307 } 308 309 310 public void testConvertCDATASection() 311 throws SAXException , IOException , ParserConfigurationException { 312 313 byte[] data = "<element><![CDATA[ here's the text ]]></element>".getBytes(); 314 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 315 316 org.w3c.dom.Element root = doc.getDocumentElement(); 317 CDATASection node = (CDATASection ) (root.getChildNodes().item(0)); 318 Text text = DOMConverter.convert(node); 319 assertEquals(node.getNodeValue(), text.getValue()); 320 321 Document xomDoc = DOMConverter.convert(doc); 323 assertEquals(node.getNodeValue(), xomDoc.getValue()); 324 325 } 326 327 328 public void testConvertProcessingInstruction() 329 throws SAXException , IOException , ParserConfigurationException { 330 331 byte[] data = "<element><?target PI data?></element>".getBytes(); 332 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 333 334 org.w3c.dom.Element root = doc.getDocumentElement(); 335 org.w3c.dom.ProcessingInstruction node 336 = (org.w3c.dom.ProcessingInstruction ) (root.getChildNodes().item(0)); 337 ProcessingInstruction pi = DOMConverter.convert(node); 338 assertEquals(node.getNodeValue(), pi.getValue()); 339 assertEquals(node.getTarget(), pi.getTarget()); 340 341 } 342 343 344 public void testConvertDocType() 345 throws SAXException , IOException , ParserConfigurationException { 346 347 byte[] data = "<!DOCTYPE root ><element />".getBytes(); 348 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 349 350 org.w3c.dom.DocumentType type = doc.getDoctype(); 351 DocType xomType = DOMConverter.convert(type); 352 assertEquals(type.getName(), xomType.getRootElementName()); 353 354 } 355 356 357 public void testChildElementAddsNamespace() { 358 359 Element root = new Element("root"); 360 Element child = new Element("pre:child", "http://www.example.org/"); 361 child.addAttribute(new Attribute("xlink:type", "http://www.w3.org/1999/xlink", "simple")); 362 root.appendChild(child); 363 Document doc = new Document(root); 364 365 assertEquals(doc, DOMConverter.convert(DOMConverter.convert(doc, impl))); 366 367 } 368 369 370 public void testChildElementUsesSameNamespace() { 371 372 Element root = new Element("pre:root", "http://www.example.org/"); 373 Element child = new Element("pre:child", "http://www.example.org/"); 374 root.appendChild(child); 375 Document doc = new Document(root); 376 assertEquals(doc, DOMConverter.convert(DOMConverter.convert(doc, impl))); 377 378 } 379 380 381 public void testPrefixMappingChanges() { 382 383 Element root = new Element("pre:root", "http://www.example.org/"); 384 Element child = new Element("pre:child", "http://www.example.net/"); 385 root.appendChild(child); 386 Document doc = new Document(root); 387 assertEquals(doc, DOMConverter.convert(DOMConverter.convert(doc, impl))); 388 389 } 390 391 392 public void testConvertDocumentFragment() { 393 394 DocumentFragment fragment = domDocument.createDocumentFragment(); 395 org.w3c.dom.Element element = domDocument.createElement("name"); 396 element.setAttribute("name", "value"); 397 fragment.appendChild(element); 398 fragment.appendChild(domDocument.createComment("data")); 399 fragment.appendChild(domDocument.createTextNode("text")); 400 401 Nodes result = DOMConverter.convert(fragment); 402 403 Element first = (Element) result.get(0); 404 assertEquals("name", first.getQualifiedName()); 405 assertEquals("name", first.getAttribute("name").getQualifiedName()); 406 assertEquals("value", first.getAttribute("name").getValue()); 407 408 Comment second = (Comment) result.get(1); 409 assertEquals("data", second.getValue()); 410 411 Text third = (Text) result.get(2); 412 assertEquals("text", third.getValue()); 413 414 } 415 416 417 public void testWolfgang() throws ParsingException, IOException { 419 420 String data = "<m>" 421 + "<a code='3.1415292'>" 422 + "<u>http://www.example.com</u>" 423 + "<h>Some data</h>" 424 + "<s>StockAccess</s>" 425 + "</a>" 426 + "</m>"; 427 428 Document doc = new Builder ().build(data, null); 429 Element article = doc.getRootElement().getChildElements().get(0); 430 Document temp = new Document(new Element(article)); 431 432 org.w3c.dom.Element domArticle 433 = DOMConverter.convert(temp, impl).getDocumentElement(); 434 assertEquals(3, domArticle.getChildNodes().getLength()); 435 Element out = DOMConverter.convert(domArticle); 436 assertEquals(article, out); 437 438 } 439 440 441 public void testConvertNullXOMDocument() { 442 443 try { 444 DOMConverter.convert((Document) null, impl); 445 fail("Converted null document"); 446 } 447 catch (NullPointerException success) { 448 } 449 450 451 } 452 453 454 public void testConvertNullDOMElement() { 455 456 try { 457 DOMConverter.convert((org.w3c.dom.Element ) null); 458 fail("Converted null element"); 459 } 460 catch (NullPointerException success) { 461 } 462 463 464 } 465 466 467 public void testConvertNullDOMDocument() { 468 469 try { 470 DOMConverter.convert((org.w3c.dom.Document ) null); 471 fail("Converted null element"); 472 } 473 catch (NullPointerException success) { 474 } 475 476 477 } 478 479 480 483 public void testFullHoschek() throws ParsingException, IOException { 484 485 File f = new File ("data"); 486 f = new File (f, "hoschektest.xml"); 487 Builder builder = new Builder (); 488 Document xomDocIn = builder.build(f); 489 org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl); 490 Document xomDocOut = DOMConverter.convert(domDoc); 491 assertEquals(xomDocIn, xomDocOut); 492 493 } 494 495 496 499 public void testSimplifiedHoschekDOMToXOM() 500 throws SAXException , IOException { 501 502 File f = new File ("data"); 503 f = new File (f, "simplehoschektest.xml"); 504 org.w3c.dom.Document domDocIn = builder.parse(f); 505 int domRootNumber = domDocIn.getDocumentElement().getChildNodes().getLength(); 506 Document xomDoc = DOMConverter.convert(domDocIn); 507 Element xomRoot = xomDoc.getRootElement(); 508 assertEquals(domRootNumber, xomRoot.getChildCount()); 509 510 } 511 512 513 public void testChildNodesRemainChildNodes() 514 throws ParsingException, IOException { 515 516 String data = "<root><scope><a>1</a><b>2</b><c /></scope></root>"; 517 Builder builder = new Builder (); 518 Document xomDocIn = builder.build(data, null); 519 int xomRootNumber = xomDocIn.getRootElement().getChildCount(); 520 org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl); 521 org.w3c.dom.Element domRoot = domDoc.getDocumentElement(); 522 assertEquals(xomRootNumber, domRoot.getChildNodes().getLength()); 523 Document xomDocOut = DOMConverter.convert(domDoc); 524 assertEquals(xomDocIn, xomDocOut); 525 526 } 527 528 529 public void testLastChildNodeIsLeafNode() 530 throws ParsingException, IOException { 531 532 String data = "<root><top><empty />ABCD</top></root>"; 533 Builder builder = new Builder (); 534 Document xomDocIn = builder.build(data, null); 535 org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl); 536 org.w3c.dom.Element domRoot = domDoc.getDocumentElement(); 537 org.w3c.dom.Node domTop = domRoot.getFirstChild(); 538 assertEquals(2, domTop.getChildNodes().getLength()); 539 540 } 541 542 543 public void testNamespaceAttributes() { 544 545 Element root = new Element("root", "http://www.example.org/"); 546 root.appendChild("text"); 547 Document xomDocIn = new Document(root); 548 org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl); 549 org.w3c.dom.Element domRoot = domDoc.getDocumentElement(); 550 assertTrue(domRoot.hasAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns")); 551 552 } 553 554 555 public void testConvertSingleElementDocumentFromXOMToDOM() { 556 557 Element root = new Element("root"); 558 Document xomDocIn = new Document(root); 559 org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl); 560 org.w3c.dom.Element domRoot = domDoc.getDocumentElement(); 561 assertEquals(0, domRoot.getChildNodes().getLength()); 562 assertEquals("root", domRoot.getNodeName()); 563 564 } 565 566 567 public void testConvertSingleElementDocumentFromDOMToXOM() 568 throws SAXException , IOException { 569 570 byte[] data = "<element />".getBytes(); 571 org.w3c.dom.Document doc = builder.parse(new ByteArrayInputStream (data)); 572 Document xomDoc = DOMConverter.convert(doc); 573 Element root = xomDoc.getRootElement(); 574 assertEquals(0, root.getChildCount()); 575 assertEquals("element", root.getLocalName()); 576 577 } 578 579 580 public void testConvertXMLSpaceAttributeFromXOMToDOM() 581 throws SAXException , IOException { 582 583 Element root = new Element("element"); 584 Document doc = new Document(root); 585 root.addAttribute(new Attribute("xml:space", 586 "http://www.w3.org/XML/1998/namespace", "preserve")); 587 org.w3c.dom.Document domDoc = DOMConverter.convert(doc, impl); 588 org.w3c.dom.Element domRoot = domDoc.getDocumentElement(); 589 assertEquals(1, domRoot.getAttributes().getLength()); 590 591 } 592 593 594 public void testConvertXMLPrefixedElementFromXOMToDOM() 595 throws SAXException , IOException { 596 597 Element root = new Element( 598 "xml:element", "http://www.w3.org/XML/1998/namespace"); 599 Document doc = new Document(root); 600 org.w3c.dom.Document domDoc = DOMConverter.convert(doc, impl); 601 org.w3c.dom.Element domRoot = domDoc.getDocumentElement(); 602 assertEquals(0, domRoot.getAttributes().getLength()); 603 604 } 605 606 607 public void testDeepConvert() { 608 609 Element top = new Element("e"); 610 Element parent = top; 611 for (int i = 0; i < 100; i++) { 612 Element child = new Element("e" + i); 613 parent.appendChild(child); 614 parent = child; 615 } 616 Document xomDocIn = new Document(top); 617 org.w3c.dom.Document domDoc = DOMConverter.convert(xomDocIn, impl); 618 Document xomDocOut = DOMConverter.convert(domDoc); 619 620 assertEquals(xomDocIn, xomDocOut); 621 622 } 623 } 624 | Popular Tags |