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.parser;
20
21 import org.w3c.css.sac.LexicalUnit;
22
23 /**
24  * This class implements the {@link LexicalUnit} interface.
25  *
26  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
27  * @version $Id: CSSLexicalUnit.java 1733416 2016-03-03 07:07:13Z gadams $
28  */

29 public abstract class CSSLexicalUnit implements LexicalUnit {
30
31     public static final String UNIT_TEXT_CENTIMETER  = "cm";
32     public static final String UNIT_TEXT_DEGREE      = "deg";
33     public static final String UNIT_TEXT_EM          = "em";
34     public static final String UNIT_TEXT_EX          = "ex";
35     public static final String UNIT_TEXT_GRADIAN     = "grad";
36     public static final String UNIT_TEXT_HERTZ       = "Hz";
37     public static final String UNIT_TEXT_INCH        = "in";
38     public static final String UNIT_TEXT_KILOHERTZ   = "kHz";
39     public static final String UNIT_TEXT_MILLIMETER  = "mm";
40     public static final String UNIT_TEXT_MILLISECOND = "ms";
41     public static final String UNIT_TEXT_PERCENTAGE  = "%";
42     public static final String UNIT_TEXT_PICA        = "pc";
43     public static final String UNIT_TEXT_PIXEL       = "px";
44     public static final String UNIT_TEXT_POINT       = "pt";
45     public static final String UNIT_TEXT_RADIAN      = "rad";
46     public static final String UNIT_TEXT_REAL        = "";
47     public static final String UNIT_TEXT_SECOND      = "s";
48     
49     public static final String TEXT_RGBCOLOR          = "rgb";
50     public static final String TEXT_RECT_FUNCTION     = "rect";
51     public static final String TEXT_COUNTER_FUNCTION  = "counter";
52     public static final String TEXT_COUNTERS_FUNCTION = "counters";
53
54     /**
55      * The lexical unit type.
56      */

57     protected short lexicalUnitType;
58
59     /**
60      * The next lexical unit.
61      */

62     protected LexicalUnit nextLexicalUnit;
63
64     /**
65      * The previous lexical unit.
66      */

67     protected LexicalUnit previousLexicalUnit;
68
69     /**
70      * Creates a new LexicalUnit.
71      */

72     protected CSSLexicalUnit(short t, LexicalUnit prev) {
73         lexicalUnitType = t;
74         previousLexicalUnit = prev;
75         if (prev != null) {
76             ((CSSLexicalUnit)prev).nextLexicalUnit = this;
77         }
78     }
79
80     /**
81      * <b>SAC</b>: Implements {@link LexicalUnit#getLexicalUnitType()}.
82      */

83     public short getLexicalUnitType() {
84         return lexicalUnitType;
85     }
86     
87     /**
88      * <b>SAC</b>: Implements {@link LexicalUnit#getNextLexicalUnit()}.
89      */

90     public LexicalUnit getNextLexicalUnit() {
91         return nextLexicalUnit;
92     }
93
94     /**
95      * Sets the next lexical unit.
96      */

97     public void setNextLexicalUnit(LexicalUnit lu) {
98         nextLexicalUnit = lu;
99     }
100     
101     /**
102      * <b>SAC</b>: Implements {@link LexicalUnit#getPreviousLexicalUnit()}.
103      */

104     public LexicalUnit getPreviousLexicalUnit() {
105         return previousLexicalUnit;
106     }
107     
108     /**
109      * Sets the previous lexical unit.
110      */

111     public void setPreviousLexicalUnit(LexicalUnit lu) {
112         previousLexicalUnit = lu;
113     }
114     
115     /**
116      * <b>SAC</b>: Implements {@link LexicalUnit#getIntegerValue()}.
117      */

118     public int getIntegerValue() {
119         throw new IllegalStateException();
120     }
121     
122     /**
123      * <b>SAC</b>: Implements {@link LexicalUnit#getFloatValue()}.
124      */

125     public float getFloatValue() {
126         throw new IllegalStateException();
127     }
128     
129     /**
130      * <b>SAC</b>: Implements {@link LexicalUnit#getDimensionUnitText()}.
131      */

132     public String getDimensionUnitText() {
133         switch (lexicalUnitType) {
134         case LexicalUnit.SAC_CENTIMETER:  return UNIT_TEXT_CENTIMETER;
135         case LexicalUnit.SAC_DEGREE:      return UNIT_TEXT_DEGREE;
136         case LexicalUnit.SAC_EM:          return UNIT_TEXT_EM;
137         case LexicalUnit.SAC_EX:          return UNIT_TEXT_EX;
138         case LexicalUnit.SAC_GRADIAN:     return UNIT_TEXT_GRADIAN;
139         case LexicalUnit.SAC_HERTZ:       return UNIT_TEXT_HERTZ;
140         case LexicalUnit.SAC_INCH:        return UNIT_TEXT_INCH;
141         case LexicalUnit.SAC_KILOHERTZ:   return UNIT_TEXT_KILOHERTZ;
142         case LexicalUnit.SAC_MILLIMETER:  return UNIT_TEXT_MILLIMETER;
143         case LexicalUnit.SAC_MILLISECOND: return UNIT_TEXT_MILLISECOND;
144         case LexicalUnit.SAC_PERCENTAGE:  return UNIT_TEXT_PERCENTAGE;
145         case LexicalUnit.SAC_PICA:        return UNIT_TEXT_PICA;
146         case LexicalUnit.SAC_PIXEL:       return UNIT_TEXT_PIXEL;
147         case LexicalUnit.SAC_POINT:       return UNIT_TEXT_POINT;
148         case LexicalUnit.SAC_RADIAN:      return UNIT_TEXT_RADIAN;
149         case LexicalUnit.SAC_REAL:        return UNIT_TEXT_REAL;
150         case LexicalUnit.SAC_SECOND:      return UNIT_TEXT_SECOND;
151         default:
152             throw new IllegalStateException("No Unit Text for type: " + 
153                                             lexicalUnitType);
154         }
155     }
156     
157     /**
158      * <b>SAC</b>: Implements {@link LexicalUnit#getFunctionName()}.
159      */

160     public String getFunctionName() {
161         throw new IllegalStateException();
162     }
163     
164     /**
165      * <b>SAC</b>: Implements {@link LexicalUnit#getParameters()}.
166      */

167     public LexicalUnit getParameters() {
168         throw new IllegalStateException();
169     }
170
171     /**
172      * <b>SAC</b>: Implements {@link LexicalUnit#getStringValue()}.
173      */

174     public String getStringValue() {
175         throw new IllegalStateException();
176     }
177
178     /**
179      * <b>SAC</b>: Implements {@link LexicalUnit#getSubValues()}.
180      */

181     public LexicalUnit getSubValues() {
182         throw new IllegalStateException();
183     }
184
185     /**
186      * Creates a new integer lexical unit.
187      */

188     public static CSSLexicalUnit createSimple(short t, LexicalUnit prev) {
189         return new SimpleLexicalUnit(t, prev);
190     }
191
192     /**
193      * This class represents a simple unit.
194      */

195     protected static class SimpleLexicalUnit extends CSSLexicalUnit {
196
197         /**
198          * Creates a new LexicalUnit.
199          */

200         public SimpleLexicalUnit(short t, LexicalUnit prev) {
201             super(t, prev);
202         }
203     }
204
205     /**
206      * Creates a new integer lexical unit.
207      */

208     public static CSSLexicalUnit createInteger(int val, LexicalUnit prev) {
209         return new IntegerLexicalUnit(val, prev);
210     }
211
212     /**
213      * This class represents an integer unit.
214      */

215     protected static class IntegerLexicalUnit extends CSSLexicalUnit {
216
217         /**
218          * The integer value.
219          */

220         protected int value;
221
222         /**
223          * Creates a new LexicalUnit.
224          */

225         public IntegerLexicalUnit(int val, LexicalUnit prev) {
226             super(LexicalUnit.SAC_INTEGER, prev);
227             value = val;
228         }
229
230         /**
231          * <b>SAC</b>: Implements {@link LexicalUnit#getIntegerValue()}.
232          */

233         public int getIntegerValue() {
234             return value;
235         }
236     }
237
238     /**
239      * Creates a new float lexical unit.
240      */

241     public static CSSLexicalUnit createFloat(short t, float val, LexicalUnit prev) {
242         return new FloatLexicalUnit(t, val, prev);
243     }
244
245     /**
246      * This class represents a float unit.
247      */

248     protected static class FloatLexicalUnit extends CSSLexicalUnit {
249
250         /**
251          * The float value.
252          */

253         protected float value;
254
255         /**
256          * Creates a new LexicalUnit.
257          */

258         public FloatLexicalUnit(short t, float val, LexicalUnit prev) {
259             super(t, prev);
260             value = val;
261         }
262
263         /**
264          * <b>SAC</b>: Implements {@link LexicalUnit#getFloatValue()}.
265          */

266         public float getFloatValue() {
267             return value;
268         }
269     }
270
271     /**
272      * Creates a new float lexical unit.
273      */

274     public static CSSLexicalUnit createDimension(float val, String dim,
275                                                  LexicalUnit prev) {
276         return new DimensionLexicalUnit(val, dim, prev);
277     }
278
279     /**
280      * This class represents a dimension unit.
281      */

282     protected static class DimensionLexicalUnit extends CSSLexicalUnit {
283
284         /**
285          * The float value.
286          */

287         protected float value;
288
289         /**
290          * The dimension.
291          */

292         protected String dimension;
293
294         /**
295          * Creates a new LexicalUnit.
296          */

297         public DimensionLexicalUnit(float val, String dim, LexicalUnit prev) {
298             super(SAC_DIMENSION, prev);
299             value = val;
300             dimension = dim;
301         }
302
303         /**
304          * <b>SAC</b>: Implements {@link LexicalUnit#getFloatValue()}.
305          */

306         public float getFloatValue() {
307             return value;
308         }
309
310         /**
311          * <b>SAC</b>: Implements {@link LexicalUnit#getDimensionUnitText()}.
312          */

313         public String getDimensionUnitText() {
314             return dimension;
315         }
316     }
317
318     /**
319      * Creates a new function lexical unit.
320      */

321     public static CSSLexicalUnit createFunction(String f, LexicalUnit params,
322                                                 LexicalUnit prev) {
323         return new FunctionLexicalUnit(f, params, prev);
324     }
325
326     /**
327      * This class represents a function unit.
328      */

329     protected static class FunctionLexicalUnit extends CSSLexicalUnit {
330
331         /**
332          * The function name.
333          */

334         protected String name;
335
336         /**
337          * The function parameters.
338          */

339         protected LexicalUnit parameters;
340
341         /**
342          * Creates a new LexicalUnit.
343          */

344         public FunctionLexicalUnit(String f, LexicalUnit params, LexicalUnit prev) {
345             super(SAC_FUNCTION, prev);
346             name = f;
347             parameters = params;
348         }
349
350         /**
351          * <b>SAC</b>: Implements {@link LexicalUnit#getFunctionName()}.
352          */

353         public String getFunctionName() {
354             return name;
355         }
356     
357         /**
358          * <b>SAC</b>: Implements {@link LexicalUnit#getParameters()}.
359          */

360         public LexicalUnit getParameters() {
361             return parameters;
362         }
363
364     }
365
366     /**
367      * Creates a new function lexical unit.
368      */

369     public static CSSLexicalUnit createPredefinedFunction(short t, LexicalUnit params,
370                                                           LexicalUnit prev) {
371         return new PredefinedFunctionLexicalUnit(t, params, prev);
372     }
373
374     /**
375      * This class represents a function unit.
376      */

377     protected static class PredefinedFunctionLexicalUnit extends CSSLexicalUnit {
378
379         /**
380          * The function parameters.
381          */

382         protected LexicalUnit parameters;
383
384         /**
385          * Creates a new LexicalUnit.
386          */

387         public PredefinedFunctionLexicalUnit(short t, LexicalUnit params,
388                                              LexicalUnit prev) {
389             super(t, prev);
390             parameters = params;
391         }
392         /**
393          * <b>SAC</b>: Implements {@link LexicalUnit#getFunctionName()}.
394          */

395         public String getFunctionName() {
396             switch (lexicalUnitType) {
397             case SAC_RGBCOLOR:          return TEXT_RGBCOLOR;
398             case SAC_RECT_FUNCTION:     return TEXT_RECT_FUNCTION;
399             case SAC_COUNTER_FUNCTION:  return TEXT_COUNTER_FUNCTION;
400             case SAC_COUNTERS_FUNCTION: return TEXT_COUNTERS_FUNCTION;
401             defaultbreak;
402             }
403             return super.getFunctionName();
404         }
405     
406
407         /**
408          * <b>SAC</b>: Implements {@link LexicalUnit#getParameters()}.
409          */

410         public LexicalUnit getParameters() {
411             return parameters;
412         }
413     }
414
415     /**
416      * Creates a new string lexical unit.
417      */

418     public static CSSLexicalUnit createString(short t, String val, LexicalUnit prev) {
419         return new StringLexicalUnit(t, val, prev);
420     }
421
422     /**
423      * This class represents a string unit.
424      */

425     protected static class StringLexicalUnit extends CSSLexicalUnit {
426
427         /**
428          * The string value.
429          */

430         protected String value;
431
432         /**
433          * Creates a new LexicalUnit.
434          */

435         public StringLexicalUnit(short t, String val, LexicalUnit prev) {
436             super(t, prev);
437             value = val;
438         }
439
440         /**
441          * <b>SAC</b>: Implements {@link LexicalUnit#getStringValue()}.
442          */

443         public String getStringValue() {
444             return value;
445         }
446     }
447 }
448