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:org.dozer.eclipse.plugin.sourcepage.util.DozerPluginUtils.java

public static String getMappingClassName(Node aOrBNode) {
    if (!"a".equals(aOrBNode.getNodeName()) && !"b".equals(aOrBNode.getNodeName()))
        return null;

    String aOrBNodeName = aOrBNode.getNodeName();
    Node mappingNode = getMappingNode(aOrBNode);

    NodeList childNodes = mappingNode.getChildNodes();
    int len = childNodes.getLength();

    //get the class-a/class-b nodes, so we know what class to go into
    for (int i = 0; i < len; i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE
                && childNode.getNodeName().equals("class-" + aOrBNodeName)) {
            Text textNode = (Text) childNode.getFirstChild();
            return textNode.getNodeValue();
        }//  w ww .  jav a2 s  .  c o m
    }

    return null;
}

From source file:net.sourceforge.eclipsetrader.charts.ChartsPlugin.java

public static Chart createDefaultChart() {
    Log logger = LogFactory.getLog(ChartsPlugin.class);
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); //$NON-NLS-1$
    Chart chart = new Chart();

    File file = ChartsPlugin.getDefault().getStateLocation().append("defaultChart.xml").toFile(); //$NON-NLS-1$
    if (file.exists() == true) {
        try {//  ww w  . j ava  2s .  c om
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(file);

            NodeList firstNode = document.getFirstChild().getChildNodes();
            for (int r = 0; r < firstNode.getLength(); r++) {
                Node item = firstNode.item(r);
                Node valueNode = item.getFirstChild();
                String nodeName = item.getNodeName();

                if (valueNode != null) {
                    if (nodeName.equalsIgnoreCase("compression") == true) //$NON-NLS-1$
                        chart.setCompression(Integer.parseInt(valueNode.getNodeValue()));
                    else if (nodeName.equalsIgnoreCase("period") == true) //$NON-NLS-1$
                        chart.setPeriod(Integer.parseInt(valueNode.getNodeValue()));
                    else if (nodeName.equalsIgnoreCase("autoScale") == true) //$NON-NLS-1$
                        chart.setAutoScale(new Boolean(valueNode.getNodeValue()).booleanValue());
                    else if (nodeName.equalsIgnoreCase("begin") == true) //$NON-NLS-1$
                    {
                        try {
                            chart.setBeginDate(dateTimeFormat.parse(valueNode.getNodeValue()));
                        } catch (Exception e) {
                            logger.warn(e.toString());
                        }
                    } else if (nodeName.equalsIgnoreCase("end") == true) //$NON-NLS-1$
                    {
                        try {
                            chart.setEndDate(dateTimeFormat.parse(valueNode.getNodeValue()));
                        } catch (Exception e) {
                            logger.warn(e.toString());
                        }
                    }
                }
                if (nodeName.equalsIgnoreCase("row")) //$NON-NLS-1$
                {
                    ChartRow row = new ChartRow(new Integer(r));
                    row.setParent(chart);

                    NodeList tabList = item.getChildNodes();
                    for (int t = 0; t < tabList.getLength(); t++) {
                        item = tabList.item(t);
                        nodeName = item.getNodeName();
                        if (nodeName.equalsIgnoreCase("tab")) //$NON-NLS-1$
                        {
                            ChartTab tab = new ChartTab(new Integer(t));
                            tab.setParent(row);
                            tab.setLabel(((Node) item).getAttributes().getNamedItem("label").getNodeValue()); //$NON-NLS-1$

                            NodeList indicatorList = item.getChildNodes();
                            for (int i = 0; i < indicatorList.getLength(); i++) {
                                item = indicatorList.item(i);
                                nodeName = item.getNodeName();
                                if (nodeName.equalsIgnoreCase("indicator")) //$NON-NLS-1$
                                {
                                    ChartIndicator indicator = new ChartIndicator(new Integer(i));
                                    indicator.setParent(tab);
                                    indicator.setPluginId(((Node) item).getAttributes().getNamedItem("pluginId") //$NON-NLS-1$
                                            .getNodeValue());

                                    NodeList parametersList = item.getChildNodes();
                                    for (int p = 0; p < parametersList.getLength(); p++) {
                                        item = parametersList.item(p);
                                        nodeName = item.getNodeName();
                                        if (nodeName.equalsIgnoreCase("param")) //$NON-NLS-1$
                                        {
                                            String key = ((Node) item).getAttributes().getNamedItem("key") //$NON-NLS-1$
                                                    .getNodeValue();
                                            String value = ((Node) item).getAttributes().getNamedItem("value") //$NON-NLS-1$
                                                    .getNodeValue();
                                            indicator.getParameters().put(key, value);
                                        }
                                    }

                                    tab.getIndicators().add(indicator);
                                } else if (nodeName.equalsIgnoreCase("object")) //$NON-NLS-1$
                                {
                                    ChartObject object = new ChartObject(new Integer(i));
                                    object.setParent(tab);
                                    object.setPluginId(((Node) item).getAttributes().getNamedItem("pluginId") //$NON-NLS-1$
                                            .getNodeValue());

                                    NodeList parametersList = item.getChildNodes();
                                    for (int p = 0; p < parametersList.getLength(); p++) {
                                        item = parametersList.item(p);
                                        nodeName = item.getNodeName();
                                        if (nodeName.equalsIgnoreCase("param")) //$NON-NLS-1$
                                        {
                                            String key = ((Node) item).getAttributes().getNamedItem("key") //$NON-NLS-1$
                                                    .getNodeValue();
                                            String value = ((Node) item).getAttributes().getNamedItem("value") //$NON-NLS-1$
                                                    .getNodeValue();
                                            object.getParameters().put(key, value);
                                        }
                                    }

                                    tab.getObjects().add(object);
                                }
                            }

                            row.getTabs().add(tab);
                        }
                    }

                    chart.getRows().add(row);
                }
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    chart.clearChanged();

    return chart;
}

From source file:com.igormaznitsa.upom.UPomModel.java

private static Node findFirstElement(final Node node) {
    if (node == null) {
        return null;
    }//from   w  w w  .jav  a  2  s  .  co m
    Node result = node.getFirstChild();
    while (result != null && result.getNodeType() != Node.ELEMENT_NODE) {
        result = result.getNextSibling();
    }
    return result;
}

From source file:Main.java

public static void removeAttribute(Node parent, String name, String value, boolean recursive) {
    NamedNodeMap nnm = parent.getAttributes();
    if (nnm != null) {
        if (value == null) {
            nnm.removeNamedItem(name);/*from ww  w .ja v a  2s .c  o  m*/
        } else {
            Node attr = nnm.getNamedItem(name);
            if (attr != null) {
                String attrVal = attr.getNodeValue();
                if (value.equals(attrVal)) {
                    nnm.removeNamedItem(name);
                }
            }
        }
    }
    if (recursive) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            removeAttribute(child, name, value, recursive);
        }
    }
}

