Java Code Examples for org.w3c.dom.ProcessingInstruction

The following examples show how to use org.w3c.dom.ProcessingInstruction. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: caja   Source File: NodesTest.java    License: Apache License 2.0 6 votes vote down vote up
public final void testProcessingInstructionInHtml() {
  Document doc = DomParser.makeDocument(null, null);
  ProcessingInstruction pi = doc.createProcessingInstruction(
      "foo", "<script>alert(1)</script>");
  Element el = doc.createElementNS(Namespaces.HTML_NAMESPACE_URI, "div");
  el.appendChild(pi);
  assertEquals(
      "<div><?foo <script>alert(1)</script>?></div>",
      Nodes.render(el, MarkupRenderMode.XML));
  try {
    Nodes.render(el, MarkupRenderMode.HTML);
  } catch (UncheckedUnrenderableException ex) {
    // OK
    return;
  }
  fail("Rendered in html");
}
 
Example 2
Source Project: hottub   Source File: DOMScanner.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void visit( Node n ) throws SAXException {
    setCurrentLocation( n );

    // if a case statement gets too big, it should be made into a separate method.
    switch(n.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        String value = n.getNodeValue();
        receiver.characters( value.toCharArray(), 0, value.length() );
        break;
    case Node.ELEMENT_NODE:
        visit( (Element)n );
        break;
    case Node.ENTITY_REFERENCE_NODE:
        receiver.skippedEntity(n.getNodeName());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ProcessingInstruction pi = (ProcessingInstruction)n;
        receiver.processingInstruction(pi.getTarget(),pi.getData());
        break;
    }
}
 
Example 3
Source Project: dragonwell8_jdk   Source File: XMLUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Method getStrFromNode
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
            currentSibling != null;
            currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return ((Attr) xpathnode).getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return ((ProcessingInstruction) xpathnode).getNodeValue();
    }

    return null;
}
 
Example 4
Source Project: TencentKona-8   Source File: XMLUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Method getStrFromNode
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
            currentSibling != null;
            currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return ((Attr) xpathnode).getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return ((ProcessingInstruction) xpathnode).getNodeValue();
    }

    return null;
}
 
Example 5
Source Project: groovy   Source File: DomToGroovy.java    License: Apache License 2.0 6 votes vote down vote up
protected void print(Node node, Map namespaces, boolean endWithComma) {
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            printElement((Element) node, namespaces, endWithComma);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE :
            printPI((ProcessingInstruction) node, endWithComma);
            break;
        case Node.TEXT_NODE :
            printText((Text) node, endWithComma);
            break;
        case Node.COMMENT_NODE :
            printComment((Comment) node, endWithComma);
            break;
    }
}
 
Example 6
Source Project: TencentKona-8   Source File: DOMScanner.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void visit( Node n ) throws SAXException {
    setCurrentLocation( n );

    // if a case statement gets too big, it should be made into a separate method.
    switch(n.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        String value = n.getNodeValue();
        receiver.characters( value.toCharArray(), 0, value.length() );
        break;
    case Node.ELEMENT_NODE:
        visit( (Element)n );
        break;
    case Node.ENTITY_REFERENCE_NODE:
        receiver.skippedEntity(n.getNodeName());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ProcessingInstruction pi = (ProcessingInstruction)n;
        receiver.processingInstruction(pi.getTarget(),pi.getData());
        break;
    }
}
 
Example 7
Source Project: xmlunit   Source File: test_DifferenceEngine.java    License: Apache License 2.0 6 votes vote down vote up
public void testCompareProcessingInstruction() throws Exception {
    String[] expected = PROC_A;
    String[] actual = PROC_B;
    ProcessingInstruction control = document.createProcessingInstruction(
                                                                         expected[0], expected[1]);
    ProcessingInstruction test = document.createProcessingInstruction(
                                                                      actual[0], actual[1]);

    assertDifferentProcessingInstructions(control, test,
                                          PROCESSING_INSTRUCTION_TARGET);

    ProcessingInstruction control2 = document.createProcessingInstruction(
                                                                          expected[0], expected[1]);
    ProcessingInstruction test2 = document.createProcessingInstruction(
                                                                       expected[0], actual[1]);
    assertDifferentProcessingInstructions(control2, test2,
                                          PROCESSING_INSTRUCTION_DATA);
}
 
Example 8
Source Project: openjdk-8   Source File: DOMScanner.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void visit( Node n ) throws SAXException {
    setCurrentLocation( n );

    // if a case statement gets too big, it should be made into a separate method.
    switch(n.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        String value = n.getNodeValue();
        receiver.characters( value.toCharArray(), 0, value.length() );
        break;
    case Node.ELEMENT_NODE:
        visit( (Element)n );
        break;
    case Node.ENTITY_REFERENCE_NODE:
        receiver.skippedEntity(n.getNodeName());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ProcessingInstruction pi = (ProcessingInstruction)n;
        receiver.processingInstruction(pi.getTarget(),pi.getData());
        break;
    }
}
 
