Example usage for org.w3c.dom Node getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:com.anite.zebra.ext.xmlloader.XMLLoadProcess.java

private void loadProcessVersion(Node root, IProcessVersions processVersions) throws Exception {
    for (int i = 0; i < root.getChildNodes().getLength(); i++) {
        Node node = root.getChildNodes().item(i);
        if (node.getNodeName().compareTo(XMLNODE_PROVER) == 0) {
            IProcessVersion pv;/*from w  w w  .j a va2 s .  co  m*/
            try {
                pv = iterateProcessNodes(node.getFirstChild(), new Long(getAttr(node, XMLATTR_PROVER)),
                        processVersions);
                processVersions.addProcessVersion(pv);
            } catch (NumberFormatException e) {
                log.info(
                        "Unable to interate over a version - will continue with other versions - this one will be ingnored:"
                                + processVersions.getName(),
                        e);
            } catch (Exception e) {
                if (i == 0) {
                    log.debug(
                            "Unable to interate over a version - this is normal if it is the first version in a file:"
                                    + processVersions.getName());
                } else {
                    log.info(
                            "Unable to interate over a version - will continue with other versions - this one will be ingnored:"
                                    + processVersions.getName(),
                            e);
                }
            }
        }
    }
}

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

private @Nullable ResourceStatus toStatus(@Nullable Node node) {
    if (node == null) {
        return null;
    }/*from   ww w.  ja  v  a  2 s  .  c o m*/
    NodeList attrs = node.getChildNodes();

    String distributionId = null;
    boolean deployed = false;

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

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

            deployed = (s != null && s.trim().equalsIgnoreCase("deployed"));
        }
    }
    if (distributionId == null) {
        return null;
    }
    return new ResourceStatus(distributionId, deployed);
}

From source file:cn.edu.bit.whitesail.parser.HtmlParser.java

private void getLinks(Node node, List<URL> URLsToFill, String anchor) {
    URL u = null;//from   w  ww .  ja  va  2  s  .  c om

    if (node.getNodeName().equalsIgnoreCase("a") || node.getNodeName().equalsIgnoreCase("link")) {
        NamedNodeMap map = node.getAttributes();
        int length = map.getLength();
        for (int i = 0; i < length; i++) {
            Node item = map.item(i);
            if (item.getNodeName().equalsIgnoreCase("href")) {
                u = URLFormat(item.getNodeValue(), anchor);
                if (null != u) {
                    URLsToFill.add(u);
                }
            }
        }
    }
    Node child = node.getFirstChild();
    while (child != null) {
        getLinks(child, URLsToFill, anchor);
        child = child.getNextSibling();
    }
}

From source file:com.github.sevntu.checkstyle.internal.ChecksTest.java

