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:org.dasein.cloud.vcloud.compute.vAppSupport.java

private @Nullable VirtualMachine toVirtualMachine(@Nonnull String vdcId, @Nonnull String parentVAppId,
        @Nonnull Node vmNode, @Nonnull Iterable<VLAN> vlans) throws CloudException, InternalException {
    Node n = vmNode.getAttributes().getNamedItem("href");
    VirtualMachine vm = new VirtualMachine();

    vm.setProviderMachineImageId("unknown");
    vm.setArchitecture(Architecture.I64);
    vm.setClonable(true);//from  w  w w . j  ava  2s  . c  o m
    vm.setCreationTimestamp(0L);
    vm.setCurrentState(VmState.PENDING);
    vm.setImagable(true);
    vm.setLastBootTimestamp(0L);
    vm.setLastPauseTimestamp(0L);
    vm.setPausable(false);
    vm.setPersistent(true);
    vm.setPlatform(Platform.UNKNOWN);
    vm.setProviderOwnerId(getContext().getAccountNumber());
    vm.setRebootable(true);
    vm.setProviderRegionId(getContext().getRegionId());
    vm.setProviderDataCenterId(vdcId);

    if (n != null) {
        vm.setProviderVirtualMachineId((getProvider()).toID(n.getNodeValue().trim()));
    }
    n = vmNode.getAttributes().getNamedItem("status");
    if (n != null) {
        vm.setCurrentState(toState(n.getNodeValue().trim()));
    }
    String vmName = null;
    String computerName = null;
    n = vmNode.getAttributes().getNamedItem("name");
    if (n != null) {
        vmName = n.getNodeValue().trim();
    }
    NodeList attributes = vmNode.getChildNodes();

    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (attribute.getNodeName().equalsIgnoreCase("Description") && attribute.hasChildNodes()) {
            vm.setDescription(attribute.getFirstChild().getNodeValue().trim());
        } else if (attribute.getNodeName().equalsIgnoreCase("GuestCustomizationSection")
                && attribute.hasChildNodes()) {
            NodeList elements = attribute.getChildNodes();
            String adminPassword = null;

            for (int j = 0; j < elements.getLength(); j++) {
                Node element = elements.item(j);

                if (element.getNodeName().equalsIgnoreCase("AdminPassword") && element.hasChildNodes()) {
                    adminPassword = element.getFirstChild().getNodeValue().trim();
                } else if (element.getNodeName().equalsIgnoreCase("ComputerName") && element.hasChildNodes()) {
                    computerName = element.getFirstChild().getNodeValue().trim();
                }
            }
            if (adminPassword != null) {
                vm.setRootUser(vm.getPlatform().isWindows() ? "administrator" : "root");
                vm.setRootPassword(adminPassword);
            }
        } else if (attribute.getNodeName().equalsIgnoreCase("DateCreated") && attribute.hasChildNodes()) {
            vm.setCreationTimestamp((getProvider()).parseTime(attribute.getFirstChild().getNodeValue().trim()));
        } else if (attribute.getNodeName().equalsIgnoreCase("NetworkConnectionSection")
                && attribute.hasChildNodes()) {
            NodeList elements = attribute.getChildNodes();
            TreeSet<String> addrs = new TreeSet<String>();

            for (int j = 0; j < elements.getLength(); j++) {
                Node element = elements.item(j);

                if (element.getNodeName().equalsIgnoreCase("NetworkConnection")) {
                    if (element.hasChildNodes()) {
                        NodeList parts = element.getChildNodes();
                        Boolean connected = null;
                        String addr = null;

                        for (int k = 0; k < parts.getLength(); k++) {
                            Node part = parts.item(k);

                            if (part.getNodeName().equalsIgnoreCase("IpAddress") && part.hasChildNodes()) {
                                addr = part.getFirstChild().getNodeValue().trim();
                            }
                            if (part.getNodeName().equalsIgnoreCase("IsConnected") && part.hasChildNodes()) {
                                connected = part.getFirstChild().getNodeValue().trim().equalsIgnoreCase("true");
                            }
                        }
                        if ((connected == null || connected) && addr != null) {
                            addrs.add(addr);
                        }
                    }
                    if (element.hasAttributes()) {
                        Node net = element.getAttributes().getNamedItem("network");

                        if (net != null) {
                            String netNameOrId = net.getNodeValue().trim();
                            boolean compat = (getProvider()).isCompat();

                            for (VLAN vlan : vlans) {
                                boolean matches = false;

                                if (!compat && vlan.getProviderVlanId().equals(netNameOrId)) {
                                    matches = true;
                                } else if (compat
                                        && vlan.getProviderVlanId().equals("/network/" + netNameOrId)) {
                                    matches = true;
                                } else if (vlan.getName().equals(netNameOrId)) {
                                    matches = true;
                                }
                                if (matches) {
                                    vm.setProviderVlanId(vlan.getProviderVlanId());
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (addrs.size() > 0) {
                if (addrs.size() == 1) {
                    RawAddress a = new RawAddress(addrs.iterator().next());

                    if (isPublicIpAddress(a)) {
                        vm.setPublicAddresses(a);
                    } else {
                        vm.setPrivateAddresses(a);
                    }
                } else {
                    ArrayList<RawAddress> pub = new ArrayList<RawAddress>();
                    ArrayList<RawAddress> priv = new ArrayList<RawAddress>();

                    for (String addr : addrs) {
                        RawAddress r = new RawAddress(addr);

                        if (isPublicIpAddress(r)) {
                            pub.add(r);
                        } else {
                            priv.add(r);
                        }
                    }
                    if (priv.size() > 0) {
                        vm.setPrivateAddresses(priv.toArray(new RawAddress[priv.size()]));
                    }
                    if (pub.size() > 0) {
                        vm.setPublicAddresses(pub.toArray(new RawAddress[pub.size()]));
                    }
                }
            }
        } else if (attribute.getNodeName().equalsIgnoreCase("ovf:OperatingSystemSection")
                && attribute.hasChildNodes()) {
            NodeList os = attribute.getChildNodes();

            for (int j = 0; j < os.getLength(); j++) {
                Node osdesc = os.item(j);

                if (osdesc.getNodeName().equalsIgnoreCase("ovf:Description") && osdesc.hasChildNodes()) {
                    String desc = osdesc.getFirstChild().getNodeValue();

                    vm.setPlatform(Platform.guess(desc));

                    if (desc.contains("32") || (desc.contains("x86") && !desc.contains("64"))) {
                        vm.setArchitecture(Architecture.I32);
                    }
                }
            }
        } else if (attribute.getNodeName().equalsIgnoreCase("ovf:VirtualHardwareSection")
                && attribute.hasChildNodes()) {
            NodeList hardware = attribute.getChildNodes();
            int memory = 0, cpu = 0;

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

                if (item.getNodeName().equalsIgnoreCase("ovf:item") && item.hasChildNodes()) {
                    NodeList bits = item.getChildNodes();
                    String rt = null;
                    int qty = 0;

                    for (int k = 0; k < bits.getLength(); k++) {
                        Node bit = bits.item(k);

                        if (bit.getNodeName().equalsIgnoreCase("rasd:ResourceType") && bit.hasChildNodes()) {
                            rt = bit.getFirstChild().getNodeValue().trim();
                        } else if (bit.getNodeName().equalsIgnoreCase("rasd:VirtualQuantity")
                                && bit.hasChildNodes()) {
                            try {
                                qty = Integer.parseInt(bit.getFirstChild().getNodeValue().trim());
                            } catch (NumberFormatException ignore) {
                                // ignore
                            }
                        }

                    }
                    if (rt != null) {
                        if (rt.equals("3")) { // cpu
                            cpu = qty;
                        } else if (rt.equals("4")) { // memory
                            memory = qty;
                        }
                        /*
                        else if( rt.equals("10") ) {     // NIC
                                
                        }
                        else if( rt.equals("17") ) {     // disk
                                
                        }
                        */
                    }
                }
            }
            VirtualMachineProduct product = null;

            for (VirtualMachineProduct prd : listProducts("bogus",
                    VirtualMachineProductFilterOptions.getInstance().withArchitecture(Architecture.I64))) {
                if (prd.getCpuCount() == cpu && memory == prd.getRamSize().intValue()) {
                    product = prd;
                    break;
                }
            }
            if (product == null) {
                vm.setProductId("custom:" + cpu + ":" + memory);
            } else {
                vm.setProductId(product.getProviderProductId());
            }
        }
    }
    if (vm.getProviderVirtualMachineId() == null) {
        return null;
    }
    if (vmName != null) {
        vm.setName(vmName);
    } else if (computerName != null) {
        vm.setName(computerName);
    } else {
        vm.setName(vm.getProviderVirtualMachineId());
    }
    if (vm.getDescription() == null) {
        vm.setDescription(vm.getName());
    }
    Platform p = vm.getPlatform();

    if (p == null || p.equals(Platform.UNKNOWN) || p.equals(Platform.UNIX)) {
        p = Platform.guess(vm.getName() + " " + vm.getDescription());
        if (Platform.UNIX.equals(vm.getPlatform())) {
            if (p.isUnix()) {
                vm.setPlatform(p);
            }
        } else {
            vm.setPlatform(p);
        }
    }
    try {
        vCloudMethod method = new vCloudMethod(getProvider());
        String xml = method.get("vApp", vm.getProviderVirtualMachineId() + "/metadata");

        if (xml != null && !xml.equals("")) {
            method.parseMetaData(vm, xml);

            String t;

            if (vm.getCreationTimestamp() < 1L) {
                t = (String) vm.getTag("dsnCreated");
                if (t != null) {
                    try {
                        vm.setCreationTimestamp(Long.parseLong(t));
                    } catch (Throwable parseWarning) {
                        if (logger.isDebugEnabled()) {
                            logger.warn("Failed to parse creation timestamp.", parseWarning);
                        } else {
                            logger.warn("Failed to parse creation timestamp.");
                        }
                    }
                }
            }
            t = (String) vm.getTag("dsnImageId");
            logger.debug("dsnImageId = " + t);
            if (t != null && "unknown".equals(vm.getProviderMachineImageId())) {
                vm.setProviderMachineImageId(t);
                logger.debug("Set provider machine image to " + t);
            }
        }
    } catch (Throwable warning) {
        if (logger.isDebugEnabled()) {
            logger.warn("Failed to get and parse vm metadata.", warning);
        } else {
            logger.warn("Failed to get and parse vm metadata.");
        }
    }
    vm.setTag(PARENT_VAPP_ID, parentVAppId);
    return vm;
}

From source file:org.dspace.administer.StructBuilder.java

/**
 * Return the String value of a Node/*w w w  .  ja va  2s. com*/
 * 
 * @param node the node from which we want to extract the string value
 * 
 * @return the string value of the node
 */
public static String getStringValue(Node node) {
    String value = node.getNodeValue();

    if (node.hasChildNodes()) {
        Node first = node.getFirstChild();

        if (first.getNodeType() == Node.TEXT_NODE) {
            return first.getNodeValue().trim();
        }
    }

    return value;
}

From source file:org.dspace.app.itemimport.ItemImport.java

/**
 * Return the String value of a Node.//w  w w . ja  v a2  s .  c  om
 * @param node
 * @return
 */
private String getStringValue(Node node) {
    String value = node.getNodeValue();

    if (node.hasChildNodes()) {
        Node first = node.getFirstChild();

        if (first.getNodeType() == Node.TEXT_NODE) {
            return first.getNodeValue();
        }
    }

    return value;
}

From source file:org.dspace.app.itemimport.ItemImportServiceImpl.java

/**
 * Return the String value of a Node./*from ww w.  ja v a  2s .co m*/
 * @param node node
 * @return string value
 */
protected String getStringValue(Node node) {
    String value = node.getNodeValue();

    if (node.hasChildNodes()) {
        Node first = node.getFirstChild();

        if (first.getNodeType() == Node.TEXT_NODE) {
            return first.getNodeValue();
        }
    }

    return value;
}

From source file:org.dspace.app.itemupdate.MetadataUtilities.java

/**
 * Return the String value of a Node.// w  ww . j ava 2  s. co  m
 * @param node
 * @return
 */
private static String getStringValue(Node node) {
    String value = node.getNodeValue();

    if (node.hasChildNodes()) {
        Node first = node.getFirstChild();

        if (first.getNodeType() == Node.TEXT_NODE) {
            return first.getNodeValue();
        }
    }

    return value;
}

From source file:org.ebayopensource.turmeric.eclipse.resources.util.SOAIntfUtil.java

/**
 * Gets the service version from wsdl.//from  w ww.  jav a2 s .c o m
 *
 * @param wsdl the wsdl
 * @param publicServiceName the public service name
 * @return the service version from wsdl
 */
public static String getServiceVersionFromWsdl(Definition wsdl, String publicServiceName) {
    if (wsdl == null) {
        return "";
    }

    for (Object obj : wsdl.getServices().values()) {
        final Service service = (Service) obj;
        if (service.getQName().getLocalPart().equals(publicServiceName)) {
            if (service.getDocumentationElement() != null) {
                final Element elem = service.getDocumentationElement();
                for (int i = 0; i < elem.getChildNodes().getLength(); i++) {
                    Node node = elem.getChildNodes().item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE
                            && (node.getLocalName().equals(ELEM_NAME_VERSION_V2_CAMEL_CASE)
                                    || node.getLocalName().equals(ELEM_NAME_VERSION_V3_CAMEL_CASE))) {
                        if (node.hasChildNodes()) {
                            return node.getFirstChild().getNodeValue();
                        }
                    }
                }
            }
        }
    }

    return "";
}

From source file:org.eclipse.ecr.common.xmap.DOMHelper.java

/**
 * Parses a string containing XML and returns a DocumentFragment containing
 * the nodes of the parsed XML./*from  w  ww .j a v a2  s .  c o  m*/
 */
public static void loadFragment(Element el, String fragment) {
    // Wrap the fragment in an arbitrary element
    fragment = "<fragment>" + fragment + "</fragment>";
    try {
        // Create a DOM builder and parse the fragment
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment)));

        Document doc = el.getOwnerDocument();

        // Import the nodes of the new document into doc so that they
        // will be compatible with doc
        Node node = doc.importNode(d.getDocumentElement(), true);

        // Create the document fragment node to hold the new nodes
        DocumentFragment docfrag = doc.createDocumentFragment();

        // Move the nodes into the fragment
        while (node.hasChildNodes()) {
            el.appendChild(node.removeChild(node.getFirstChild()));
        }

    } catch (Exception e) {
        log.error(e, e);
    }
}

