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.css2;
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.AbstractValueManager;
25 import org.apache.batik.css.engine.value.ListValue;
26 import org.apache.batik.css.engine.value.StringMap;
27 import org.apache.batik.css.engine.value.URIValue;
28 import org.apache.batik.css.engine.value.Value;
29 import org.apache.batik.css.engine.value.ValueConstants;
30 import org.apache.batik.css.engine.value.ValueManager;
31 import org.apache.batik.util.CSSConstants;
32 import org.apache.batik.util.SVGTypes;
33
34 import org.w3c.css.sac.LexicalUnit;
35 import org.w3c.dom.DOMException;
36 import org.w3c.dom.css.CSSPrimitiveValue;
37 import org.w3c.dom.css.CSSValue;
38
39 /**
40  * This class provides a manager for the 'cursor' property values.
41  *
42  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
43  * @version $Id: CursorManager.java 1733416 2016-03-03 07:07:13Z gadams $
44  */

45 public class CursorManager extends AbstractValueManager {
46
47     /**
48      * The identifier values.
49      */

50     protected static final StringMap values = new StringMap();
51     static {
52         values.put(CSSConstants.CSS_AUTO_VALUE,
53                    ValueConstants.AUTO_VALUE);
54         values.put(CSSConstants.CSS_CROSSHAIR_VALUE,
55                    ValueConstants.CROSSHAIR_VALUE);
56         values.put(CSSConstants.CSS_DEFAULT_VALUE,
57                    ValueConstants.DEFAULT_VALUE);
58         values.put(CSSConstants.CSS_E_RESIZE_VALUE,
59                    ValueConstants.E_RESIZE_VALUE);
60         values.put(CSSConstants.CSS_HELP_VALUE,
61                    ValueConstants.HELP_VALUE);
62         values.put(CSSConstants.CSS_MOVE_VALUE,
63                    ValueConstants.MOVE_VALUE);
64         values.put(CSSConstants.CSS_N_RESIZE_VALUE,
65                    ValueConstants.N_RESIZE_VALUE);
66         values.put(CSSConstants.CSS_NE_RESIZE_VALUE,
67                    ValueConstants.NE_RESIZE_VALUE);
68         values.put(CSSConstants.CSS_NW_RESIZE_VALUE,
69                    ValueConstants.NW_RESIZE_VALUE);
70         values.put(CSSConstants.CSS_POINTER_VALUE,
71                    ValueConstants.POINTER_VALUE);
72         values.put(CSSConstants.CSS_S_RESIZE_VALUE,
73                    ValueConstants.S_RESIZE_VALUE);
74         values.put(CSSConstants.CSS_SE_RESIZE_VALUE,
75                    ValueConstants.SE_RESIZE_VALUE);
76         values.put(CSSConstants.CSS_SW_RESIZE_VALUE,
77                    ValueConstants.SW_RESIZE_VALUE);
78         values.put(CSSConstants.CSS_TEXT_VALUE,
79                    ValueConstants.TEXT_VALUE);
80         values.put(CSSConstants.CSS_W_RESIZE_VALUE,
81                    ValueConstants.W_RESIZE_VALUE);
82         values.put(CSSConstants.CSS_WAIT_VALUE,
83                    ValueConstants.WAIT_VALUE);
84     }
85
86     /**
87      * Implements {@link ValueManager#isInheritedProperty()}.
88      */

89     public boolean isInheritedProperty() {
90         return true;
91     }
92
93     /**
94      * Implements {@link ValueManager#isAnimatableProperty()}.
95      */

96     public boolean isAnimatableProperty() {
97         return true;
98     }
99
100     /**
101      * Implements {@link ValueManager#isAdditiveProperty()}.
102      */

103     public boolean isAdditiveProperty() {
104         return false;
105     }
106
107     /**
108      * Implements {@link ValueManager#getPropertyType()}.
109      */

110     public int getPropertyType() {
111         return SVGTypes.TYPE_CURSOR_VALUE;
112     }
113
114     /**
115      * Implements {@link ValueManager#getPropertyName()}.
116      */

117     public String getPropertyName() {
118         return CSSConstants.CSS_CURSOR_PROPERTY;
119     }
120
121     /**
122      * Implements {@link ValueManager#getDefaultValue()}.
123      */

124     public Value getDefaultValue() {
125         return ValueConstants.AUTO_VALUE;
126     }
127
128     /**
129      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
130      */

131     public Value createValue(LexicalUnit lu, CSSEngine engine)
132         throws DOMException {
133         ListValue result = new ListValue();
134         switch (lu.getLexicalUnitType()) {
135         case LexicalUnit.SAC_INHERIT:
136             return ValueConstants.INHERIT_VALUE;
137
138         case LexicalUnit.SAC_URI:
139             do {
140                 result.append(new URIValue(lu.getStringValue(),
141                                            resolveURI(engine.getCSSBaseURI(),
142                                                       lu.getStringValue())));
143                 lu = lu.getNextLexicalUnit();
144                 if (lu == null) {
145                     throw createMalformedLexicalUnitDOMException();
146                 }
147                 if (lu.getLexicalUnitType() !=
148                     LexicalUnit.SAC_OPERATOR_COMMA) {
149                     throw createInvalidLexicalUnitDOMException
150                         (lu.getLexicalUnitType());
151                 }
152                 lu = lu.getNextLexicalUnit();
153                 if (lu == null) {
154                     throw createMalformedLexicalUnitDOMException();
155                 }
156             } while (lu.getLexicalUnitType() == LexicalUnit.SAC_URI);
157             if (lu.getLexicalUnitType() != LexicalUnit.SAC_IDENT) {
158                 throw createInvalidLexicalUnitDOMException
159                     (lu.getLexicalUnitType());
160             }
161             // Fall through...
162
163         case LexicalUnit.SAC_IDENT:
164             String s = lu.getStringValue().toLowerCase().intern();
165             Object v = values.get(s);
166             if (v == null) {
167                 throw createInvalidIdentifierDOMException(lu.getStringValue());
168             }
169             result.append((Value)v);
170             lu = lu.getNextLexicalUnit();
171         }
172         if (lu != null) {
173             throw createInvalidLexicalUnitDOMException
174                 (lu.getLexicalUnitType());
175         }
176         return result;
177     }
178
179     /**
180      * Implements {@link
181      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
182      */

183     public Value computeValue(CSSStylableElement elt,
184                               String pseudo,
185                               CSSEngine engine,
186                               int idx,
187                               StyleMap sm,
188                               Value value) {
189         if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
190             ListValue lv = (ListValue)value;
191             int len = lv.getLength();
192             ListValue result = new ListValue(' ');
193             for (int i=0; i<len; i++) {
194                 Value v = lv.item(0);
195                 if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_URI) {
196                     // Reveal the absolute value as the cssText now.
197                     result.append(new URIValue(v.getStringValue(),
198                                                v.getStringValue()));
199                 } else {
200                     result.append(v);
201                 }
202             }
203             return result;
204         }
205         return super.computeValue(elt, pseudo, engine, idx, sm, value);
206     }
207
208 }
209