KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > steadystate > css > parser > LexicalUnitImpl


1 /*
2  * LexicalUnitImpl.java
3  *
4  * Steady State CSS2 Parser
5  *
6  * Copyright (C) 1999, 2002 Steady State Software Ltd. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * To contact the authors of the library, write to Steady State Software Ltd.,
23  * 49 Littleworth, Wing, Buckinghamshire, LU7 0JX, England
24  *
25  * http://www.steadystate.com/css/
26  * mailto:css@steadystate.co.uk
27  *
28  * $Id: LexicalUnitImpl.java,v 1.2 2005/04/28 20:57:20 waldbaer Exp $
29  */

30
31 package com.steadystate.css.parser;
32
33 import java.io.Serializable JavaDoc;
34 import org.w3c.css.sac.*;
35
36 /**
37  *
38  * @author David Schweinsberg
39  * @version $Release$
40  */

41 public class LexicalUnitImpl implements LexicalUnit, Serializable JavaDoc {
42
43 /*
44     public static final short SAC_OPERATOR_COMMA = 0;
45     public static final short SAC_OPERATOR_PLUS = 1;
46     public static final short SAC_OPERATOR_MINUS = 2;
47     public static final short SAC_OPERATOR_MULTIPLY = 3;
48     public static final short SAC_OPERATOR_SLASH = 4;
49     public static final short SAC_OPERATOR_MOD = 5;
50     public static final short SAC_OPERATOR_EXP = 6;
51     public static final short SAC_OPERATOR_LT = 7;
52     public static final short SAC_OPERATOR_GT = 8;
53     public static final short SAC_OPERATOR_LE = 9;
54     public static final short SAC_OPERATOR_GE = 10;
55     public static final short SAC_OPERATOR_TILDE = 11;
56     public static final short SAC_INHERIT = 12;
57     public static final short SAC_INTEGER = 13;
58     public static final short SAC_REAL = 14;
59     public static final short SAC_EM = 15;
60     public static final short SAC_EX = 16;
61     public static final short SAC_PIXEL = 17;
62     public static final short SAC_INCH = 18;
63     public static final short SAC_CENTIMETER = 19;
64     public static final short SAC_MILLIMETER = 20;
65     public static final short SAC_POINT = 21;
66     public static final short SAC_PICA = 22;
67     public static final short SAC_PERCENTAGE = 23;
68     public static final short SAC_URI = 24;
69     public static final short SAC_COUNTER_FUNCTION = 25;
70     public static final short SAC_COUNTERS_FUNCTION = 26;
71     public static final short SAC_RGBCOLOR = 27;
72     public static final short SAC_DEGREE = 28;
73     public static final short SAC_GRADIAN = 29;
74     public static final short SAC_RADIAN = 30;
75     public static final short SAC_MILLISECOND = 31;
76     public static final short SAC_SECOND = 32;
77     public static final short SAC_HERTZ = 33;
78     public static final short SAC_KILOHERTZ = 34;
79     public static final short SAC_IDENT = 35;
80     public static final short SAC_STRING_VALUE = 36;
81     public static final short SAC_ATTR = 37;
82     public static final short SAC_RECT_FUNCTION = 38;
83     public static final short SAC_UNICODERANGE = 39;
84     public static final short SAC_SUB_EXPRESSION = 40;
85     public static final short SAC_FUNCTION = 41;
86     public static final short SAC_DIMENSION = 42;
87 */

88
89     private short _type;
90     private LexicalUnit _next;
91     private LexicalUnit _prev;
92 // private int _intVal;
93
private float _floatVal;
94     private String JavaDoc _dimension;
95     private String JavaDoc _function;
96     private LexicalUnit _params;
97     private String JavaDoc _stringVal;
98
99     protected LexicalUnitImpl(LexicalUnit previous, short type) {
100         _type = type;
101         _prev = previous;
102         if (_prev != null) {
103             ((LexicalUnitImpl)_prev)._next = this;
104         }
105     }
106
107     /**
108      * Integer
109      */

110     protected LexicalUnitImpl(LexicalUnit previous, int value) {
111         this(previous, SAC_INTEGER);
112 // _intVal = value;
113
_floatVal = value;
114     }
115
116     /**
117      * Dimension
118      */

119     protected LexicalUnitImpl(LexicalUnit previous, short type, float value) {
120         this(previous, type);
121         _floatVal = value;
122     }
123
124     /**
125      * Unknown dimension
126      */

127     protected LexicalUnitImpl(
128             LexicalUnit previous,
129             short type,
130             String JavaDoc dimension,
131             float value) {
132         this(previous, type);
133         _dimension = dimension;
134         _floatVal = value;
135     }
136
137     /**
138      * String
139      */

140     protected LexicalUnitImpl(LexicalUnit previous, short type, String JavaDoc value) {
141         this(previous, type);
142         _stringVal = value;
143     }
144
145     /**
146      * Function
147      */

148     protected LexicalUnitImpl(
149             LexicalUnit previous,
150             short type,
151             String JavaDoc name,
152             LexicalUnit params) {
153         this(previous, type);
154         _function = name;
155         _params = params;
156     }
157
158     public short getLexicalUnitType() {
159         return _type;
160     }
161     
162     public LexicalUnit getNextLexicalUnit() {
163         return _next;
164     }
165     
166     public LexicalUnit getPreviousLexicalUnit() {
167         return _prev;
168     }
169     
170     public int getIntegerValue() {
171 // return _intVal;
172
return (int) _floatVal;
173     }
174     
175     public float getFloatValue() {
176         return _floatVal;
177     }
178     
179     public String JavaDoc getDimensionUnitText() {
180         switch (_type) {
181         case SAC_EM:
182             return "em";
183         case SAC_EX:
184             return "ex";
185         case SAC_PIXEL:
186             return "px";
187         case SAC_INCH:
188             return "in";
189         case SAC_CENTIMETER:
190             return "cm";
191         case SAC_MILLIMETER:
192             return "mm";
193         case SAC_POINT:
194             return "pt";
195         case SAC_PICA:
196             return "pc";
197         case SAC_PERCENTAGE:
198             return "%";
199         case SAC_DEGREE:
200             return "deg";
201         case SAC_GRADIAN:
202             return "grad";
203         case SAC_RADIAN:
204             return "rad";
205         case SAC_MILLISECOND:
206             return "ms";
207         case SAC_SECOND:
208             return "s";
209         case SAC_HERTZ:
210             return "Hz";
211         case SAC_KILOHERTZ:
212             return "kHz";
213         case SAC_DIMENSION:
214             return _dimension;
215         }
216         return "";
217     }
218     
219     public String JavaDoc getFunctionName() {
220         return _function;
221     }
222     
223     public LexicalUnit getParameters() {
224         return _params;
225     }
226
227     public String JavaDoc getStringValue() {
228         return _stringVal;
229     }
230
231     public LexicalUnit getSubValues() {
232         return _params;
233     }
234     
235     public String JavaDoc toString() {
236         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
237         switch (_type) {
238         case SAC_OPERATOR_COMMA:
239             sb.append(",");
240             break;
241         case SAC_OPERATOR_PLUS:
242             sb.append("+");
243             break;
244         case SAC_OPERATOR_MINUS:
245             sb.append("-");
246             break;
247         case SAC_OPERATOR_MULTIPLY:
248             sb.append("*");
249             break;
250         case SAC_OPERATOR_SLASH:
251             sb.append("/");
252             break;
253         case SAC_OPERATOR_MOD:
254             sb.append("%");
255             break;
256         case SAC_OPERATOR_EXP:
257             sb.append("^");
258             break;
259         case SAC_OPERATOR_LT:
260             sb.append("<");
261             break;
262         case SAC_OPERATOR_GT:
263             sb.append(">");
264             break;
265         case SAC_OPERATOR_LE:
266             sb.append("<=");
267             break;
268         case SAC_OPERATOR_GE:
269             sb.append(">=");
270             break;
271         case SAC_OPERATOR_TILDE:
272             sb.append("~");
273             break;
274         case SAC_INHERIT:
275             sb.append("inherit");
276             break;
277         case SAC_INTEGER:
278             sb.append(String.valueOf(getIntegerValue()));
279             break;
280         case SAC_REAL:
281             sb.append(trimFloat(getFloatValue()));
282             break;
283         case SAC_EM:
284         case SAC_EX:
285         case SAC_PIXEL:
286         case SAC_INCH:
287         case SAC_CENTIMETER:
288         case SAC_MILLIMETER:
289         case SAC_POINT:
290         case SAC_PICA:
291         case SAC_PERCENTAGE:
292         case SAC_DEGREE:
293         case SAC_GRADIAN:
294         case SAC_RADIAN:
295         case SAC_MILLISECOND:
296         case SAC_SECOND:
297         case SAC_HERTZ:
298         case SAC_KILOHERTZ:
299         case SAC_DIMENSION:
300             sb.append(trimFloat(getFloatValue()))
301               .append(getDimensionUnitText());
302             break;
303         case SAC_URI:
304             sb.append("url(").append(getStringValue()).append(")");
305             break;
306         case SAC_COUNTER_FUNCTION:
307             sb.append("counter(");
308             appendParams(sb, _params);
309             sb.append(")");
310             break;
311         case SAC_COUNTERS_FUNCTION:
312             sb.append("counters(");
313             appendParams(sb, _params);
314             sb.append(")");
315             break;
316         case SAC_RGBCOLOR:
317             sb.append("rgb(");
318             appendParams(sb, _params);
319             sb.append(")");
320             break;
321         case SAC_IDENT:
322             sb.append(getStringValue());
323             break;
324         case SAC_STRING_VALUE:
325             sb.append("\"").append(getStringValue()).append("\"");
326             break;
327         case SAC_ATTR:
328             sb.append("attr(");
329             appendParams(sb, _params);
330             sb.append(")");
331             break;
332         case SAC_RECT_FUNCTION:
333             sb.append("rect(");
334             appendParams(sb, _params);
335             sb.append(")");
336             break;
337         case SAC_UNICODERANGE:
338             sb.append(getStringValue());
339             break;
340         case SAC_SUB_EXPRESSION:
341             sb.append(getStringValue());
342             break;
343         case SAC_FUNCTION:
344             sb.append(getFunctionName());
345             sb.append("(");
346             appendParams(sb, _params);
347             sb.append(")");
348             break;
349         }
350         return sb.toString();
351     }
352
353     public String JavaDoc toDebugString() {
354         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
355         switch (_type) {
356         case SAC_OPERATOR_COMMA:
357             sb.append("SAC_OPERATOR_COMMA");
358             break;
359         case SAC_OPERATOR_PLUS:
360             sb.append("SAC_OPERATOR_PLUS");
361             break;
362         case SAC_OPERATOR_MINUS:
363             sb.append("SAC_OPERATOR_MINUS");
364             break;
365         case SAC_OPERATOR_MULTIPLY:
366             sb.append("SAC_OPERATOR_MULTIPLY");
367             break;
368         case SAC_OPERATOR_SLASH:
369             sb.append("SAC_OPERATOR_SLASH");
370             break;
371         case SAC_OPERATOR_MOD:
372             sb.append("SAC_OPERATOR_MOD");
373             break;
374         case SAC_OPERATOR_EXP:
375             sb.append("SAC_OPERATOR_EXP");
376             break;
377         case SAC_OPERATOR_LT:
378             sb.append("SAC_OPERATOR_LT");
379             break;
380         case SAC_OPERATOR_GT:
381             sb.append("SAC_OPERATOR_GT");
382             break;
383         case SAC_OPERATOR_LE:
384             sb.append("SAC_OPERATOR_LE");
385             break;
386         case SAC_OPERATOR_GE:
387             sb.append("SAC_OPERATOR_GE");
388             break;
389         case SAC_OPERATOR_TILDE:
390             sb.append("SAC_OPERATOR_TILDE");
391             break;
392         case SAC_INHERIT:
393             sb.append("SAC_INHERIT");
394             break;
395         case SAC_INTEGER:
396             sb.append("SAC_INTEGER(")
397                 .append(String.valueOf(getIntegerValue()))
398                 .append(")");
399             break;
400         case SAC_REAL:
401             sb.append("SAC_REAL(")
402                 .append(trimFloat(getFloatValue()))
403                 .append(")");
404             break;
405         case SAC_EM:
406             sb.append("SAC_EM(")
407                 .append(trimFloat(getFloatValue()))
408                 .append(getDimensionUnitText())
409                 .append(")");
410             break;
411         case SAC_EX:
412             sb.append("SAC_EX(")
413                 .append(trimFloat(getFloatValue()))
414                 .append(getDimensionUnitText())
415                 .append(")");
416             break;
417         case SAC_PIXEL:
418             sb.append("SAC_PIXEL(")
419                 .append(trimFloat(getFloatValue()))
420                 .append(getDimensionUnitText())
421                 .append(")");
422             break;
423         case SAC_INCH:
424             sb.append("SAC_INCH(")
425                 .append(trimFloat(getFloatValue()))
426                 .append(getDimensionUnitText())
427                 .append(")");
428             break;
429         case SAC_CENTIMETER:
430             sb.append("SAC_CENTIMETER(")
431                 .append(trimFloat(getFloatValue()))
432                 .append(getDimensionUnitText())
433                 .append(")");
434             break;
435         case SAC_MILLIMETER:
436             sb.append("SAC_MILLIMETER(")
437                 .append(trimFloat(getFloatValue()))
438                 .append(getDimensionUnitText())
439                 .append(")");
440             break;
441         case SAC_POINT:
442             sb.append("SAC_POINT(")
443                 .append(trimFloat(getFloatValue()))
444                 .append(getDimensionUnitText())
445                 .append(")");
446             break;
447         case SAC_PICA:
448             sb.append("SAC_PICA(")
449                 .append(trimFloat(getFloatValue()))
450                 .append(getDimensionUnitText())
451                 .append(")");
452             break;
453         case SAC_PERCENTAGE:
454             sb.append("SAC_PERCENTAGE(")
455                 .append(trimFloat(getFloatValue()))
456                 .append(getDimensionUnitText())
457                 .append(")");
458             break;
459         case SAC_DEGREE:
460             sb.append("SAC_DEGREE(")
461                 .append(trimFloat(getFloatValue()))
462                 .append(getDimensionUnitText())
463                 .append(")");
464             break;
465         case SAC_GRADIAN:
466             sb.append("SAC_GRADIAN(")
467                 .append(trimFloat(getFloatValue()))
468                 .append(getDimensionUnitText())
469                 .append(")");
470             break;
471         case SAC_RADIAN:
472             sb.append("SAC_RADIAN(")
473                 .append(trimFloat(getFloatValue()))
474                 .append(getDimensionUnitText())
475                 .append(")");
476             break;
477         case SAC_MILLISECOND:
478             sb.append("SAC_MILLISECOND(")
479                 .append(trimFloat(getFloatValue()))
480                 .append(getDimensionUnitText())
481                 .append(")");
482             break;
483         case SAC_SECOND:
484             sb.append("SAC_SECOND(")
485                 .append(trimFloat(getFloatValue()))
486                 .append(getDimensionUnitText())
487                 .append(")");
488             break;
489         case SAC_HERTZ:
490             sb.append("SAC_HERTZ(")
491                 .append(trimFloat(getFloatValue()))
492                 .append(getDimensionUnitText())
493                 .append(")");
494             break;
495         case SAC_KILOHERTZ:
496             sb.append("SAC_KILOHERTZ(")
497                 .append(trimFloat(getFloatValue()))
498                 .append(getDimensionUnitText())
499                 .append(")");
500             break;
501         case SAC_DIMENSION:
502             sb.append("SAC_DIMENSION(")
503                 .append(trimFloat(getFloatValue()))
504                 .append(getDimensionUnitText())
505                 .append(")");
506             break;
507         case SAC_URI:
508             sb.append("SAC_URI(url(")
509                 .append(getStringValue())
510                 .append("))");
511             break;
512         case SAC_COUNTER_FUNCTION:
513             sb.append("SAC_COUNTER_FUNCTION(counter(");
514             appendParams(sb, _params);
515             sb.append("))");
516             break;
517         case SAC_COUNTERS_FUNCTION:
518             sb.append("SAC_COUNTERS_FUNCTION(counters(");
519             appendParams(sb, _params);
520             sb.append("))");
521             break;
522         case SAC_RGBCOLOR:
523             sb.append("SAC_RGBCOLOR(rgb(");
524             appendParams(sb, _params);
525             sb.append("))");
526             break;
527         case SAC_IDENT:
528             sb.append("SAC_IDENT(")
529                 .append(getStringValue())
530                 .append(")");
531             break;
532         case SAC_STRING_VALUE:
533             sb.append("SAC_STRING_VALUE(\"")
534                 .append(getStringValue())
535                 .append("\")");
536             break;
537         case SAC_ATTR:
538             sb.append("SAC_ATTR(attr(");
539             appendParams(sb, _params);
540             sb.append("))");
541             break;
542         case SAC_RECT_FUNCTION:
543             sb.append("SAC_RECT_FUNCTION(rect(");
544             appendParams(sb, _params);
545             sb.append("))");
546             break;
547         case SAC_UNICODERANGE:
548             sb.append("SAC_UNICODERANGE(")
549                 .append(getStringValue())
550                 .append(")");
551             break;
552         case SAC_SUB_EXPRESSION:
553             sb.append("SAC_SUB_EXPRESSION(")
554                 .append(getStringValue())
555                 .append(")");
556             break;
557         case SAC_FUNCTION:
558             sb.append("SAC_FUNCTION(")
559                 .append(getFunctionName())
560                 .append("(");
561             appendParams(sb, _params);
562             sb.append("))");
563             break;
564         }
565         return sb.toString();
566     }
567
568     private void appendParams(StringBuffer JavaDoc sb, LexicalUnit first) {
569         LexicalUnit l = first;
570         while (l != null) {
571             sb.append(l.toString());
572             l = l.getNextLexicalUnit();
573         }
574     }
575     
576     private String JavaDoc trimFloat(float f) {
577         String JavaDoc s = String.valueOf(getFloatValue());
578         return (f - (int) f != 0) ? s : s.substring(0, s.length() - 2);
579     }
580
581     // TODO what is this method for? It is not used locally.
582
/*
583     private static float value(char op, String s) {
584         return ((op == '-') ? -1 : 1) * Float.valueOf(s).floatValue();
585     }
586     */

587     
588     public static LexicalUnit createNumber(LexicalUnit prev, float f) {
589         if (f > (int) f) {
590             return new LexicalUnitImpl(prev, LexicalUnit.SAC_REAL, f);
591         }
592         return new LexicalUnitImpl(prev, (int) f);
593     }
594     
595     public static LexicalUnit createPercentage(LexicalUnit prev, float f) {
596         return new LexicalUnitImpl(prev, LexicalUnit.SAC_PERCENTAGE, f);
597     }
598     
599     public static LexicalUnit createPixel(LexicalUnit prev, float f) {
600         return new LexicalUnitImpl(prev, LexicalUnit.SAC_PIXEL, f);
601     }
602     
603     public static LexicalUnit createCentimeter(LexicalUnit prev, float f) {
604         return new LexicalUnitImpl(prev, LexicalUnit.SAC_CENTIMETER, f);
605     }
606     
607     public static LexicalUnit createMillimeter(LexicalUnit prev, float f) {
608         return new LexicalUnitImpl(prev, LexicalUnit.SAC_MILLIMETER, f);
609     }
610     
611     public static LexicalUnit createInch(LexicalUnit prev, float f) {
612         return new LexicalUnitImpl(prev, LexicalUnit.SAC_INCH, f);
613     }
614     
615     public static LexicalUnit createPoint(LexicalUnit prev, float f) {
616         return new LexicalUnitImpl(prev, LexicalUnit.SAC_POINT, f);
617     }
618     
619     public static LexicalUnit createPica(LexicalUnit prev, float f) {
620         return new LexicalUnitImpl(prev, LexicalUnit.SAC_PICA, f);
621     }
622     
623     public static LexicalUnit createEm(LexicalUnit prev, float f) {
624         return new LexicalUnitImpl(prev, LexicalUnit.SAC_EM, f);
625     }
626     
627     public static LexicalUnit createEx(LexicalUnit prev, float f) {
628         return new LexicalUnitImpl(prev, LexicalUnit.SAC_EX, f);
629     }
630     
631     public static LexicalUnit createDegree(LexicalUnit prev, float f) {
632         return new LexicalUnitImpl(prev, LexicalUnit.SAC_DEGREE, f);
633     }
634     
635     public static LexicalUnit createRadian(LexicalUnit prev, float f) {
636         return new LexicalUnitImpl(prev, LexicalUnit.SAC_RADIAN, f);
637     }
638     
639     public static LexicalUnit createGradian(LexicalUnit prev, float f) {
640         return new LexicalUnitImpl(prev, LexicalUnit.SAC_GRADIAN, f);
641     }
642     
643     public static LexicalUnit createMillisecond(LexicalUnit prev, float f) {
644         return new LexicalUnitImpl(prev, LexicalUnit.SAC_MILLISECOND, f);
645     }
646     
647     public static LexicalUnit createSecond(LexicalUnit prev, float f) {
648         return new LexicalUnitImpl(prev, LexicalUnit.SAC_SECOND, f);
649     }
650     
651     public static LexicalUnit createHertz(LexicalUnit prev, float f) {
652         return new LexicalUnitImpl(prev, LexicalUnit.SAC_HERTZ, f);
653     }
654     
655     public static LexicalUnit createDimension(LexicalUnit prev, float f, String JavaDoc dim) {
656         return new LexicalUnitImpl(prev, LexicalUnit.SAC_DIMENSION, dim, f);
657     }
658     
659     public static LexicalUnit createKiloHertz(LexicalUnit prev, float f) {
660         return new LexicalUnitImpl(prev, LexicalUnit.SAC_KILOHERTZ, f);
661     }
662     
663     public static LexicalUnit createCounter(LexicalUnit prev, LexicalUnit params) {
664         return new LexicalUnitImpl(prev, LexicalUnit.SAC_COUNTER_FUNCTION, "counter", params);
665     }
666     
667     public static LexicalUnit createCounters(LexicalUnit prev, LexicalUnit params) {
668         return new LexicalUnitImpl(prev, LexicalUnit.SAC_COUNTERS_FUNCTION, "counters", params);
669     }
670     
671     public static LexicalUnit createAttr(LexicalUnit prev, LexicalUnit params) {
672         return new LexicalUnitImpl(prev, LexicalUnit.SAC_ATTR, "attr", params);
673     }
674     
675     public static LexicalUnit createRect(LexicalUnit prev, LexicalUnit params) {
676         return new LexicalUnitImpl(prev, LexicalUnit.SAC_RECT_FUNCTION, "rect", params);
677     }
678     
679     public static LexicalUnit createRgbColor(LexicalUnit prev, LexicalUnit params) {
680         return new LexicalUnitImpl(prev, LexicalUnit.SAC_RGBCOLOR, "rgb", params);
681     }
682     
683     public static LexicalUnit createFunction(LexicalUnit prev, String JavaDoc name, LexicalUnit params) {
684         return new LexicalUnitImpl(prev, LexicalUnit.SAC_FUNCTION, name, params);
685     }
686
687     public static LexicalUnit createString(LexicalUnit prev, String JavaDoc value) {
688         return new LexicalUnitImpl(prev, LexicalUnit.SAC_STRING_VALUE, value);
689     }
690     
691     public static LexicalUnit createIdent(LexicalUnit prev, String JavaDoc value) {
692         return new LexicalUnitImpl(prev, LexicalUnit.SAC_IDENT, value);
693     }
694     
695     public static LexicalUnit createURI(LexicalUnit prev, String JavaDoc value) {
696         return new LexicalUnitImpl(prev, LexicalUnit.SAC_URI, value);
697     }
698     
699     public static LexicalUnit createComma(LexicalUnit prev) {
700         return new LexicalUnitImpl(prev, SAC_OPERATOR_COMMA);
701     }
702 }
703
Popular Tags