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:org.tizzit.util.XercesHelper.java

public static String nodeList2string(NodeList nl) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nde = nl.item(i);//w w  w .  ja  v  a 2  s  .c o  m
        String attributes = "";
        if (nde.getNodeType() == Node.TEXT_NODE) {
            sb.append(getHexEncoded(nde.getNodeValue()));
            if (nde.hasChildNodes()) {
                sb.append(nodeList2string(nde.getChildNodes()));
            }
        } else if (nde.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append("<![CDATA[" + nde.getNodeValue() + "]]>");
        } else if (nde.getNodeType() == Node.COMMENT_NODE) {
            sb.append("<!-- -->");
        } else {
            if (nde.hasAttributes()) {
                NamedNodeMap attr = nde.getAttributes();
                for (int j = 0; j < attr.getLength(); j++) {
                    attributes += " " + attr.item(j).getNodeName() + "=\""
                            + getHexEncoded(attr.item(j).getNodeValue()) + "\"";
                }
            }
            sb.append("<" + nde.getNodeName() + attributes);
            if (nde.hasChildNodes()) {
                sb.append(">" + nodeList2string(nde.getChildNodes()) + "</" + nde.getNodeName() + ">");
            } else {
                sb.append("/>");
            }
        }
    }
    return sb.toString();
}

From source file:org.unitedinternet.cosmo.dav.acl.report.PrincipalPropertySearchReport.java

private boolean matchText(Element parent, String match) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) child;
            if (!matchText(e, match)) {
                return false;
            }/*w  w w.  ja  v  a 2s  . com*/
        } else if (child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE) {
            String data = ((CharacterData) child).getData();
            if (!matchText(data, match)) {
                return false;
            }
        } // else we skip the node
    }
    return true;
}

From source file:org.unitedinternet.cosmo.util.DomWriter.java

private static void writeNode(Node n, XMLStreamWriter writer) throws XMLStreamException {
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        writeElement((Element) n, writer);
    } else if (n.getNodeType() == Node.CDATA_SECTION_NODE || n.getNodeType() == Node.TEXT_NODE) {
        writeCharacters((CharacterData) n, writer);
    } else {//  w ww . ja v a2  s . c  o m
        LOG.warn("Skipping element " + n.getNodeName());
    }
}

From source file:org.yawlfoundation.yawl.util.DOMUtil.java

/**
 * Extracts to text from the supplied node and it's children.
 *
 * @param node to extract text from.//  w w w . ja  v  a2 s.c  om
 * @return String representation of the node text.
 */
public static String getNodeText(Node node) {

    if (node == null || !node.hasChildNodes())
        return "";
    StringBuilder result = new StringBuilder();

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        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(getNodeText(subnode));
        }
    }
    return result.toString();
}

From source file:pl.otros.logview.importer.UtilLoggingXmlLogImporter.java

/**
 * Get contents of CDATASection.//  w  ww. j  a  v  a2s  . com
 *
 * @param n
 *          CDATASection
 * @return text content of all text or CDATA children of node.
 */
