Search in sources :

Example 36 with CDATASection

use of org.w3c.dom.CDATASection in project calcite by apache.

the class DiffRepository method getText.

/**
 * Returns the text under an element.
 */
private static String getText(Element element) {
    // If there is a <![CDATA[ ... ]]> child, return its text and ignore
    // all other child elements.
    final NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof CDATASection) {
            return node.getNodeValue();
        }
    }
    // Otherwise return all the text under this element (including
    // whitespace).
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof Text) {
            buf.append(((Text) node).getWholeText());
        }
    }
    return buf.toString();
}
Also used : CDATASection(org.w3c.dom.CDATASection) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text)

Example 37 with CDATASection

use of org.w3c.dom.CDATASection in project calcite by apache.

the class DiffRepository method writeNode.

private static void writeNode(Node node, XmlOutput out) {
    final NodeList childNodes;
    switch(node.getNodeType()) {
        case Node.DOCUMENT_NODE:
            out.print("<?xml version=\"1.0\" ?>\n");
            childNodes = node.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node child = childNodes.item(i);
                writeNode(child, out);
            }
            // out);
            break;
        case Node.ELEMENT_NODE:
            Element element = (Element) node;
            final String tagName = element.getTagName();
            out.beginBeginTag(tagName);
            // Attributes.
            final NamedNodeMap attributeMap = element.getAttributes();
            for (int i = 0; i < attributeMap.getLength(); i++) {
                final Node att = attributeMap.item(i);
                out.attribute(att.getNodeName(), att.getNodeValue());
            }
            out.endBeginTag(tagName);
            // Write child nodes, ignoring attributes but including text.
            childNodes = node.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node child = childNodes.item(i);
                if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
                    continue;
                }
                writeNode(child, out);
            }
            out.endTag(tagName);
            break;
        case Node.ATTRIBUTE_NODE:
            out.attribute(node.getNodeName(), node.getNodeValue());
            break;
        case Node.CDATA_SECTION_NODE:
            CDATASection cdata = (CDATASection) node;
            out.cdata(cdata.getNodeValue(), true);
            break;
        case Node.TEXT_NODE:
            Text text = (Text) node;
            final String wholeText = text.getNodeValue();
            if (!isWhitespace(wholeText)) {
                out.cdata(wholeText, false);
            }
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) node;
            out.print("<!--" + comment.getNodeValue() + "-->\n");
            break;
        default:
            throw new RuntimeException("unexpected node type: " + node.getNodeType() + " (" + node + ")");
    }
}
Also used : Comment(org.w3c.dom.Comment) NamedNodeMap(org.w3c.dom.NamedNodeMap) CDATASection(org.w3c.dom.CDATASection) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text)

Example 38 with CDATASection

use of org.w3c.dom.CDATASection in project flink by apache.

the class DiffRepository method getText.

/**
 * Returns the text under an element.
 */
private static String getText(Element element) {
    // If there is a <![CDATA[ ... ]]> child, return its text and ignore
    // all other child elements.
    final NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof CDATASection) {
            return node.getNodeValue();
        }
    }
    // Otherwise return all the text under this element (including
    // whitespace).
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof Text) {
            buf.append(((Text) node).getWholeText());
        }
    }
    return buf.toString();
}
Also used : CDATASection(org.w3c.dom.CDATASection) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text)

Aggregations

CDATASection (org.w3c.dom.CDATASection)38 Element (org.w3c.dom.Element)19 Node (org.w3c.dom.Node)15 Document (org.w3c.dom.Document)14 NodeList (org.w3c.dom.NodeList)9 Text (org.w3c.dom.Text)9 Comment (org.w3c.dom.Comment)6 File (java.io.File)4 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 Transformer (javax.xml.transform.Transformer)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2