Example usage for org.w3c.dom Node TEXT_NODE

List of usage examples for org.w3c.dom Node TEXT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node TEXT_NODE.

Prototype

short TEXT_NODE

To view the source code for org.w3c.dom Node TEXT_NODE.

Click Source Link

Document

The node is a Text node.

Usage

From source file:com.axelor.data.xml.XMLBinder.java

private Object value(List<Node> nodes, final XMLBind bind) {
    List<Object> result = Lists.transform(nodes, new Function<Node, Object>() {
        @Override/*  w w  w . j  av a  2s .c o m*/
        public Object apply(@Nullable Node input) {
            if (bind.getBindings() != null) {
                return toMap(input, bind);
            }
            if (input.getNodeType() == Node.ELEMENT_NODE) {
                Node child = input.getFirstChild();
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
                return toMap(input, bind);
            }
            return input.getNodeValue();
        }
    });
    if (result.size() == 1) {
        return result.get(0);
    }
    return result.size() == 0 ? null : result;
}

From source file:com.doculibre.constellio.feedprotocol.parse.xml.FeedParser.java

private List<FeedContent> parseContentElements(NodeList contentsNodeList) throws ParseFeedException {
    List<FeedContent> contents = new ArrayList<FeedContent>();
    for (int i = 0; i < contentsNodeList.getLength(); i++) {
        Element contentElement = (Element) contentsNodeList.item(i);

        String encoding = getAttrValueIfPresent(contentElement, XML_CONTENT_ENCODING);
        NodeList nodeList = contentElement.getChildNodes();
        //System.out.println("Length: " + nodeList.getLength());
        if (nodeList.getLength() == 1) {
            Node childNode = nodeList.item(0);
            if (childNode.getNodeType() == Node.TEXT_NODE
                    || childNode.getNodeType() == Node.CDATA_SECTION_NODE) {
                String value = contentElement.getChildNodes().item(0).getNodeValue();
                //System.out.println("Value: " + value);
                throw new NotImplementedException("Feed must be transfed to file");
                //FeedContent content = new FeedContentImpl(value, encoding);
                //contents.add(content);
            }/*from   w ww .j a v a2s.c om*/
        }
    }
    return contents;
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private void replace(NodeList nodes, Node newNode, short newNodeType) {
    Node currentNode = nodes.item(0);
    switch (newNodeType) {
    case Node.ATTRIBUTE_NODE:
        Element parent = ((Attr) currentNode).getOwnerElement();
        parent.removeAttributeNode((Attr) currentNode);
        parent.setAttributeNode((Attr) newNode);
        break;/* w w  w .  j  a  v  a 2 s  .com*/
    case Node.ELEMENT_NODE:
        currentNode.getParentNode().replaceChild(newNode, currentNode);
        break;
    case Node.TEXT_NODE:
        currentNode.setNodeValue(newNode.getNodeValue());
        break;
    }
}

From source file:com.alfaariss.oa.util.configuration.ConfigurationManager.java

/**
 * Retrieve a configuration parameter value.
 *
 * Retrieves the value of the config parameter from the config section 
  * that is supplied.// w w  w .  j  a va 2s.c om
 * @param eSection The base section.
 * @param sName The parameter name.
 * @return The paramater value.
 * @throws ConfigurationException If retrieving fails.
 */
public synchronized String getParam(Element eSection, String sName) throws ConfigurationException {
    if (eSection == null)
        throw new IllegalArgumentException("Suplied section is empty");
    if (sName == null)
        throw new IllegalArgumentException("Suplied name is empty");

    String sValue = null;
    try {
        //check attributes within the section tag
        if (eSection.hasAttributes()) {
            NamedNodeMap oNodeMap = eSection.getAttributes();
            Node nAttribute = oNodeMap.getNamedItem(sName);
            if (nAttribute != null)
                sValue = nAttribute.getNodeValue();
        }

        if (sValue == null) {//check sub sections
            NodeList nlChilds = eSection.getChildNodes();
            for (int i = 0; i < nlChilds.getLength(); i++) {
                Node nTemp = nlChilds.item(i);
                if (nTemp != null && nTemp.getNodeName().equalsIgnoreCase(sName)) {
                    NodeList nlSubNodes = nTemp.getChildNodes();
                    if (nlSubNodes.getLength() > 0) {
                        for (int iSub = 0; iSub < nlSubNodes.getLength(); iSub++) {
                            Node nSubTemp = nlSubNodes.item(iSub);
                            if (nSubTemp.getNodeType() == Node.TEXT_NODE) {
                                sValue = nSubTemp.getNodeValue();
                                if (sValue == null)
                                    sValue = "";

                                return sValue;
                            }
                        }
                    } else {
                        if (sValue == null)
                            sValue = "";

                        return sValue;
                    }
                }
            }
        }
    } catch (DOMException e) {
        _logger.error("Could not retrieve parameter: " + sValue, e);
        throw new ConfigurationException(SystemErrors.ERROR_CONFIG_READ);
    }
    return sValue;
}

From source file:org.dozer.eclipse.plugin.sourcepage.hyperlink.DozerClassHyperlinkDetector.java

@SuppressWarnings("restriction")
protected final IRegion getHyperlinkRegion(Node node) {
    if (node != null) {
        switch (node.getNodeType()) {
        case Node.DOCUMENT_TYPE_NODE:
        case Node.TEXT_NODE:
            IDOMNode docNode = (IDOMNode) node;
            return new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset());

        case Node.ELEMENT_NODE:
            IDOMElement element = (IDOMElement) node;
            int endOffset;
            if (element.hasEndTag() && element.isClosed()) {
                endOffset = element.getStartEndOffset();
            } else {
                endOffset = element.getEndOffset();
            }//from  www.ja va  2s  .  c om
            return new Region(element.getStartOffset(), endOffset - element.getStartOffset());

        case Node.ATTRIBUTE_NODE:
            IDOMAttr att = (IDOMAttr) node;
            // do not include quotes in attribute value region
            int regOffset = att.getValueRegionStartOffset();
            int regLength = att.getValueRegionText().length();
            String attValue = att.getValueRegionText();
            if (StringUtils.isQuoted(attValue)) {
                regOffset += 1;
                regLength = regLength - 2;
            }
            return new Region(regOffset, regLength);
        }
    }
    return null;
}

