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.aws.platform.SimpleDB.java

@Override
public Iterable<String> list() throws CloudException, InternalException {
    APITrace.begin(provider, "KVDB.list");
    try {//w  ww .  j a va 2s . c om
        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:com.stratelia.webactiv.util.XMLConfigurationStore.java

public Node findNode(Node node, String name) {
    if (node.getNodeName().equals(name)) {
        return node;
    }//from   www . j  a  v a 2  s.  c  om
    if (node.hasChildNodes()) {
        NodeList list = node.getChildNodes();
        int size = list.getLength();
        for (int i = 0; i < size; i++) {
            Node found = findNode(list.item(i), name);
            if (found != null) {
                return found;
            }
        }
    }
    return null;
}

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;
        }/*from w  w w  .  j  a  va 2 s  .co  m*/
        return true;
    }
    return false;
}

From source file:com.inbravo.scribe.rest.service.crm.ms.MSCRMMessageFormatUtils.java

public static final List<Element> createEntityFromBusinessObject(final BusinessEntity businessEntity)
        throws Exception {

    /* Create list of elements */
    final List<Element> elementList = new ArrayList<Element>();

    if (businessEntity != null) {
        final Node node = businessEntity.getDomNode();
        if (node.hasChildNodes()) {
            final NodeList nodeList = node.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {

                /* To avoid : 'DOM Level 3 Not implemented' error */
                final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                final Document document = builder.newDocument();
                final Element element = (Element) document.importNode(nodeList.item(i), true);

                if (element.getNodeName() != null && element.getNodeName().contains(":")) {
                    final String nodeName = element.getNodeName().split(":")[1];

                    /* Check for attributes */
                    final NamedNodeMap attributes = element.getAttributes();

                    if (attributes != null && attributes.getLength() != 0) {

                        /* Create new map for attributes */
                        final Map<String, String> attributeMap = new HashMap<String, String>();

                        for (int j = 0; j < attributes.getLength(); j++) {
                            final Attr attr = (Attr) attributes.item(j);

                            /* Set node name and value in map */
                            attributeMap.put(attr.getNodeName(), attr.getNodeValue());
                        }//from w  w  w .ja  v  a  2s .  com

                        /* Create node with attributes */
                        elementList.add(MSCRMMessageFormatUtils.createMessageElement(nodeName,
                                element.getTextContent(), attributeMap));
                    } else {

                        /* Create node without attributes */
                        elementList.add(MSCRMMessageFormatUtils.createMessageElement(nodeName,
                                element.getTextContent()));
                    }
                }
            }
        }
        return elementList;
    } else {
        return null;
    }
}

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

@Override
public Map<String, Set<KeyValuePair>> query(String queryString, boolean consistentRead)
        throws CloudException, InternalException {
    APITrace.begin(provider, "KVDB.query");
    try {/* w ww. j a v  a2  s.  c  o  m*/
        Map<String, Set<KeyValuePair>> pairs = new HashMap<String, Set<KeyValuePair>>();
        String marker = null;

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

            if (marker != null) {
                parameters.put("NextToken", marker);
            }
            parameters.put("SelectExpression", queryString);
            parameters.put("ConsistentRead", String.valueOf(consistentRead));
            method = new EC2Method(SERVICE_ID, provider, parameters);
            try {
                doc = method.invoke();
            } catch (EC2Exception e) {
                String code = e.getCode();

                if (code != null && code.equals("NoSuchDomain")) {
                    return null;
                }
                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("Item");
            for (int i = 0; i < blocks.getLength(); i++) {
                Node item = blocks.item(i);

                if (item.hasChildNodes()) {
                    TreeSet<KeyValuePair> itemPairs = new TreeSet<KeyValuePair>();
                    NodeList children = item.getChildNodes();
                    String itemId = null;

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

                        if (child.hasChildNodes()) {
                            String nn = child.getNodeName();

                            if (nn.equals("Name")) {
                                itemId = child.getFirstChild().getNodeValue();
                            } else if (nn.equals("Attribute")) {
                                NodeList parts = child.getChildNodes();
                                String key = null, value = null;

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

                                    if (part.hasChildNodes()) {
                                        String nv = part.getFirstChild().getNodeValue();

                                        if (part.getNodeName().equals("Name")) {
                                            key = nv;
                                        } else if (part.getNodeName().equals("Value")) {
                                            value = nv;
                                        }
                                    }
                                }
                                if (key != null) {
                                    itemPairs.add(new KeyValuePair(key, value));
                                }
                            }
                        }
                    }
                    if (itemId != null) {
                        pairs.put(itemId, itemPairs);
                    }
                }
            }
        } while (marker != null);
        return pairs;
    } finally {
        APITrace.end();
    }
}

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

