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.openhab.habdroid.model.OpenHABSitemapPage.java

public OpenHABSitemapPage(Document document) {
    Node rootNode = document.getFirstChild();
    if (rootNode == null)
        return;/* ww w.j  av  a2s  . com*/
    mRootWidget = new OpenHAB1Widget();
    mRootWidget.setType("root");
    if (rootNode.hasChildNodes()) {
        NodeList childNodes = rootNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeName().equals("widget")) {
                OpenHABWidget newOpenHABWidget = OpenHAB1Widget.createOpenHABWidgetFromNode(mRootWidget,
                        childNode);
                mWidgets.add(newOpenHABWidget);
            } else if (childNode.getNodeName().equals("title")) {
                this.setTitle(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("id")) {
                this.setPageId(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("icon")) {
                this.setIcon(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("link")) {
                this.setLink(childNode.getTextContent());
            }
        }
    }

}

From source file:org.openhab.habdroid.model.OpenHABWidgetDataSource.java

public void setSourceNode(Node rootNode) {
    Log.i(TAG, "Loading new data");
    if (rootNode == null)
        return;//w w w  . ja v a  2s.c om
    rootWidget = new OpenHAB1Widget();
    rootWidget.setType("root");
    if (rootNode.hasChildNodes()) {
        NodeList childNodes = rootNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeName().equals("widget")) {
                OpenHAB1Widget.createOpenHABWidgetFromNode(rootWidget, childNode);
            } else if (childNode.getNodeName().equals("title")) {
                this.setTitle(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("id")) {
                this.setId(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("icon")) {
                this.setIcon(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("link")) {
                this.setLink(childNode.getTextContent());
            }
        }
    }
}

From source file:org.openhealthtools.openatna.net.DescriptionLoader.java

/**
 * Get the text included within an XML DOM element
 *
 * @param node The XML DOM node holding the text
 * @return The text/*  w  ww  . j  a  v  a  2 s . co m*/
 */
private static String getNodeAsText(Node node) {
    if (!node.hasChildNodes()) {
        return null;
    }
    Text nodeTextContents = (Text) node.getFirstChild();
    return nodeTextContents.getData();
}

From source file:org.openhealthtools.openexchange.actorconfig.ActorConfigurationLoader.java

/**
 * Gets the text included within an XML DOM element
 * /*www  . j a  v a 2 s  . c om*/
 * @param node the XML DOM node holding the text
 * @return the text
 */
private String getNodeAsText(Node node) {
    if (!node.hasChildNodes())
        return null;
    Text nodeTextContents = (Text) node.getFirstChild();
    return nodeTextContents.getData();
}

From source file:org.openhealthtools.openexchange.actorconfig.net.DescriptionLoader.java

/**
 * Gets the text included within an XML DOM element
 *
 * @param node The XML DOM node holding the text
 * @return The text//w  w w. java2  s .  c  o m
 */
protected static String getNodeAsText(Node node) {
    if (!node.hasChildNodes()) {
        return null;
    }
    Text nodeTextContents = (Text) node.getFirstChild();
    return nodeTextContents.getData();
}

From source file:org.openhealthtools.openexchange.actorconfig.net.DescriptionLoader.java

/**
 * Gets the text included within an XML DOM element
 *
 * @param node The XML DOM node holding the text
 * @return The text/*from  w  w  w.j ava  2s .  c  o m*/
 */
protected static String getChildNodeAsText(Node node, String childElemName) {
    if (!node.hasChildNodes()) {
        return null;
    }
    if (childElemName == null) {
        return null;
    }
    NodeList children = node.getChildNodes();
    for (int valueIndex = 0; valueIndex < children.getLength(); valueIndex++) {
        Node item = children.item(valueIndex);
        String itemName = item.getNodeName();
        if (childElemName.equalsIgnoreCase(itemName)) {
            return getNodeAsText(item);
        }
    }

    return null;
}

From source file:org.openmrs.module.muzima.handler.XmlEncounterQueueDataHandler.java

private Node findSubNode(final String name, final Node node) {
    if (!node.hasChildNodes()) {
        return null;
    }/*from w w  w. j a v a 2 s.c  o m*/
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node subNode = list.item(i);
        if (subNode.getNodeType() == Node.ELEMENT_NODE) {
            if (subNode.getNodeName().equals(name))
                return subNode;
        }
    }
    return null;
}

From source file:org.openmrs.module.muzima.handler.XmlEncounterQueueDataHandler.java

private void processObs(final Encounter encounter, final NodeList obsNodeList) throws QueueProcessorException {
    Node obsNode = obsNodeList.item(0);
    NodeList obsElementNodes = obsNode.getChildNodes();
    for (int i = 0; i < obsElementNodes.getLength(); i++) {
        Node obsElementNode = obsElementNodes.item(i);
        // skip all top level obs nodes without child element or without attribute
        // no attribute: temporary elements
        // no child: element with no answer
        if (obsElementNode.hasAttributes() && obsElementNode.hasChildNodes()) {
            processObsNode(encounter, null, obsElementNode);
        }/*from w  ww.  j av a 2  s . c om*/
    }
}

From source file:org.openxdata.server.export.rdbms.engine.DataBuilder.java

private void createStatements(boolean update, Document schemaDocument, Node tableElement, FormData formData,
        List<DataQuery> statements, String parentId, List<String> tableNames) {

    Map<String, Object> columnValues = new HashMap<String, Object>();
    String tableName = tableElement.getNodeName();

    if (tableElement.hasChildNodes() && Functions.hasValidChildNodes(tableElement)) {
        if (tableName != null && tableName.trim().length() > 0) {
            tableNames.add(tableName);//from w  w  w  . jav a  2s. c  o m

            // initialise generic columns
            String id = tableName;
            if (!update) {
                id = UUID.randomUUID().toString();
                columnValues.put("Id", id);
                columnValues.put("openxdata_form_data_id", String.valueOf(formData.getFormDataId()));
            }
            columnValues.put("openxdata_user_name", formData.getCreator().getName());
            columnValues.put("openxdata_user_id", String.valueOf(formData.getCreator().getUserId()));

            // initialise the columns specific to this form data (or form data child)
            NodeList columnElements = tableElement.getChildNodes();
            if (columnElements != null && columnElements.getLength() > 0) {
                for (int index = 0; index < columnElements.getLength(); index++) {
                    Node columnElement = columnElements.item(index);
                    if (Functions.isValidNode(columnElement)) {
                        Functions.cleanNode(columnElement);
                        if (Functions.isRepeat(schemaDocument, columnElement)) {
                            // if the column element is a repeat
                            log.debug(tableName + ">column repeat '" + columnElement.getNodeName() + "'");
                            createStatements(update, schemaDocument, columnElement, formData, statements, id,
                                    tableNames);
                        } else if (columnElement.hasChildNodes()
                                && Functions.hasValidChildNodes(columnElement)) {
                            // if there are child nodes, go through them
                            log.debug(tableName + ">column with children " + columnElement.getNodeName());
                            NodeList children = columnElement.getChildNodes();
                            if (children != null && children.getLength() > 0) {
                                for (int child = 0; child < children.getLength(); child++) {
                                    Node childElement = children.item(child);
                                    if (Functions.isRepeat(schemaDocument, childElement)) {
                                        // if the column child element is a repeat
                                        log.debug(tableName + ">child repeat '" + childElement.getNodeName()
                                                + "'");
                                        createStatements(update, schemaDocument, childElement, formData,
                                                statements, id, tableNames);
                                    } else if (Functions.isValidNode(childElement)) {
                                        log.debug(tableName + ">child ordinary " + childElement.getNodeName());
                                        Object columnValue = getColumnValue(schemaDocument, childElement);
                                        columnValues.put(childElement.getNodeName(), columnValue);
                                    }
                                }
                            }
                        } else {
                            // ordinary column with no child nodes
                            log.debug(tableName + ">ordinary column " + columnElement.getNodeName());
                            Object columnValue = getColumnValue(schemaDocument, columnElement);
                            columnValues.put(columnElement.getNodeName(), columnValue);
                        }
                    }
                }
            }

            // now create the statement (either update or insert) and insert into the list
            DataQuery stmt = null;
            if (update) {
                stmt = createUpdateStatement(tableName, formData.getFormDataId(), parentId, columnValues);
            } else {
                stmt = createInsertStatement(tableName, parentId, columnValues);
            }
            if (log.isDebugEnabled()) {
                log.debug(tableName + ">sql=" + stmt.getSql() + " parameters=" + stmt.getParameters());
            }
            statements.add(0, stmt);
        }
    }
}

From source file:org.structr.web.maintenance.deploy.PageImportVisitor.java

/**
 * Remove duplicate Head element from import process.
 * @param page//  ww w  . j a v  a  2  s .c  om
 */
private void fixDocumentElements(final Page page) {

    final NodeList heads = page.getElementsByTagName("head");
    if (heads.getLength() > 1) {

        final Node head1 = heads.item(0);
        final Node head2 = heads.item(1);
        final Node parent = head1.getParentNode();

        final boolean h1 = head1.hasChildNodes();
        final boolean h2 = head2.hasChildNodes();

        if (h1 && h2) {

            // merge
            for (Node child = head2.getFirstChild(); child != null; child = child.getNextSibling()) {

                head2.removeChild(child);
                head1.appendChild(child);
            }

            parent.removeChild(head2);

        } else if (h1 && !h2) {

            // remove head2
            parent.removeChild(head2);

        } else if (!h1 && h2) {

            // remove head1
            parent.removeChild(head1);

        } else {

            // remove first, doesn't matter
            parent.removeChild(head1);
        }
    }
}