From source file:org.eclipse.skalli.core.rest.ProjectsV1V2Diff.java

protected boolean hasEmptyChildNode(NodeDetail nodeDetails, String name) {
    Node node = nodeDetails.getNode().getFirstChild();
    while (node != null) {
        if (name.equals(node.getNodeName())) {
            return !node.hasChildNodes();
        }/*from w  w w .j  ava2 s .  c  o  m*/
        node = node.getNextSibling();
    }
    return false;
}

From source file:org.eclipse.swordfish.plugins.compression.CompressorImpl.java

public boolean isSourceEmpty(Source src) {
    if (src == null) {
        return true;
    } else {//from w w w .j  a v  a 2s .co  m
        if (src instanceof DOMSource) {
            Node node = ((DOMSource) src).getNode();
            if (node == null) {
                return true;
            }
            if (node instanceof Document && !node.hasChildNodes()) {
                return true;
            }
        } else if (src instanceof StreamSource) {
            InputStream is = ((StreamSource) src).getInputStream();
            if (is == null) {
                return true;
                // should probably also check if the stream has data available
            }
        }
    }
    return false;
}

From source file:org.eclipse.wst.xsl.xalan.debugger.XalanVariable.java

private String createElement(String value, Node node) {
    value = value + "<";
    //      if (node.getPrefix() != null && node.getPrefix().length() > 0) {
    //         value = value + node.getPrefix() + ":";
    //      }/*from w  w w.j  av a2 s  .c o  m*/
    if (node.getNodeName() != null) {
        value = value + node.getNodeName();
        if (node.hasAttributes()) {
            NamedNodeMap attr = node.getAttributes();
            value = value + buildAttributes(attr);
        }
        value = value + ">";
        if (node.getNodeValue() != null) {
            value = value + node.getNodeValue();
        }
    }
    if (node.hasChildNodes()) {
        value = value + processNodeList(node.getChildNodes());
    }
    value = value + "</" + node.getNodeName() + ">";
    return value;
}