KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > svg > SVGColorManager


1 /*
2
3    Copyright 2002-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.css.engine.value.svg;
19
20 import org.apache.batik.css.engine.CSSEngine;
21 import org.apache.batik.css.engine.CSSStylableElement;
22 import org.apache.batik.css.engine.StyleMap;
23 import org.apache.batik.css.engine.value.ListValue;
24 import org.apache.batik.css.engine.value.Value;
25 import org.apache.batik.css.engine.value.ValueManager;
26 import org.apache.batik.util.CSSConstants;
27 import org.w3c.css.sac.LexicalUnit;
28 import org.w3c.dom.DOMException JavaDoc;
29 import org.w3c.dom.css.CSSValue;
30
31 /**
32  * This class provides a manager for the SVGColor property values.
33  *
34  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
35  * @version $Id: SVGColorManager.java,v 1.6 2005/03/27 08:58:31 cam Exp $
36  */

37 public class SVGColorManager extends ColorManager {
38
39     /**
40      * The name of the handled property.
41      */

42     protected String JavaDoc property;
43
44     /**
45      * The default value.
46      */

47     protected Value defaultValue;
48
49     /**
50      * Creates a new SVGColorManager.
51      * The default value is black.
52      */

53     public SVGColorManager(String JavaDoc prop) {
54         this(prop, SVGValueConstants.BLACK_RGB_VALUE);
55     }
56
57     /**
58      * Creates a new SVGColorManager.
59      */

60     public SVGColorManager(String JavaDoc prop, Value v) {
61         property = prop;
62         defaultValue = v;
63     }
64
65     /**
66      * Implements {@link ValueManager#isInheritedProperty()}.
67      */

68     public boolean isInheritedProperty() {
69     return false;
70     }
71
72     /**
73      * Implements {@link ValueManager#getPropertyName()}.
74      */

75     public String JavaDoc getPropertyName() {
76     return property;
77     }
78
79     
80     /**
81      * Implements {@link
82      * org.apache.batik.css.engine.value.ValueManager#getDefaultValue()}.
83      */

84     public Value getDefaultValue() {
85         return defaultValue;
86     }
87     
88     /**
89      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
90      */

91     public Value createValue(LexicalUnit lu, CSSEngine engine)
92         throws DOMException JavaDoc {
93         if (lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
94             if (lu.getStringValue().equalsIgnoreCase
95                 (CSSConstants.CSS_CURRENTCOLOR_VALUE)) {
96                 return SVGValueConstants.CURRENTCOLOR_VALUE;
97             }
98         }
99         Value v = super.createValue(lu, engine);
100         lu = lu.getNextLexicalUnit();
101         if (lu == null) {
102             return v;
103         }
104         if (lu.getLexicalUnitType() != LexicalUnit.SAC_FUNCTION ||
105             !lu.getFunctionName().equalsIgnoreCase("icc-color")) {
106             throw createInvalidLexicalUnitDOMException
107                 (lu.getLexicalUnitType());
108         }
109         lu = lu.getParameters();
110         if (lu.getLexicalUnitType() != LexicalUnit.SAC_IDENT) {
111             throw createInvalidLexicalUnitDOMException
112                 (lu.getLexicalUnitType());
113         }
114         ListValue result = new ListValue(' ');
115         result.append(v);
116
117         ICCColor icc = new ICCColor(lu.getStringValue());
118         result.append(icc);
119
120         lu = lu.getNextLexicalUnit();
121         while (lu != null) {
122             if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
123                 throw createInvalidLexicalUnitDOMException
124                     (lu.getLexicalUnitType());
125             }
126             lu = lu.getNextLexicalUnit();
127             if (lu == null) {
128                 throw createInvalidLexicalUnitDOMException((short)-1);
129             }
130             icc.append(getColorValue(lu));
131             lu = lu.getNextLexicalUnit();
132         }
133         return result;
134     }
135
136     /**
137      * Implements {@link
138      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
139      */

140     public Value computeValue(CSSStylableElement elt,
141                               String JavaDoc pseudo,
142                               CSSEngine engine,
143                               int idx,
144                               StyleMap sm,
145                               Value value) {
146         if (value == SVGValueConstants.CURRENTCOLOR_VALUE) {
147             sm.putColorRelative(idx, true);
148
149             int ci = engine.getColorIndex();
150             return engine.getComputedStyle(elt, pseudo, ci);
151         }
152         if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
153             ListValue lv = (ListValue)value;
154             Value v = lv.item(0);
155             Value t = super.computeValue(elt, pseudo, engine, idx, sm, v);
156             if (t != v) {
157                 ListValue result = new ListValue(' ');
158                 result.append(t);
159                 result.append(lv.item(1));
160                 return result;
161             }
162             return value;
163         }
164         return super.computeValue(elt, pseudo, engine, idx, sm, value);
165     }
166
167     /**
168      * Creates a float value usable as a component of an RGBColor.
169      */

170     protected float getColorValue(LexicalUnit lu) {
171     switch (lu.getLexicalUnitType()) {
172     case LexicalUnit.SAC_INTEGER:
173         return lu.getIntegerValue();
174     case LexicalUnit.SAC_REAL:
175         return lu.getFloatValue();
176     }
177         throw createInvalidLexicalUnitDOMException(lu.getLexicalUnitType());
178     }
179 }
180
Popular Tags