Java Code Examples for org.w3c.dom.Document#createProcessingInstruction()

The following examples show how to use org.w3c.dom.Document#createProcessingInstruction() . 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   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
protected Document initRepository ( final String processingType, final String type )
{
    final Document doc = this.xml.create ();

    {
        final ProcessingInstruction pi = doc.createProcessingInstruction ( processingType, "version=\"1.1.0\"" );
        doc.appendChild ( pi );
    }

    final Element root = doc.createElement ( "repository" );
    doc.appendChild ( root );
    root.setAttribute ( "name", this.title );
    root.setAttribute ( "type", type );
    root.setAttribute ( "version", "1" );

    return doc;
}
 
Example 3
private Document createContent ( final File targetDir, final String type ) throws Exception
{
    final Document doc = createDocument ();

    final ProcessingInstruction pi = doc.createProcessingInstruction ( type, "" );
    pi.setData ( "version=\"1.0.0\"" );
    doc.appendChild ( pi );

    final Element rep = doc.createElement ( "repository" );
    doc.appendChild ( rep );

    final String typeUpper = Character.toUpperCase ( type.charAt ( 0 ) ) + type.substring ( 1 );

    rep.setAttribute ( "name", this.repositoryName );
    rep.setAttribute ( "type", "org.eclipse.equinox.internal.p2.metadata.repository." + typeUpper );
    rep.setAttribute ( "version", "1.0.0" );

    final Element props = doc.createElement ( "properties" );
    rep.appendChild ( props );
    props.setAttribute ( "size", "" + this.properties.size () );

    for ( final Map.Entry<String, String> entry : this.properties.entrySet () )
    {
        addProperty ( props, entry.getKey (), entry.getValue () );
    }

    final Element children = doc.createElement ( "children" );
    rep.appendChild ( children );
    children.setAttribute ( "size", "" + this.dirs.size () );

    for ( final File file : this.dirs )
    {
        final String ref = targetDir.toPath ().relativize ( file.toPath () ).toString ();
        final Element c = doc.createElement ( "child" );
        children.appendChild ( c );
        c.setAttribute ( "location", ref );
    }

    return doc;
}
 
Example 4
@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 5
Source Project: xmlunit   File: test_XpathNodeTracker.java    License: Apache License 2.0 5 votes vote down vote up
public void testNodes() throws Exception {
    Document doc = XMLUnit.newControlParser().newDocument();
    Element element = doc.createElementNS("http://example.com/xmlunit", "eg:root");
    xpathNodeTracker.visited(element);
    assertEquals("root element", "/root[1]", xpathNodeTracker.toXpathString());
            
    Attr attr = doc.createAttributeNS("http://example.com/xmlunit", "eg:type");
    attr.setValue("qwerty");
    element.setAttributeNodeNS(attr);
    xpathNodeTracker.visited(attr);
    assertEquals("root element attribute", "/root[1]/@type", xpathNodeTracker.toXpathString());             
            
    xpathNodeTracker.indent();
            
    Comment comment = doc.createComment("testing a comment");
    xpathNodeTracker.visited(comment);
    assertEquals("comment", "/root[1]/comment()[1]", xpathNodeTracker.toXpathString());

    ProcessingInstruction pi = doc.createProcessingInstruction("target","data");
    xpathNodeTracker.visited(pi);
    assertEquals("p-i", "/root[1]/processing-instruction()[1]", xpathNodeTracker.toXpathString());

    Text text = doc.createTextNode("some text");
    xpathNodeTracker.visited(text);
    assertEquals("text", "/root[1]/text()[1]", xpathNodeTracker.toXpathString());

    CDATASection cdata = doc.createCDATASection("some characters");
    xpathNodeTracker.visited(cdata);
    assertEquals("cdata", "/root[1]/text()[2]", xpathNodeTracker.toXpathString());
}
 
