Example usage for org.w3c.dom Node CDATA_SECTION_NODE

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

Introduction

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

Prototype

short CDATA_SECTION_NODE

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

Click Source Link

Document

The node is a CDATASection.

Usage

From source file:Main.java

/**
 * We get a object that connected text node of the child node and the CDATA section as character string.
 * @param node/*from  w  w w .  j  a  v a 2  s.com*/
 * @return
 */
public static String getChildText(Node node) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            buf.append(n.getNodeValue());
        }
    }
    return buf.toString();
}

From source file:Main.java

/**
 * returns the Node Type As String/*from   ww w . jav a 2  s. com*/
 * @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

public static String getText(Node node)

{

    StringBuffer result = new StringBuffer();

    if (!node.hasChildNodes())

        return "";

    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++)

    {/*w w  w  .  java  2  s .co  m*/

        Node subnode = list.item(i);

        if (subnode.getNodeType() == Node.TEXT_NODE)

        {

            result.append(subnode.getNodeValue());

        } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE)

        {

            result.append(subnode.getNodeValue());

        } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE)

        {

            // Recurse into the subtree for text

            // (and ignore comments)

            result.append(getText(subnode));

        }

    }

    return result.toString();

}

From source file:Main.java

/**
 * Returns the child text from a DOM node.
 * @param node the node to parse/* w  ww  . j  a v a2 s . c om*/
 * @return the node text, or <tt>null</tt> if the node did not contain any text
 */
public static String getText(Node node) {
    StringBuilder s = null;
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.TEXT_NODE) {
            if (s == null) {
                s = new StringBuilder();
            }
            s.append(((Text) child).getTextContent());
        } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
            if (s == null) {
                s = new StringBuilder();
            }
            s.append(((CDATASection) child).getData());
        }
        child = child.getNextSibling();
    }
    return s == null ? null : s.toString();
}

From source file:Main.java

/**
 * Remove empty CDATA sections from the given node and all of its descendants. Some parsers
 * silently discard empty CDATA sections, and this method may be used to compare the output from
 * parsers that behave differently with respect to empty CDATA sections.
 * /*from w w w  . j av  a 2  s. c  om*/
 * @param node
 *            the node to process
 */
public static void removeEmptyCDATASections(Node node) {
    Node child = node.getFirstChild();
    while (child != null) {
        Node next = child.getNextSibling();
        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            if (child.getNodeValue().length() == 0) {
                child.getParentNode().removeChild(child);
            }
            break;
        case Node.ELEMENT_NODE:
            removeEmptyCDATASections(child);
        }
        child = next;
    }
}

From source file:Main.java

/**
 * Returns the node type name from the node type code
 *
 * @param nodeType//from   w  w w . java2  s.c om
 * @return
 */
public static String getTypeName(short nodeType) {
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        return "element";
    case Node.DOCUMENT_NODE:
        return "document";
    case Node.TEXT_NODE:
        return "text";
    case Node.ATTRIBUTE_NODE:
        return "attribute";
    case Node.CDATA_SECTION_NODE:
        return "cdata";
    }
    return "Unknown[" + nodeType + "]";
}

From source file:Main.java

/**
 * Convert a node type to a string. For debug purpose only.
 * //from   w w  w  .  j  a  v  a  2s  . c  o 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

public static String getXpathExpressionValue(Object xprContext, String xpExpression)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression xpe = xpath.compile(xpExpression);
    Node valueNode = (Node) xpe.evaluate(xprContext, XPathConstants.NODE);
    String value = null;/*from w ww . j a  va  2 s.c  om*/
    if (valueNode != null)
        value = valueNode.getNodeValue();
    if (value != null) {
        // if the node is a text node - then we trim and (potentially) look for CDATA
        if (valueNode.getNodeType() == Node.TEXT_NODE) {
            value = value.trim();

            // look for CDATA if we got nothing (stupid whitespace)
            if (value.length() == 0) {
                Node siblingForCDATA = valueNode.getNextSibling();
                if (siblingForCDATA.getNodeType() == Node.CDATA_SECTION_NODE) {
                    value = siblingForCDATA.getNodeValue();
                }
            }
        }
    }

    return value;
}

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;/*from w  w  w.  jav  a2s  .c om*/
    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:Main.java

private static boolean checkNodeTypes(Node childNode) {
    short nodeType = childNode.getNodeType();

    if (nodeType == Node.ELEMENT_NODE) {
        cleanEmptyTextNodes(childNode); // recurse into subtree
    }//  ww  w. ja v  a  2s .c o m

    if (nodeType == Node.ELEMENT_NODE || nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.COMMENT_NODE) {
        return true;
    } else {
        return false;
    }
}