Example usage for org.w3c.dom Text getTextContent

List of usage examples for org.w3c.dom Text getTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Text getTextContent.

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static boolean isJunk(Node node) {
    if (node.getNodeType() == Node.COMMENT_NODE) {
        return true;
    }/*  ww w.  j a va2s  .  c o  m*/
    if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return true;
    }
    if (node.getNodeType() == Node.TEXT_NODE) {
        Text text = (Text) node;
        if (text.getTextContent().matches("^\\s*$")) {
            return true;
        }
        return false;
    }
    return false;
}

From source file:eu.stratosphere.nephele.configuration.GlobalConfiguration.java

/**
 * Loads an XML document of key-values pairs.
 * /*from   w w  w  .j  a va2 s.  com*/
 * @param file
 *        the XML document file
 */
private void loadResource(final File file) {

    final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    // Ignore comments in the XML file
    docBuilderFactory.setIgnoringComments(true);
    docBuilderFactory.setNamespaceAware(true);

    // TODO: Trying to set this option causes an exception. What do we need it for? (DW)
    /*
     * try {
     * docBuilderFactory.setXIncludeAware(true);
     * } catch (UnsupportedOperationException e) {
     * LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e);
     * }
     */

    try {

        final DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = null;
        Element root = null;

        doc = builder.parse(file);

        if (doc == null) {
            LOG.warn("Cannot load configuration: doc is null");
            return;
        }

        root = doc.getDocumentElement();
        if (root == null) {
            LOG.warn("Cannot load configuration: root is null");
            return;
        }

        if (!"configuration".equals(root.getNodeName())) {
            LOG.warn("Cannot load configuration: unknown element " + root.getNodeName());
            return;
        }

        final NodeList props = root.getChildNodes();
        int propNumber = -1;

        synchronized (this.confData) {

            for (int i = 0; i < props.getLength(); i++) {

                final Node propNode = props.item(i);
                String key = null;
                String value = null;

                // Ignore text at this point
                if (propNode instanceof Text) {
                    continue;
                }

                if (!(propNode instanceof Element)) {
                    LOG.warn("Error while reading configuration: " + propNode.getNodeName()
                            + " is not of type element");
                    continue;
                }

                Element property = (Element) propNode;
                if (!"property".equals(property.getNodeName())) {
                    LOG.warn("Error while reading configuration: unknown element " + property.getNodeName());
                    continue;
                }

                propNumber++;
                final NodeList propChildren = property.getChildNodes();
                if (propChildren == null) {
                    LOG.warn("Error while reading configuration: property has no children, skipping...");
                    continue;
                }

                for (int j = 0; j < propChildren.getLength(); j++) {

                    final Node propChild = propChildren.item(j);
                    if (propChild instanceof Element) {
                        if ("key".equals(propChild.getNodeName())) {
                            if (propChild.getChildNodes() != null) {
                                if (propChild.getChildNodes().getLength() == 1) {
                                    if (propChild.getChildNodes().item(0) instanceof Text) {
                                        final Text t = (Text) propChild.getChildNodes().item(0);
                                        key = t.getTextContent();
                                    }
                                }
                            }
                        }

                        if ("value".equals(propChild.getNodeName())) {
                            if (propChild.getChildNodes() != null) {
                                if (propChild.getChildNodes().getLength() == 1) {
                                    if (propChild.getChildNodes().item(0) instanceof Text) {
                                        final Text t = (Text) propChild.getChildNodes().item(0);
                                        value = t.getTextContent();
                                    }
                                }
                            }
                        }
                    }
                }

                if (key != null && value != null) {
                    // Put key, value pair into the map
                    LOG.debug("Loading configuration property: " + key + ", " + value);
                    this.confData.put(key, value);
                } else {
                    LOG.warn("Error while reading configuration: Cannot read property " + propNumber);
                    continue;
                }
            }
        }

    } catch (ParserConfigurationException e) {
        LOG.warn("Cannot load configuration: " + StringUtils.stringifyException(e));
    } catch (IOException e) {
        LOG.warn("Cannot load configuration: " + StringUtils.stringifyException(e));
    } catch (SAXException e) {
        LOG.warn("Cannot load configuration: " + StringUtils.stringifyException(e));
    }
}

From source file:eu.stratosphere.configuration.GlobalConfiguration.java

/**
 * Loads an XML document of key-values pairs.
 * /* ww w  . j  a  va 2  s.co m*/
 * @param file
 *        the XML document file
 */
