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

19 package org.apache.batik.css.engine.value.svg;
20
21 import org.apache.batik.css.engine.CSSEngine;
22 import org.apache.batik.css.engine.CSSStylableElement;
23 import org.apache.batik.css.engine.StyleMap;
24 import org.apache.batik.css.engine.value.ListValue;
25 import org.apache.batik.css.engine.value.URIValue;
26 import org.apache.batik.css.engine.value.Value;
27 import org.apache.batik.css.engine.value.ValueManager;
28 import org.apache.batik.util.CSSConstants;
29 import org.apache.batik.util.SVGTypes;
30
31 import org.w3c.css.sac.LexicalUnit;
32 import org.w3c.dom.css.CSSPrimitiveValue;
33 import org.w3c.dom.css.CSSValue;
34 import org.w3c.dom.DOMException;
35
36 /**
37  * This class provides a manager for the SVGPaint property values.
38  *
39  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
40  * @version $Id: SVGPaintManager.java 1733416 2016-03-03 07:07:13Z gadams $
41  */

42 public class SVGPaintManager extends SVGColorManager {
43     
44     /**
45      * Creates a new SVGPaintManager.
46      */

47     public SVGPaintManager(String prop) {
48         super(prop);
49     }
50
51     /**
52      * Creates a new SVGPaintManager.
53      * @param prop The property name.
54      * @param v The default value.
55      */

56     public SVGPaintManager(String prop, Value v) {
57         super(prop, v);
58     }
59
60     /**
61      * Implements {@link ValueManager#isInheritedProperty()}.
62      */

63     public boolean isInheritedProperty() {
64         return true;
65     }
66
67     /**
68      * Implements {@link ValueManager#isAnimatableProperty()}.
69      */

70     public boolean isAnimatableProperty() {
71         return true;
72     }
73
74     /**
75      * Implements {@link ValueManager#isAdditiveProperty()}.
76      */

77     public boolean isAdditiveProperty() {
78         return true;
79     }
80
81     /**
82      * Implements {@link ValueManager#getPropertyType()}.
83      */

84     public int getPropertyType() {
85         return SVGTypes.TYPE_PAINT;
86     }
87
88     /**
89      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
90      */

91     public Value createValue(LexicalUnit lu, CSSEngine engine)
92         throws DOMException {
93         switch (lu.getLexicalUnitType()) {
94         case LexicalUnit.SAC_IDENT:
95             if (lu.getStringValue().equalsIgnoreCase
96                 (CSSConstants.CSS_NONE_VALUE)) {
97                 return SVGValueConstants.NONE_VALUE;
98             }
99             // Fall through
100         default:
101             return super.createValue(lu, engine);
102         case LexicalUnit.SAC_URI:
103         }
104         String value = lu.getStringValue();
105         String uri = resolveURI(engine.getCSSBaseURI(), value);
106         lu = lu.getNextLexicalUnit();
107         if (lu == null) {
108             return new URIValue(value, uri);
109         }
110
111         ListValue result = new ListValue(' ');
112         result.append(new URIValue(value, uri));
113
114         if (lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
115             if (lu.getStringValue().equalsIgnoreCase
116                 (CSSConstants.CSS_NONE_VALUE)) {
117                 result.append(SVGValueConstants.NONE_VALUE);
118                 return result;
119             }
120         }
121         Value v = super.createValue(lu, engine);
122         if (v.getCssValueType() == CSSValue.CSS_CUSTOM) {
123             ListValue lv = (ListValue)v;
124             for (int i = 0; i < lv.getLength(); i++) {
125                 result.append(lv.item(i));
126             }
127         } else {
128             result.append(v);
129         }
130         return result;
131     }
132
133     /**
134      * Implements {@link
135      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
136      */

137     public Value computeValue(CSSStylableElement elt,
138                               String pseudo,
139                               CSSEngine engine,
140                               int idx,
141                               StyleMap sm,
142                               Value value) {
143         if (value == SVGValueConstants.NONE_VALUE) {
144             return value;
145         }
146         if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
147             ListValue lv = (ListValue)value;
148             Value v = lv.item(0);
149             if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_URI) {
150                 v = lv.item(1);
151                 if (v == SVGValueConstants.NONE_VALUE) {
152                     return value;
153                 }
154                 Value t = super.computeValue(elt, pseudo, engine, idx, sm, v);
155                 if (t != v) {
156                     ListValue result = new ListValue(' ');
157                     result.append(lv.item(0));
158                     result.append(t);
159                     if (lv.getLength() == 3) {
160                         result.append(lv.item(1));
161                     }
162                     return result;
163                 }
164                 return value;
165             }
166         }
167         return super.computeValue(elt, pseudo, engine, idx, sm, value);
168     }
169 }
170