Example 9
Source Project: htmlunit   Source File: HtmlPage.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void checkChildHierarchy(final org.w3c.dom.Node newChild) throws DOMException {
    if (newChild instanceof Element) {
        if (getDocumentElement() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                "The Document may only have a single child Element.");
        }
    }
    else if (newChild instanceof DocumentType) {
        if (getDoctype() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                "The Document may only have a single child DocumentType.");
        }
    }
    else if (!(newChild instanceof Comment || newChild instanceof ProcessingInstruction)) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
            "The Document may not have a child of this type: " + newChild.getNodeType());
    }
    super.checkChildHierarchy(newChild);
}
 
Example 10
Source Project: JsDroidCmd   Source File: XmlNode.java    License: Mozilla Public License 2.0 6 votes vote down vote up
String ecmaValue() {
    //    TODO    See ECMA 357 Section 9.1
    if (isTextType()) {
        return ((org.w3c.dom.Text)dom).getData();
    } else if (isAttributeType()) {
        return ((org.w3c.dom.Attr)dom).getValue();
    } else if (isProcessingInstructionType()) {
        return ((org.w3c.dom.ProcessingInstruction)dom).getData();
    } else if (isCommentType()) {
        return ((org.w3c.dom.Comment)dom).getNodeValue();
    } else if (isElementType()) {
        throw new RuntimeException("Unimplemented ecmaValue() for elements.");
    } else {
        throw new RuntimeException("Unimplemented for node " + dom);
    }
}
 
Example 11
/**
 * is not supported in this release.
 * @param target {@inheritDoc}
 * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
 */
public void writeProcessingInstruction(String target) throws XMLStreamException {
    if(target == null){
        throw new XMLStreamException("Target cannot be null");
    }
    ProcessingInstruction pi = ownerDoc.createProcessingInstruction(target, "");
    currentNode.appendChild(pi);
}
 
Example 12
Source Project: org.hl7.fhir.core   Source File: XhtmlGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private void writeNodePlain(Writer out, Node node, int level) throws FHIRException, DOMException, IOException  {
  if (node.getNodeType() == Node.ELEMENT_NODE)
    writeElementPlain(out, (Element) node, level);
  else if (node.getNodeType() == Node.TEXT_NODE)
    writeTextPlain(out, (Text) node, level);
  else if (node.getNodeType() == Node.COMMENT_NODE)
    writeCommentPlain(out, (Comment) node, level);
  else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)
    writeProcessingInstructionPlain(out, (ProcessingInstruction) node);
  else if (node.getNodeType() != Node.ATTRIBUTE_NODE)
    throw new FHIRException("Unhandled node type");
}
 
Example 13
Source Project: jdk1.8-source-analysis   Source File: SAX2DOM.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * adds processing instruction node to DOM.
 */
public void processingInstruction(String target, String data) {
    appendTextNode();
    final Node last = (Node)_nodeStk.peek();
    ProcessingInstruction pi = _document.createProcessingInstruction(
            target, data);
    if (pi != null){
      if (last == _root && _nextSibling != null)
          last.insertBefore(pi, _nextSibling);
      else
          last.appendChild(pi);

      _lastSibling = pi;
    }
}
 
Example 14
Source Project: openjdk-8-source   Source File: DOMPrinter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 15
Source Project: openjdk-jdk8u-backup   Source File: SAX2DOM.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * adds processing instruction node to DOM.
 */
public void processingInstruction(String target, String data) {
    appendTextNode();
    final Node last = (Node)_nodeStk.peek();
    ProcessingInstruction pi = _document.createProcessingInstruction(
            target, data);
    if (pi != null){
      if (last == _root && _nextSibling != null)
          last.insertBefore(pi, _nextSibling);
      else
          last.appendChild(pi);

      _lastSibling = pi;
    }
}
 
