Search in sources :

Example 6 with CharacterData

use of org.w3c.dom.CharacterData in project knime-core by knime.

the class XMLDOMCellReader method removeEmptyTextRecursive.

/**
 * Removes all descendent text nodes that contain only whitespace. These
 * come from the newlines and indentation between child elements. Take
 * xml:space declaration into account.
 */
private void removeEmptyTextRecursive(final Node node, final List<Boolean> preserveSpaceStack) {
    boolean hasXmlSpaceAttr = false;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            if (attr.getNodeName().equals("xml:space") && attr.getNamespaceURI().equals(XMLConstants.XML_NS_URI)) {
                if (attr.getNodeValue().equals("preserve")) {
                    hasXmlSpaceAttr = true;
                    preserveSpaceStack.add(0, true);
                } else if (attr.getNodeValue().equals("default")) {
                    hasXmlSpaceAttr = true;
                    preserveSpaceStack.add(0, false);
                } else {
                // Wrong attribute value of xml:space, ignored.
                }
            }
        }
    }
    boolean preserveSpace = !preserveSpaceStack.isEmpty() && preserveSpaceStack.get(0);
    List<Node> toRemove = new ArrayList<Node>();
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);
        switch(child.getNodeType()) {
            case Node.ELEMENT_NODE:
                removeEmptyTextRecursive(child, preserveSpaceStack);
                break;
            case Node.CDATA_SECTION_NODE:
            case Node.TEXT_NODE:
                if (!preserveSpace) {
                    String str = child.getNodeValue();
                    if (null == str || str.trim().isEmpty()) {
                        toRemove.add(child);
                    } else {
                        ((CharacterData) child).setData(str);
                    }
                }
                break;
            default:
        }
    }
    for (Node child : toRemove) {
        node.removeChild(child);
    }
    if (hasXmlSpaceAttr) {
        preserveSpaceStack.remove(0);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CharacterData(org.w3c.dom.CharacterData) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList)

Example 7 with CharacterData

use of org.w3c.dom.CharacterData in project yamcs-studio by yamcs.

the class SimpleImageTranscoder method changeColor.

private void changeColor(Document doc, Color oldColor, Color newColor) {
    if (oldColor.equals(newColor))
        return;
    Matcher matcher = null;
    String svgOldColor = toHexString(oldColor.getRed(), oldColor.getGreen(), oldColor.getBlue());
    String svgNewColor = toHexString(newColor.getRed(), newColor.getGreen(), newColor.getBlue());
    Pattern fillPattern = Pattern.compile("(?i)" + CSSConstants.CSS_FILL_PROPERTY + ":" + svgOldColor);
    Pattern strokePattern = Pattern.compile("(?i)" + CSSConstants.CSS_STROKE_PROPERTY + ":" + svgOldColor);
    String fillReplace = CSSConstants.CSS_FILL_PROPERTY + ":" + svgNewColor;
    String strokeReplace = CSSConstants.CSS_STROKE_PROPERTY + ":" + svgNewColor;
    // Search for global style element <style type="text/css"></style>
    NodeList styleList = doc.getElementsByTagName("style");
    for (int i = 0; i < styleList.getLength(); i++) {
        Element style = (Element) styleList.item(i);
        NodeList childList = style.getChildNodes();
        if (childList != null) {
            for (int j = 0; j < childList.getLength(); j++) {
                Node child = childList.item(j);
                if (child instanceof GenericText || child instanceof GenericCDATASection) {
                    CharacterData cdata = (CharacterData) child;
                    String data = cdata.getData();
                    matcher = fillPattern.matcher(data);
                    data = matcher.replaceAll(fillReplace);
                    matcher = strokePattern.matcher(data);
                    data = matcher.replaceAll(strokeReplace);
                    data = replaceRGB(oldColor, newColor, data);
                    cdata.setData(data);
                }
            }
        }
    }
    recursiveCC(doc.getDocumentElement(), oldColor, newColor, fillPattern, strokePattern, fillReplace, strokeReplace);
}
Also used : Pattern(java.util.regex.Pattern) GenericCDATASection(org.apache.batik.dom.GenericCDATASection) CharacterData(org.w3c.dom.CharacterData) Matcher(java.util.regex.Matcher) NodeList(org.w3c.dom.NodeList) SVGSVGElement(org.w3c.dom.svg.SVGSVGElement) SVGStylableElement(org.apache.batik.anim.dom.SVGStylableElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) GenericText(org.apache.batik.dom.GenericText)

Example 8 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 9 with CharacterData

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

the class NamespaceHandler 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 10 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)

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