Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/steadystate/css/parser/LexicalUnitImpl.java


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.1.1.1 2003/12/28 21:23:08 davidsch Exp $
29   */
30  
31  package com.steadystate.css.parser;
32  
33  import java.io.Serializable;
34  import org.w3c.css.sac.*;
35  
36  /** 
37   *
38   * @author  David Schweinsberg
39   * @version $Release$
40   */
41  public class LexicalUnitImpl implements LexicalUnit, Serializable {
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 _dimension;
95      private String _function;
96      private LexicalUnit _params;
97      private String _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 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 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 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 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 getFunctionName() {
220         return _function;
221     }
222     
223     public LexicalUnit getParameters() {
224         return _params;
225     }
226 
227     public String getStringValue() {
228         return _stringVal;
229     }
230 
231     public LexicalUnit getSubValues() {
232         return _params;
233     }
234     
235     public String toString() {
236         StringBuffer sb = new StringBuffer();
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             appendParams(sb, _params);
346             sb.append(")");
347             break;
348         }
349         return sb.toString();
350     }
351 
352     public String toDebugString() {
353         StringBuffer sb = new StringBuffer();
354         switch (_type) {
355         case SAC_OPERATOR_COMMA:
356             sb.append("SAC_OPERATOR_COMMA");
357             break;
358         case SAC_OPERATOR_PLUS:
359             sb.append("SAC_OPERATOR_PLUS");
360             break;
361         case SAC_OPERATOR_MINUS:
362             sb.append("SAC_OPERATOR_MINUS");
363             break;
364         case SAC_OPERATOR_MULTIPLY:
365             sb.append("SAC_OPERATOR_MULTIPLY");
366             break;
367         case SAC_OPERATOR_SLASH:
368             sb.append("SAC_OPERATOR_SLASH");
369             break;
370         case SAC_OPERATOR_MOD:
371             sb.append("SAC_OPERATOR_MOD");
372             break;
373         case SAC_OPERATOR_EXP:
374             sb.append("SAC_OPERATOR_EXP");
375             break;
376         case SAC_OPERATOR_LT:
377             sb.append("SAC_OPERATOR_LT");
378             break;
379         case SAC_OPERATOR_GT:
380             sb.append("SAC_OPERATOR_GT");
381             break;
382         case SAC_OPERATOR_LE:
383             sb.append("SAC_OPERATOR_LE");
384             break;
385         case SAC_OPERATOR_GE:
386             sb.append("SAC_OPERATOR_GE");
387             break;
388         case SAC_OPERATOR_TILDE:
389             sb.append("SAC_OPERATOR_TILDE");
390             break;
391         case SAC_INHERIT:
392             sb.append("SAC_INHERIT");
393             break;
394         case SAC_INTEGER:
395             sb.append("SAC_INTEGER(")
396                 .append(String.valueOf(getIntegerValue()))
397                 .append(")");
398             break;
399         case SAC_REAL:
400             sb.append("SAC_REAL(")
401                 .append(trimFloat(getFloatValue()))
402                 .append(")");
403             break;
404         case SAC_EM:
405             sb.append("SAC_EM(")
406                 .append(trimFloat(getFloatValue()))
407                 .append(getDimensionUnitText())
408                 .append(")");
409             break;
410         case SAC_EX:
411             sb.append("SAC_EX(")
412                 .append(trimFloat(getFloatValue()))
413                 .append(getDimensionUnitText())
414                 .append(")");
415             break;
416         case SAC_PIXEL:
417             sb.append("SAC_PIXEL(")
418                 .append(trimFloat(getFloatValue()))
419                 .append(getDimensionUnitText())
420                 .append(")");
421             break;
422         case SAC_INCH:
423             sb.append("SAC_INCH(")
424                 .append(trimFloat(getFloatValue()))
425                 .append(getDimensionUnitText())
426                 .append(")");
427             break;
428         case SAC_CENTIMETER:
429             sb.append("SAC_CENTIMETER(")
430                 .append(trimFloat(getFloatValue()))
431                 .append(getDimensionUnitText())
432                 .append(")");
433             break;
434         case SAC_MILLIMETER:
435             sb.append("SAC_MILLIMETER(")
436                 .append(trimFloat(getFloatValue()))
437                 .append(getDimensionUnitText())
438                 .append(")");
439             break;
440         case SAC_POINT:
441             sb.append("SAC_POINT(")
442                 .append(trimFloat(getFloatValue()))
443                 .append(getDimensionUnitText())
444                 .append(")");
445             break;
446         case SAC_PICA:
447             sb.append("SAC_PICA(")
448                 .append(trimFloat(getFloatValue()))
449                 .append(getDimensionUnitText())
450                 .append(")");
451             break;
452         case SAC_PERCENTAGE:
453             sb.append("SAC_PERCENTAGE(")
454                 .append(trimFloat(getFloatValue()))
455                 .append(getDimensionUnitText())
456                 .append(")");
457             break;
458         case SAC_DEGREE:
459             sb.append("SAC_DEGREE(")
460                 .append(trimFloat(getFloatValue()))
461                 .append(getDimensionUnitText())
462                 .append(")");
463             break;
464         case SAC_GRADIAN:
465             sb.append("SAC_GRADIAN(")
466                 .append(trimFloat(getFloatValue()))
467                 .append(getDimensionUnitText())
468                 .append(")");
469             break;
470         case SAC_RADIAN:
471             sb.append("SAC_RADIAN(")
472                 .append(trimFloat(getFloatValue()))
473                 .append(getDimensionUnitText())
474                 .append(")");
475             break;
476         case SAC_MILLISECOND:
477             sb.append("SAC_MILLISECOND(")
478                 .append(trimFloat(getFloatValue()))
479                 .append(getDimensionUnitText())
480                 .append(")");
481             break;
482         case SAC_SECOND:
483             sb.append("SAC_SECOND(")
484                 .append(trimFloat(getFloatValue()))
485                 .append(getDimensionUnitText())
486                 .append(")");
487             break;
488         case SAC_HERTZ:
489             sb.append("SAC_HERTZ(")
490                 .append(trimFloat(getFloatValue()))
491                 .append(getDimensionUnitText())
492                 .append(")");
493             break;
494         case SAC_KILOHERTZ:
495             sb.append("SAC_KILOHERTZ(")
496                 .append(trimFloat(getFloatValue()))
497                 .append(getDimensionUnitText())
498                 .append(")");
499             break;
500         case SAC_DIMENSION:
501             sb.append("SAC_DIMENSION(")
502                 .append(trimFloat(getFloatValue()))
503                 .append(getDimensionUnitText())
504                 .append(")");
505             break;
506         case SAC_URI:
507             sb.append("SAC_URI(url(")
508                 .append(getStringValue())
509                 .append("))");
510             break;
511         case SAC_COUNTER_FUNCTION:
512             sb.append("SAC_COUNTER_FUNCTION(counter(");
513             appendParams(sb, _params);
514             sb.append("))");
515             break;
516         case SAC_COUNTERS_FUNCTION:
517             sb.append("SAC_COUNTERS_FUNCTION(counters(");
518             appendParams(sb, _params);
519             sb.append("))");
520             break;
521         case SAC_RGBCOLOR:
522             sb.append("SAC_RGBCOLOR(rgb(");
523             appendParams(sb, _params);
524             sb.append("))");
525             break;
526         case SAC_IDENT:
527             sb.append("SAC_IDENT(")
528                 .append(getStringValue())
529                 .append(")");
530             break;
531         case SAC_STRING_VALUE:
532             sb.append("SAC_STRING_VALUE(\"")
533                 .append(getStringValue())
534                 .append("\")");
535             break;
536         case SAC_ATTR:
537             sb.append("SAC_ATTR(attr(");
538             appendParams(sb, _params);
539             sb.append("))");
540             break;
541         case SAC_RECT_FUNCTION:
542             sb.append("SAC_RECT_FUNCTION(rect(");
543             appendParams(sb, _params);
544             sb.append("))");
545             break;
546         case SAC_UNICODERANGE:
547             sb.append("SAC_UNICODERANGE(")
548                 .append(getStringValue())
549                 .append(")");
550             break;
551         case SAC_SUB_EXPRESSION:
552             sb.append("SAC_SUB_EXPRESSION(")
553                 .append(getStringValue())
554                 .append(")");
555             break;
556         case SAC_FUNCTION:
557             sb.append("SAC_FUNCTION(")
558                 .append(getFunctionName())
559                 .append("(");
560             appendParams(sb, _params);
561             sb.append("))");
562             break;
563         }
564         return sb.toString();
565     }
566 
567     private void appendParams(StringBuffer sb, LexicalUnit first) {
568         LexicalUnit l = first;
569         while (l != null) {
570             sb.append(l.toString());
571             l = l.getNextLexicalUnit();
572         }
573     }
574     
575     private String trimFloat(float f) {
576         String s = String.valueOf(getFloatValue());
577         return (f - (int) f != 0) ? s : s.substring(0, s.length() - 2);
578     }
579 
580     private static float value(char op, String s) {
581         return ((op == '-') ? -1 : 1) * Float.valueOf(s).floatValue();
582     }
583     
584     public static LexicalUnit createNumber(LexicalUnit prev, float f) {
585         if (f > (int) f) {
586             return new LexicalUnitImpl(prev, LexicalUnit.SAC_REAL, f);
587         } else {
588             return new LexicalUnitImpl(prev, (int) f);
589         }
590     }
591     
592     public static LexicalUnit createPercentage(LexicalUnit prev, float f) {
593         return new LexicalUnitImpl(prev, LexicalUnit.SAC_PERCENTAGE, f);
594     }
595     
596     public static LexicalUnit createPixel(LexicalUnit prev, float f) {
597         return new LexicalUnitImpl(prev, LexicalUnit.SAC_PIXEL, f);
598     }
599     
600     public static LexicalUnit createCentimeter(LexicalUnit prev, float f) {
601         return new LexicalUnitImpl(prev, LexicalUnit.SAC_CENTIMETER, f);
602     }
603     
604     public static LexicalUnit createMillimeter(LexicalUnit prev, float f) {
605         return new LexicalUnitImpl(prev, LexicalUnit.SAC_MILLIMETER, f);
606     }
607     
608     public static LexicalUnit createInch(LexicalUnit prev, float f) {
609         return new LexicalUnitImpl(prev, LexicalUnit.SAC_INCH, f);
610     }
611     
612     public static LexicalUnit createPoint(LexicalUnit prev, float f) {
613         return new LexicalUnitImpl(prev, LexicalUnit.SAC_POINT, f);
614     }
615     
616     public static LexicalUnit createPica(LexicalUnit prev, float f) {
617         return new LexicalUnitImpl(prev, LexicalUnit.SAC_PICA, f);
618     }
619     
620     public static LexicalUnit createEm(LexicalUnit prev, float f) {
621         return new LexicalUnitImpl(prev, LexicalUnit.SAC_EM, f);
622     }
623     
624     public static LexicalUnit createEx(LexicalUnit prev, float f) {
625         return new LexicalUnitImpl(prev, LexicalUnit.SAC_EX, f);
626     }
627     
628     public static LexicalUnit createDegree(LexicalUnit prev, float f) {
629         return new LexicalUnitImpl(prev, LexicalUnit.SAC_DEGREE, f);
630     }
631     
632     public static LexicalUnit createRadian(LexicalUnit prev, float f) {
633         return new LexicalUnitImpl(prev, LexicalUnit.SAC_RADIAN, f);
634     }
635     
636     public static LexicalUnit createGradian(LexicalUnit prev, float f) {
637         return new LexicalUnitImpl(prev, LexicalUnit.SAC_GRADIAN, f);
638     }
639     
640     public static LexicalUnit createMillisecond(LexicalUnit prev, float f) {
641         return new LexicalUnitImpl(prev, LexicalUnit.SAC_MILLISECOND, f);
642     }
643     
644     public static LexicalUnit createSecond(LexicalUnit prev, float f) {
645         return new LexicalUnitImpl(prev, LexicalUnit.SAC_SECOND, f);
646     }
647     
648     public static LexicalUnit createHertz(LexicalUnit prev, float f) {
649         return new LexicalUnitImpl(prev, LexicalUnit.SAC_HERTZ, f);
650     }
651     
652     public static LexicalUnit createDimension(LexicalUnit prev, float f, String dim) {
653         return new LexicalUnitImpl(prev, LexicalUnit.SAC_DIMENSION, dim, f);
654     }
655     
656     public static LexicalUnit createKiloHertz(LexicalUnit prev, float f) {
657         return new LexicalUnitImpl(prev, LexicalUnit.SAC_KILOHERTZ, f);
658     }
659     
660     public static LexicalUnit createCounter(LexicalUnit prev, LexicalUnit params) {
661         return new LexicalUnitImpl(prev, LexicalUnit.SAC_COUNTER_FUNCTION, "counter", params);
662     }
663     
664     public static LexicalUnit createCounters(LexicalUnit prev, LexicalUnit params) {
665         return new LexicalUnitImpl(prev, LexicalUnit.SAC_COUNTERS_FUNCTION, "counters", params);
666     }
667     
668     public static LexicalUnit createAttr(LexicalUnit prev, LexicalUnit params) {
669         return new LexicalUnitImpl(prev, LexicalUnit.SAC_ATTR, "attr", params);
670     }
671     
672     public static LexicalUnit createRect(LexicalUnit prev, LexicalUnit params) {
673         return new LexicalUnitImpl(prev, LexicalUnit.SAC_RECT_FUNCTION, "rect", params);
674     }
675     
676     public static LexicalUnit createRgbColor(LexicalUnit prev, LexicalUnit params) {
677         return new LexicalUnitImpl(prev, LexicalUnit.SAC_RGBCOLOR, "rgb", params);
678     }
679     
680     public static LexicalUnit createFunction(LexicalUnit prev, String name, LexicalUnit params) {
681         return new LexicalUnitImpl(prev, LexicalUnit.SAC_FUNCTION, name, params);
682     }
683 
684     public static LexicalUnit createString(LexicalUnit prev, String value) {
685         return new LexicalUnitImpl(prev, LexicalUnit.SAC_STRING_VALUE, value);
686     }
687     
688     public static LexicalUnit createIdent(LexicalUnit prev, String value) {
689         return new LexicalUnitImpl(prev, LexicalUnit.SAC_IDENT, value);
690     }
691     
692     public static LexicalUnit createURI(LexicalUnit prev, String value) {
693         return new LexicalUnitImpl(prev, LexicalUnit.SAC_URI, value);
694     }
695     
696     public static LexicalUnit createComma(LexicalUnit prev) {
697         return new LexicalUnitImpl(prev, SAC_OPERATOR_COMMA);
698     }
699 }