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:DOMProcessor.java

/** Searches for a given element in the given node and updates list
  * of text within matched elements. Recursively searches for sub-nodes
  * of the given one.//ww w.  j av  a  2s.com
  * @param element Element to search for. If null, all elements searched.
  * @param node Node to start search from.
  */
private void searchText(String element, Node node) {
    // Only consider document or element nodes.
    if ((node.getNodeType() != Node.DOCUMENT_NODE) && (node.getNodeType() != Node.ELEMENT_NODE)) {
        return;
    }

    if ((element == null) || (node.getNodeName().equalsIgnoreCase(element))) {
        // Match found so look for text in children.
        NodeList children = node.getChildNodes();

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

            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                if (child.getNodeValue().trim().length() > 0) {
                    //matches.add(child.getNodeValue());
                    matches.add(child);
                }
            }
        }
    }

    if ((node.getNodeType() == Node.DOCUMENT_NODE) || (node.getNodeType() == Node.ELEMENT_NODE)) {
        // Search child nodes.
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            searchText(element, children.item(i));
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.AbstractODataBinder.java

protected ODataProperty fromCollectionPropertyElement(final Element prop, final EdmType edmType) {
    final ODataCollectionValue value = new ODataCollectionValue(
            edmType == null ? null : edmType.getTypeExpression());

    final EdmType type = edmType == null ? null : newEdmType(edmType.getBaseType());
    final NodeList elements = prop.getChildNodes();

    for (int i = 0; i < elements.getLength(); i++) {
        final Element child = (Element) elements.item(i);
        if (child.getNodeType() != Node.TEXT_NODE) {
            switch (guessPropertyType(child)) {
            case COMPLEX:
                value.add(fromComplexValueElement(child, type));
                break;
            case PRIMITIVE:
                value.add(fromPrimitiveValueElement(child, type));
                break;
            default:
                // do not add null or empty values
            }//from   w  ww .  java 2 s  .c om
        }
    }

    return ODataObjectFactory.newCollectionProperty(XMLUtils.getSimpleName(prop), value);
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Iterate through the DOM tree//from   w  w w  .java  2  s  .com
 * 
 * @param node
 * @param output
 * @throws IOException
 */
public static void writeNode(Node node, Writer output) throws IOException {

    int type = node.getNodeType();

    switch (type) {
    case Node.ATTRIBUTE_NODE:
        output.write(' ');
        output.write(node.getNodeName());
        output.write("=\"");
        writeXMLData(node.getNodeValue(), true, output);
        output.write('"');
        break;
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        writeXMLData(node.getNodeValue(), false, output);
        break;
    case Node.COMMENT_NODE:
        output.write("<!--");
        output.write(((Comment) node).getNodeValue());
        output.write("-->");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ELEMENT_NODE: {
        NamedNodeMap atts = node.getAttributes();

        output.write('<');
        output.write(node.getNodeName());
        if (atts != null) {
            int length = atts.getLength();
            for (int i = 0; i < length; i++)
                writeNode(atts.item(i), output);
        }

        if (node.hasChildNodes()) {
            output.write('>');
            writeNodes(node.getChildNodes(), output);
            output.write("</");
            output.write(node.getNodeName());
            output.write('>');
        } else {
            output.write("/>");
        }
        break;
    }
    case Node.ENTITY_NODE:
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.NOTATION_NODE:
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;
    default:
        throw new Error("Unexpected DOM node type: " + type);
    }
}

From source file:DomUtils.java

/**
 * Get all the text DOM sibling nodes before the supplied node and 
 * concatenate them together into a single String.
 * @param node Text node./*w  ww  .  jav  a2 s .c  o  m*/
 * @return String containing the concatentated text.
 */
public static String getTextBefore(Node node) {
    Node parent = node.getParentNode();
    if (parent == null) {
        System.out.println("Cannot get text before node [" + node + "]. [" + node + "] has no parent.");
        return "";
    }

    NodeList siblings = parent.getChildNodes();
    StringBuffer text = new StringBuffer();
    int siblingCount = siblings.getLength();

    for (int i = 0; i < siblingCount; i++) {
        Node sibling = siblings.item(i);

        if (sibling == node) {
            break;
        }
        if (sibling.getNodeType() == Node.TEXT_NODE) {
            text.append(((Text) sibling).getData());
        }
    }

    return text.toString();
}

From source file:com.msopentech.odatajclient.engine.data.impl.AbstractODataBinder.java

protected ODataProperty fromCollectionPropertyElement(final Element prop, final EdmType edmType) {
    final ODataCollectionValue value = new ODataCollectionValue(
            edmType == null ? null : edmType.getTypeExpression());

    final EdmType type = edmType == null ? null : newEdmType(edmType.getBaseType());
    final NodeList elements = prop.getChildNodes();

    for (int i = 0; i < elements.getLength(); i++) {
        if (elements.item(i).getNodeType() != Node.TEXT_NODE) {
            final Element child = (Element) elements.item(i);

            switch (guessPropertyType(child)) {
            case COMPLEX:
                value.add(fromComplexValueElement(child, type));
                break;
            case PRIMITIVE:
                value.add(fromPrimitiveValueElement(child, type));
                break;
            default:
                // do not add null or empty values
            }//w  w w . jav  a  2s .co  m
        }
    }

    return client.getObjectFactory().newCollectionProperty(XMLUtils.getSimpleName(prop), value);
}

From source file:org.adl.samplerte.server.LMSManifestHandler.java

License:asdf

/****************************************************************************
 ** /*ww w.j  a va2  s  .  com*/
 ** Method: getText() Input: Node node - The current node Output: String -
 * The text of the desired node. Description: This method gets the child
 * text node of the node that is passed in for input. Side Effects:
 ** 
 ****************************************************************************/
public String getText(Node node) {
    String value = new String();
    NodeList kids = node.getChildNodes();
    // cycle through all children of node to get the text
    if (kids != null) {
        for (int i = 0; i < kids.getLength(); i++) {
            // make sure we have a text element
            if ((kids.item(i).getNodeType() == Node.TEXT_NODE)
                    || (kids.item(i).getNodeType() == Node.CDATA_SECTION_NODE)) {
                value = value + kids.item(i).getNodeValue().trim();
            }
        }
    } else {
        if (DebugIndicator.ON) {
            System.out.println("node has no kids");
        }
    }

    return value;
}

From source file:com.draagon.meta.loader.xml.XMLFileMetaDataLoader.java

/** 
 * Parse the MetaAttribute Value /*from   ww  w  .j a  va  2 s .  co m*/
 */
protected void parseMetaAttributeValue(MetaAttribute attr, Element el) {

    ///////////////////////////////////////////////////
    // Get the Node value

    // Get the first node
    Node nv = el.getFirstChild();

    // Loop through and ignore the comments
    while (nv != null && nv.getNodeType() == Node.COMMENT_NODE) {
        nv.getNextSibling();
    }

    // If a valid node exists, then get the data
    if (nv != null) {
        switch (nv.getNodeType()) {
        // If CDATA just set the whole thing
        case Node.CDATA_SECTION_NODE:
            attr.setValueAsString(((CharacterData) nv).getData());
            break;

        // If an Element just pass it in for parsing
        case Node.ELEMENT_NODE:
            attr.setValue(nv);
            break;

        // If just text, then pass it in
        case Node.TEXT_NODE:
            attr.setValueAsString(nv.getNodeValue());
            break;

        default:
            log.warn("Unsupported Node Type for node [" + nv + "]");
        }
    }
}

From source file:DomUtils.java

/**
 * Get all the text DOM sibling nodes before the supplied node and 
 * concatenate them together into a single String.
 * @param node1 Test node.// w ww  .j  av  a2s.co m
 * @return String containing the concatentated text.
 */
public static String getTextBetween(Node node1, Node node2) {
    Node parent1 = node1.getParentNode();
    if (parent1 == null) {
        System.out.println("Cannot get text between nodes [" + node1 + "] and [" + node2 + "]. [" + node1
                + "] has no parent.");
        return "";
    }

    Node parent2 = node2.getParentNode();
    if (parent2 == null) {
        System.out.println("Cannot get text between nodes [" + node1 + "] and [" + node2 + "]. [" + node2
                + "] has no parent.");
        return "";
    }

    if (parent1 != parent2) {
        System.out.println("Cannot get text between nodes [" + node1 + "] and [" + node2
                + "]. These nodes do not share the same sparent.");
        return "";
    }

    NodeList siblings = parent1.getChildNodes();
    StringBuffer text = new StringBuffer();
    boolean append = false;
    int siblingCount = siblings.getLength();

    for (int i = 0; i < siblingCount; i++) {
        Node sibling = siblings.item(i);

        if (sibling == node1) {
            append = true;
        }
        if (sibling == node2) {
            break;
        }
        if (append && sibling.getNodeType() == Node.TEXT_NODE) {
            text.append(((Text) sibling).getData());
        }
    }

    return text.toString();
}

From source file:com.concursive.connect.web.modules.wiki.utils.HTMLToWikiUtils.java

public static void processTable(NodeList nodeList, StringBuffer sb, int rowCount, boolean doText,
        boolean anyNodeType, String contextPath, int projectId, int pass) {
    LOG.trace("line reset");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n != null) {
            if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
                if (doText && anyNodeType) {
                    if (StringUtils.hasText(n.getNodeValue())) {
                        String thisLine = StringUtils.fromHtmlValue(n.getNodeValue());
                        LOG.trace("table - text: " + thisLine);
                        sb.append(thisLine);
                    }/*from  w w  w.ja  va2s. c  o  m*/
                }
            } else if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element element = ((Element) n);
                String tag = element.getTagName();
                if ("tr".equals(tag)) {
                    LOG.trace("table - tr");
                    ++rowCount;
                    if (rowCount == 1) {
                        LOG.debug("Looking for style");
                        processTable(n.getChildNodes(), sb, rowCount, false, false, contextPath, projectId, 1);
                    }
                    processTable(n.getChildNodes(), sb, rowCount, false, false, contextPath, projectId, 2);
                    sb.append(CRLF);
                } else if ("td".equals(tag) || "th".equals(tag)) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("table - " + tag + " - " + i + " " + hasNonTextNodes(n.getChildNodes())
                                + " - pass " + pass);
                    }
                    String separator = "|";
                    if (tag.equals("th")) {
                        separator = "||";
                    }

                    // Determin how many columns are spanned by this column
                    int colspan = 1;
                    if (element.hasAttribute("colspan")) {
                        colspan = Integer.parseInt(element.getAttribute("colspan"));
                    }
                    //            if (sb.toString().endsWith(separator)) {
                    //              sb.append(" ");
                    //            }

                    // Style pass
                    boolean hasStyle = false;
                    if (pass == 1) {
                        if (element.hasAttribute("style")) {
                            String style = element.getAttribute("style");
                            if (style.endsWith(";")) {
                                style = style.substring(0, style.length() - 1);
                            }
                            // Start the wiki markup
                            for (int sI = 0; sI < colspan; sI++) {
                                sb.append(separator);
                            }
                            // Append the style data
                            sb.append("{").append(style).append("}");
                            hasStyle = true;
                        }

                        // Close the wiki markup if the last cell
                        if (hasStyle) {
                            if (i + 1 == nodeList.getLength()) {
                                sb.append(separator);
                                // The style pass needs to add it's own CRLF
                                sb.append(CRLF);
                            }
                        }
                    }

                    // Data pass
                    if (pass == 2) {

                        // Start the wiki markup
                        for (int sI = 0; sI < colspan; sI++) {
                            sb.append(separator);
                        }

                        if (n.getChildNodes().getLength() > 0) {
                            // Cell data uses a "!" for each return
                            if (hasNonTextNodes(n.getChildNodes())) {
                                processChildNodes(getNodeList(n), sb, 0, true, true, false, "!", contextPath,
                                        projectId);
                            } else {
                                processChildNodes(getNodeList(n), sb, 0, true, true, false, "!", contextPath,
                                        projectId);
                            }
                            // If the cell didn't have any data, then add a space
                            if (sb.toString().endsWith(separator)) {
                                sb.append(" ");
                            }
                        } else {
                            sb.append(" ");
                        }

                        // Close the wiki markup
                        if (i + 1 == nodeList.getLength()) {
                            sb.append(separator);
                        }
                    }
                } else {
                    LOG.trace("table - text - ** " + tag);
                    processTable(n.getChildNodes(), sb, rowCount, true, true, contextPath, projectId, 0);
                }
            }
        }
    }
}

From source file:de.knowwe.defi.usermanager.XMLUserDatabase.java

/**
 * Extracts all of the text nodes that are immediate children of an Element.
 * @param element the base element/* w w w. jav a  2s . co  m*/
 * @return the text nodes that are immediate children of the base element, concatenated together
 */
private String extractText(Element element) {
    String text = "";
    if (element.getChildNodes().getLength() > 0) {
        NodeList children = element.getChildNodes();
        for (int k = 0; k < children.getLength(); k++) {
            Node child = children.item(k);
            if (child.getNodeType() == Node.TEXT_NODE) {
                text = text + ((Text) child).getData();
            }
        }
    }
    return text;
}