private static void validateEclipseCsMetaXmlFileRule(String pkg, Class<?> module, Set<Node> children)
        throws Exception {
    final String moduleName = module.getSimpleName();
    final Set<String> properties = getFinalProperties(module);
    final Set<Field> fieldMessages = CheckUtil.getCheckMessages(module);
    final Set<String> messages = new TreeSet<>();

    for (Field fieldMessage : fieldMessages) {
        // below is required for package/private classes
        if (!fieldMessage.isAccessible()) {
            fieldMessage.setAccessible(true);
        }/*w  ww  . jav  a 2s . com*/

        messages.add(fieldMessage.get(null).toString());
    }

    for (Node child : children) {
        final NamedNodeMap attributes = child.getAttributes();

        switch (child.getNodeName()) {
        case "alternative-name":
            final Node internalNameNode = attributes.getNamedItem("internal-name");

            Assert.assertNotNull(
                    pkg + " checkstyle-metadata.xml must contain an internal name for " + moduleName,
                    internalNameNode);

            final String internalName = internalNameNode.getTextContent();

            Assert.assertEquals(
                    pkg + " checkstyle-metadata.xml requires a valid internal-name for " + moduleName,
                    module.getName(), internalName);
            break;
        case "description":
            Assert.assertEquals(pkg + " checkstyle-metadata.xml requires a valid description for " + moduleName,
                    "%" + moduleName + ".desc", child.getTextContent());
            break;
        case "property-metadata":
            final String propertyName = attributes.getNamedItem("name").getTextContent();

            Assert.assertTrue(pkg + " checkstyle-metadata.xml has an unknown parameter for " + moduleName + ": "
                    + propertyName, properties.remove(propertyName));

            final Node firstChild = child.getFirstChild().getNextSibling();

            Assert.assertNotNull(pkg + " checkstyle-metadata.xml requires atleast one child for " + moduleName
                    + ", " + propertyName, firstChild);
            Assert.assertEquals(pkg + " checkstyle-metadata.xml should have a description for the "
                    + "first child of " + moduleName + ", " + propertyName, "description",
                    firstChild.getNodeName());
            Assert.assertEquals(pkg + " checkstyle-metadata.xml requires a valid description for " + moduleName
                    + ", " + propertyName, "%" + moduleName + "." + propertyName, firstChild.getTextContent());
            break;
        case "message-key":
            final String key = attributes.getNamedItem("key").getTextContent();

            Assert.assertTrue(
                    pkg + " checkstyle-metadata.xml has an unknown message for " + moduleName + ": " + key,
                    messages.remove(key));
            break;
        default:
            Assert.fail(pkg + " checkstyle-metadata.xml unknown node for " + moduleName + ": "
                    + child.getNodeName());
            break;
        }
    }

    for (String property : properties) {
        Assert.fail(pkg + " checkstyle-metadata.xml missing parameter for " + moduleName + ": " + property);
    }

    for (String message : messages) {
        Assert.fail(pkg + " checkstyle-metadata.xml missing message for " + moduleName + ": " + message);
    }
}

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

