use of org.w3c.dom.CharacterData in project aries by apache.
the class Parser method getTextValue.
private static String getTextValue(Element element) {
StringBuffer value = new StringBuffer();
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
value.append(item.getNodeValue());
}
}
return value.toString();
}
use of org.w3c.dom.CharacterData in project Lucee by lucee.
the class XMLCaster method toHTML.
private static void toHTML(Node node, StringBuilder sb) throws ExpressionException {
short type = node.getNodeType();
if (Node.ELEMENT_NODE == type) {
Element el = (Element) node;
String tagName = el.getTagName();
sb.append('<');
sb.append(tagName);
NamedNodeMap attrs = el.getAttributes();
Attr attr;
int len = attrs.getLength();
for (int i = 0; i < len; i++) {
attr = (Attr) attrs.item(i);
sb.append(' ');
sb.append(attr.getName());
sb.append("=\"");
sb.append(attr.getValue());
sb.append('"');
}
NodeList children = el.getChildNodes();
len = children.getLength();
boolean doEndTag = len != 0 || (tagName.length() == 4 && (tagName.equalsIgnoreCase("head") || tagName.equalsIgnoreCase("body")));
if (!doEndTag)
sb.append(" />");
else
sb.append('>');
for (int i = 0; i < len; i++) {
toHTML(children.item(i), sb);
}
if (doEndTag) {
sb.append("</");
sb.append(el.getTagName());
sb.append('>');
}
} else if (node instanceof CharacterData) {
sb.append(HTMLEntities.escapeHTML(node.getNodeValue()));
}
}
use of org.w3c.dom.CharacterData in project knime-core by knime.
the class XmlDomComparer method areNodesEqual.
private static Diff areNodesEqual(final Node node1, final Node node2, final XmlDomComparerCustomizer l) {
// Check for incompatible types
if (node1.getNodeType() != node2.getNodeType()) {
return new Diff(node1, node1.getNodeType(), node2.getNodeType(), Type.TYPE_MISSMATCH);
}
switch(node1.getNodeType()) {
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
case Node.COMMENT_NODE:
String textNode1 = ((CharacterData) node1).getData();
String textNode2 = ((CharacterData) node2).getData();
if (!Objects.equals(textNode1, textNode2)) {
return new Diff(node1, textNode1, textNode2, Type.TEXT_MISSMATCH);
}
return null;
case Node.DOCUMENT_NODE:
return areNodesEqual(((Document) node1).getDocumentElement(), ((Document) node2).getDocumentElement(), l);
case Node.ELEMENT_NODE:
// Different names means different nodes
Element element1 = (Element) node1;
Element element2 = (Element) node2;
Diff namesComparison = checkNamesAndNS(element1, element2);
if (namesComparison != null) {
return namesComparison;
}
Diff attributeComparison = areAttributesEqual(node1, node2, l);
if (attributeComparison != null) {
return attributeComparison;
}
return areChildsEqual(node1, node2, l);
case Node.PROCESSING_INSTRUCTION_NODE:
String piNode1 = ((ProcessingInstruction) node1).getData();
String piNode2 = ((ProcessingInstruction) node2).getData();
if (!Objects.equals(piNode1, piNode2)) {
return new Diff(node1, piNode1, piNode2, Type.TEXT_MISSMATCH);
}
return null;
default:
throw new IllegalArgumentException("Nodes with type: " + node1.getNodeType() + " are not supported");
}
}
use of org.w3c.dom.CharacterData in project knime-core by knime.
the class XmlDomComparer method hashCode.
/**
* Computes a default hashCode from a given XML Node. The customizer defines the Nodes which are considered during
* the hashCode computation. If the customizer is <code>null</code> all nodes are considered.
*
* @param arg0 the argument
* @param customizer the filter
* @return hashcode based on given node and filter
*/
public static int hashCode(final Node arg0, final XmlDomComparerCustomizer customizer) {
int hash = 1;
switch(arg0.getNodeType()) {
case Node.DOCUMENT_NODE:
Document expectedDoc = (Document) arg0;
hash += hashCode(expectedDoc.getDocumentElement(), customizer);
break;
case Node.ELEMENT_NODE:
Element expectedElement = (Element) arg0;
hash += PRIME * getQualifiedName(expectedElement).hashCode();
hash += hashAttributes(expectedElement.getAttributes(), customizer);
NodeList expectedChildren = expectedElement.getChildNodes();
int includedChildren = 0;
for (int i = 0; i < expectedChildren.getLength(); i++) {
Node item = expectedChildren.item(i);
if (customizer.include(item)) {
includedChildren++;
switch(customizer.determineCompareStrategy(item)) {
case UNORDERED:
hash = hash + hashCode(item, customizer);
break;
case ORDERED:
default:
// take also the sequence of the elements into account
hash = hash * includedChildren * PRIME + hashCode(item, customizer);
break;
}
}
}
break;
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
hash += hash * PRIME + ((CharacterData) arg0).getData().trim().hashCode();
break;
default:
break;
}
return hash;
}
Aggregations