Example 16
Source Project: xmlunit   Source File: DOMDifferenceEngineTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void compareProcessingInstructions() {
    DOMDifferenceEngine d = new DOMDifferenceEngine();
    DiffExpecter ex = new DiffExpecter(ComparisonType.PROCESSING_INSTRUCTION_TARGET);
    d.addDifferenceListener(ex);
    d.setComparisonController(ComparisonControllers.StopWhenDifferent);

    ProcessingInstruction foo1 = doc.createProcessingInstruction("foo", "1");
    ProcessingInstruction bar1 = doc.createProcessingInstruction("bar", "1");
    assertEquals(wrap(ComparisonResult.EQUAL),
                 d.compareNodes(foo1, new XPathContext(),
                                foo1, new XPathContext()));
    assertEquals(wrapAndStop(ComparisonResult.DIFFERENT),
                 d.compareNodes(foo1, new XPathContext(),
                                bar1, new XPathContext()));
    assertEquals(1, ex.invoked);

    d = new DOMDifferenceEngine();
    ex = new DiffExpecter(ComparisonType.PROCESSING_INSTRUCTION_DATA);
    d.addDifferenceListener(ex);
    d.setComparisonController(ComparisonControllers.StopWhenDifferent);
    ProcessingInstruction foo2 = doc.createProcessingInstruction("foo", "2");
    assertEquals(wrap(ComparisonResult.EQUAL),
                 d.compareNodes(foo1, new XPathContext(),
                                foo1, new XPathContext()));
    assertEquals(wrapAndStop(ComparisonResult.DIFFERENT),
                 d.compareNodes(foo1, new XPathContext(),
                                foo2, new XPathContext()));
    assertEquals(1, ex.invoked);
}
 
Example 17
Source Project: TencentKona-8   Source File: DOMPrinter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 18
Source Project: openjdk-jdk9   Source File: Bug6354955.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPINode() {
    try {
        Document xmlDocument = createNewDocument();
        ProcessingInstruction piNode = xmlDocument.createProcessingInstruction("execute", "test");
        String outerXML = getOuterXML(piNode);
        System.out.println("OuterXML of Comment Node is:" + outerXML);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
Example 19
Source Project: openjdk-jdk9   Source File: PITest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    Document document = createDOMWithNS("PITest01.xml");
    ProcessingInstruction pi = document.createProcessingInstruction("PI", "processing");
    assertEquals(pi.getData(), "processing");
    assertEquals(pi.getTarget(), "PI");

    pi.setData("newProcessing");
    assertEquals(pi.getData(), "newProcessing");
}
 
Example 20
Source Project: astor   Source File: XmlNode.java    License: GNU General Public License v2.0 5 votes vote down vote up
final void setLocalName(String localName) {
    if (dom instanceof ProcessingInstruction) {
        setProcessingInstructionName(localName);
    } else {
        String prefix = dom.getPrefix();
        if (prefix == null) prefix = "";
        this.dom = dom.getOwnerDocument().renameNode(dom, dom.getNamespaceURI(), QName.qualify(prefix, localName));
    }
}
 
Example 21
Source Project: openjdk-8   Source File: DOMPrinter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 22
Source Project: jdk8u60   Source File: DOMPrinter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 23
Source Project: jdk8u60   Source File: SAX2DOM.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * adds processing instruction node to DOM.
 */
public void processingInstruction(String target, String data) {
    appendTextNode();
    final Node last = (Node)_nodeStk.peek();
    ProcessingInstruction pi = _document.createProcessingInstruction(
            target, data);
    if (pi != null){
      if (last == _root && _nextSibling != null)
          last.insertBefore(pi, _nextSibling);
      else
          last.appendChild(pi);

      _lastSibling = pi;
    }
}
 
Example 24
/**
 * is not supported in this release.
 * @param target {@inheritDoc}
 * @throws javax.xml.stream.XMLStreamException {@inheritDoc}
 */
public void writeProcessingInstruction(String target) throws XMLStreamException {
    if(target == null){
        throw new XMLStreamException("Target cannot be null");
    }
    ProcessingInstruction pi = ownerDoc.createProcessingInstruction(target, "");
    currentNode.appendChild(pi);
}
 
Example 25
Source Project: cxf   Source File: JAXRSClientServerSpringBookTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetWadlFromWadlLocation() throws Exception {
    String address = "http://localhost:" + PORT + "/the/generated";
    WebClient client = WebClient.create(address + "/bookstore" + "?_wadl&_type=xml");
    Document doc = StaxUtils.read(new InputStreamReader(client.get(InputStream.class), StandardCharsets.UTF_8));
    List<Element> resources = checkWadlResourcesInfo(doc, address, "/schemas/book.xsd", 2);
    assertEquals("", resources.get(0).getAttribute("type"));
    String type = resources.get(1).getAttribute("type");
    String resourceTypeAddress = address + "/bookstoreImportResourceType.wadl#bookstoreType";
    assertEquals(resourceTypeAddress, type);

    checkSchemas(address, "/schemas/book.xsd", "/schemas/chapter.xsd", "include");
    checkSchemas(address, "/schemas/chapter.xsd", null, null);

    // check resource type resource
    checkWadlResourcesType(address, resourceTypeAddress, "/schemas/book.xsd");

    String templateRef = null;
    NodeList nd = doc.getChildNodes();
    for (int i = 0; i < nd.getLength(); i++) {
        Node n = nd.item(i);
        if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            String piData = ((ProcessingInstruction)n).getData();
            int hRefStart = piData.indexOf("href=\"");
            if (hRefStart > 0) {
                int hRefEnd = piData.indexOf("\"", hRefStart + 6);
                templateRef = piData.substring(hRefStart + 6, hRefEnd);
            }
        }
    }
    assertNotNull(templateRef);
    WebClient client2 = WebClient.create(templateRef);
    WebClient.getConfig(client2).getHttpConduit().getClient().setReceiveTimeout(1000000L);
    String template = client2.get(String.class);
    assertNotNull(template);
    assertTrue(template.indexOf("<xsl:stylesheet") != -1);
}
 
