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: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  .ja v a 2  s  . 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:com.marklogic.dom.NodeImpl.java

/** {@inheritDoc} */
public boolean isEqualNode(Node other) {

    // Note that normalization can affect equality; to avoid this,
    // nodes should be normalized before being compared.
    // For the moment, normalization cannot be done.
    if (other == null)
        return false;
    if (getNodeType() != other.getNodeType())
        return false;
    if (!getLocalName().equals(other.getLocalName()))
        return false;
    if (notequals(getNamespaceURI(), other.getNamespaceURI()))
        return false;
    if (notequals(getPrefix(), other.getPrefix()))
        return false;
    if (notequals(getNodeValue(), other.getNodeValue()))
        return false;
    if (hasChildNodes() != other.hasChildNodes())
        return false;
    if (hasAttributes() != other.hasAttributes())
        return false;
    if (hasChildNodes()) {
        NamedNodeMap thisAttr = getAttributes();
        NamedNodeMap otherAttr = other.getAttributes();
        if (thisAttr.getLength() != otherAttr.getLength())
            return false;
        for (int i = 0; i < thisAttr.getLength(); i++)
            if (thisAttr.item(i).isEqualNode(otherAttr.item(i)))
                return false;
    }//from   w  ww  . j a  v  a 2s. c o m
    if (hasAttributes()) {
        NodeList thisChild = getChildNodes();
        NodeList otherChild = other.getChildNodes();
        if (thisChild.getLength() != otherChild.getLength())
            return false;
        for (int i = 0; i < thisChild.getLength(); i++)
            if (thisChild.item(i).isEqualNode(otherChild.item(i)))
                return false;
    }
    return true;
}

From source file:DOMProcessor.java

/** Renames the given element with the given new name.
  * @param existingElement Element to rename.
  * @param newName New name to give element.
  *///from  w  w  w .java  2  s .c  om
public void renameElement(Node existingElement, String newName) {
    // Create an element with the new name
    Node newElement = dom.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = existingElement.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) dom.importNode(attrs.item(i), true);
        newElement.getAttributes().setNamedItem(attr2);
    }

    // Move all the children
    while (existingElement.hasChildNodes()) {
        newElement.appendChild(existingElement.getFirstChild());
    }

    // Replace the old node with the new node
    existingElement.getParentNode().replaceChild(newElement, existingElement);
}

From source file:com.twinsoft.convertigo.beans.core.Sequence.java

private static Node setOutputUserData(Node node, Object value, boolean recurse) {
    if (node != null) {
        // set output mode as userdata (element or attribute)
        node.setUserData(Step.NODE_USERDATA_OUTPUT, value, null);

        // recurse on element child nodes only
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (recurse && node.hasChildNodes()) {
                NodeList list = node.getChildNodes();
                for (int i = 0; i < list.getLength(); i++) {
                    setOutputUserData(list.item(i), value, recurse);
                }/*from w w w.jav  a2  s .c  o  m*/
            }
        }
    }
    return node;
}

From source file:com.mingo.parser.xml.dom.QuerySetParser.java

/**
 * Parse <case/> tag.//from  ww w .  j  av a  2 s  .c  om
 *
 * @param node node
 * @return {@link QueryCase}
 */
private void parseCaseTag(Node node, Query query, QuerySet querySet) throws ParserException {
    if (node == null || !CASE_TAG.equals(node.getNodeName())) {
        return;
    }
    QueryCase queryCase = new QueryCase();
    queryCase.setId(getAttributeString(node, ID));
    queryCase.setPriority(getAttributeInt(node, CASE_PRIORITY_ATTR));
    queryCase.setCondition(getAttributeString(node, CONDITION));
    QueryType type = QueryType.getByName(getAttributeString(node, TYPE_ATTR));
    queryCase.setQueryType(type != null ? type : query.getQueryType());
    boolean inheritConverter = getAttributeBoolean(node, CONVERTER_INHERIT_ATTR);
    if (!inheritConverter) {
        queryCase.setConverter(getAttributeString(node, CONVERTER_CLASS_ATTR));
        queryCase.setConverterMethod(getAttributeString(node, CONVERTER_METHOD_ATTR));
    } else {
        queryCase.setConverterMethod(query.getConverterMethod());
        queryCase.setConverter(query.getConverter());
    }

    // parse case body
    if (node.hasChildNodes()) {
        StringBuilder caseBodyBuilder = new StringBuilder();
        NodeList childList = node.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            Node child = childList.item(i);
            parseBody(caseBodyBuilder, child, querySet);
        }

        queryCase.setBody(StringUtils.strip(caseBodyBuilder.toString()));
        if (!validate(wrap(queryCase.getBody()))) {
            throw new ParserException(MessageFormatter
                    .format(INVALID_QUERY_ERROR_MSG, queryCase.getId(), queryCase).getMessage());
        }
    }
    query.addQueryCase(queryCase);
}

