Search in sources :

Example 16 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 17 with CharacterData

use of org.w3c.dom.CharacterData in project ma-core-public by infiniteautomation.

the class DomUtil method getText.

/**
 * Extract the textual content from a Node.
 * This is rather like the XPath value of a Node.
 * @param node The node to extract the text from
 * @return The textual value of the node
 */
public static String getText(Node node) {
    StringBuffer reply = new StringBuffer();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child instanceof CharacterData && !(child instanceof Comment)) || child instanceof EntityReference) {
            reply.append(child.getNodeValue());
        } else if (child.getNodeType() == Node.ELEMENT_NODE) {
            reply.append(getText(child));
        }
    }
    return reply.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 18 with CharacterData

use of org.w3c.dom.CharacterData in project spring-framework by spring-projects.

the class DomUtils method getTextValue.

/**
 * Extracts the text value from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes into a single
 * String value, excluding Comment nodes. Only exposes actual user-specified
 * text, no default values of any kind.
 * @see CharacterData
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    Assert.notNull(valueEle, "Element must not be null");
    StringBuilder sb = new StringBuilder();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            sb.append(item.getNodeValue());
        }
    }
    return sb.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 19 with CharacterData

use of org.w3c.dom.CharacterData in project aries by apache.

the class ExtNamespaceHandler 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) org.apache.aries.blueprint(org.apache.aries.blueprint)

Example 20 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)

Aggregations

CharacterData (org.w3c.dom.CharacterData)21 Node (org.w3c.dom.Node)15 NodeList (org.w3c.dom.NodeList)15 Comment (org.w3c.dom.Comment)10 EntityReference (org.w3c.dom.EntityReference)10 Element (org.w3c.dom.Element)8 Document (org.w3c.dom.Document)5 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)2 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 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 org.apache.aries.blueprint (org.apache.aries.blueprint)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