1 18 package org.apache.batik.svggen; 19 20 import java.util.Vector ; 21 22 import org.w3c.dom.Attr ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.NamedNodeMap ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 28 35 public class SVGCSSStyler implements SVGSyntax{ 36 static private String CSS_PROPERTY_VALUE_SEPARATOR = ":"; 37 static private String CSS_RULE_SEPARATOR = ";"; 38 static private String SPACE = " "; 39 40 48 public static void style(Node node){ 49 NamedNodeMap attributes = node.getAttributes(); 50 if (attributes != null){ 51 Element element = (Element )node; 54 StringBuffer styleAttrBuffer = new StringBuffer (); 55 int nAttr = attributes.getLength(); 56 Vector toBeRemoved = new Vector (); 57 for(int i=0; i<nAttr; i++){ 58 Attr attr = (Attr )attributes.item(i); 59 if(SVGStylingAttributes.set.contains(attr.getName())){ 60 styleAttrBuffer.append(attr.getName()); 62 styleAttrBuffer.append(CSS_PROPERTY_VALUE_SEPARATOR); 63 styleAttrBuffer.append(attr.getValue()); 64 styleAttrBuffer.append(CSS_RULE_SEPARATOR); 65 styleAttrBuffer.append(SPACE); 66 toBeRemoved.addElement(attr.getName()); 67 } 68 } 69 70 if(styleAttrBuffer.length() > 0){ 71 element.setAttributeNS(null, 74 SVG_STYLE_ATTRIBUTE, 75 styleAttrBuffer.toString().trim()); 76 77 int n = toBeRemoved.size(); 78 for(int i=0; i<n; i++) 79 element.removeAttribute((String )toBeRemoved.elementAt(i)); 80 } 81 } 84 85 NodeList children = node.getChildNodes(); 87 int nChildren = children.getLength(); 88 for(int i=0; i<nChildren; i++){ 89 Node child = children.item(i); 90 style(child); 91 } 92 93 } 94 } 95 | Popular Tags |