/**
 * Parse <query/> node./*from  w w  w . j a v a  2  s  .c om*/
 *
 * @param node node
 * @return {@link Query}
 */
private void parseQueryTag(Node node, QuerySet querySet) throws ParserException {
    if (node == null || !QUERY_TAG.equals(node.getNodeName())) {
        return;
    }
    Map<String, String> attributes = getAttributes(node);
    StringBuilder queryBodyBuilder = new StringBuilder();
    Query query = new Query(attributes.get(ID));
    query.setQueryType(QueryType.getByName(attributes.get(TYPE_ATTR)));
    query.setConverter(attributes.get(CONVERTER_CLASS_ATTR));
    query.setConverterMethod(attributes.get(CONVERTER_METHOD_ATTR));
    if (node.hasChildNodes()) {
        NodeList childList = node.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            Node child = childList.item(i);
            // parse query body
            parseBody(queryBodyBuilder, child, querySet);
            // parse <case> tag
            parseCaseTag(child, query, querySet);
        }
    }
    query.setBody(queryBodyBuilder.toString());

    if (!validate(wrap(query.getBody()))) {
        throw new ParserException(
                MessageFormatter.format(INVALID_QUERY_ERROR_MSG, query.getId(), query).getMessage());
    }
    querySet.addQuery(query);
}

From source file:org.dasein.cloud.ibm.sce.compute.disk.SCEDisk.java