Example 26
Source Project: JsDroidCmd   Source File: XmlNode.java    License: Mozilla Public License 2.0 5 votes vote down vote up
final void setLocalName(String localName) {
    if (dom instanceof ProcessingInstruction) {
        setProcessingInstructionName(localName);
    } else {
        String prefix = dom.getPrefix();
        if (prefix == null) prefix = "";
        this.dom = dom.getOwnerDocument().renameNode(dom, dom.getNamespaceURI(), QName.qualify(prefix, localName));
    }
}
 
Example 27
Source Project: JsDroidCmd   Source File: XmlNode.java    License: Mozilla Public License 2.0 5 votes vote down vote up
static Filter PROCESSING_INSTRUCTION(final XMLName name) {
    return new Filter() {
        @Override
        boolean accept(Node node) {
            if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
                ProcessingInstruction pi = (ProcessingInstruction)node;
                return name.matchesLocalName(pi.getTarget());
            }
            return false;
        }
    };
}
 
Example 28
Source Project: caja   Source File: NodesTest.java    License: Apache License 2.0 4 votes vote down vote up
public final void testProcessingInstructions() {
  Document doc = DomParser.makeDocument(null, null);
  ProcessingInstruction pi = doc.createProcessingInstruction("foo", "bar");
  assertEquals("<?foo bar?>", Nodes.render(pi, MarkupRenderMode.XML));
}
 
Example 29
Source Project: xmlunit   Source File: NodesTest.java    License: Apache License 2.0 4 votes vote down vote up
private void handleWsDoesntAlterOriginal(Map.Entry<Document, Node> s) {
    NodeList top = s.getKey().getChildNodes();
    assertEquals(1, top.getLength());
    assertTrue(top.item(0) instanceof Element);
    assertEquals("root", top.item(0).getNodeName());
    NodeList rootsChildren = top.item(0).getChildNodes();
    assertEquals(10, rootsChildren.getLength());
    assertNewlineTextNode(rootsChildren.item(0));
    assertTrue("should be comment, is " + rootsChildren.item(1).getClass(),
               rootsChildren.item(1) instanceof Comment);
    assertEquals(" trim\tme ", ((Comment) rootsChildren.item(1)).getData());
    assertNewlineTextNode(rootsChildren.item(2));
    assertTrue("should be element, is " + rootsChildren.item(3).getClass(),
               rootsChildren.item(3) instanceof Element);
    assertEquals("child", rootsChildren.item(3).getNodeName());
    assertTrue("should be cdata, is " + rootsChildren.item(4).getClass(),
               rootsChildren.item(4) instanceof CDATASection);
    assertEquals(" trim me ",
                 ((CDATASection) rootsChildren.item(4)).getData());
    assertNewlineTextNode(rootsChildren.item(5));
    assertTrue("should be PI, is " + rootsChildren.item(6).getClass(),
               rootsChildren.item(6) instanceof ProcessingInstruction);
    assertEquals("trim me ",
                 ((ProcessingInstruction) rootsChildren.item(6)).getData());
    assertNewlineTextNode(rootsChildren.item(7));
    assertTrue("should be cdata, is " + rootsChildren.item(8).getClass(),
               rootsChildren.item(8) instanceof CDATASection);
    assertEquals("          ",
                 ((CDATASection) rootsChildren.item(8)).getData());
    assertNewlineTextNode(rootsChildren.item(9));
    Node child = rootsChildren.item(3);
    NodeList grandChildren = child.getChildNodes();
    assertEquals(1, grandChildren.getLength());
    assertTrue("should be text, is " + grandChildren.item(0).getClass(),
               grandChildren.item(0) instanceof Text);
    assertEquals("\n trim me \n", ((Text) grandChildren.item(0)).getData());
    NamedNodeMap attrs = child.getAttributes();
    assertEquals(2, attrs.getLength());
    Attr a = (Attr) attrs.getNamedItem("attr");
    assertEquals(" trim me ", a.getValue());
    Attr a2 = (Attr) attrs.getNamedItem("attr2");
    assertEquals("not me", a2.getValue());
}
 
Example 30
Source Project: hottub   Source File: CanonicalizerPhysical.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void outputPItoWriter(ProcessingInstruction currentPI,
                                OutputStream writer, int position) throws IOException {
    // Processing Instructions before or after the document element are not treated specially
    super.outputPItoWriter(currentPI, writer, NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT);
}