Example usage for org.w3c.dom Element hasChildNodes

List of usage examples for org.w3c.dom Element hasChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Element hasChildNodes.

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

From source file:org.kuali.test.utils.Utils.java

public static Element getFirstChildNodeByNodeNameAndAttribute(Element parent, String nodeName,
        String attributeName, String attributeValue) {
    Element retval = null;/*from  w ww  .ja va2 s. c o  m*/
    Element firstChildElement = null;
    if (parent.hasChildNodes()) {
        NodeList nl = parent.getChildNodes();

        for (int i = 0; i < nl.getLength(); ++i) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element curElement = (Element) nl.item(i);

                // if the first child element is a span, div or nobr then we will save it
                // if we do not find the desired child we will look in the first child span
                if ((firstChildElement == null)
                        && (Constants.HTML_TAG_TYPE_SPAN.equalsIgnoreCase(curElement.getTagName())
                                || Constants.HTML_TAG_TYPE_DIV.equalsIgnoreCase(curElement.getTagName())
                                || Constants.HTML_TAG_TYPE_NOBR.equalsIgnoreCase(curElement.getTagName()))) {
                    firstChildElement = curElement;
                }

                if (curElement.getNodeName().equalsIgnoreCase(nodeName)) {
                    if (StringUtils.isBlank(attributeName) && StringUtils.isBlank(attributeValue)) {
                        retval = curElement;
                        break;
                    } else if (attributeValue.equalsIgnoreCase(curElement.getAttribute(attributeName))) {
                        retval = curElement;
                        break;
                    }
                }
            }
        }
    }

    if ((retval == null) && (firstChildElement != null)) {
        retval = getFirstChildNodeByNodeNameAndAttribute(firstChildElement, nodeName, attributeName,
                attributeValue);
    }

    return retval;
}

From source file:org.odk.aggregate.parser.SubmissionParser.java

/**
 * Recursive function that prints the nodes from an XML tree
 * //from  w ww .  j a va 2s  . c  o  m
 * @param node xml node to be recursively printed
 */
private void printNode(Element node) {
    System.out.println(ParserConsts.NODE_FORMATTED + node.getTagName());
    if (node.hasAttributes()) {
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            System.out.println(ParserConsts.ATTRIBUTE_FORMATTED + attr.getNodeName() + BasicConsts.EQUALS
                    + attr.getNodeValue());
        }
    }
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                printNode((Element) child);
            } else if (child.getNodeType() == Node.TEXT_NODE) {
                String value = child.getNodeValue().trim();
                if (value.length() > 0) {
                    System.out.println(ParserConsts.VALUE_FORMATTED + value);
                }
            }
        }
    }

}

From source file:org.opendatakit.aggregate.parser.SubmissionParser.java

/**
 * Recursive function that prints the nodes from an XML tree
 * //from  ww w . j  a v a  2 s  .  c o m
 * @param node
 *          xml node to be recursively printed
 */
@SuppressWarnings("unused")
private void printNode(Element node) {
    System.out.println(ParserConsts.NODE_FORMATTED + node.getTagName());
    if (node.hasAttributes()) {
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            System.out.println(ParserConsts.ATTRIBUTE_FORMATTED + attr.getNodeName() + BasicConsts.EQUALS
                    + attr.getNodeValue());
        }
    }
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                printNode((Element) child);
            } else if (child.getNodeType() == Node.TEXT_NODE) {
                String value = child.getNodeValue().trim();
                if (value.length() > 0) {
                    System.out.println(ParserConsts.VALUE_FORMATTED + value);
                }
            }
        }
    }

}

From source file:org.openestate.io.openimmo.converters.OpenImmo_1_2_5.java

/**
 * Upgrade &lt;energiepass&gt; elements to OpenImmo 1.2.5.
 * <p>// ww w.j av  a  2s .c o  m
 * The &lt;user_defined_simplefield&gt; elements for Austria, that were
 * suggested by OpenImmo e.V., are explicitly supported in OpenImmo 1.2.5 as
 * child elements of &lt;energiepass&gt;. Any matching
 * &lt;user_defined_simplefield&gt; elements are moved into the
 * &lt;energiepass&gt; element.
 *
 * @param doc OpenImmo document in version 1.2.4
 * @throws JaxenException
 */
