KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > parser > CSSLexicalUnit


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

18 package org.apache.batik.css.parser;
19
20 import org.w3c.css.sac.LexicalUnit;
21
22 /**
23  * This class implements the {@link LexicalUnit} interface.
24  *
25  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
26  * @version $Id: CSSLexicalUnit.java,v 1.5 2004/08/18 07:13:02 vhardy Exp $
27  */

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

52     protected short lexicalUnitType;
53
54     /**
55      * The next lexical unit.
56      */

57     protected LexicalUnit nextLexicalUnit;
58
59     /**
60      * The previous lexical unit.
61      */

62     protected LexicalUnit previousLexicalUnit;
63
64     /**
65      * Creates a new LexicalUnit.
66      */

67     protected CSSLexicalUnit(short t, LexicalUnit prev) {
68         lexicalUnitType = t;
69         previousLexicalUnit = prev;
70         if (prev != null) {
71             ((CSSLexicalUnit)prev).nextLexicalUnit = this;
72         }
73     }
74
75     /**
76      * <b>SAC</b>: Implements {@link LexicalUnit#getLexicalUnitType()}.
77      */

78     public short getLexicalUnitType() {
79         return lexicalUnitType;
80     }
81     
82     /**
83      * <b>SAC</b>: Implements {@link LexicalUnit#getNextLexicalUnit()}.
84      */

85     public LexicalUnit getNextLexicalUnit() {
86         return nextLexicalUnit;
87     }
88
89     /**
90      * Sets the next lexical unit.
91      */

92     public void setNextLexicalUnit(LexicalUnit lu) {
93         nextLexicalUnit = lu;
94     }
95     
96     /**
97      * <b>SAC</b>: Implements {@link LexicalUnit#getPreviousLexicalUnit()}.
98      */

99     public LexicalUnit getPreviousLexicalUnit() {
100         return previousLexicalUnit;
101     }
102     
103     /**
104      * Sets the previous lexical unit.
105      */

106     public void setPreviousLexicalUnit(LexicalUnit lu) {
107         previousLexicalUnit = lu;
108     }
109     
110     /**
111      * <b>SAC</b>: Implements {@link LexicalUnit#getIntegerValue()}.
112      */

113     public int getIntegerValue() {
114         throw new IllegalStateException JavaDoc();
115     }
116     
117     /**
118      * <b>SAC</b>: Implements {@link LexicalUnit#getFloatValue()}.
119      */

120     public float getFloatValue() {
121         throw new IllegalStateException JavaDoc();
122     }
123     
124     /**
125      * <b>SAC</b>: Implements {@link LexicalUnit#getDimensionUnitText()}.
126      */

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

155     public String JavaDoc getFunctionName() {
156         throw new IllegalStateException JavaDoc();
157     }
158     
159     /**
160      * <b>SAC</b>: Implements {@link LexicalUnit#getParameters()}.
161      */

162     public LexicalUnit getParameters() {
163         throw new IllegalStateException JavaDoc();
164     }
165
166     /**
167      * <b>SAC</b>: Implements {@link LexicalUnit#getStringValue()}.
168      */

169     public String JavaDoc getStringValue() {
170         throw new IllegalStateException JavaDoc();
171     }
172
173     /**
174      * <b>SAC</b>: Implements {@link LexicalUnit#getSubValues()}.
175      */

176     public LexicalUnit getSubValues() {
177         throw new IllegalStateException JavaDoc();
178     }
179
180     /**
181      * Creates a new integer lexical unit.
182      */

183     public static CSSLexicalUnit createSimple(short t, LexicalUnit prev) {
184         return new SimpleLexicalUnit(t, prev);
185     }
186
187     /**
188      * This class represents a simple unit.
189      */

190     protected static class SimpleLexicalUnit extends CSSLexicalUnit {
191
192         /**
193          * Creates a new LexicalUnit.
194          */

195         public SimpleLexicalUnit(short t, LexicalUnit prev) {
196             super(t, prev);
197         }
198     }
199
200     /**
201      * Creates a new integer lexical unit.
202      */

203     public static CSSLexicalUnit createInteger(int val, LexicalUnit prev) {
204         return new IntegerLexicalUnit(val, prev);
205     }
206
207     /**
208      * This class represents an integer unit.
209      */

210     protected static class IntegerLexicalUnit extends CSSLexicalUnit {
211
212         /**
213          * The integer value.
214          */

215         protected int value;
216
217         /**
218          * Creates a new LexicalUnit.
219          */

220         public IntegerLexicalUnit(int val, LexicalUnit prev) {
221             super(LexicalUnit.SAC_INTEGER, prev);
222             value = val;
223         }
224
225         /**
226          * <b>SAC</b>: Implements {@link LexicalUnit#getIntegerValue()}.
227          */

228         public int getIntegerValue() {
229             return value;
230         }
231     }
232
233     /**
234      * Creates a new float lexical unit.
235      */

236     public static CSSLexicalUnit createFloat(short t, float val, LexicalUnit prev) {
237         return new FloatLexicalUnit(t, val, prev);
238     }
239
240     /**
241      * This class represents a float unit.
242      */

