Example usage for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE

List of usage examples for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE.

Prototype

short PROCESSING_INSTRUCTION_NODE

To view the source code for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE.

Click Source Link

Document

The node is a ProcessingInstruction.

Usage

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *///from w  ww  . j  av  a 2 s . com
public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if (target == null || node.getOwnerDocument() == target)
        // same Document
        return node.cloneNode(deep);
    else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null)
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep)
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
                newNode.appendChild(cloneNode(child, target, true));

        return newNode;
    }
}

From source file:Main.java

/**
 * Gets Node type String for a given node type constant
 *//*from   ww w . j  a va 2  s.  c o m*/

public static String getNodeTypeStr(int nodeType)

{

    switch (nodeType)

    {

    case Node.ATTRIBUTE_NODE:

        return "ATTRIBUTE_NODE ";

    case Node.CDATA_SECTION_NODE:

        return "CDATA_SECTION_NODE";

    case Node.COMMENT_NODE:

        return "COMMENT_NODE";

    case Node.DOCUMENT_FRAGMENT_NODE:

        return "DOCUMENT_FRAGMENT_NODE";

    case Node.DOCUMENT_TYPE_NODE:

        return "DOCUMENT_TYPE_NODE";

    case Node.ELEMENT_NODE:

        return "ELEMENT_NODE";

    case Node.ENTITY_NODE:

        return "ENTITY_NODE";

    case Node.ENTITY_REFERENCE_NODE:

        return "ENTITY_REFERENCE_NODE";

    case Node.NOTATION_NODE:

        return "NOTATION_NODE";

    case Node.PROCESSING_INSTRUCTION_NODE:

        return "PROCESSING_INSTRUCTION_NODE";

    case Node.TEXT_NODE:

        return "TEXT_NODE";

    case Node.DOCUMENT_NODE:

        return "DOCUMENT_NODE";

    default:

        return "UN-INDENTIFIED NODE";

    }

}

From source file:Main.java

public String getNodeType(Node node) {
    String type;/* w ww. ja v  a 2s .  c  om*/

    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE: {
        type = "Element";
        break;
    }
    case Node.ATTRIBUTE_NODE: {
        type = "Attribute";
        break;
    }
    case Node.TEXT_NODE: {
        type = "Text";
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        type = "CData section";
        break;
    }
    case Node.ENTITY_REFERENCE_NODE: {
        type = "Entity reference";
        break;
    }
    case Node.ENTITY_NODE: {
        type = "Entity";
        break;
    }
    case Node.PROCESSING_INSTRUCTION_NODE: {
        type = "Processing instruction";
        break;
    }
    case Node.COMMENT_NODE: {
        type = "Comment";
        break;
    }
    case Node.DOCUMENT_NODE: {
        type = "Document";
        break;
    }
    case Node.DOCUMENT_TYPE_NODE: {
        type = "Document type";
        break;
    }
    case Node.DOCUMENT_FRAGMENT_NODE: {
        type = "Document fragment";
        break;
    }
    case Node.NOTATION_NODE: {
        type = "Notation";
        break;
    }
    default: {
        type = "???";
        break;
    }
    }
    return type;
}

From source file:Main.java

/**
 * based on the following quote from public Java5 javadoc of org.w3c.dom.Node.getTextContent method:
 * /*from ww  w .  java2 s  .c o m*/
 * "concatenation of the textContent attribute value of every child node, excluding COMMENT_NODE and
 * PROCESSING_INSTRUCTION_NODE nodes. This is the empty string if the node has no children"
 */
private static String mergeTextContent(NodeList nodes) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        final String text;

        switch (n.getNodeType()) {
        case Node.COMMENT_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
            text = null;
            break;
        default:
            text = getTextContent(n);
            break;
        }

        if (text != null) {
            buf.append(text);
        }
    }
    return buf.toString();
}

From source file:Main.java

public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;/*  ww  w.ja  v  a2  s.  c o  m*/
    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);
            }
            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;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:DOMDump.java

private static void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;/*w w w . j av a2  s .  c o m*/
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.print(indent + "TEXT_NODE");
        System.out.println(" : " + node.getTextContent());
        break;
    default:
        System.out.println(indent + "Unknown node");
        break;
    }

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        dumpLoop(list.item(i), indent + "   ");
    }
}

From source file:Main.java

private static String getXPathFromVector(Vector path) {
    StringBuffer strBuf = new StringBuffer();
    int length = path.size();

    for (int i = 0; i < length; i++) {
        Node tempNode = (Node) path.elementAt(i);
        short nodeType = getNodeType(tempNode);
        String targetValue = getValue(tempNode, nodeType);
        int position = 1;

        tempNode = getPreviousTypedNode(tempNode, nodeType);

        while (tempNode != null) {
            if (nodeType == Node.ELEMENT_NODE) {
                if (getValue(tempNode, nodeType).equals(targetValue)) {
                    position++;/*  www .  j a va 2  s  . co m*/
                }
            } else {
                position++;
            }

            tempNode = getPreviousTypedNode(tempNode, nodeType);
        }

        boolean hasMatchingSiblings = (position > 1);

        if (!hasMatchingSiblings) {
            tempNode = (Node) path.elementAt(i);
            tempNode = getNextTypedNode(tempNode, nodeType);

            while (!hasMatchingSiblings && tempNode != null) {
                if (nodeType == Node.ELEMENT_NODE) {
                    if (getValue(tempNode, nodeType).equals(targetValue)) {
                        hasMatchingSiblings = true;
                    } else {
                        tempNode = getNextTypedNode(tempNode, nodeType);
                    }
                } else {
                    hasMatchingSiblings = true;
                }
            }
        }

        String step;

        switch (nodeType) {
        case Node.TEXT_NODE:
            step = "text()";
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            step = "processing-instruction()";
            break;
        default:
            step = targetValue;
            break;
        }

        if (step != null && step.length() > 0) {
            strBuf.append('/' + step);
        }

        if (hasMatchingSiblings) {
            strBuf.append("[" + position + "]");
        }
    }

    return strBuf.toString();
}