private String getCData(final Node n) {
    StringBuffer buf = new StringBuffer();
    NodeList nl = n.getChildNodes();

    for (int x = 0; x < nl.getLength(); x++) {
        Node innerNode = nl.item(x);

        if ((innerNode.getNodeType() == Node.TEXT_NODE)
                || (innerNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
            buf.append(innerNode.getNodeValue());
        }
    }

    return buf.toString();
}

From source file:tufts.vue.ds.XMLIngest.java

private static boolean isText(int type) {
    return type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE;
}

From source file:tufts.vue.ds.XMLIngest.java

private static final String getNodeType(int t) {
    if (t == Node.ATTRIBUTE_NODE)
        return "attr";
    if (t == Node.CDATA_SECTION_NODE)
        return "cdata";
    if (t == Node.COMMENT_NODE)
        return "comment";
    if (t == Node.DOCUMENT_NODE)
        return "document";
    if (t == Node.ELEMENT_NODE)
        return "element";
    if (t == Node.ENTITY_NODE)
        return "entity";
    if (t == Node.TEXT_NODE)
        return "text";
    return "" + t;

}

From source file:tufts.vue.ds.XMLIngest.java

private static void scanNode(final XmlSchema schema, final org.w3c.dom.Node node, final int type,
        final String parentPath, final String parentName, final String nodeName, final String value) {
    final boolean isAttribute = (type == Node.ATTRIBUTE_NODE);
    final boolean isMergedText = FOLD_TEXT && isText(type);
    final boolean hasAttributes = (!isAttribute && node != null && node.hasAttributes());
    Node firstChild = null, lastChild = null;

    if (node != null) {
        firstChild = node.getFirstChild();
        lastChild = node.getLastChild();
    }// w  w  w  .jav  a2 s  .c o m

    final String XMLName;

    if (isAttribute)
        XMLName = parentName + ATTR_SEPARATOR + nodeName;
    else
        XMLName = nodeName;

    final String fullName;

    if (parentPath != null) { // should only be null first time in at the top root
        if (isMergedText)
            fullName = parentPath;
        else if (isAttribute)
            fullName = parentPath + ATTR_SEPARATOR + nodeName;
        else
            fullName = parentPath + '.' + nodeName;
    } else {
        fullName = nodeName;
    }

    if (type == Node.ELEMENT_NODE)
        schema.trackNodeOpen(fullName);

    if (depth < REPORT_THRESH) {
        if (depth < REPORT_THRESH - 1) {
            if (type == Node.TEXT_NODE)
                eoutln(String.format("node(%s) {%s} (len=%d)", getNodeType(type), fullName, value.length()));
            else
                eoutln(String.format("NODE(%s) {%s} %.192s", getNodeType(type), fullName, node,
                        Util.tags(firstChild)));
        }
        //eoutln("NODE: " + type + " name=" + name + " " + Util.tags(n) + " firstChild=" + Util.tags(firstChild));
        //System.err.println(name);
        else if (XML_DEBUG)
            System.err.print(".");
    }

    if (hasAttributes && ATTRIBUTES_IMMEDIATE)
        scanAttributes(schema, fullName, nodeName, node.getAttributes());

    String outputValue = null;

    if (value != null) {
        outputValue = value.trim();
        if (outputValue.length() > 0) {
            schema.trackFieldValuePair(fullName, outputValue);
        } else
            outputValue = null;
    }

    final NodeList children = node == null ? null : node.getChildNodes();
    final boolean DO_TAG;

    if (isMergedText) {
        DO_TAG = false;
    } else if (outputValue == null && node != null) {
        if (!node.hasChildNodes()) {
            DO_TAG = false;
        } else if (children.getLength() == 1 && isText(firstChild)
                && firstChild.getNodeValue().trim().length() == 0) {
            DO_TAG = false;
        } else
            DO_TAG = true;

        // if (!DO_TAG) ioutln("<!-- empty: " + nodeName + " -->");
    } else
        DO_TAG = true;

    boolean closeOnSameLine = false;

    if (DO_TAG) {

        iout("<");
        out(XMLName);
        //if (node.hasChildNodes()) out(" children=" + node.getChildNodes().getLength() + " first=" + node.getFirstChild());
        out(">");

        if (firstChild == null || (isText(firstChild) && firstChild == lastChild)) {
            //                 if (firstChild != null && firstChild.getNodeType() == Node.CDATA_SECTION_NODE)
            //                     ;
            //                 else
            closeOnSameLine = true;
        } else if (XML_OUTPUT)
            System.out.print('\n');

        if (FOLD_TEXT && (type != Node.ELEMENT_NODE && type != Node.ATTRIBUTE_NODE)) {
            final String err = "UNHANDLED TYPE=" + type + "; " + nodeName;
            outln("<" + err + ">");
            errout(err);
        }
    }

    if (outputValue != null) {
        if (type == Node.CDATA_SECTION_NODE) {
            out("<![CDATA[");
            out(outputValue);
            out("]]>");
        } else {
            out(XMLEntityEncode(outputValue));
        }
    }

    if (!isAttribute && node != null) {

        // god knows why, but attributes have themselves as children?  (or is that
        // the #text entry?)  Anyway, if we allow this for an attribute dump, the
        // value of the attribute will literally appear twice in the output,
        // back-to-back as one string.

        depth++;

        if (FOLD_KEYS || schema.isXMLKeyFold()) {

            scanFoldedChildren(schema, children, fullName, nodeName);

        } else {

            for (int i = 0; i < children.getLength(); i++)
                scanNode(schema, children.item(i), fullName, nodeName);
        }

        depth--;

    }

    if (DO_TAG) {

        if (closeOnSameLine)
            outln("</" + XMLName + ">");
        else
            ioutln("</" + XMLName + ">");
    }

    if (type == Node.ELEMENT_NODE)
        schema.trackNodeClose(fullName);

    if (hasAttributes && !ATTRIBUTES_IMMEDIATE)
        scanAttributes(schema, fullName, nodeName, node.getAttributes());

    //iout("children: " + Util.tags(n.getChildNodes()));
}