From source file:importer.handler.post.stages.Discriminator.java

/**
 * Is the given element the first sibling of its type in sequence?
 * @param elem the sibling to test/*from   w w w . jav  a 2s .  com*/
 * @param s the SPlitter object
 * @return true if it is NOT preceded by another sibling of its type
 */
boolean isFirstAdjacentSibling(Element elem, Splitter s) {
    Node start = elem;
    String sName = start.getNodeName();
    String sibName = getSibling(sName);
    while (start != null) {
        Node prev = start.getPreviousSibling();
        if (prev != null) {
            if (prev.getNodeType() == Node.ELEMENT_NODE) {
                String pName = prev.getNodeName();
                if ((pName.equals(sName) || pName.equals(sibName)))
                    return false;
            } else if (prev.getNodeType() != Node.TEXT_NODE || !isWhitespace(prev.getTextContent()))
                return true;
        }
        start = prev;
    }
    return true;
}

From source file:DOM2SAX.java

/**
 * Writes a node using the given writer.
 * @param node node to serialize// w  w  w . j av  a 2s.  c  o m
 * @throws SAXException In case of a problem while writing XML
 */
private void writeNode(Node node) throws SAXException {
    if (node == null) {
        return;
    }

    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.NOTATION_NODE:
        // These node types are ignored!!!
        break;
    case Node.CDATA_SECTION_NODE:
        final String cdata = node.getNodeValue();
        if (lexicalHandler != null) {
            lexicalHandler.startCDATA();
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
            lexicalHandler.endCDATA();
        } else {
            // in the case where there is no lex handler, we still
            // want the text of the cdate to make its way through.
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
        }
        break;

    case Node.COMMENT_NODE: // should be handled!!!
        if (lexicalHandler != null) {
            final String value = node.getNodeValue();
            lexicalHandler.comment(value.toCharArray(), 0, value.length());
        }
        break;
    case Node.DOCUMENT_NODE:
        contentHandler.startDocument();
        Node next = node.getFirstChild();
        while (next != null) {
            writeNode(next);
            next = next.getNextSibling();
        }
        contentHandler.endDocument();
        break;

    case Node.ELEMENT_NODE:
        String prefix;
        List pushedPrefixes = new java.util.ArrayList();
        final AttributesImpl attrs = new AttributesImpl();
        final NamedNodeMap map = node.getAttributes();
        final int length = map.getLength();

        // Process all namespace declarations
        for (int i = 0; i < length; i++) {
            final Node attr = map.item(i);
            final String qnameAttr = attr.getNodeName();

            // Ignore everything but NS declarations here
            if (qnameAttr.startsWith(XMLNS_PREFIX)) {
                final String uriAttr = attr.getNodeValue();
                final int colon = qnameAttr.lastIndexOf(':');
                prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING;
                if (startPrefixMapping(prefix, uriAttr)) {
                    pushedPrefixes.add(prefix);
                }
            }
        }

        // Process all other attributes
        for (int i = 0; i < length; i++) {
            final Node attr = map.item(i);
            final String qnameAttr = attr.getNodeName();

            // Ignore NS declarations here
            if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
                final String uriAttr = attr.getNamespaceURI();

                // Uri may be implicitly declared
                if (uriAttr != null) {
                    final int colon = qnameAttr.lastIndexOf(':');
                    prefix = (colon > 0) ? qnameAttr.substring(0, colon) : EMPTYSTRING;
                    if (startPrefixMapping(prefix, uriAttr)) {
                        pushedPrefixes.add(prefix);
                    }
                }

                // Add attribute to list
                attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr), qnameAttr, "CDATA",
                        attr.getNodeValue());
            }
        }

        // Now process the element itself
        final String qname = node.getNodeName();
        final String uri = node.getNamespaceURI();
        final String localName = getLocalName(node);

        // Uri may be implicitly declared
        if (uri != null) {
            final int colon = qname.lastIndexOf(':');
            prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
            if (startPrefixMapping(prefix, uri)) {
                pushedPrefixes.add(prefix);
            }
        }

        // Generate SAX event to start element
        contentHandler.startElement(uri, localName, qname, attrs);

        // Traverse all child nodes of the element (if any)
        next = node.getFirstChild();
        while (next != null) {
            writeNode(next);
            next = next.getNextSibling();
        }

        // Generate SAX event to close element
        contentHandler.endElement(uri, localName, qname);

        // Generate endPrefixMapping() for all pushed prefixes
        final int nPushedPrefixes = pushedPrefixes.size();
        for (int i = 0; i < nPushedPrefixes; i++) {
            endPrefixMapping((String) pushedPrefixes.get(i));
        }
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        contentHandler.processingInstruction(node.getNodeName(), node.getNodeValue());
        break;

    case Node.TEXT_NODE:
        final String data = node.getNodeValue();
        contentHandler.characters(data.toCharArray(), 0, data.length());
        break;
    default:
        //nop
    }
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private void remove(NodeList nodes) {
    Node currentNode = nodes.item(0);
    // delete adjacent empty text node if it is empty/whitespace
    Node prevSibling = currentNode.getPreviousSibling();
    if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE
            && prevSibling.getNodeValue().trim().isEmpty()) {
        currentNode.getParentNode().removeChild(prevSibling);
    }/*from   w w  w.java  2 s  .  com*/
    currentNode.getParentNode().removeChild(currentNode);
}

From source file:com.blackbear.flatworm.config.impl.DefaultConfigurationReaderImpl.java

/**
 * Retrieve the text value found within the given {@code node} and return it.
 *
 * @param node The node from which the text value will be retrieved.
 * @return the text value found in the given {@code node} or {@code null} if no text value exists.
 *//* w  ww.j av  a 2 s .c  o  m*/
protected String getChildTextNodeValue(Node node) {
    String textValue = null;
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.TEXT_NODE) {
                textValue = child.getNodeValue();
            }
        }
    }
    return textValue;
}

From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java

/**
 * Get the content of the given element.
 * /*  w w  w.  ja  v  a2 s .co m*/
 * @param element
 *            The element to get the content for.
 * @param defaultStr
 *            The default to return when there is no content.
 * @return The content of the element or the default.
 * @throws Exception
 *             the exception
 */
private static String getElementContent(Element element, String defaultStr) throws Exception {
    if (element == null) {
        return defaultStr;
    }

    NodeList children = element.getChildNodes();
    StringBuilder result = new StringBuilder("");
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.TEXT_NODE
                || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(children.item(i).getNodeValue());
        }
    }
    return result.toString().trim();
}