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.value.IdentifierManager;
23 import org.apache.batik.css.engine.value.ListValue;
24 import org.apache.batik.css.engine.value.StringMap;
25 import org.apache.batik.css.engine.value.StringValue;
26 import org.apache.batik.css.engine.value.URIValue;
27 import org.apache.batik.css.engine.value.Value;
28 import org.apache.batik.css.engine.value.ValueConstants;
29 import org.apache.batik.css.engine.value.ValueManager;
30 import org.apache.batik.util.CSSConstants;
31 import org.apache.batik.util.SVGTypes;
32
33 import org.w3c.dom.css.CSSPrimitiveValue;
34 import org.w3c.dom.DOMException;
35 import org.w3c.css.sac.LexicalUnit;
36
37 /**
38  * One line Class Desc
39  *
40  * Complete Class Desc
41  *
42  * @author <a href="mailto:deweese@apache.org">l449433</a>
43  * @version $Id: SrcManager.java 1733416 2016-03-03 07:07:13Z gadams $
44  */

45 public class SrcManager extends IdentifierManager {
46
47     /**
48      * The identifier values.
49      */

50     protected static final StringMap values = new StringMap();
51     static {
52         values.put(CSSConstants.CSS_NONE_VALUE,
53                    ValueConstants.NONE_VALUE);
54     }
55
56     public SrcManager() {
57     }
58
59     /**
60      * Implements {@link
61      * org.apache.batik.css.engine.value.ValueManager#isInheritedProperty()}.
62      */

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

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

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

84     public int getPropertyType() {
85         return SVGTypes.TYPE_FONT_DESCRIPTOR_SRC_VALUE;
86     }
87
88     /**
89      * Implements {@link
90      * org.apache.batik.css.engine.value.ValueManager#getPropertyName()}.
91      */

92     public String getPropertyName() {
93         return CSSConstants.CSS_SRC_PROPERTY;
94     }
95
96     /**
97      * Implements {@link
98      * org.apache.batik.css.engine.value.ValueManager#getDefaultValue()}.
99      */

100     public Value getDefaultValue() {
101         return ValueConstants.NONE_VALUE;
102     }
103
104
105     /**
106      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
107      */

108     public Value createValue(LexicalUnit lu, CSSEngine engine)
109         throws DOMException {
110
111         switch (lu.getLexicalUnitType()) {
112         case LexicalUnit.SAC_INHERIT:
113             return ValueConstants.INHERIT_VALUE;
114
115         default:
116             throw createInvalidLexicalUnitDOMException
117                 (lu.getLexicalUnitType());
118
119         case LexicalUnit.SAC_IDENT:
120         case LexicalUnit.SAC_STRING_VALUE:
121         case LexicalUnit.SAC_URI:
122         }
123
124         ListValue result = new ListValue();
125         for (;;) {
126             switch (lu.getLexicalUnitType()) {
127             case LexicalUnit.SAC_STRING_VALUE:
128                 result.append(new StringValue(CSSPrimitiveValue.CSS_STRING,
129                                               lu.getStringValue()));
130                 lu = lu.getNextLexicalUnit();
131                 break;
132
133             case LexicalUnit.SAC_URI:
134                 String uri = resolveURI(engine.getCSSBaseURI(),
135                                         lu.getStringValue());
136
137                 result.append(new URIValue(lu.getStringValue(), uri));
138                 lu = lu.getNextLexicalUnit();
139                 if ((lu != null) &&
140                     (lu.getLexicalUnitType() == LexicalUnit.SAC_FUNCTION)) {
141                     if (!lu.getFunctionName().equalsIgnoreCase("format")) {
142                         break;
143                     }
144                     // Format really does us no good so just ignore it.
145
146                     // TODO: Should probably turn this into a ListValue
147                     // and append the format function CSS Value.
148                     lu = lu.getNextLexicalUnit();
149                 }
150                 break;
151
152             case LexicalUnit.SAC_IDENT:
153                 StringBuffer sb = new StringBuffer(lu.getStringValue());
154                 lu = lu.getNextLexicalUnit();
155                 if (lu != null &&
156                     lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
157                     do {
158                         sb.append(' ');
159                         sb.append(lu.getStringValue());
160                         lu = lu.getNextLexicalUnit();
161                     } while (lu != null &&
162                              lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT);
163                     result.append(new StringValue(CSSPrimitiveValue.CSS_STRING,
164                                                   sb.toString()));
165                 } else {
166                     String id = sb.toString();
167                     String s = id.toLowerCase().intern();
168                     Value v = (Value)values.get(s);
169                     result.append((v != null)
170                                   ? v
171                                   : new StringValue
172                                         (CSSPrimitiveValue.CSS_STRING, id));
173                 }
174                 break;
175             }
176             if (lu == null) {
177                 return result;
178             }
179             if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
180                 throw createInvalidLexicalUnitDOMException
181                     (lu.getLexicalUnitType());
182             }
183             lu = lu.getNextLexicalUnit();
184             if (lu == null) {
185                 throw createMalformedLexicalUnitDOMException();
186             }
187         }
188     }
189
190     /**
191      * Implements {@link IdentifierManager#getIdentifiers()}.
192      */

193     public StringMap getIdentifiers() {
194         return values;
195     }
196 }
197