From source file:Main.java

/**
 * @param node/*from  w w w . j  ava  2  s  .  c  o m*/
 * @throws IOException
 */
public static void serializeNode(Node node) throws IOException {
    if (writer == null)
        writer = new BufferedWriter(new OutputStreamWriter(System.out));

    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        Document doc = (Document) node;
        writer.write("<?xml version=\"");
        writer.write(doc.getXmlVersion());
        writer.write("\" encoding=\"UTF-8\" standalone=\"");
        if (doc.getXmlStandalone())
            writer.write("yes");
        else
            writer.write("no");
        writer.write("\"?>\n");

        NodeList nodes = node.getChildNodes();
        if (nodes != null)
            for (int i = 0; i < nodes.getLength(); i++)
                serializeNode(nodes.item(i));
        break;
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        writer.write("<" + name);
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            writer.write(" " + current.getNodeName() + "=\"");
            print(current.getNodeValue());
            writer.write("\"");
        }
        writer.write(">");

        NodeList children = node.getChildNodes();
        if (children != null) {
            //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE))
            //  writer.write("\n");

            for (int i = 0; i < children.getLength(); i++)
                serializeNode(children.item(i));
            if ((children.item(0) != null)
                    && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE))
                writer.write("");
        }

        writer.write("</" + name + ">");
        break;
    case Node.TEXT_NODE:
        print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        writer.write("CDATA");
        print(node.getNodeValue());
        writer.write("");
        break;
    case Node.COMMENT_NODE:
        writer.write("<!-- " + node.getNodeValue() + " -->\n");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writer.write("&" + node.getNodeName() + ";");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        String publicId = docType.getPublicId();
        String systemId = docType.getSystemId();
        String internalSubset = docType.getInternalSubset();
        writer.write("<!DOCTYPE " + docType.getName());
        if (publicId != null)
            writer.write(" PUBLIC \"" + publicId + "\" ");
        else
            writer.write(" SYSTEM ");
        writer.write("\"" + systemId + "\"");
        if (internalSubset != null)
            writer.write(" [" + internalSubset + "]");
        writer.write(">\n");
        break;
    }
    writer.flush();
}

From source file:Main.java

private static void prettyPrintLoop(Node node, StringBuilder string, String indentation) {
    if (node == null) {
        return;//  w  w w. j  ava 2  s .c om
    }

    int type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE:
        string.append("\n");
        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");
        break;

    case Node.ELEMENT_NODE:
        string.append(indentation);
        string.append("<");
        string.append(node.getNodeName());

        Attr[] attributes;
        if (node.getAttributes() != null) {
            int length = node.getAttributes().getLength();
            attributes = new Attr[length];
            for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                attributes[loopIndex] = (Attr) node.getAttributes().item(loopIndex);
            }
        } else {
            attributes = new Attr[0];
        }

        for (Attr attribute : attributes) {
            string.append(" ");
            string.append(attribute.getNodeName());
            string.append("=\"");
            string.append(attribute.getNodeValue());
            string.append("\"");
        }

        string.append(">\n");

        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");

        string.append(indentation);
        string.append("</");
        string.append(node.getNodeName());
        string.append(">\n");

        break;

    case Node.TEXT_NODE:
        string.append(indentation);
        string.append(node.getNodeValue().trim());
        string.append("\n");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        string.append(indentation);
        string.append("<?");
        string.append(node.getNodeName());
        String text = node.getNodeValue();
        if (text != null && text.length() > 0) {
            string.append(text);
        }
        string.append("?>\n");
        break;

    case Node.CDATA_SECTION_NODE:
        string.append(indentation);
        string.append("<![CDATA[");
        string.append(node.getNodeValue());
        string.append("]]>");
        break;
    }
}

From source file:Main.java

/** Prints the specified node, then prints all of its children. */

public static void printDOM(Node node) {

    int type = node.getNodeType();

    switch (type) {

    // print the document element
    case Node.DOCUMENT_NODE: {
        System.out.print("<?xml version=\"1.0\" ?>");
        printDOM(((Document) node).getDocumentElement());
        break;/*from  www .ja va 2s  . com*/
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        System.out.println();
        System.out.print("<");
        System.out.print(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\"");
        }
        System.out.print(">");
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++)
                printDOM(children.item(i));
        }
        break;
    }

    // handle entity reference nodes

    case Node.ENTITY_REFERENCE_NODE: {
        System.out.print("&");
        System.out.print(node.getNodeName().trim());
        System.out.print(";");
        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        System.out.print("<![CDATA[");
        System.out.print(node.getNodeValue().trim());
        System.out.print("]]>");
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        System.out.println();
        System.out.print(node.getNodeValue().trim());
        break;
    }

    // print processing instruction

    case Node.PROCESSING_INSTRUCTION_NODE: {
        System.out.print("<?");
        System.out.print(node.getNodeName().trim());
        String data = node.getNodeValue().trim();
        {
            System.out.print(" ");
            System.out.print(data);
        }
        System.out.print("?>");
        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        System.out.println();
        System.out.print("</");
        System.out.print(node.getNodeName().trim());
        System.out.print('>');
    }
}