@Override
public @Nonnull Iterable<VolumeProduct> listVolumeProducts() throws InternalException, CloudException {
    ArrayList<VolumeProduct> products = new ArrayList<VolumeProduct>();

    ProviderContext ctx = provider.getContext();

    if (ctx == null) {
        throw new SCEConfigException("No context was configured for this request");
    }/*from   w w w. ja v a2  s . c  o  m*/
    SCEMethod method = new SCEMethod(provider);

    Document xml = method.getAsXML("offerings/storage");

    if (xml == null) {
        throw new CloudException("No storage offerings exist");
    }
    NodeList nodes = xml.getElementsByTagName("Offerings");

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

        if (item.hasChildNodes()) {
            NodeList attrs = item.getChildNodes();
            String id = null, format = null;
            int[] sizes = new int[0];

            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                String n = attr.getNodeName();

                if (n.equalsIgnoreCase("ID")) {
                    id = attr.getFirstChild().getNodeValue().trim();
                } else if (n.equalsIgnoreCase("SupportedSizes")) {
                    String s = attr.getFirstChild().getNodeValue().trim();
                    String[] parts;

                    if (s.contains(",")) {
                        parts = s.split(",");
                    } else {
                        parts = new String[] { s };
                    }
                    sizes = new int[parts.length];
                    for (int k = 0; k < parts.length; k++) {
                        sizes[k] = Integer.parseInt(parts[k].trim());
                    }
                } else if (n.equalsIgnoreCase("SupportedFormats") && attr.hasChildNodes()) {
                    NodeList formats = attr.getChildNodes();

                    for (int k = 0; k < formats.getLength(); k++) {
                        Node fmt = formats.item(k);

                        if (fmt.getNodeName().equalsIgnoreCase("Format") && fmt.hasChildNodes()) {
                            NodeList fa = fmt.getChildNodes();

                            for (int l = 0; l < fa.getLength(); l++) {
                                Node fan = fa.item(l);

                                if (fan.getNodeName().equalsIgnoreCase("ID") && fan.hasChildNodes()) {
                                    format = fan.getFirstChild().getNodeValue().trim();
                                    if (!format.equalsIgnoreCase("RAW")) {
                                        format = null;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (sizes.length > 0 && format != null) {
                products.add(VolumeProduct.getInstance(id, id + " - " + format,
                        id + " - " + format + " - " + sizes[0], VolumeType.HDD,
                        new Storage<Gigabyte>(sizes[0], Storage.GIGABYTE)));
            }
        }
    }
    return products;
}

From source file:org.dasein.cloud.ibm.sce.compute.disk.SCEDisk.java

private @Nullable ResourceStatus toStatus(@Nullable Node node) throws CloudException, InternalException {
    if (node == null || !node.hasChildNodes()) {
        return null;
    }/*  ww w  . j  a  va  2 s .  c o m*/
    NodeList attributes = node.getChildNodes();
    VolumeState state = null;
    String volumeId = null;

    for (int i = 0; i < attributes.getLength(); i++) {
        Node attr = attributes.item(i);
        String nodeName = attr.getNodeName();

        if (nodeName.equalsIgnoreCase("ID") && attr.hasChildNodes()) {
            volumeId = attr.getFirstChild().getNodeValue().trim();
        } else if (nodeName.equalsIgnoreCase("State") && attr.hasChildNodes()) {
            String status = attr.getFirstChild().getNodeValue().trim();

            state = toState(status);
        }
        if (volumeId != null && state != null) {
            break;
        }
    }
    if (volumeId == null) {
        return null;
    }
    return new ResourceStatus(volumeId, state == null ? VolumeState.PENDING : state);
}

From source file:org.dasein.cloud.ibm.sce.compute.disk.SCEDisk.java

private SCEOffering findOffering(@Nonnull String productId) throws InternalException, CloudException {
    SCEOffering offering;//w w  w  .  ja  v  a  2 s  . c om

    ProviderContext ctx = provider.getContext();

    if (ctx == null) {
        throw new SCEConfigException("No context was configured for this request");
    }
    SCEMethod method = new SCEMethod(provider);

    Document xml = method.getAsXML("offerings/storage");

    if (xml == null) {
        throw new CloudException("No storage offerings exist");
    }
    NodeList nodes = xml.getElementsByTagName("Offerings");

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

        if (item.hasChildNodes()) {
            NodeList attrs = item.getChildNodes();
            String id = null, format = null;
            int[] sizes = new int[0];

            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                String n = attr.getNodeName();

                if (n.equalsIgnoreCase("ID")) {
                    id = attr.getFirstChild().getNodeValue().trim();
                } else if (n.equalsIgnoreCase("SupportedSizes")) {
                    String s = attr.getFirstChild().getNodeValue().trim();
                    String[] parts;

                    if (s.contains(",")) {
                        parts = s.split(",");
                    } else {
                        parts = new String[] { s };
                    }
                    sizes = new int[parts.length];
                    for (int k = 0; k < parts.length; k++) {
                        sizes[k] = Integer.parseInt(parts[k].trim());
                    }
                } else if (n.equalsIgnoreCase("SupportedFormats") && attr.hasChildNodes()) {
                    NodeList formats = attr.getChildNodes();

                    for (int k = 0; k < formats.getLength(); k++) {
                        Node fmt = formats.item(k);

                        if (fmt.getNodeName().equalsIgnoreCase("Format") && fmt.hasChildNodes()) {
                            NodeList fa = fmt.getChildNodes();

                            for (int l = 0; l < fa.getLength(); l++) {
                                Node fan = fa.item(l);

                                if (fan.getNodeName().equalsIgnoreCase("ID") && fan.hasChildNodes()) {
                                    format = fan.getFirstChild().getNodeValue().trim();
                                    if (!format.equalsIgnoreCase("RAW")) {
                                        format = null;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (id == null || !id.equals(productId)) {
                continue;
            }
            offering = new SCEOffering();
            offering.format = format;
            offering.offeringId = id;
            offering.size = sizes[0];
            return offering;
        }
    }
    return null;
}

From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java

private static HashMap<String, Object> parseHtmlTable(Node table_node, String replaceWith) {
    if (table_node.getNodeName().equalsIgnoreCase("table") && table_node.hasChildNodes()) {
        Node topNode = table_node;

        boolean tbody = table_node.getFirstChild().getNodeName().equalsIgnoreCase("tbody");

        if (tbody)
            topNode = table_node.getFirstChild();

        if (topNode.hasChildNodes()) {
            NodeList rows = topNode.getChildNodes();

            List<String> headers = null;
            ArrayList<HashMap<String, String>> data = null;
            int headerLength = 0;
            boolean[] skip = null;

            if (null != replaceWith) {
                if (replaceWith.equals("[]")) {
                    headers = new ArrayList<String>();
                    headerLength = 0;//from   www . j  a  v  a2 s.  c  om
                } // TESTED (by eye - 2)
                else {
                    //Remove square brackets
                    if (replaceWith.startsWith("[") && replaceWith.endsWith("]"))
                        replaceWith = replaceWith.substring(1, replaceWith.length() - 1);
                    //Turn the provided list of headers into a list object
                    headers = Arrays.asList(replaceWith.split("\\s*,\\s*"));
                    headerLength = headers.size();
                    skip = new boolean[headerLength];
                    for (int h = 0; h < headerLength; h++) {
                        String val = headers.get(h);
                        if (val.length() == 0 || val.equalsIgnoreCase("null"))
                            skip[h] = true;
                        else
                            skip[h] = false;
                    }

                } // TESTED (by eye - 3a)
            }

            //traverse rows
            for (int i = 0; i < rows.getLength(); i++) {
                Node row = rows.item(i);
                if (row.getNodeName().equalsIgnoreCase("tr") || row.getNodeName().equalsIgnoreCase("th")) {
                    //If the header value has not been set, the first row will be set as the headers
                    if (null == headers) {
                        //Traverse through cells
                        headers = new ArrayList<String>();
                        if (row.hasChildNodes()) {
                            NodeList cells = row.getChildNodes();
                            headerLength = cells.getLength();
                            skip = new boolean[headerLength];
                            for (int j = 0; j < headerLength; j++) {
                                headers.add(cells.item(j).getTextContent());
                                skip[j] = false;
                            }
                        } // TESTED (by eye - 1)
                    } else {
                        if (null == data) {
                            data = new ArrayList<HashMap<String, String>>();
                        }
                        if (row.hasChildNodes()) {
                            HashMap<String, String> cellList = new HashMap<String, String>();
                            NodeList cells = row.getChildNodes();
                            for (int j = 0; j < cells.getLength(); j++) {
                                // Skip Code (TESTED by eye - 4)
                                if (headerLength == 0 || (j < headerLength && skip[j] == false)) {
                                    String key = Integer.toString(j); // TESTED (by eye - 3b)
                                    if (j < headerLength)
                                        key = headers.get(j);

                                    cellList.put(key, cells.item(j).getTextContent());
                                }
                            }
                            data.add(cellList);
                        }

                    }
                }
            }
            //Create final hashmap containing attributes
            HashMap<String, Object> table_attrib = new HashMap<String, Object>();

            NamedNodeMap nnm = table_node.getAttributes();
            for (int i = 0; i < nnm.getLength(); i++) {
                Node att = nnm.item(i);
                table_attrib.put(att.getNodeName(), att.getNodeValue());
            }
            table_attrib.put("table", data);

            //TESTED (by eye) attributes added to table value
            // eg: {"id":"search","cellpadding":"1","table":[{"Status":"B","two":"ONE6313" ......

            return table_attrib;
        }
    }
    return null;
}