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

public static boolean setNodeValue(Node domNode, String string) {
    if (domNode == null)
        return false;

    short nodeType = domNode.getNodeType();

    switch (nodeType) {
    case Node.ELEMENT_NODE: {
        setElementText((Element) domNode, string);
        break;/*w ww .  j a  v  a 2  s .  co m*/
    }
    case Node.ATTRIBUTE_NODE:
    case Node.TEXT_NODE: {
        domNode.setNodeValue(string);
        break;
    }
    case Node.PROCESSING_INSTRUCTION_NODE: {
        ((ProcessingInstruction) domNode).setData(string);
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        ((CDATASection) domNode).setData(string);
        break;
    }
    default: {
        return false;
    }
    }

    return true;
}

From source file:Main.java

/**
 * Identify variables in attribute values and CDATA contents and replace
 * with their values as defined in the variableDefs map. Variables without
 * definitions are left alone.//from  w ww. j  av a  2  s  . c  o m
 *
 * @param node the node to do variable replacement in
 * @param variableDefs map from variable names to values.
 */
public static void replaceVariables(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap atts = element.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {
            final Attr attr = (Attr) atts.item(i);
            attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs));
        }
        break;

    case Node.CDATA_SECTION_NODE:
        String content = node.getTextContent();
        node.setNodeValue(replaceVariablesInString(content, variableDefs));
        break;

    default:
        break;
    }

    // process children
    final NodeList children = node.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); childIndex++)
        replaceVariables(children.item(childIndex), variableDefs);
}

From source file:Main.java

/**
 * Extract default values of variables defined in the DOM subtree below node.
 * Default values are used to populate the variableDefs map.  Variables
 * already defined in this map will NOT be modified.
 *
 * @param node root node of DOM subtree to extract default values from.
 * @param variableDefs map which default values will be added to.
 */// ww  w  .  ja  v a 2s  . com
public static void extractVariableDefaults(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attr = (Attr) attrs.item(i);
            extractVariableDefaultsFromString(attr.getValue(), variableDefs);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        String content = node.getTextContent();
        extractVariableDefaultsFromString(content, variableDefs);
        break;

    default:
        break;
    }

    final NodeList children = node.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); childIndex++)
        extractVariableDefaults(children.item(childIndex), variableDefs);
}

From source file:Main.java

/**
 * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method
 *//*from  ww  w .  j a  v a  2  s .  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

/**
 * Get the text that is associated with this element.
 * //from w  w  w.  j a v a 2  s  .  c  o  m
 * @param element an Element object.
 * @return the text that is associated with this element.
 */
public static String getText(Element element) {
    String text = null;

    // Get first child element
    Node node = element.getFirstChild();

    // NodeList nodeList = element.getChildNodes();

    // int length = nodeList.getLength();

    // Process while there are nodes and the text hasn't been found
    while ((node != null) && (text == null)) {
        // If a text node or cdata section is found, then get text
        if ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) {
            text = ((CharacterData) node).getData();
        }

        // Get next sibling
        node = node.getNextSibling();
    }

    if (text != null)
        text = text.trim();

    return text;
}

From source file:Main.java

/**
 * Concat all the text and cdata node children of this elem and return
 * the resulting text.//w  ww  . j  ava2 s  .  c o  m
 * (by Matt Duftler)
 *
 * @param parentEl the element whose cdata/text node values are to
 *                 be combined.
 * @return the concatanated string.
 */
public static String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }
    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:Main.java

/**
 * Returns first of the <tt>element</tt>'s child nodes that is of type
 * <tt>nodeType</tt>./*from  ww w. j av  a2  s  .  c  om*/
 * @param element the element whose child we need.
 * @param nodeType the type of the child we need.
 * @return a child of the specified <tt>nodeType</tt> or null if none
 * was found.
 */
public static Node getChildByType(Element element, short nodeType) {
    if (element == null)
        return null;

    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() < 1)
        return null;

    Node node;
    String data;
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        short type = node.getNodeType();
        if (type == nodeType) {
            if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
                data = ((Text) node).getData();
                if (data == null || data.trim().length() < 1)
                    continue;
            }

            return node;
        }
    }

    return null;
}

From source file:Main.java

public static String getCDATAContent(Node node) {
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node child = nodeList.item(i);
            if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
                CDATASection cdata = (CDATASection) child;
                return cdata.getData();
            }// w  w w .  j a  va 2 s  .  c o  m
        }
    }
    return null;
}

From source file:Main.java

/** 
 * Decide if the node is text./*from  ww  w .  j av a  2  s.  c  o m*/
 * 
 * @param n Node to check
 * @return True if node is text; otherwise retur false
 */
public static boolean isTextNode(Node n) {
    //Sanity check
    if (n == null)
        return false;

    //Get node type
    short nodeType = n.getNodeType();

    //Check type and return result
    return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}

From source file:Main.java

public static String textContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        StringBuffer sb = new StringBuffer();
        Node nextChild = node.getFirstChild();
        while (nextChild != null) {
            sb.append(textContent(nextChild));
            nextChild = nextChild.getNextSibling();
        }/*  w  ww  .  j av  a2  s .c  om*/
        return sb.toString();
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    default:
        return "";
    }
}