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:gov.nih.nci.ncicb.tcga.dcc.qclive.common.action.validation.BcrExperimentFieldValidator.java

/**
 * Determines if a tag is a simple text element
 * @param node to determine if it is a simple text element
 * @return true if a node is a simple text eleement
 *//*  w w w. j a  v  a2 s.  c  o  m*/
private boolean isSimpleTextElement(final Node node) {
    // either the node has no children, or it has just one child of type text node
    // (i.e. false if it has other element nodes nested inside of it
    return !node.hasChildNodes()
            || (node.getChildNodes().getLength() == 1 && node.getFirstChild().getNodeType() == Node.TEXT_NODE);
}

From source file:com.prowidesoftware.swift.io.parser.XMLParser.java

private String getText(final Node n) {
    String text = null;/*  www  .ja va2s. c  om*/
    final Node c = n.getFirstChild();
    if (c != null) {
        if (c.getNodeType() == Node.TEXT_NODE) {
            text = c.getNodeValue().trim();
        } else {
            log.warning("Node is not TEXT_NODE: " + c);
        }
    }
    return text;
}

From source file:net.vexelon.bgrates.Utils.java

/**
 * Serialize an XML element recursively//from w w  w  . ja  va  2s .co m
 * @param node
 * @param serializer
 * @throws IOException
 */
private static void serializeXmlElement(Node node, XmlSerializer serializer) throws IOException {

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node current = children.item(i);

        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element child = (Element) current;
            serializer.startTag("", child.getNodeName());
            serializeXmlElement(child, serializer);
            serializer.endTag("", child.getNodeName());
        } else if (current.getNodeType() == Node.TEXT_NODE) {
            Text child = (Text) current;
            serializer.text(child.getData());
        } else if (current.getNodeType() == Node.CDATA_SECTION_NODE) {
            CDATASection child = (CDATASection) current;
            serializer.cdsect(child.getData());
        } else if (current.getNodeType() == Node.COMMENT_NODE) {
            Comment child = (Comment) current;
            serializer.comment(child.getData());
        }
    }
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static String getElementStrValue(Element anElement) {
    int count;/*from  w w  w .j  a v a2 s  .c o  m*/
    Node textNode;
    NodeList nodeList;

    Node curNode = (Node) anElement;
    nodeList = curNode.getChildNodes();
    count = nodeList.getLength();

    for (int i = 0; i < count; i++) {
        textNode = nodeList.item(i);

        if (textNode.getNodeType() == Node.TEXT_NODE)
            return textNode.getNodeValue();
    }

    return StringUtils.EMPTY;
}

From source file:Main.java

private static Node copyNodeExceptNamespace(Document document, Node srcNode, Set<String> namespace,
        List<String> exceptNamespaces) {

    if (srcNode.getNodeType() == Node.ELEMENT_NODE) {

        String nodeName = srcNode.getNodeName();
        nodeName = nodeName.substring(nodeName.indexOf(":") + 1);
        Element element = document.createElement(nodeName);

        for (int i = 0; i < srcNode.getAttributes().getLength(); i++) {
            Attr attr = (Attr) srcNode.getAttributes().item(i);
            String name = attr.getName();
            if (name.startsWith("xmlns:")) {
                String suffix = name.substring(6);
                if (!exceptNamespaces.contains(suffix)) {
                    namespace.add(suffix);
                }//from   ww  w. ja v a2 s.  c  om
                continue;
            }
        }

        for (int i = 0; i < srcNode.getAttributes().getLength(); i++) {
            Attr attr = (Attr) srcNode.getAttributes().item(i);
            String name = attr.getName();
            if (name.startsWith("xmlns:")) {
                continue;
            }
            int semi = name.indexOf(":");
            if (semi > 0) {
                if (namespace.contains(name.substring(0, semi))) {
                    name = name.substring(semi + 1);
                }
            }
            element.setAttribute(name, attr.getValue());
        }

        NodeList nodeList = srcNode.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node childNode = nodeList.item(i);
            if (childNode.getNodeType() == Node.TEXT_NODE) {
                element.appendChild(document.createTextNode(childNode.getTextContent()));
            } else if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                Node node = copyNodeExceptNamespace(document, childNode, namespace, exceptNamespaces);
                element.appendChild(node);
            }
        }

        return element;
    }

    if (srcNode.getNodeType() == Node.TEXT_NODE) {
        Text text = document.createTextNode(srcNode.getTextContent());
        return text;
    }

    return null;
}

From source file:de.codesourcery.utils.xml.XmlHelper.java

