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