KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.batik.util.SVGConstants;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * The <code>DefaultStyleHandler</code> class provides the default
31  * way to style an SVG <code>Element</code>.
32  *
33  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
34  * @version $Id: DefaultStyleHandler.java,v 1.5 2004/08/18 07:14:59 vhardy Exp $
35  */

36 public class DefaultStyleHandler implements StyleHandler, SVGConstants {
37     /**
38      * Static initializer for which attributes should be ignored on
39      * some elements.
40      */

41     static HashMap JavaDoc ignoreAttributes = new HashMap JavaDoc();
42
43     static {
44         Vector JavaDoc textAttributes = new Vector JavaDoc();
45         textAttributes.addElement(SVG_FONT_SIZE_ATTRIBUTE);
46         textAttributes.addElement(SVG_FONT_FAMILY_ATTRIBUTE);
47         textAttributes.addElement(SVG_FONT_STYLE_ATTRIBUTE);
48         textAttributes.addElement(SVG_FONT_WEIGHT_ATTRIBUTE);
49
50         ignoreAttributes.put(SVG_RECT_TAG, textAttributes);
51         ignoreAttributes.put(SVG_CIRCLE_TAG, textAttributes);
52         ignoreAttributes.put(SVG_ELLIPSE_TAG, textAttributes);
53         ignoreAttributes.put(SVG_POLYGON_TAG, textAttributes);
54         ignoreAttributes.put(SVG_POLYGON_TAG, textAttributes);
55         ignoreAttributes.put(SVG_LINE_TAG, textAttributes);
56         ignoreAttributes.put(SVG_PATH_TAG, textAttributes);
57     }
58
59     /**
60      * Sets the style described by <code>styleMap</code> on the given
61      * <code>element</code>. That is sets the xml attributes with their
62      * styled value.
63      * @param element the SVG <code>Element</code> to be styled.
64      * @param styleMap the <code>Map</code> containing pairs of style
65      * property names, style values.
66      */

67     public void setStyle(Element JavaDoc element, Map JavaDoc styleMap,
68                          SVGGeneratorContext generatorContext) {
69         String JavaDoc tagName = element.getTagName();
70         Iterator JavaDoc iter = styleMap.keySet().iterator();
71         String JavaDoc styleName = null;
72         while (iter.hasNext()) {
73             styleName = (String JavaDoc)iter.next();
74             if (element.getAttributeNS(null, styleName).length() == 0){
75                 if (appliesTo(styleName, tagName)) {
76                     element.setAttributeNS(null, styleName,
77                                            (String JavaDoc)styleMap.get(styleName));
78                 }
79             }
80         }
81     }
82
83     /**
84      * Controls whether or not a given attribute applies to a particular
85      * element.
86      */

87     protected boolean appliesTo(String JavaDoc styleName, String JavaDoc tagName) {
88         Vector JavaDoc v = (Vector JavaDoc)ignoreAttributes.get(tagName);
89         if (v == null) {
90             return true;
91         } else {
92             return !v.contains(styleName);
93         }
94     }
95 }
96
Popular Tags