From source file:org.jboss.as.test.integration.logging.formatters.XmlFormatterTestCase.java

private static void validateStackTrace(final Node element) {
    Assert.assertEquals(Node.ELEMENT_NODE, element.getNodeType());
    Assert.assertNotNull(element.getAttributes().getNamedItem("refId"));
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node child = children.item(i);
        final String name = child.getNodeName();
        if ("causedBy".equals(name) || "suppressed".equals(name)) {
            validateStackTrace(child.getFirstChild());
        }// w ww.ja v a 2s.c  om
    }
}

From source file:com.puppycrawl.tools.checkstyle.XDocsPagesTest.java

private static Set<Node> getChildrenElements(Node node) {
    final Set<Node> result = new LinkedHashSet<>();

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() != Node.TEXT_NODE) {
            result.add(child);/* w  w w .  j  av a  2  s .c  om*/
        }
    }

    return result;
}

From source file:de.mpg.imeji.presentation.metadata.extractors.BasicExtractor.java

static void displayMetadata(List<String> techMd, Node node, int level) {
    StringBuffer sb = new StringBuffer();
    // print open tag of element
    indent(techMd, sb, level);/*from w ww. ja v  a  2 s .co m*/
    sb.append("<" + node.getNodeName());
    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        // print attribute values
        int length = map.getLength();
        for (int i = 0; i < length; i++) {
            Node attr = map.item(i);
            sb.append(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"");
        }
    }
    Node child = node.getFirstChild();
    if (child == null) {
        // no children, so close element and return
        sb.append("/>");
        techMd.add(sb.toString());
        sb.delete(0, sb.length());
        return;
    }
    // children, so close current tag
    sb.append(">");
    techMd.add(sb.toString());
    sb.delete(0, sb.length());
    while (child != null) {
        // print children recursively
        displayMetadata(techMd, child, level + 1);
        child = child.getNextSibling();
    }
    // print close tag of element
    indent(techMd, sb, level);
    sb.append("</" + node.getNodeName() + ">");
    techMd.add(sb.toString());
    sb.delete(0, sb.length());
}