From source file:com.github.podd.resources.RestletPoddClientImpl.java

private static void printNote(NodeList nodeList) {

    for (int count = 0; count < nodeList.getLength(); count++) {

        Node tempNode = nodeList.item(count);

        // make sure it's element node.
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

            // get node name and value
            if (!tempNode.getNodeName().startsWith("rdf:") && tempNode.getTextContent().length() > 0) {
                if (tempNode.getNodeName().startsWith("rdfs:label")) {

                    System.out.println("Name" + ": " + tempNode.getTextContent());
                } else if (tempNode.getNodeName().startsWith("rdfs:comment")) {
                    System.out.println("Description" + ": " + tempNode.getTextContent());
                } else if (tempNode.getNodeName().startsWith("hasPotColumnNumberOverall")) {
                    System.out.println("Lane" + ": " + tempNode.getTextContent());
                } else if (tempNode.getNodeName().startsWith("hasPotPositionTray")) {
                    System.out.println("Position" + ": " + tempNode.getTextContent());
                } else {
                    System.out.println(tempNode.getNodeName() + ": " + tempNode.getTextContent());
                }//from  w  w  w. j a  v a  2s  .  co  m

            }

            if (tempNode.hasChildNodes()) {
                // loop again if has child node

                printNote(tempNode.getChildNodes());
                System.out.println("");

            }

        }

    }
}

From source file:org.dasein.cloud.aws.platform.CloudFront.java

private @Nullable Distribution toDistributionFromInfo(@Nonnull ProviderContext ctx, @Nullable Node node) {
    if (node == null) {
        return null;
    }/*ww w . j  a  va 2  s .  co m*/
    ArrayList<String> cnames = new ArrayList<String>();
    Distribution distribution = new Distribution();
    NodeList attrs = node.getChildNodes();

    distribution.setProviderOwnerId(ctx.getAccountNumber());
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        String name;

        name = attr.getNodeName();
        if (name.equals("Id")) {
            distribution.setProviderDistributionId(attr.getFirstChild().getNodeValue().trim());
        } else if (name.equals("Status")) {
            String s = attr.getFirstChild().getNodeValue();

            distribution.setDeployed(s != null && s.trim().equalsIgnoreCase("deployed"));
        } else if (name.equals("DomainName")) {
            distribution.setDnsName(attr.getFirstChild().getNodeValue().trim());
        } else if (name.equals("DistributionConfig")) {
            NodeList configList = attr.getChildNodes();

            for (int j = 0; j < configList.getLength(); j++) {
                Node config = configList.item(j);

                if (config.getNodeName().equals("Enabled")) {
                    String s = config.getFirstChild().getNodeValue();

                    distribution.setActive(s != null && s.trim().equalsIgnoreCase("true"));
                } else if (config.getNodeName().equals("Origin")) {
                    String origin = config.getFirstChild().getNodeValue().trim();

                    distribution.setLocation(origin);
                } else if (config.getNodeName().equals("CNAME")) {
                    cnames.add(config.getFirstChild().getNodeValue().trim());
                } else if (config.getNodeName().equals("Logging")) {
                    if (config.hasChildNodes()) {
                        NodeList logging = config.getChildNodes();

                        for (int k = 0; k < logging.getLength(); k++) {
                            Node logInfo = logging.item(k);

                            if (logInfo.getNodeName().equals("Bucket")) {
                                if (logInfo.hasChildNodes()) {
                                    distribution.setLogDirectory(logInfo.getFirstChild().getNodeValue());
                                }
                            } else if (logInfo.getNodeName().equals("Prefix")) {
                                if (logInfo.hasChildNodes()) {
                                    distribution.setLogName(logInfo.getFirstChild().getNodeValue());
                                }
                            }
                        }
                    }
                } else if (config.getNodeName().equals("Comment")) {
                    if (config.hasChildNodes()) {
                        String comment = config.getFirstChild().getNodeValue();

                        if (comment != null) {
                            distribution.setName(comment.trim());
                        }
                    }
                }
            }
        }
    }
    if (distribution.getName() == null) {
        String name = distribution.getDnsName();

        if (name == null) {
            name = distribution.getProviderDistributionId();
            if (name == null) {
                return null;
            }
        }
        distribution.setName(name);
    }
    String[] aliases = new String[cnames.size()];
    int i = 0;
    for (String cname : cnames) {
        aliases[i++] = cname;
    }
    distribution.setAliases(aliases);
    return distribution;
}

