001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.css.engine.value.svg;
020:
021: import org.apache.batik.css.engine.CSSEngine;
022: import org.apache.batik.css.engine.CSSStylableElement;
023: import org.apache.batik.css.engine.StyleMap;
024: import org.apache.batik.css.engine.value.ListValue;
025: import org.apache.batik.css.engine.value.Value;
026: import org.apache.batik.css.engine.value.ValueManager;
027: import org.apache.batik.util.CSSConstants;
028: import org.apache.batik.util.SVGTypes;
029:
030: import org.w3c.css.sac.LexicalUnit;
031: import org.w3c.dom.DOMException;
032: import org.w3c.dom.css.CSSValue;
033:
034: /**
035: * This class provides a manager for the SVGColor property values.
036: *
037: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
038: * @version $Id: SVGColorManager.java 475685 2006-11-16 11:16:05Z cam $
039: */
040: public class SVGColorManager extends ColorManager {
041:
042: /**
043: * The name of the handled property.
044: */
045: protected String property;
046:
047: /**
048: * The default value.
049: */
050: protected Value defaultValue;
051:
052: /**
053: * Creates a new SVGColorManager.
054: * The default value is black.
055: */
056: public SVGColorManager(String prop) {
057: this (prop, SVGValueConstants.BLACK_RGB_VALUE);
058: }
059:
060: /**
061: * Creates a new SVGColorManager.
062: */
063: public SVGColorManager(String prop, Value v) {
064: property = prop;
065: defaultValue = v;
066: }
067:
068: /**
069: * Implements {@link ValueManager#isInheritedProperty()}.
070: */
071: public boolean isInheritedProperty() {
072: return false;
073: }
074:
075: /**
076: * Implements {@link ValueManager#isAnimatableProperty()}.
077: */
078: public boolean isAnimatableProperty() {
079: return true;
080: }
081:
082: /**
083: * Implements {@link ValueManager#isAdditiveProperty()}.
084: */
085: public boolean isAdditiveProperty() {
086: return true;
087: }
088:
089: /**
090: * Implements {@link ValueManager#getPropertyType()}.
091: */
092: public int getPropertyType() {
093: return SVGTypes.TYPE_COLOR;
094: }
095:
096: /**
097: * Implements {@link ValueManager#getPropertyName()}.
098: */
099: public String getPropertyName() {
100: return property;
101: }
102:
103: /**
104: * Implements {@link
105: * org.apache.batik.css.engine.value.ValueManager#getDefaultValue()}.
106: */
107: public Value getDefaultValue() {
108: return defaultValue;
109: }
110:
111: /**
112: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
113: */
114: public Value createValue(LexicalUnit lu, CSSEngine engine)
115: throws DOMException {
116: if (lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
117: if (lu.getStringValue().equalsIgnoreCase(
118: CSSConstants.CSS_CURRENTCOLOR_VALUE)) {
119: return SVGValueConstants.CURRENTCOLOR_VALUE;
120: }
121: }
122: Value v = super .createValue(lu, engine);
123: lu = lu.getNextLexicalUnit();
124: if (lu == null) {
125: return v;
126: }
127: if (lu.getLexicalUnitType() != LexicalUnit.SAC_FUNCTION
128: || !lu.getFunctionName().equalsIgnoreCase("icc-color")) {
129: throw createInvalidLexicalUnitDOMException(lu
130: .getLexicalUnitType());
131: }
132: lu = lu.getParameters();
133: if (lu.getLexicalUnitType() != LexicalUnit.SAC_IDENT) {
134: throw createInvalidLexicalUnitDOMException(lu
135: .getLexicalUnitType());
136: }
137: ListValue result = new ListValue(' ');
138: result.append(v);
139:
140: ICCColor icc = new ICCColor(lu.getStringValue());
141: result.append(icc);
142:
143: lu = lu.getNextLexicalUnit();
144: while (lu != null) {
145: if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
146: throw createInvalidLexicalUnitDOMException(lu
147: .getLexicalUnitType());
148: }
149: lu = lu.getNextLexicalUnit();
150: if (lu == null) {
151: throw createInvalidLexicalUnitDOMException((short) -1);
152: }
153: icc.append(getColorValue(lu));
154: lu = lu.getNextLexicalUnit();
155: }
156: return result;
157: }
158:
159: /**
160: * Implements {@link
161: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
162: */
163: public Value computeValue(CSSStylableElement elt, String pseudo,
164: CSSEngine engine, int idx, StyleMap sm, Value value) {
165: if (value == SVGValueConstants.CURRENTCOLOR_VALUE) {
166: sm.putColorRelative(idx, true);
167:
168: int ci = engine.getColorIndex();
169: return engine.getComputedStyle(elt, pseudo, ci);
170: }
171: if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
172: ListValue lv = (ListValue) value;
173: Value v = lv.item(0);
174: Value t = super .computeValue(elt, pseudo, engine, idx, sm,
175: v);
176: if (t != v) {
177: ListValue result = new ListValue(' ');
178: result.append(t);
179: result.append(lv.item(1));
180: return result;
181: }
182: return value;
183: }
184: return super .computeValue(elt, pseudo, engine, idx, sm, value);
185: }
186:
187: /**
188: * Creates a float value usable as a component of an RGBColor.
189: */
190: protected float getColorValue(LexicalUnit lu) {
191: switch (lu.getLexicalUnitType()) {
192: case LexicalUnit.SAC_INTEGER:
193: return lu.getIntegerValue();
194: case LexicalUnit.SAC_REAL:
195: return lu.getFloatValue();
196: }
197: throw createInvalidLexicalUnitDOMException(lu
198: .getLexicalUnitType());
199: }
200: }
|