public static String getNodeValue(Node n, String defaultValue, boolean isRequired) throws ParseException {

    if (n == null) {

        if (isRequired) {
            final String msg = "Unable to determine node value";
            log.error("getNodeValue(): " + msg);
            throw new ParseException(msg, -1);
        }/*  w w w  .  ja v a2  s.  c o  m*/
        return defaultValue;
    }

    String result = null;
    if (n.getNodeType() == Node.TEXT_NODE) {
        result = n.getNodeValue();
    } else {
        NodeList children = n.getChildNodes();

        if (children.getLength() == 0) {
            log.trace("getNodeValue(): Node " + n.getLocalName() + " is no TEXT_NODE and has no children");
        } else if (children.getLength() == 1) {
            if (children.item(0).getNodeType() == Node.TEXT_NODE) {
                result = children.item(0).getNodeValue();
            } else {
                log.trace("getNodeValue(): Node " + n.getLocalName() + " doesn't have TEXT_NODE children");
            }
        } else {
            log.error(
                    "getNodeValue(): Node " + n.getLocalName() + " is no TEXT_NODE and has multiple children");
            throw new ParseException("Node " + n.getLocalName() + " is no TEXT_NODE and has multiple children",
                    -1);
        }

    }

    if (StringUtils.isBlank(result)) {
        if (isRequired) {
            final String msg = "Node " + n.getLocalName() + " requires a non-blank value";
            log.error("getNodeValue(): " + msg);
            throw new ParseException(msg, -1);
        } else {
            return defaultValue;
        }
    }

    return result;
}

From source file:com.gatf.executor.validator.ResponseValidator.java

public static String getXMLNodeValue(Node node) {
    String xmlValue = null;//w  w  w.j  a  v  a2  s .  c om
    if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
        xmlValue = node.getNodeValue();
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        xmlValue = ((Attr) node).getValue();
    } else if (node.getNodeType() == Node.ELEMENT_NODE && node.getChildNodes().getLength() >= 1
            && (node.getFirstChild().getNodeType() == Node.TEXT_NODE
                    || node.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)) {
        xmlValue = node.getFirstChild().getNodeValue();
    }
    return xmlValue;
}

From source file:Counter.java

/** Traverses the specified node, recursively. */
public void count(Node node) {

    // is there anything to do?
    if (node == null) {
        return;//from w  w  w.j a va2s. co m
    }

    int type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        fElements = 0;
        fAttributes = 0;
        fCharacters = 0;
        fIgnorableWhitespace = 0;
        Document document = (Document) node;
        count(document.getDocumentElement());
        break;
    }

    case Node.ELEMENT_NODE: {
        fElements++;
        NamedNodeMap attrs = node.getAttributes();
        if (attrs != null) {
            fAttributes += attrs.getLength();
        }
        // drop through to entity reference
    }

    case Node.ENTITY_REFERENCE_NODE: {
        Node child = node.getFirstChild();
        while (child != null) {
            count(child);
            child = child.getNextSibling();
        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        fCharacters += ((Text) node).getLength();
        break;
    }

    case Node.TEXT_NODE: {
        if (fDocumentInfo != null) {
            Text text = (Text) node;
            int length = text.getLength();
            if (fDocumentInfo.isIgnorableWhitespace(text)) {
                fIgnorableWhitespace += length;
            } else {
                fCharacters += length;
            }
        }
        break;
    }
    }

}

From source file:cc.siara.csv_ml_demo.MainActivity.java

/**
 * Evaluates given XPath from Input box against Document generated by
 * parsing csv_ml in input box and sets value or node list to output box.
 *//*from  w  w  w  .j ava2  s  . com*/