From source file:Main.java

/**
 * @param n1 first Node to test/*ww w  .ja va 2 s  .  co m*/
 * @param n2 second Node to test
 * @return true if a deep compare show the same children and attributes in the same order
 */
public static boolean equals(Node n1, Node n2) {
    // compare type
    if (!n1.getNodeName().equals(n2.getNodeName()))
        return false;
    // compare attributes
    NamedNodeMap nnm1 = n1.getAttributes();
    NamedNodeMap nnm2 = n2.getAttributes();
    if (nnm1.getLength() != nnm2.getLength())
        return false;
    for (int i = 0; i < nnm1.getLength(); i++) {
        Node attr1 = nnm1.item(i);
        if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName())))
            return false;
    }
    // compare children
    Node c1 = n1.getFirstChild();
    Node c2 = n2.getFirstChild();
    for (;;) {
        while ((c1 != null) && c1.getNodeName().startsWith("#"))
            c1 = c1.getNextSibling();
        while ((c2 != null) && c2.getNodeName().startsWith("#"))
            c2 = c2.getNextSibling();
        if ((c1 == null) && (c2 == null))
            break;
        if ((c1 == null) || (c2 == null))
            return false;
        if (!equals(c1, c2))
            return false;
        c1 = c1.getNextSibling();
        c2 = c2.getNextSibling();
    }
    return true;
}

From source file:com.puppycrawl.tools.checkstyle.XDocsPagesTest.java

private static Set<Node> findChildElementsByTag(Node node, String tag) {
    final Set<Node> result = new LinkedHashSet<>();

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (tag.equals(child.getNodeName())) {
            result.add(child);/*from w w w .j  a  va 2 s  . com*/
        } else if (child.hasChildNodes()) {
            result.addAll(findChildElementsByTag(child, tag));
        }
    }

    return result;
}

From source file:Main.java

/**
 * Locate the first text node at any level below the given node. If the
 * ignoreEmpty flag is true, we will ignore text nodes that contain only
 * whitespace characteres./*from   ww  w  .java 2  s .  c  o m*/
 * <p/>
 * Note that if you're trying to extract element content, you probably don't
 * want this since parser's can break up pcdata into multiple adjacent text
 * nodes. See getContent() for a more useful method.
 */
private static Text findText(Node node, boolean ignoreEmpty) {

    Text found = null;

    if (node != null) {

        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {

            Text t = (Text) node;
            if (!ignoreEmpty) {
                found = t;
            } else {
                String s = t.getData().trim();
                if (s.length() > 0) {
                    found = t;
                }
            }
        }

        if (found == null) {

            for (Node child = node.getFirstChild(); child != null
                    && found == null; child = child.getNextSibling()) {

                found = findText(child, ignoreEmpty);
            }
        }
    }

    return found;
}