private Distribution toDistributionFromSummary(Node node) {
    ArrayList<String> cnames = new ArrayList<String>();
    Distribution distribution = new Distribution();
    NodeList attrs = node.getChildNodes();

    //noinspection ConstantConditions
    distribution.setProviderOwnerId(provider.getContext().getAccountNumber());
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        String name;//from   www.  jav a2 s  . co  m

        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("Enabled")) {
            String s = attr.getFirstChild().getNodeValue();

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

            distribution.setLocation(origin);
        } else if (name.equals("CNAME")) {
            cnames.add(attr.getFirstChild().getNodeValue().trim());
        } else if (name.equals("Comment")) {
            if (attr.hasChildNodes()) {
                String comment = attr.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:org.dasein.cloud.aws.platform.SimpleDB.java

@Override
public Iterable<ResourceStatus> listKeyValueDatabaseStatus() throws CloudException, InternalException {
    APITrace.begin(provider, "KVDB.listKeyValueDatabaseStatus");
    try {/*from w w  w  .  j ava2s . com*/
        ArrayList<ResourceStatus> list = new ArrayList<ResourceStatus>();
        String marker = null;

        do {
            Map<String, String> parameters = provider.getStandardSimpleDBParameters(provider.getContext(),
                    LIST_DOMAINS);
            EC2Method method;
            NodeList blocks;
            Document doc;

            if (marker != null) {
                parameters.put("NextToken", marker);
            }
            method = new EC2Method(SERVICE_ID, provider, parameters);
            try {
                doc = method.invoke();
            } catch (EC2Exception e) {
                throw new CloudException(e);
            }
            marker = null;
            blocks = doc.getElementsByTagName("NextToken");
            if (blocks.getLength() > 0) {
                for (int i = 0; i < blocks.getLength(); i++) {
                    Node item = blocks.item(i);

                    if (item.hasChildNodes()) {
                        marker = item.getFirstChild().getNodeValue().trim();
                    }
                }
                if (marker != null) {
                    break;
                }
            }
            blocks = doc.getElementsByTagName("DomainName");
            for (int i = 0; i < blocks.getLength(); i++) {
                Node name = blocks.item(i);

                if (name.hasChildNodes()) {
                    list.add(new ResourceStatus(name.getFirstChild().getNodeValue(), true));
                }
            }
        } while (marker != null);
        return list;
    } finally {
        APITrace.end();
    }
}

From source file:it.polito.tellmefirst.web.rest.clients.ClientEpub.java

private void getFirstChildElement(Node navMap) {

    LOG.debug("[getFirstChildElement] - BEGIN");

    Node navPoint = navMap.getFirstChild();
    while (navPoint != null) {
        if (navPoint.getNodeType() == Node.ELEMENT_NODE) {
            Node navLabel = navPoint.getFirstChild();
            Node text = navLabel.getFirstChild();
            String title = text.getTextContent().toString();

            Node content = navLabel.getNextSibling();
            String link = content.getAttributes().getNamedItem("src").getNodeValue().toString();

            epub.put(title, link);/* www .java  2  s  .  co m*/
        }
        navPoint = navPoint.getNextSibling();
    }

    LOG.debug("[getFirstChildElement] - END");
}

From source file:cern.c2mon.shared.common.datatag.address.impl.HardwareAddressImpl.java

/**
 * Create a HardwareAddress object from its XML representation.
 *
 * @param pElement DOM element containing the XML representation of a HardwareAddress object, as created by the
 *                 toConfigXML() method.
 * @throws RuntimeException if unable to instantiate the Hardware address
 * @see cern.c2mon.shared.common.datatag.address.HardwareAddress#toConfigXML()
 *//*  w w  w  . j  a v  a2s . c  o  m*/
public final synchronized HardwareAddress fromConfigXML(Element pElement) {
    Class hwAddressClass = null;
    HardwareAddressImpl hwAddress = null;

    try {
        hwAddressClass = Class.forName(pElement.getAttribute("class"));
        hwAddress = (HardwareAddressImpl) hwAddressClass.newInstance();
    } catch (ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
        throw new RuntimeException("Exception caught when instantiating a hardware address from XML", cnfe);
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
        throw new RuntimeException("Exception caught when instantiating a hardware address from XML", iae);
    } catch (InstantiationException ie) {
        ie.printStackTrace();
        throw new RuntimeException("Exception caught when instantiating a hardware address from XML", ie);
    }

    NodeList fields = pElement.getChildNodes();
    Node fieldNode = null;
    int fieldsCount = fields.getLength();
    String fieldName;
    String fieldValueString;
    String fieldTypeName = "";

    for (int i = 0; i < fieldsCount; i++) {
        fieldNode = fields.item(i);
        if (fieldNode.getNodeType() == Node.ELEMENT_NODE) {
            fieldName = fieldNode.getNodeName();

            if (fieldNode.getFirstChild() != null) {
                fieldValueString = fieldNode.getFirstChild().getNodeValue();
            } else {
                fieldValueString = "";
            }
            try {
                Field field = hwAddressClass.getDeclaredField(decodeFieldName(fieldName));
                fieldTypeName = field.getType().getName();

                if (fieldTypeName.equals("short")) {
                    field.setShort(hwAddress, Short.parseShort(fieldValueString));
                } else if (fieldTypeName.equals("java.lang.Short")) {
                    field.set(hwAddress, new Integer(Integer.parseInt(fieldValueString)));
                } else if (fieldTypeName.equals("int")) {
                    field.setInt(hwAddress, Integer.parseInt(fieldValueString));
                } else if (fieldTypeName.equals("java.lang.Integer")) {
                    field.set(hwAddress, new Integer(Integer.parseInt(fieldValueString)));
                } else if (fieldTypeName.equals("float")) {
                    field.setFloat(hwAddress, Float.parseFloat(fieldValueString));
                } else if (fieldTypeName.equals("java.lang.Float")) {
                    field.set(hwAddress, new Float(Float.parseFloat(fieldValueString)));
                } else if (fieldTypeName.equals("double")) {
                    field.setDouble(hwAddress, Double.parseDouble(fieldValueString));
                } else if (fieldTypeName.equals("java.lang.Double")) {
                    field.set(hwAddress, new Double(Double.parseDouble(fieldValueString)));
                } else if (fieldTypeName.equals("long")) {
                    field.setLong(hwAddress, Long.parseLong(fieldValueString));
                } else if (fieldTypeName.equals("java.lang.Long")) {
                    field.set(hwAddress, new Long(Long.parseLong(fieldValueString)));
                } else if (fieldTypeName.equals("byte")) {
                    field.setByte(hwAddress, Byte.parseByte(fieldValueString));
                } else if (fieldTypeName.equals("java.lang.Byte")) {
                    field.set(hwAddress, new Byte(Byte.parseByte(fieldValueString)));
                } else if (fieldTypeName.equals("char")) {
                    field.setChar(hwAddress, fieldValueString.charAt(0));
                } else if (fieldTypeName.equals("java.lang.Character")) {
                    field.set(hwAddress, new Character(fieldValueString.charAt(0)));
                } else if (fieldTypeName.equals("boolean")) {
                    field.setBoolean(hwAddress, Boolean.getBoolean(fieldValueString));
                } else if (fieldTypeName.equals("java.lang.Boolean")) {
                    field.set(hwAddress, new Boolean(Boolean.getBoolean(fieldValueString)));
                } else if (fieldTypeName.equals("java.util.HashMap")) {
                    field.set(hwAddress, SimpleXMLParser.domNodeToMap(fieldNode));
                } else if (field.getType().isEnum()) {
                    Object[] enumConstants = field.getType().getEnumConstants();
                    for (Object enumConstant : enumConstants) {
                        if (enumConstant.toString().equals(fieldValueString)) {
                            field.set(hwAddress, enumConstant);
                        }
                    }
                } else {
                    field.set(hwAddress, fieldValueString);
                }
            } catch (NoSuchFieldException nsfe) {
                String errorMsg = "fromConfigXML(...) - Error occured while parsing XML <HardwareAddress> tag. "
                        + "The following variable does not exist in " + hwAddressClass.toString() + ": \""
                        + decodeFieldName(fieldName) + "\"";
                log.error(errorMsg);
                throw new IllegalArgumentException(errorMsg);
            } catch (IllegalAccessException iae) {
                iae.printStackTrace();
                throw new RuntimeException(iae);
            } catch (NumberFormatException npe) {
                String errorMsg = "fromConfigXML(...) - Error occured while parsing XML <HardwareAddress> tag. Field \""
                        + fieldName + "\" shall not be empty since we expect a \"" + fieldTypeName
                        + "\" value. Please correct the XML configuration for " + hwAddressClass.toString();
                log.error(errorMsg);
                throw new IllegalArgumentException(errorMsg);
            }
        }
    }
    return hwAddress;
}

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

@Override
public Iterable<String> list() throws CloudException, InternalException {
    APITrace.begin(provider, "KVDB.list");
    try {//from  w w  w  . j a va 2s.  co m
        ArrayList<String> list = new ArrayList<String>();
        String marker = null;

        do {
            Map<String, String> parameters = provider.getStandardSimpleDBParameters(provider.getContext(),
                    LIST_DOMAINS);
            EC2Method method;
            NodeList blocks;
            Document doc;

            if (marker != null) {
                parameters.put("NextToken", marker);
            }
            method = new EC2Method(SERVICE_ID, provider, parameters);
            try {
                doc = method.invoke();
            } catch (EC2Exception e) {
                throw new CloudException(e);
            }
            marker = null;
            blocks = doc.getElementsByTagName("NextToken");
            if (blocks.getLength() > 0) {
                for (int i = 0; i < blocks.getLength(); i++) {
                    Node item = blocks.item(i);

                    if (item.hasChildNodes()) {
                        marker = item.getFirstChild().getNodeValue().trim();
                    }
                }
                if (marker != null) {
                    break;
                }
            }
            blocks = doc.getElementsByTagName("DomainName");
            for (int i = 0; i < blocks.getLength(); i++) {
                Node name = blocks.item(i);

                if (name.hasChildNodes()) {
                    String domain = name.getFirstChild().getNodeValue();

                    if (domain != null) {
                        list.add(domain);
                    }
                }
            }
        } while (marker != null);
        return list;
    } finally {
        APITrace.end();
    }
}

From source file:DomPrintUtil.java

private boolean checkNewLine(Node target) {
    if (indent && target.hasChildNodes()) {
        short type = target.getFirstChild().getNodeType();
        if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
            return false;
        }//  w w  w .  j a va2 s  . co  m
        return true;
    }
    return false;
}