void processXPath() {
    EditText etInput = (EditText) findViewById(R.id.etInput);
    EditText etXPath = (EditText) findViewById(R.id.etXPath);
    CheckBox cbPretty = (CheckBox) findViewById(R.id.cbPretty);
    XPath xpath = XPathFactory.newInstance().newXPath();
    MultiLevelCSVParser parser = new MultiLevelCSVParser();
    Document doc = null;
    try {
        doc = parser.parseToDOM(new StringReader(etInput.getText().toString()), false);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    if (doc == null)
        return;
    StringBuffer out_str = new StringBuffer();
    try {
        XPathExpression expr = xpath.compile(etXPath.getText().toString());
        try {
            Document outDoc = Util.parseXMLToDOM("<output></output>");
            Element rootElement = outDoc.getDocumentElement();
            NodeList ret = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < ret.getLength(); i++) {
                Object o = ret.item(i);
                if (o instanceof String) {
                    out_str.append(o);
                } else if (o instanceof Node) {
                    Node n = (Node) o;
                    short nt = n.getNodeType();
                    switch (nt) {
                    case Node.TEXT_NODE:
                    case Node.ATTRIBUTE_NODE:
                    case Node.CDATA_SECTION_NODE: // Only one value gets
                                                  // evaluated?
                        if (out_str.length() > 0)
                            out_str.append(',');
                        if (nt == Node.ATTRIBUTE_NODE)
                            out_str.append(n.getNodeValue());
                        else
                            out_str.append(n.getTextContent());
                        break;
                    case Node.ELEMENT_NODE:
                        rootElement.appendChild(outDoc.importNode(n, true));
                        break;
                    }
                }
            }
            if (out_str.length() > 0) {
                rootElement.setTextContent(out_str.toString());
                out_str.setLength(0);
            }
            out_str.append(Util.docToString(outDoc, true));
        } catch (Exception e) {
            // Thrown most likely because the given XPath evaluates to a
            // string
            out_str.append(expr.evaluate(doc));
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    if (out_str.length() > 5 && out_str.substring(0, 5).equals("<?xml"))
        out_str.delete(0, out_str.indexOf(">") + 1);
    EditText etOutput = (EditText) findViewById(R.id.etOutput);
    etOutput.setText(out_str.toString());
    // tfOutputSize.setText(String.valueOf(xmlString.length()));
}

From source file:org.dozer.eclipse.plugin.sourcepage.validation.Validator.java

@SuppressWarnings("restriction")
private void checkFieldNodes(NodeList nodeList, IFile file, ValidationInfo validationReport)
        throws JavaModelException, DOMException {
    int len = nodeList.getLength();
    //check all field-nodes...
    for (int i = 0; i < len; i++) {
        Node node = nodeList.item(i); //field

        //..have custom-converter attributes
        Node attrNode = node.getAttributes().getNamedItem("custom-converter");
        if (attrNode != null) {
            //...doesnt implement the CustomConverter Interface
            if (!checkClassImplementsCustomConverter(file.getProject(), attrNode.getNodeValue())) {
                //this class doesnt implement the interface and is worth an error
                Integer[] location = (Integer[]) node.getUserData("location");
                validationReport.addError("Class does not implement interface CustomConverter", location[0],
                        location[1], validationReport.getFileURI());
            }/*from ww w .  j a  v  a 2  s.c o  m*/
            //...have custom-converter-id attributes
        } else {
            attrNode = node.getAttributes().getNamedItem("custom-converter-id");
            if (attrNode != null) {
                String className = DozerPluginUtils.getClassNameForCCI(file, attrNode.getNodeValue());
                if (className != null && !checkClassImplementsCustomConverter(file.getProject(), className)) {
                    //this class doesnt implement the interface and is worth an error
                    Integer[] location = (Integer[]) node.getUserData("location");
                    validationReport.addError("Bean does not implement interface CustomConverter", location[0],
                            location[1], validationReport.getFileURI());
                }
            }
        }

        NodeList abList = node.getChildNodes();
        int abLen = abList.getLength();
        for (int a = 0; a < abLen; a++) {
            Node abNode = abList.item(a); //a or b
            if ("a".equals(abNode.getNodeName()) || "b".equals(abNode.getNodeName())) {
                Node textNode = abNode.getFirstChild();
                //...no value set between <a></a> or <b></b>?
                if (textNode == null) {
                    Integer[] location = (Integer[]) node.getUserData("location");
                    String className = DozerPluginUtils.getMappingClassName(abNode);
                    String nodeName = abNode.getNodeName();

                    validationReport.addError(
                            "Unsetted property value in node " + nodeName + " for class " + className,
                            location[0], location[1], validationReport.getFileURI());
                }
                //...is fieldname correct?
                else if (textNode.getNodeType() == Node.TEXT_NODE) {
                    String property = textNode.getNodeValue();
                    String className = DozerPluginUtils.getMappingClassName(abNode);

                    attrNode = DozerPluginUtils.getMappingNode(abNode).getAttributes().getNamedItem("type");
                    boolean bIsBiDirectional = true;
                    if (attrNode != null) {
                        bIsBiDirectional = "bi-directional".equals(attrNode.getNodeValue());
                    }

                    if (!"this".equals(property)) {
                        Node isAccessibleNode = abNode.getAttributes().getNamedItem("is-accessible");
                        boolean isAccessible = isAccessibleNode != null
                                && "true".equals(isAccessibleNode.getNodeValue());

                        if ((bIsBiDirectional || "a".equals(abNode.getNodeName()))
                                && DozerPluginUtils.hasReadProperty(property, className, file.getProject(),
                                        isAccessible) == null) {
                            Integer[] location = (Integer[]) abNode.getUserData("location");
                            validationReport.addError(
                                    "Property " + property + " for class " + className
                                            + " cannot be read from.",
                                    location[0], location[1], validationReport.getFileURI());
                        } else if ((bIsBiDirectional || "b".equals(abNode.getNodeName())) && DozerPluginUtils
                                .hasWriteProperty(property, className, file.getProject()) == null) {
                            Integer[] location = (Integer[]) abNode.getUserData("location");
                            validationReport.addError(
                                    "Property " + property + " for class " + className
                                            + " cannot be written to.",
                                    location[0], location[1], validationReport.getFileURI());
                        }
                    }
                }
            }
        }
    }
}