Example usage for org.w3c.dom Node hasChildNodes

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

Introduction

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

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

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  ww.j a  v a2 s. co  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()));
}