Search in sources :

Example 11 with CharacterData

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();
}
Also used : Comment(org.w3c.dom.Comment) CharacterData(org.w3c.dom.CharacterData) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference)

Example 12 with CharacterData

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()));
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CharacterData(org.w3c.dom.CharacterData) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Attr(org.w3c.dom.Attr)

Example 13 with CharacterData

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");
    }
}
Also used : CharacterData(org.w3c.dom.CharacterData) Element(org.w3c.dom.Element) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 14 with CharacterData

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;
}
Also used : CharacterData(org.w3c.dom.CharacterData) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Aggregations

CharacterData (org.w3c.dom.CharacterData)14 NodeList (org.w3c.dom.NodeList)13 Node (org.w3c.dom.Node)11 Comment (org.w3c.dom.Comment)7 EntityReference (org.w3c.dom.EntityReference)7 Element (org.w3c.dom.Element)6 Document (org.w3c.dom.Document)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SVGStylableElement (org.apache.batik.anim.dom.SVGStylableElement)1 GenericCDATASection (org.apache.batik.dom.GenericCDATASection)1 GenericText (org.apache.batik.dom.GenericText)1 Attr (org.w3c.dom.Attr)1 ProcessingInstruction (org.w3c.dom.ProcessingInstruction)1