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.LengthManager;
25 import org.apache.batik.css.engine.value.ListValue;
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.DOMException;
33 import org.w3c.dom.css.CSSPrimitiveValue;
34 import org.w3c.dom.css.CSSValue;
35
36 /**
37  * This class provides a manager for the 'enable-background' property values.
38  *
39  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
40  * @version $Id: EnableBackgroundManager.java 1733416 2016-03-03 07:07:13Z gadams $
41  */

42 public class EnableBackgroundManager extends LengthManager {
43     
44     /**
45      * The length orientation.
46      */

47     protected int orientation;
48
49     /**
50      * Implements {@link ValueManager#isInheritedProperty()}.
51      */

52     public boolean isInheritedProperty() {
53         return false;
54     }
55
56     /**
57      * Implements {@link ValueManager#isAnimatableProperty()}.
58      */

59     public boolean isAnimatableProperty() {
60         return false;
61     }
62
63     /**
64      * Implements {@link ValueManager#isAdditiveProperty()}.
65      */

66     public boolean isAdditiveProperty() {
67         return false;
68     }
69
70     /**
71      * Implements {@link ValueManager#getPropertyType()}.
72      */

73     public int getPropertyType() {
74         return SVGTypes.TYPE_ENABLE_BACKGROUND_VALUE;
75     }
76
77     /**
78      * Implements {@link ValueManager#getPropertyName()}.
79      */

80     public String getPropertyName() {
81         return CSSConstants.CSS_ENABLE_BACKGROUND_PROPERTY;
82     }
83     
84     /**
85      * Implements {@link ValueManager#getDefaultValue()}.
86      */

87     public Value getDefaultValue() {
88         return SVGValueConstants.ACCUMULATE_VALUE;
89     }
90
91     /**
92      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
93      */

94     public Value createValue(LexicalUnit lu, CSSEngine engine)
95         throws DOMException {
96         switch (lu.getLexicalUnitType()) {
97         case LexicalUnit.SAC_INHERIT:
98             return SVGValueConstants.INHERIT_VALUE;
99
100         default:
101             throw createInvalidLexicalUnitDOMException
102                 (lu.getLexicalUnitType());
103
104         case LexicalUnit.SAC_IDENT:
105             String id = lu.getStringValue().toLowerCase().intern();
106             if (id == CSSConstants.CSS_ACCUMULATE_VALUE) {
107                 return SVGValueConstants.ACCUMULATE_VALUE;
108             }
109             if (id != CSSConstants.CSS_NEW_VALUE) {
110                 throw createInvalidIdentifierDOMException(id);
111             }
112             ListValue result = new ListValue(' ');
113             result.append(SVGValueConstants.NEW_VALUE);
114             lu = lu.getNextLexicalUnit();
115             if (lu == null) {
116                 return result;
117             }
118             result.append(super.createValue(lu, engine));
119             for (int i = 1; i < 4; i++) {
120                 lu = lu.getNextLexicalUnit();
121                 if (lu == null){
122                     throw createMalformedLexicalUnitDOMException();
123                 }
124                 result.append(super.createValue(lu, engine));
125             }
126             return result;
127         }
128     }
129
130     /**
131      * Implements {@link
132      * ValueManager#createStringValue(short,String,CSSEngine)}.
133      */

134     public Value createStringValue(short type, String value,
135                                    CSSEngine engine) {
136         if (type != CSSPrimitiveValue.CSS_IDENT) {
137             throw createInvalidStringTypeDOMException(type);
138         }
139         if (!value.equalsIgnoreCase(CSSConstants.CSS_ACCUMULATE_VALUE)) {
140             throw createInvalidIdentifierDOMException(value);
141         }
142         return SVGValueConstants.ACCUMULATE_VALUE;
143     }
144
145     /**
146      * Implements {@link ValueManager#createFloatValue(short,float)}.
147      */

148     public Value createFloatValue(short unitType, float floatValue)
149         throws DOMException {
150         throw createDOMException();
151     }
152
153     /**
154      * Implements {@link
155      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
156      */

157     public Value computeValue(CSSStylableElement elt,
158                               String pseudo,
159                               CSSEngine engine,
160                               int idx,
161                               StyleMap sm,
162                               Value value) {
163         if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
164             ListValue lv = (ListValue)value;
165             if (lv.getLength() == 5) {
166                 Value lv1 = lv.item(1);
167                 orientation = HORIZONTAL_ORIENTATION;
168                 Value v1 = super.computeValue(elt, pseudo, engine,
169                                               idx, sm, lv1);
170                 Value lv2 = lv.item(2);
171                 orientation = VERTICAL_ORIENTATION;
172                 Value v2 = super.computeValue(elt, pseudo, engine,
173                                               idx, sm, lv2);
174                 Value lv3 = lv.item(3);
175                 orientation = HORIZONTAL_ORIENTATION;
176                 Value v3 = super.computeValue(elt, pseudo, engine,
177                                               idx, sm, lv3);
178                 Value lv4 = lv.item(4);
179                 orientation = VERTICAL_ORIENTATION;
180                 Value v4 = super.computeValue(elt, pseudo, engine,
181                                               idx, sm, lv4);
182
183                 if (lv1 != v1 || lv2 != v2 ||
184                     lv3 != v3 || lv4 != v4) {
185                     ListValue result = new ListValue(' ');
186                     result.append(lv.item(0));
187                     result.append(v1);
188                     result.append(v2);
189                     result.append(v3);
190                     result.append(v4);
191                     return result;
192                 }
193             }
194         }
195         return value;
196     }
197
198     /**
199      * Indicates the orientation of the property associated with
200      * this manager.
201      */

202     protected int getOrientation() {
203         return orientation;
204     }
205
206 }
207