protected void upgradeEnergiepassElements(Document doc) throws JaxenException {
    Map<String, String> fields = new HashMap<String, String>();
    fields.put("hwbwert", "user_defined_simplefield[@feldname='epass_hwbwert']");
    fields.put("hwbklasse", "user_defined_simplefield[@feldname='epass_hwbklasse']");
    fields.put("fgeewert", "user_defined_simplefield[@feldname='epass_fgeewert']");
    fields.put("fgeeklasse", "user_defined_simplefield[@feldname='epass_fgeeklasse']");

    List nodes = XmlUtils.newXPath("/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben", doc)
            .selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;

        Element energiepassNode = (Element) XmlUtils.newXPath("io:energiepass", doc).selectSingleNode(node);
        if (energiepassNode == null) {
            energiepassNode = doc.createElementNS(StringUtils.EMPTY, "energiepass");
        }
        for (Map.Entry<String, String> entry : fields.entrySet()) {
            boolean fieldProcessed = false;
            List childNodes = XmlUtils.newXPath(entry.getValue(), doc).selectNodes(node);
            for (Object childItem : childNodes) {
                Node childNode = (Node) childItem;
                if (!fieldProcessed) {
                    String value = StringUtils.trimToNull(childNode.getTextContent());
                    if (value != null) {
                        Element newElement = doc.createElementNS(StringUtils.EMPTY, entry.getKey());
                        newElement.setTextContent(value);
                        energiepassNode.appendChild(newElement);
                        fieldProcessed = true;
                    }
                }
                node.removeChild(childNode);
            }
        }
        if (energiepassNode.getParentNode() == null && energiepassNode.hasChildNodes()) {
            node.appendChild(energiepassNode);
        }
    }
}

From source file:org.openestate.io.openimmo.converters.OpenImmo_1_2_7.java

/**
 * Upgrade &lt;energiepass&gt; elements to OpenImmo 1.2.7.
 * <p>//from w  ww  .  ja  v  a 2  s .  co  m
 * The &lt;user_defined_simplefield&gt; elements for EnEv2014, that were
 * <a href="http://www.openimmo.de/go.php/p/44/cm_enev2014.htm">suggested by OpenImmo e.V.</a>,
 * are explicitly supported in OpenImmo 1.2.7 as child elements of
 * &lt;energiepass&gt;. Any matching &lt;user_defined_simplefield&gt; elements
 * are moved into the &lt;energiepass&gt; element.
 *
 * @param doc OpenImmo document in version 1.2.6
 * @throws JaxenException
 */
protected void upgradeEnergiepassElements(Document doc) throws JaxenException {
    Map<String, String> fields = new HashMap<String, String>();
    fields.put("stromwert", "user_defined_simplefield[@feldname='epass_stromwert']");
    fields.put("waermewert", "user_defined_simplefield[@feldname='epass_waermewert']");
    fields.put("wertklasse", "user_defined_simplefield[@feldname='epass_wertklasse']");
    fields.put("baujahr", "user_defined_simplefield[@feldname='epass_baujahr']");
    fields.put("ausstelldatum", "user_defined_simplefield[@feldname='epass_ausstelldatum']");
    fields.put("jahrgang", "user_defined_simplefield[@feldname='epass_jahrgang']");
    fields.put("gebaeudeart", "user_defined_simplefield[@feldname='epass_gebaeudeart']");

    List nodes = XmlUtils.newXPath("/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben", doc)
            .selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;

        Element energiepassNode = (Element) XmlUtils.newXPath("io:energiepass", doc).selectSingleNode(node);
        if (energiepassNode == null) {
            energiepassNode = doc.createElementNS(StringUtils.EMPTY, "energiepass");
        }
        for (Map.Entry<String, String> entry : fields.entrySet()) {
            boolean fieldProcessed = false;
            List childNodes = XmlUtils.newXPath(entry.getValue(), doc).selectNodes(node);
            for (Object childItem : childNodes) {
                Node childNode = (Node) childItem;
                if (!fieldProcessed) {
                    String value = StringUtils.trimToNull(childNode.getTextContent());
                    if (value != null) {
                        Element newElement = doc.createElementNS(StringUtils.EMPTY, entry.getKey());
                        newElement.setTextContent(value);
                        energiepassNode.appendChild(newElement);
                        fieldProcessed = true;
                    }
                }
                node.removeChild(childNode);
            }
        }
        if (energiepassNode.getParentNode() == null && energiepassNode.hasChildNodes()) {
            node.appendChild(energiepassNode);
        }
    }
}