Example 6
Source Project: jdk1.8-source-analysis   File: DOMUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 7
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 8
Source Project: jdk8u60   File: DOMUtil.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 9
Source Project: Pydev   File: PyUnitTestRun.java    License: Eclipse Public License 1.0 4 votes vote down vote up
public String toXML() {
    try {
        DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
        icFactory.setFeature("http://xml.org/sax/features/namespaces", false);
        icFactory.setFeature("http://xml.org/sax/features/validation", false);
        icFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        icFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        DocumentBuilder icBuilder = icFactory.newDocumentBuilder();
        Document document = icBuilder.newDocument();
        ProcessingInstruction version = document.createProcessingInstruction("pydev-testrun", "version=\"1.0\""); //$NON-NLS-1$ //$NON-NLS-2$
        document.appendChild(version);

        PyUnitTestRun pyUnitTestRun = this;

        Element root = document.createElement("pydev-testsuite");
        document.appendChild(root);

        Element summary = document.createElement("summary");
        summary.setAttribute("name", name);
        summary.setAttribute("errors", String.valueOf(pyUnitTestRun.getNumberOfErrors()));
        summary.setAttribute("failures", String.valueOf(pyUnitTestRun.getNumberOfFailures()));
        summary.setAttribute("tests", String.valueOf(pyUnitTestRun.getTotalNumberOfRuns()));
        summary.setAttribute("finished", String.valueOf(pyUnitTestRun.getFinished()));
        summary.setAttribute("total_time", String.valueOf(pyUnitTestRun.getTotalTime()));
        root.appendChild(summary);

        for (PyUnitTestResult pyUnitTestResult : pyUnitTestRun.getResults()) {
            Element test = document.createElement("test");
            test.setAttribute("status", pyUnitTestResult.status);
            test.setAttribute("location", pyUnitTestResult.location);
            test.setAttribute("test", pyUnitTestResult.test);
            test.setAttribute("time", pyUnitTestResult.time);

            Element stdout = document.createElement("stdout");
            test.appendChild(stdout);
            stdout.appendChild(document.createCDATASection(pyUnitTestResult.capturedOutput));

            Element stderr = document.createElement("stderr");
            test.appendChild(stderr);
            stderr.appendChild(document.createCDATASection(pyUnitTestResult.errorContents));
            root.appendChild(test);
        }

        Element launchElement = document.createElement("launch");
        root.appendChild(launchElement);
        if (pyUnitLaunch != null) {
            pyUnitLaunch.fillXMLElement(document, launchElement);
        }

        ByteArrayOutputStream s = new ByteArrayOutputStream();

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

        DOMSource source = new DOMSource(document);
        StreamResult outputTarget = new StreamResult(s);
        transformer.transform(source, outputTarget);
        return new String(s.toByteArray(), StandardCharsets.UTF_8);
    } catch (Exception e) {
        Log.log(e);
    }
    return "";

}
 
Example 10
Source Project: icafe   File: XMLUtils.java    License: Eclipse Public License 1.0 4 votes vote down vote up
public static void insertTrailingPI(Document doc, String target, String data) {
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    doc.appendChild(pi);
}
 
Example 11
Source Project: Bytecoder   File: DOMUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 12
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 13
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the doc contains two subsequent processing
 * instrictions, <br>
 * <b>name</b>: canonical-form <br>
 * <b>value</b>: true. <br>
 * <b>Expected results</b>: the subsequent processing instrictions are
 * separated with a single line break
 */
@Test
public void testCanonicalForm001() {
    DOMImplementation domImpl = null;
    try {
        domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        Assert.fail(pce.toString());
    } catch (FactoryConfigurationError fce) {
        Assert.fail(fce.toString());
    }

    Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);

    DOMConfiguration config = doc.getDomConfig();

    Element root = doc.getDocumentElement();
    ProcessingInstruction pi1 = doc.createProcessingInstruction("target1", "data1");
    ProcessingInstruction pi2 = doc.createProcessingInstruction("target2", "data2");

    root.appendChild(pi1);
    root.appendChild(pi2);

    if (!config.canSetParameter("canonical-form", Boolean.TRUE)) {
        System.out.println("OK, setting 'canonical-form' to true is not supported");
        return;
    }

    config.setParameter("canonical-form", Boolean.TRUE);
    setHandler(doc);
    doc.normalizeDocument();

    Node child1 = root.getFirstChild();
    Node child2 = child1.getNextSibling();

    if (child2.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        Assert.fail("the second child is expected to be a" + "single line break, returned: " + child2);
    }

    // return Status.passed("OK");
}
 
Example 14
Source Project: icafe   File: XMLUtils.java    License: Eclipse Public License 1.0 4 votes vote down vote up
public static void insertLeadingPI(Document doc, String target, String data) {
	Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    doc.insertBefore(pi, element);
}
 
Example 15
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 16
protected Node cloneNode(Document doc, boolean deep) {
    return doc.createProcessingInstruction(getTarget(), getData());
}
 
Example 17
Source Project: openjdk-8   File: DOMUtil.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 18
public static void insertLeadingPI(Document doc, String target, String data) {
	Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().insertBefore(pi, element);
}
 
Example 19
public static void insertTrailingPI(Document doc, String target, String data) {
	Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().appendChild(pi);
}
 
Example 20
Source Project: caja   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));
}