KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGCSSStyler


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen;
19
20 import java.util.Vector JavaDoc;
21
22 import org.w3c.dom.Attr JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.NamedNodeMap JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28 /**
29  * This utility class converts a standard SVG document that uses
30  * attribute into one that uses the CSS style attribute instead
31  *
32  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
33  * @version $Id: SVGCSSStyler.java,v 1.9 2004/08/18 07:14:59 vhardy Exp $
34  */

35 public class SVGCSSStyler implements SVGSyntax{
36     static private String JavaDoc CSS_PROPERTY_VALUE_SEPARATOR = ":";
37     static private String JavaDoc CSS_RULE_SEPARATOR = ";";
38     static private String JavaDoc SPACE = " ";
39
40     /**
41      * Invoking this method removes all the styling attributes
42      * (such as 'fill' or 'fill-opacity') from the input element
43      * and its descendant and replaces them with their CSS2
44      * property counterparts.
45      * @param node SVG Node to be converted to use style
46      *
47      */

48     public static void style(Node JavaDoc node){
49         NamedNodeMap JavaDoc attributes = node.getAttributes();
50         if (attributes != null){
51             // Has to be an Element, as it has attributes
52
// According to spec.
53
Element JavaDoc element = (Element JavaDoc)node;
54             StringBuffer JavaDoc styleAttrBuffer = new StringBuffer JavaDoc();
55             int nAttr = attributes.getLength();
56             Vector JavaDoc toBeRemoved = new Vector JavaDoc();
57             for(int i=0; i<nAttr; i++){
58                 Attr JavaDoc attr = (Attr JavaDoc)attributes.item(i);
59                 if(SVGStylingAttributes.set.contains(attr.getName())){
60                     // System.out.println("Found new style attribute");
61
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                                 // System.out.println("Setting style attribute on node: " + styleAttrBuffer.toString().trim());
72
// There were some styling attributes
73
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 JavaDoc)toBeRemoved.elementAt(i));
80             }
81             // else
82
// System.out.println("NO STYLE PROPERTIES");
83
}
84
85         // Now, process child elements
86
NodeList JavaDoc children = node.getChildNodes();
87         int nChildren = children.getLength();
88         for(int i=0; i<nChildren; i++){
89             Node JavaDoc child = children.item(i);
90             style(child);
91         }
92
93     }
94 }
95
Popular Tags