243     protected static class FloatLexicalUnit extends CSSLexicalUnit {
244
245         /**
246          * The float value.
247          */

248         protected float value;
249
250         /**
251          * Creates a new LexicalUnit.
252          */

253         public FloatLexicalUnit(short t, float val, LexicalUnit prev) {
254             super(t, prev);
255             value = val;
256         }
257
258         /**
259          * <b>SAC</b>: Implements {@link LexicalUnit#getFloatValue()}.
260          */

261         public float getFloatValue() {
262             return value;
263         }
264     }
265
266     /**
267      * Creates a new float lexical unit.
268      */

269     public static CSSLexicalUnit createDimension(float val, String JavaDoc dim,
270                                                  LexicalUnit prev) {
271         return new DimensionLexicalUnit(val, dim, prev);
272     }
273
274     /**
275      * This class represents a dimension unit.
276      */

277     protected static class DimensionLexicalUnit extends CSSLexicalUnit {
278
279         /**
280          * The float value.
281          */

282         protected float value;
283
284         /**
285          * The dimension.
286          */

287         protected String JavaDoc dimension;
288
289         /**
290          * Creates a new LexicalUnit.
291          */

292         public DimensionLexicalUnit(float val, String JavaDoc dim, LexicalUnit prev) {
293             super(SAC_DIMENSION, prev);
294             value = val;
295             dimension = dim;
296         }
297
298         /**
299          * <b>SAC</b>: Implements {@link LexicalUnit#getFloatValue()}.
300          */

301         public float getFloatValue() {
302             return value;
303         }
304
305         /**
306          * <b>SAC</b>: Implements {@link LexicalUnit#getDimensionUnitText()}.
307          */

308         public String JavaDoc getDimensionUnitText() {
309             return dimension;
310         }
311     }
312
313     /**
314      * Creates a new function lexical unit.
315      */

316     public static CSSLexicalUnit createFunction(String JavaDoc f, LexicalUnit params,
317                                                 LexicalUnit prev) {
318         return new FunctionLexicalUnit(f, params, prev);
319     }
320
321     /**
322      * This class represents a function unit.
323      */

324     protected static class FunctionLexicalUnit extends CSSLexicalUnit {
325
326         /**
327          * The function name.
328          */

329         protected String JavaDoc name;
330
331         /**
332          * The function parameters.
333          */

334         protected LexicalUnit parameters;
335
336         /**
337          * Creates a new LexicalUnit.
338          */

339         public FunctionLexicalUnit(String JavaDoc f, LexicalUnit params, LexicalUnit prev) {
340             super(SAC_FUNCTION, prev);
341             name = f;
342             parameters = params;
343         }
344
345         /**
346          * <b>SAC</b>: Implements {@link LexicalUnit#getFunctionName()}.
347          */

348         public String JavaDoc getFunctionName() {
349             return name;
350         }
351     
352         /**
353          * <b>SAC</b>: Implements {@link LexicalUnit#getParameters()}.
354          */

355         public LexicalUnit getParameters() {
356             return parameters;
357         }
358
359     }
360
361     /**
362      * Creates a new function lexical unit.
363      */

364     public static CSSLexicalUnit createPredefinedFunction(short t, LexicalUnit params,
365                                                           LexicalUnit prev) {
366         return new PredefinedFunctionLexicalUnit(t, params, prev);
367     }
368
369     /**
370      * This class represents a function unit.
371      */

372     protected static class PredefinedFunctionLexicalUnit extends CSSLexicalUnit {
373
374         /**
375          * The function parameters.
376          */

377         protected LexicalUnit parameters;
378
379         /**
380          * Creates a new LexicalUnit.
381          */

382         public PredefinedFunctionLexicalUnit(short t, LexicalUnit params,
383                                              LexicalUnit prev) {
384             super(t, prev);
385             parameters = params;
386         }
387
388         /**
389          * <b>SAC</b>: Implements {@link LexicalUnit#getParameters()}.
390          */

391         public LexicalUnit getParameters() {
392             return parameters;
393         }
394     }
395
396     /**
397      * Creates a new string lexical unit.
398      */

399     public static CSSLexicalUnit createString(short t, String JavaDoc val, LexicalUnit prev) {
400         return new StringLexicalUnit(t, val, prev);
401     }
402
403     /**
404      * This class represents a string unit.
405      */

406     protected static class StringLexicalUnit extends CSSLexicalUnit {
407
408         /**
409          * The string value.
410          */

411         protected String JavaDoc value;
412
413         /**
414          * Creates a new LexicalUnit.
415          */

416         public StringLexicalUnit(short t, String JavaDoc val, LexicalUnit prev) {
417             super(t, prev);
418             value = val;
419         }
420
421         /**
422          * <b>SAC</b>: Implements {@link LexicalUnit#getStringValue()}.
423          */

424         public String JavaDoc getStringValue() {
425             return value;
426         }
427     }
428 }
429
Popular Tags