From source file:org.pepstock.jem.jbpm.XmlParser.java

/**
 * Loads all task defined as workitem based on JEM, to load all info about data description, data sources and locks.
 * @param jclFile BPMN file to parse//from ww  w.  j  a  v a 2  s. c  o m
 * @return list of all tasks of JCL
 * @throws ParserConfigurationException if any exception occurs parsing XML
 * @throws SAXException if any exception occurs parsing XML
 * @throws IOException if any exception occurs reading file
 */
public static final List<TaskDescription> getTaskDescription(String jclFile)
        throws ParserConfigurationException, SAXException, IOException {
    // creates an input soure
    InputSource source = new InputSource(new FileInputStream(jclFile));

    // DOM document and parsing
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringComments(false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(source);

    // scan XML to read comment
    NodeList firstLevel = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < firstLevel.getLength(); i++) {
        Node node = firstLevel.item(i);
        // gets tag name 
        String tagName = getElementName(node);
        // if is a process, starts scanning nodes
        if (tagName != null && PROCESS_ELEMENT.equalsIgnoreCase(tagName)) {
            Element element = (Element) node;
            if (element.hasChildNodes()) {
                // scans children only if the element has got children
                return getTasks(element.getChildNodes());
            }
        }
    }
    return Collections.emptyList();
}

From source file:org.pepstock.jem.jbpm.XmlParser.java

/**
 * Scans all children of PROCESS element, all BPMN tasks
 * @param list list of task, children of process
 * @return collection of tasks/*  ww  w.j a va2  s  .  c  o  m*/
 */
private static List<TaskDescription> getTasks(NodeList list) {
    // creates list of tasks to return
    List<TaskDescription> result = new ArrayList<TaskDescription>();
    // scans all nodes
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        // gets tagname
        String tagName = getElementName(node);
        // checks if is TASK element
        if (tagName != null && TASK_ELEMENT.equalsIgnoreCase(tagName)) {
            Element element = (Element) node;
            boolean isJemWorkItem = false;
            // scans attributes to check if is a JEM node
            for (int k = 0; k < element.getAttributes().getLength(); k++) {
                // gets attribute and value
                String attrName = element.getAttributes().item(k).getNodeName();
                String value = element.getAttributes().item(k).getTextContent();
                // checks if the task is a JEM task.
                // chekcs if there is a namespace also
                if (value != null && (attrName.endsWith(":" + TASK_NAME_ATTRIBUTE)
                        || attrName.equalsIgnoreCase(TASK_NAME_ATTRIBUTE))) {
                    isJemWorkItem = value.equalsIgnoreCase(JBpmKeys.JBPM_JEM_WORKITEM_NAME);
                }
            }
            // if it has found JEM task, loads information to task
            if (isJemWorkItem && element.hasChildNodes()) {
                // loads data
                TaskDescription task = new TaskDescription();
                task.setId(element.getAttribute(ID_ATTRIBUTE));
                task.setName(element.getAttribute(NAME_ATTRIBUTE));
                // gets IOSPECIFICATION, where all IO information are written
                task.setIoSpecification(getIoSpecification(element.getChildNodes()));
                result.add(task);
            }
        }
    }
    return result;
}

From source file:org.pepstock.jem.jbpm.XmlParser.java

/**
 * Scans all IOSPECIFICATION information. In this area you can find all parameters for the task.
 * JEM uses this parameters to set data description, data source and locks
 * //from ww  w .  j a v a 2 s. c  o  m
 * @param list list of nodes to scan
 * @return IoSpecification instance
 */
