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();
}
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 + ")");
}
}
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();
}
Aggregations