From source file:com.stratelia.webactiv.util.XMLConfigurationStore.java

/**
 * This method returns the value of the node <strong>entry</strong>, starting from the node
 * <strong>n</strong>. It may consist of the concatenation of various text en entity reference
 * child nodes. <p> This method returns null if the node wasn't found
 *///from   www .  j a  va 2s  .  c o  m
public String getXMLParamValue(Node n, String entry, String key) {
    String res = null;

    if (n == null) {
        n = rootNode;
    }
    // find the node, starting from the current node
    Node paramNode = findNode(n, entry);

    // node not found
    if (paramNode == null) {
        return (null);
    }

    // get all node descendants, and concatenate all their string values. --TODO
    // What about sublevels?
    NodeList paramList = paramNode.getChildNodes();
    int paramsize = paramList.getLength();
    for (int pi = 0; pi < paramsize; pi++) {
        Node psn = paramList.item(pi);
        // some parsers expand entity references, some don't. A possibility
        // encountered is that an entity
        // reference node has children whose values make up the value of the
        // reference, which in turn should
        // be grouped
        // with the rest of the text (concatenated in the res variable). This code
        // may need to be completed
        // when more parsers are put to test... Reminder: an typical entity
        // reference could be a string like
        // &amp; , whereas what we really want is the '&' character.
        if (psn instanceof EntityReference) {
            if (psn.hasChildNodes()) {
                NodeList erKids = psn.getChildNodes();
                int erKidsSize = erKids.getLength();
                for (int ii = 0; ii < erKidsSize; ii++) {
                    Node entity = erKids.item(ii);
                    if (entity instanceof Text) {
                        String v = entity.getNodeValue();
                        if (res == null) {
                            res = v;
                        } else {
                            res = res + v;
                        }
                    }
                }
            }
        } // standard text, concatenate the value with other values
        else if (psn instanceof Text) {
            String v;
            if ((v = psn.getNodeValue()) == null) {
                return (null);
            }
            if (key != null) {
                if (v.trim().equalsIgnoreCase(key)) {
                    return (key);
                }
            } else {
                if (res == null) {
                    res = v.trim();
                } else {
                    res = res + v.trim();
                }
            }
        }
    }
    if (res != null) {
        // if a key match was asked for, compare the value, return null if not
        // equal
        if (key != null) {
            if (res.trim().equalsIgnoreCase(key)) {
                return (key);
            }
            return (null);
        }
        return (res);
    }
    return (null);
}

From source file:com.stratelia.webactiv.util.XMLConfigurationStore.java

/**
 * This method returns the values of the node <strong>entry</strong>, starting from the node
 * <strong>n</strong>. Each value may consist of the concatenation of various text en entity
 * reference child nodes. <p> This method returns null if the node wasn't found
 */// w w w  . j  a va 2  s.c om
public String[] getXMLParamValues(Node n, String entry, String key) {
    String res = null;
    List<String> vres = new ArrayList<String>(10);

    // find the node, starting from the current node
    Node paramNodes[] = findNodes(n, entry);

    // node not found
    if (paramNodes == null) {
        return (null);
    }

    // get all node descendants, and concatenate all their string values. --TODO
    // What about sublevels?
    for (Node paramNode : paramNodes) {
        NodeList paramList = paramNode.getChildNodes();
        int paramsize = paramList.getLength();
        for (int pi = 0; pi < paramsize; pi++) {
            Node psn = paramList.item(pi);
            // some parsers expand entity references, some don't. A possibility
            // encountered is that an entity
            // reference node has children whose values make up the value of the
            // reference, which in turn should
            // be grouped
            // with the rest of the text (concatenated in the res variable). This
            // code may need to be completed
            // when more parsers are put to test... Reminder: an typical entity
            // reference could be a string like
            // &amp; , whereas what we really want is the '&' character.
            if (psn instanceof EntityReference) {
                if (psn.hasChildNodes()) {
                    NodeList erKids = psn.getChildNodes();
                    int erKidsSize = erKids.getLength();
                    for (int ii = 0; ii < erKidsSize; ii++) {
                        Node entity = erKids.item(ii);
                        if (entity instanceof Text) {
                            String v = entity.getNodeValue();
                            if (res == null) {
                                res = v;
                            } else {
                                res = res + v;
                            }
                        }
                    }
                }
            } // standard text, concatenate the value with other values
            else if (psn instanceof Text) {
                String v;
                if ((v = psn.getNodeValue()) == null) {
                    return null;
                }
                vres.add(v.trim());
            }
        }
    }
    if (!vres.isEmpty()) {
        return vres.toArray(new String[vres.size()]);
    }
    return null;
}