private static IoSpecification getIoSpecification(NodeList list) {
    // creates instance to return 
    IoSpecification ioSpecification = new IoSpecification();
    // scans all nodes
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        // gets tagname
        String tagName = getElementName(node);
        if (tagName != null) {
            // if is IO SPECIFICATION, then load all DATA INPUT
            if (IO_SPECIFICATION_ELEMENT.equalsIgnoreCase(tagName)) {
                Element element = (Element) node;
                if (element.hasChildNodes()) {
                    // loads data input
                    loadDataInput(element.getChildNodes(), ioSpecification);
                }
            } else if (DATA_INPUT_ASSOCIATION_ELEMENT.equalsIgnoreCase(tagName)) {
                // if is DATA_INOUT _ASSOCIATION, scan for all assignments
                Element element = (Element) node;
                if (element.hasChildNodes()) {
                    // scans children loading assignments
                    DataInputAssociation result = getDataInputAssociation(element.getChildNodes());
                    // checks if target_ref and value of assignment is the same
                    if (result.isValid()) {
                        // adds assignment
                        ioSpecification.getAssociations().put(result.getTargetRef(), result);
                    }
                }
            }
        }
    }
    return ioSpecification;
}

From source file:org.sakaiproject.myshowcase.tool.MyShowcaseGetShowcaseEvidenceMappingsController.java

private void processElement(Element e) {

    String nodeClass = "";

    if (e.getTagName().equals("competency")) {

        if (!started)

            started = true;// w w w. ja  v a2 s.  co  m

        if (e.hasChildNodes()) {

            nodeClass = "folder";

        } else {

            nodeClass = "file";

            mappingList.add(new Parameter(e.getAttribute("id").toString(), e.getAttribute("label").toString()));
        }
    }

    NodeList children = e.getChildNodes();

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

        if (child instanceof Element)

            processElement((Element) child);
    }
}

From source file:org.sakaiproject.myshowcase.tool.MyShowcaseGetTreeWSController.java

private void processElement(Element e) {

    String nodeClass = "";

    String nodeString = "";

    String checkedString = "";

    int aCount = 0;

    if (e.getTagName().equals("competency")) {

        if (!started) {

            nodeString = "<ul id=\"tree\"><li class=\"compNode\" id=\"" + e.getAttribute("id").toString()
                    + "\">\n";

            started = true;//w  w  w  .  j  av  a 2s.c  o  m

        } else {

            nodeString = "<ul><li class=\"compNode\" id=\"" + e.getAttribute("id").toString() + "\">\n";
        }

        outputString += nodeString;

        if (e.hasChildNodes()) {

            nodeClass = "folder";

            nodeString = "\t<span class=\"" + nodeClass + "\">" + e.getAttribute("label").toString()
                    + "</span>\n";

        } else {

            nodeClass = "file";

            if (returnType.equals("Finder")) {

                aCount = mappingCount(new Long(e.getAttribute("id")));

                if (aCount > 0) {
                    nodeString = "\t<span class=\"" + nodeClass + "\"><a id=\"href" + e.getAttribute("id")
                            + "\" href=\"#\" OnClick=\"MyShowcaseArtefact.loadArtefactsForMapping("
                            + competencyId + "," + e.getAttribute("id") + ")\">"
                            + e.getAttribute("label").toString() + "(" + aCount + ")</a>";
                } else {
                    nodeString = "\t<span class=\"" + nodeClass + "\">" + e.getAttribute("label").toString();
                }
            }

            if (returnType.equals("Checkbox")) {

                nodeString = "\t<span class=\"" + nodeClass + "\">" + e.getAttribute("label").toString();

                if (checked(new Long(e.getAttribute("id")))) {

                    checkedString = " checked=\"yes\"";
                } else {

                    checkedString = "";
                }

                nodeString += "<input type=\"checkbox\" name=\"chk" + artefactId + "-" + e.getAttribute("id")
                        + "\" id=\"chk" + artefactId + "-" + e.getAttribute("id") + "\" "
                        + "OnClick=\"MyShowcaseArtefact.updateCompetency(" + artefactId + "," + competencyId
                        + "," + e.getAttribute("id") + ")\"" + checkedString + ">";
            }

            nodeString += "</span>\n";
        }

        outputString += nodeString;
    }

    NodeList children = e.getChildNodes();

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

        if (child instanceof Element)

            processElement((Element) child);
    }

    if (e.getTagName().equals("competency")) {

        outputString += "</li></ul>\n";
    }
}