private void loadXMLResource(final File file) {

    final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    // Ignore comments in the XML file
    docBuilderFactory.setIgnoringComments(true);
    docBuilderFactory.setNamespaceAware(true);

    try {

        final DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = null;
        Element root = null;

        doc = builder.parse(file);

        if (doc == null) {
            LOG.warn("Cannot load configuration: doc is null");
            return;
        }

        root = doc.getDocumentElement();
        if (root == null) {
            LOG.warn("Cannot load configuration: root is null");
            return;
        }

        if (!"configuration".equals(root.getNodeName())) {
            LOG.warn("Cannot load configuration: unknown element " + root.getNodeName());
            return;
        }

        final NodeList props = root.getChildNodes();
        int propNumber = -1;

        synchronized (this.confData) {

            for (int i = 0; i < props.getLength(); i++) {

                final Node propNode = props.item(i);
                String key = null;
                String value = null;

                // Ignore text at this point
                if (propNode instanceof Text) {
                    continue;
                }

                if (!(propNode instanceof Element)) {
                    LOG.warn("Error while reading configuration: " + propNode.getNodeName()
                            + " is not of type element");
                    continue;
                }

                Element property = (Element) propNode;
                if (!"property".equals(property.getNodeName())) {
                    LOG.warn("Error while reading configuration: unknown element " + property.getNodeName());
                    continue;
                }

                propNumber++;
                final NodeList propChildren = property.getChildNodes();
                if (propChildren == null) {
                    LOG.warn("Error while reading configuration: property has no children, skipping...");
                    continue;
                }

                for (int j = 0; j < propChildren.getLength(); j++) {

                    final Node propChild = propChildren.item(j);
                    if (propChild instanceof Element) {
                        if ("key".equals(propChild.getNodeName())) {
                            if (propChild.getChildNodes() != null) {
                                if (propChild.getChildNodes().getLength() == 1) {
                                    if (propChild.getChildNodes().item(0) instanceof Text) {
                                        final Text t = (Text) propChild.getChildNodes().item(0);
                                        key = t.getTextContent();
                                    }
                                }
                            }
                        }

                        if ("value".equals(propChild.getNodeName())) {
                            if (propChild.getChildNodes() != null) {
                                if (propChild.getChildNodes().getLength() == 1) {
                                    if (propChild.getChildNodes().item(0) instanceof Text) {
                                        final Text t = (Text) propChild.getChildNodes().item(0);
                                        value = t.getTextContent();
                                    }
                                }
                            }
                        }
                    }
                }

                if (key != null && value != null) {
                    // Put key, value pair into the map
                    LOG.debug("Loading configuration property: " + key + ", " + value);
                    this.confData.put(key, value);
                } else {
                    LOG.warn("Error while reading configuration: Cannot read property " + propNumber);
                    continue;
                }
            }
        }

    } catch (ParserConfigurationException e) {
        LOG.warn("Cannot load configuration: " + StringUtils.stringifyException(e));
    } catch (IOException e) {
        LOG.warn("Cannot load configuration: " + StringUtils.stringifyException(e));
    } catch (SAXException e) {
        LOG.warn("Cannot load configuration: " + StringUtils.stringifyException(e));
    }
}

From source file:org.limy.common.xml.XmlUtils.java

private static void parse(XmlElement root, Node node) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            root.setAttribute(attr.getNodeName(), attr.getNodeValue());
        }//from w ww  . j  a va  2  s .  c o  m
    }

    NodeList children = node.getChildNodes();
    if (children != null) {
        Text text = null;
        boolean isChild = false;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child instanceof Element) {
                isChild = true;
                SimpleElement el = new SimpleElement(root, child.getNodeName());
                parse(el, child);
            }
            if (child instanceof Text) {
                text = (Text) child;
            }
        }
        if (!isChild && text != null) {
            // ????????????
            root.setValue(text.getTextContent());
        }
    }

}

From source file:org.openestate.io.core.XmlUtils.java

/**
 * Replace all text values of a {@link Node} with CDATA values.
 *
 * @param doc/*  ww  w .  jav  a 2  s .c o m*/
 * the document to update
 *
 * @param node
 * the node to update
 */
public static void replaceTextWithCData(Document doc, Node node) {
    if (node instanceof Text) {
        Text text = (Text) node;
        CDATASection cdata = doc.createCDATASection(text.getTextContent());
        Element parent = (Element) text.getParentNode();
        parent.replaceChild(cdata, text);
    } else if (node instanceof Element) {
        //LOGGER.debug( "ELEMENT " + element.getTagName() );
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            //LOGGER.debug( "> " + children.item( i ).getClass().getName() );
            XmlUtils.replaceTextWithCData(doc, children.item(i));
        }
    }
}