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

/**
 * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method
 *///  w ww  . ja v a 2s  . c  o m
public static String getTextContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:
        return null;
    }
}

From source file:Main.java

/**
 * Convert a node type to a string. For debug purpose only.
 * /*from   w w  w.j  a v a 2  s.  co m*/
 * @param nodeType
 *            the node type
 * @return the string
 * @throws Exception
 *             the exception
 */
public static String nodeTypeToString(short nodeType) throws Exception {
    if (nodeType == Node.ELEMENT_NODE)
        return "ELEMENT_NODE";
    if (nodeType == Node.ATTRIBUTE_NODE)
        return "ATTRIBUTE_NODE";
    if (nodeType == Node.TEXT_NODE)
        return "TEXT_NODE";
    if (nodeType == Node.CDATA_SECTION_NODE)
        return "CDATA_SECTION_NODE";
    if (nodeType == Node.ENTITY_REFERENCE_NODE)
        return "ENTITY_REFERENCE_NODE";
    if (nodeType == Node.ENTITY_NODE)
        return "ENTITY_NODE";
    if (nodeType == Node.PROCESSING_INSTRUCTION_NODE)
        return "PROCESSING_INSTRUCTION_NODE";
    if (nodeType == Node.COMMENT_NODE)
        return "COMMENT_NODE";
    if (nodeType == Node.DOCUMENT_NODE)
        return "DOCUMENT_NODE";
    if (nodeType == Node.DOCUMENT_TYPE_NODE)
        return "DOCUMENT_TYPE_NODE";
    if (nodeType == Node.DOCUMENT_FRAGMENT_NODE)
        return "DOCUMENT_FRAGMENT_NODE";
    if (nodeType == Node.NOTATION_NODE)
        return "NOTATION_NODE";
    if (nodeType == Node.DOCUMENT_POSITION_DISCONNECTED)
        return "DOCUMENT_POSITION_DISCONNECTED";
    if (nodeType == Node.DOCUMENT_POSITION_PRECEDING)
        return "DOCUMENT_POSITION_PRECEDING";
    if (nodeType == Node.DOCUMENT_POSITION_FOLLOWING)
        return "DOCUMENT_POSITION_FOLLOWING";
    if (nodeType == Node.DOCUMENT_POSITION_CONTAINS)
        return "DOCUMENT_POSITION_CONTAINS";
    if (nodeType == Node.DOCUMENT_POSITION_CONTAINED_BY)
        return "DOCUMENT_POSITION_CONTAINED_BY";
    if (nodeType == Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC)
        return "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC";

    throw new Exception("Unknown value : " + nodeType);
}

From source file:Main.java

/**
 * /* www  .  j a v  a  2s .  com*/
 * @param currentNode
 * @param tagName
 * @param attributeValue
 * @return
 */
public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName,
        String attributeValue) {
    String result = "";

    NodeList childNodeList = currentNode.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node childNode = childNodeList.item(i);

        switch (childNode.getNodeType()) {
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            Element childElement = (Element) childNodeList.item(i);
            // logger.debug("childElement name : " + childElement.getTagName());
            if (childElement != null && childElement.getNodeName().equals(tagName)) {
                NamedNodeMap attributes = childElement.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node current = attributes.item(j);

                    if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) {
                        result = childElement.getTextContent();
                        break;
                    }
                }
            }
        case Node.TEXT_NODE:
            // logger.debug("textElement name : " + currentNode.getNodeValue());
            break;
        case Node.COMMENT_NODE:
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            break;
        case Node.ENTITY_REFERENCE_NODE:
            break;
        case Node.DOCUMENT_TYPE_NODE:
            break;
        }
    }

    return result;
}

From source file:Main.java

private static String mergeTextContent(final NodeList nodes) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);//from w  w  w.j  a v a 2 s  . c  om
        final String text;

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

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

From source file:Main.java

/**
 * Generates an XPath expression that will return only the given node as its
 * result. This method only works for element, text, document and PI nodes.
 *
 * @param node the node to generate an XPath expression for. This node must
 * be an element node, a text node, a document node, or a processing
 * instruction node.//ww  w.  jav a  2  s.c om
 * @return an XPath expression that will return only the given node as its
 * result.
 * @exception IllegalArgumentException if the given node is not an element,
 * text, document or PI node.
 */
public static String getXPathExprFromNode(Node node) throws IllegalArgumentException {
    short nodeType = getNodeType(node);

    switch (nodeType) {
    case Node.ELEMENT_NODE:
    case Node.TEXT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return getXPathFromVector(getVectorPathFromNode(node));

    case Node.DOCUMENT_NODE:
        return "/";

    default:
        throw new IllegalArgumentException("Only works for element, text, " + "document, and PI nodes.");
    }
}

From source file:Main.java

private static String getTextContent(final Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:/*w w  w  . j a  va  2 s.  c o  m*/
        return null;
    }
}

From source file:Main.java

/**
 * returns the Node Type As String//from  w w  w  . j  a v  a  2s.co  m
 * @param node
 * @param cftype 
 * @return
 */
public static String getTypeAsString(Node node, boolean cftype) {
    String suffix = cftype ? "" : "_NODE";

    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        return "ATTRIBUTE" + suffix;
    case Node.CDATA_SECTION_NODE:
        return "CDATA_SECTION" + suffix;
    case Node.COMMENT_NODE:
        return "COMMENT" + suffix;
    case Node.DOCUMENT_FRAGMENT_NODE:
        return "DOCUMENT_FRAGMENT" + suffix;
    case Node.DOCUMENT_NODE:
        return "DOCUMENT" + suffix;
    case Node.DOCUMENT_TYPE_NODE:
        return "DOCUMENT_TYPE" + suffix;
    case Node.ELEMENT_NODE:
        return "ELEMENT" + suffix;
    case Node.ENTITY_NODE:
        return "ENTITY" + suffix;
    case Node.ENTITY_REFERENCE_NODE:
        return "ENTITY_REFERENCE" + suffix;
    case Node.NOTATION_NODE:
        return "NOTATION" + suffix;
    case Node.PROCESSING_INSTRUCTION_NODE:
        return "PROCESSING_INSTRUCTION" + suffix;
    case Node.TEXT_NODE:
        return "TEXT" + suffix;
    default:
        return "UNKNOW" + suffix;
    }
}

From source file:Main.java

/**
 * Method getStrFromNode/*  w  w  w. j a  v  a  2 s  .  co m*/
 *
 * @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;
}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//w  w  w.  j  av a  2s  . 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);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) 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 + " (" + node.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 && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

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 w w . jav  a 2s  .co  m
public final 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;
    }
}