Lines 49-54
Link Here
|
49 |
import java.io.StringReader; |
49 |
import java.io.StringReader; |
50 |
import java.lang.ref.Reference; |
50 |
import java.lang.ref.Reference; |
51 |
import java.lang.ref.WeakReference; |
51 |
import java.lang.ref.WeakReference; |
|
|
52 |
import java.util.List; |
52 |
import javax.xml.XMLConstants; |
53 |
import javax.xml.XMLConstants; |
53 |
import javax.xml.parsers.DocumentBuilderFactory; |
54 |
import javax.xml.parsers.DocumentBuilderFactory; |
54 |
import javax.xml.transform.stream.StreamSource; |
55 |
import javax.xml.transform.stream.StreamSource; |
Lines 58-63
Link Here
|
58 |
import org.openide.util.test.TestFileUtils; |
59 |
import org.openide.util.test.TestFileUtils; |
59 |
import org.w3c.dom.CDATASection; |
60 |
import org.w3c.dom.CDATASection; |
60 |
import org.w3c.dom.Document; |
61 |
import org.w3c.dom.Document; |
|
|
62 |
import org.w3c.dom.DocumentFragment; |
61 |
import org.w3c.dom.DocumentType; |
63 |
import org.w3c.dom.DocumentType; |
62 |
import org.w3c.dom.Element; |
64 |
import org.w3c.dom.Element; |
63 |
import org.w3c.dom.Node; |
65 |
import org.w3c.dom.Node; |
Lines 497-500
Link Here
|
497 |
assertGC("can collect resolver", resolverRef); |
499 |
assertGC("can collect resolver", resolverRef); |
498 |
} |
500 |
} |
499 |
|
501 |
|
|
|
502 |
public void testAppendChildElement() throws Exception { |
503 |
Document doc = XMLUtil.parse(new InputSource(new StringReader("<root></root>")), false, true, null, null); |
504 |
Element parent = doc.createElementNS("unittest", "parent"); |
505 |
Element newElement1 = doc.createElementNS("unittest", "new_element1"); |
506 |
Element newElement2 = doc.createElementNS("unittest", "new_element2"); |
507 |
|
508 |
XMLUtil.appendChildElement(parent, newElement1, new String[] { "new_element2", "new_element1" }); |
509 |
NodeList children = parent.getChildNodes(); |
510 |
assertEquals(1, children.getLength()); |
511 |
|
512 |
XMLUtil.appendChildElement(parent, newElement2, new String[] { "new_element2", "new_element1" }); |
513 |
|
514 |
children = parent.getChildNodes(); |
515 |
assertEquals(2, children.getLength()); |
516 |
Node firstChild = parent.getChildNodes().item(0); |
517 |
Node secondChild = parent.getChildNodes().item(1); |
518 |
assertEquals("new_element2", firstChild.getNodeName()); |
519 |
assertEquals("new_element1", secondChild.getNodeName()); |
520 |
} |
521 |
|
522 |
public void testFindSubElements() throws Exception { |
523 |
Document doc = XMLUtil.parse(new InputSource(new StringReader("<root> <child1></child1><child2/><!-- comment --><child3/></root>")), |
524 |
false, true, null, null); |
525 |
|
526 |
Element parent = doc.getDocumentElement(); |
527 |
assertEquals(5, parent.getChildNodes().getLength()); |
528 |
List<Element> subElements = XMLUtil.findSubElements(parent); |
529 |
assertEquals(3, subElements.size()); |
530 |
Element firstChild = subElements.get(0); |
531 |
Element secondChild = subElements.get(1); |
532 |
Element thirdChild = subElements.get(2); |
533 |
|
534 |
assertEquals("child1", firstChild.getNodeName()); |
535 |
assertEquals("child2", secondChild.getNodeName()); |
536 |
assertEquals("child3", thirdChild.getNodeName()); |
537 |
|
538 |
Document failureDoc = XMLUtil.parse(new InputSource(new StringReader("<root>Non whitespace<!-- comment --></root>")), |
539 |
false, true, null, null); |
540 |
Element failedParent = failureDoc.getDocumentElement(); |
541 |
try { |
542 |
XMLUtil.findSubElements(failedParent); |
543 |
fail("expected IAE"); |
544 |
} catch (IllegalArgumentException e) { } |
545 |
} |
546 |
|
547 |
public void testFindElement() throws Exception { |
548 |
String xmlDoc = "<root> " + |
549 |
" <h:table xmlns:h=\"http://www.w3.org/TR/html4/\">" + |
550 |
" </h:table>" + |
551 |
" <f:table xmlns:f=\"http://www.w3schools.com/furniture\">" + |
552 |
" </f:table>" + |
553 |
" <dup/><dup/>" + |
554 |
"</root>"; |
555 |
|
556 |
Document doc = XMLUtil.parse(new InputSource(new StringReader(xmlDoc)), false, true, null, null); |
557 |
Element parent = doc.getDocumentElement(); |
558 |
|
559 |
Element noTable1 = XMLUtil.findElement(parent, "table", null); |
560 |
assertNull(noTable1); |
561 |
|
562 |
Element noTable2 = XMLUtil.findElement(parent, "table", "h"); |
563 |
assertNull(noTable2); |
564 |
|
565 |
Element noTable3 = XMLUtil.findElement(parent, "table", "f"); |
566 |
assertNull(noTable3); |
567 |
|
568 |
Element table1 = XMLUtil.findElement(parent, "table", "http://www.w3.org/TR/html4/"); |
569 |
assertNotNull(table1); |
570 |
|
571 |
Element table2 = XMLUtil.findElement(parent, "table", "http://www.w3schools.com/furniture"); |
572 |
assertNotNull(table2); |
573 |
|
574 |
try { |
575 |
XMLUtil.findElement(parent, "dup", null); |
576 |
fail("Expected IAE"); |
577 |
} catch (IllegalArgumentException e) { } |
578 |
} |
579 |
|
580 |
public void testFindText() throws Exception { |
581 |
Document doc = XMLUtil.parse(new InputSource(new StringReader("<root>Text To Find<child></child></root>")), |
582 |
false, true, null, null); |
583 |
|
584 |
Element parent = doc.getDocumentElement(); |
585 |
String foundText = XMLUtil.findText(parent); |
586 |
|
587 |
assertEquals("Text To Find", foundText); |
588 |
|
589 |
String notFoundText = XMLUtil.findText(parent.getFirstChild()); |
590 |
|
591 |
assertNull(notFoundText); |
592 |
} |
593 |
|
594 |
public void testTranslateXML() throws Exception { |
595 |
// don't add any whitespace to the first 3 nodes |
596 |
String xmlDoc = "<root><table bgcolor='red'><tr>" + |
597 |
" <td>Apples</td>" + |
598 |
" <td>Bananas</td>" + |
599 |
" </tr>" + |
600 |
" </table>" + |
601 |
"</root>"; |
602 |
|
603 |
Document doc = XMLUtil.parse(new InputSource(new StringReader(xmlDoc)), false, true, null, null); |
604 |
Element parent = doc.getDocumentElement(); |
605 |
|
606 |
Element translated = XMLUtil.translateXML((Element) parent.getFirstChild(), "http://www.w3.org/TR/html4/"); |
607 |
assertEquals("http://www.w3.org/TR/html4/", translated.getNamespaceURI()); |
608 |
assertEquals(1, translated.getAttributes().getLength()); |
609 |
|
610 |
assertEquals("http://www.w3.org/TR/html4/", translated.getFirstChild().getNamespaceURI()); |
611 |
assertEquals(0, translated.getFirstChild().getAttributes().getLength()); |
612 |
} |
613 |
|
614 |
public void testCopyDocument() throws Exception { |
615 |
String srcXml = "<root><table bgcolor='red'><tr>" + |
616 |
" <td>Apples</td>" + |
617 |
" <td>Bananas</td>" + |
618 |
" </tr>" + |
619 |
" </table>" + |
620 |
"</root>"; |
621 |
|
622 |
Document srcDoc = XMLUtil.parse(new InputSource(new StringReader(srcXml)), false, true, null, null); |
623 |
Element srcRoot = srcDoc.getDocumentElement(); |
624 |
|
625 |
Document dstDoc = XMLUtil.parse(new InputSource(new StringReader("<dst_root/>")), false, true, null, null); |
626 |
|
627 |
Element dstRoot = dstDoc.getDocumentElement(); |
628 |
XMLUtil.copyDocument(dstDoc, srcRoot, dstRoot, "http://www.w3.org/TR/html4/"); |
629 |
// XMLUtil.write(dstDoc, System.out, "UTF-8"); |
630 |
|
631 |
assertEquals(1, dstDoc.getChildNodes().getLength()); |
632 |
Element root = dstDoc.getDocumentElement(); |
633 |
Element firstChild = (Element) root.getFirstChild(); |
634 |
|
635 |
assertEquals("table", firstChild.getNodeName()); |
636 |
assertEquals("http://www.w3.org/TR/html4/", firstChild.getNamespaceURI()); |
637 |
assertEquals(1, firstChild.getAttributes().getLength()); |
638 |
} |
639 |
|
500 |
} |
640 |
} |