KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > steadystate > css > dom > CSSValueImpl


1 /*
2  * CSSValueImpl.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: CSSValueImpl.java,v 1.2 2005/04/28 20:57:20 waldbaer Exp $
29  */

30
31 package com.steadystate.css.dom;
32
33 import java.io.Serializable JavaDoc;
34 import java.io.StringReader JavaDoc;
35 import java.util.*;
36 import org.w3c.css.sac.*;
37 import org.w3c.dom.*;
38 import org.w3c.dom.css.*;
39 import com.steadystate.css.parser.CSSOMParser;
40 import com.steadystate.css.parser.LexicalUnitImpl;
41
42 /**
43  * The <code>CSSValueImpl</code> class can represent either a
44  * <code>CSSPrimitiveValue</code> or a <code>CSSValueList</code> so that
45  * the type can successfully change when using <code>setCssText</code>.
46  *
47  * TO DO:
48  * Float unit conversions,
49  * A means of checking valid primitive types for properties
50  *
51  * @author David Schweinsberg
52  * @version $Release$
53  */

54 public class CSSValueImpl implements CSSPrimitiveValue, CSSValueList, Serializable JavaDoc {
55
56     private Object JavaDoc _value = null;
57
58     /**
59      * Constructor
60      */

61     public CSSValueImpl(LexicalUnit value, boolean forcePrimitive) {
62         if (value.getParameters() != null) {
63             if (value.getLexicalUnitType() == LexicalUnit.SAC_RECT_FUNCTION) {
64                 // Rect
65
_value = new RectImpl(value.getParameters());
66             } else if (value.getLexicalUnitType() == LexicalUnit.SAC_RGBCOLOR) {
67                 // RGBColor
68
_value = new RGBColorImpl(value.getParameters());
69             } else if (value.getLexicalUnitType() == LexicalUnit.SAC_COUNTER_FUNCTION) {
70                 // Counter
71
_value = new CounterImpl(false, value.getParameters());
72             } else if (value.getLexicalUnitType() == LexicalUnit.SAC_COUNTERS_FUNCTION) {
73                 // Counter
74
_value = new CounterImpl(true, value.getParameters());
75             } else {
76                 _value = value;
77             }
78         } else if (forcePrimitive || (value.getNextLexicalUnit() == null)) {
79             
80             // We need to be a CSSPrimitiveValue
81
_value = value;
82         } else {
83             
84             // We need to be a CSSValueList
85
// Values in an "expr" can be seperated by "operator"s, which are
86
// either '/' or ',' - ignore these operators
87
Vector v = new Vector();
88             LexicalUnit lu = value;
89             while (lu != null) {
90                 if ((lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA)
91                     && (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_SLASH)) {
92                     v.addElement(new CSSValueImpl(lu, true));
93                 }
94                 lu = lu.getNextLexicalUnit();
95             }
96             _value = v;
97         }
98     }
99
100     public CSSValueImpl(LexicalUnit value) {
101         this(value, false);
102     }
103
104     public String JavaDoc getCssText() {
105         if (getCssValueType() == CSS_VALUE_LIST) {
106             
107             // Create the string from the LexicalUnits so we include the correct
108
// operators in the string
109
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
110             Vector v = (Vector) _value;
111             LexicalUnit lu = (LexicalUnit) ((CSSValueImpl) v.elementAt(0))._value;
112             while (lu != null) {
113                 sb.append(lu.toString());
114                 
115                 // Step to the next lexical unit, determining what spacing we
116
// need to put around the operators
117
LexicalUnit prev = lu;
118                 lu = lu.getNextLexicalUnit();
119                 if ((lu != null)
120                     && (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA)
121                     && (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_SLASH)
122                     && (prev.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_SLASH)) {
123                     sb.append(" ");
124                 }
125             }
126             return sb.toString();
127         }
128         return _value.toString();
129     }
130
131     public void setCssText(String JavaDoc cssText) throws DOMException {
132         try {
133             InputSource is = new InputSource(new StringReader JavaDoc(cssText));
134             CSSOMParser parser = new CSSOMParser();
135             CSSValueImpl v2 = (CSSValueImpl) parser.parsePropertyValue(is);
136             _value = v2._value;
137         } catch (Exception JavaDoc e) {
138             throw new DOMExceptionImpl(
139                 DOMException.SYNTAX_ERR,
140                 DOMExceptionImpl.SYNTAX_ERROR,
141                 e.getMessage() );
142         }
143     }
144
145     public short getCssValueType() {
146         return (_value instanceof Vector) ? CSS_VALUE_LIST : CSS_PRIMITIVE_VALUE;
147     }
148
149     public short getPrimitiveType() {
150         if (_value instanceof LexicalUnit) {
151             LexicalUnit lu = (LexicalUnit) _value;
152             switch (lu.getLexicalUnitType()) {
153             case LexicalUnit.SAC_INHERIT:
154                 return CSS_IDENT;
155             case LexicalUnit.SAC_INTEGER:
156             case LexicalUnit.SAC_REAL:
157                 return CSS_NUMBER;
158             case LexicalUnit.SAC_EM:
159                 return CSS_EMS;
160             case LexicalUnit.SAC_EX:
161                 return CSS_EXS;
162             case LexicalUnit.SAC_PIXEL:
163                 return CSS_PX;
164             case LexicalUnit.SAC_INCH:
165                 return CSS_IN;
166             case LexicalUnit.SAC_CENTIMETER:
167                 return CSS_CM;
168             case LexicalUnit.SAC_MILLIMETER:
169                 return CSS_MM;
170             case LexicalUnit.SAC_POINT:
171                 return CSS_PT;
172             case LexicalUnit.SAC_PICA:
173                 return CSS_PC;
174             case LexicalUnit.SAC_PERCENTAGE:
175                 return CSS_PERCENTAGE;
176             case LexicalUnit.SAC_URI:
177                 return CSS_URI;
178 // case LexicalUnit.SAC_COUNTER_FUNCTION:
179
// case LexicalUnit.SAC_COUNTERS_FUNCTION:
180
// return CSS_COUNTER;
181
// case LexicalUnit.SAC_RGBCOLOR:
182
// return CSS_RGBCOLOR;
183
case LexicalUnit.SAC_DEGREE:
184                 return CSS_DEG;
185             case LexicalUnit.SAC_GRADIAN:
186                 return CSS_GRAD;
187             case LexicalUnit.SAC_RADIAN:
188                 return CSS_RAD;
189             case LexicalUnit.SAC_MILLISECOND:
190                 return CSS_MS;
191             case LexicalUnit.SAC_SECOND:
192                 return CSS_S;
193             case LexicalUnit.SAC_HERTZ:
194                 return CSS_KHZ;
195             case LexicalUnit.SAC_KILOHERTZ:
196                 return CSS_HZ;
197             case LexicalUnit.SAC_IDENT:
198                 return CSS_IDENT;
199             case LexicalUnit.SAC_STRING_VALUE:
200                 return CSS_STRING;
201             case LexicalUnit.SAC_ATTR:
202                 return CSS_ATTR;
203 // case LexicalUnit.SAC_RECT_FUNCTION:
204
// return CSS_RECT;
205
case LexicalUnit.SAC_UNICODERANGE:
206             case LexicalUnit.SAC_SUB_EXPRESSION:
207             case LexicalUnit.SAC_FUNCTION:
208                 return CSS_STRING;
209             case LexicalUnit.SAC_DIMENSION:
210                 return CSS_DIMENSION;
211             }
212         } else if (_value instanceof RectImpl) {
213             return CSS_RECT;
214         } else if (_value instanceof RGBColorImpl) {
215             return CSS_RGBCOLOR;
216         } else if (_value instanceof CounterImpl) {
217             return CSS_COUNTER;
218         }
219         return CSS_UNKNOWN;
220     }
221
222     public void setFloatValue(short unitType, float floatValue) throws DOMException {
223         _value = LexicalUnitImpl.createNumber(null, floatValue);
224     }
225
226     public float getFloatValue(short unitType) throws DOMException {
227         if (_value instanceof LexicalUnit) {
228             LexicalUnit lu = (LexicalUnit) _value;
229             return lu.getFloatValue();
230         }
231         throw new DOMExceptionImpl(
232             DOMException.INVALID_ACCESS_ERR,
233             DOMExceptionImpl.FLOAT_ERROR);
234
235         // We need to attempt a conversion
236
// return 0;
237
}
238
239     public void setStringValue(short stringType, String JavaDoc stringValue) throws DOMException {
240         switch (stringType) {
241         case CSS_STRING:
242             _value = LexicalUnitImpl.createString(null, stringValue);
243             break;
244         case CSS_URI:
245             _value = LexicalUnitImpl.createURI(null, stringValue);
246             break;
247         case CSS_IDENT:
248             _value = LexicalUnitImpl.createIdent(null, stringValue);
249             break;
250         case CSS_ATTR:
251 // _value = LexicalUnitImpl.createAttr(null, stringValue);
252
// break;
253
throw new DOMExceptionImpl(
254                 DOMException.NOT_SUPPORTED_ERR,
255                 DOMExceptionImpl.NOT_IMPLEMENTED);
256         default:
257             throw new DOMExceptionImpl(
258                 DOMException.INVALID_ACCESS_ERR,
259                 DOMExceptionImpl.STRING_ERROR);
260         }
261     }
262
263     /**
264      * TODO: return a value for a list type
265      */

266     public String JavaDoc getStringValue() throws DOMException {
267         if (_value instanceof LexicalUnit) {
268             LexicalUnit lu = (LexicalUnit) _value;
269             if ((lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT)
270                 || (lu.getLexicalUnitType() == LexicalUnit.SAC_STRING_VALUE)
271                 || (lu.getLexicalUnitType() == LexicalUnit.SAC_URI)
272                 || (lu.getLexicalUnitType() == LexicalUnit.SAC_ATTR)) {
273                 return lu.getStringValue();
274             } else if (lu.getLexicalUnitType() == LexicalUnit.SAC_FUNCTION) {
275                 return lu.toString();
276             }
277         } else if (_value instanceof Vector) {
278             return null;
279         }
280
281         throw new DOMExceptionImpl(
282             DOMException.INVALID_ACCESS_ERR,
283             DOMExceptionImpl.STRING_ERROR);
284     }
285
286     public Counter getCounterValue() throws DOMException {
287         if ((_value instanceof Counter) == false) {
288             throw new DOMExceptionImpl(
289                 DOMException.INVALID_ACCESS_ERR,
290                 DOMExceptionImpl.COUNTER_ERROR);
291         }
292         return (Counter) _value;
293     }
294
295     public Rect getRectValue() throws DOMException {
296         if ((_value instanceof Rect) == false) {
297             throw new DOMExceptionImpl(
298                 DOMException.INVALID_ACCESS_ERR,
299                 DOMExceptionImpl.RECT_ERROR);
300         }
301         return (Rect) _value;
302     }
303
304     public RGBColor getRGBColorValue() throws DOMException {
305         if ((_value instanceof RGBColor) == false) {
306             throw new DOMExceptionImpl(
307                 DOMException.INVALID_ACCESS_ERR,
308                 DOMExceptionImpl.RGBCOLOR_ERROR);
309         }
310         return (RGBColor) _value;
311     }
312
313     public int getLength() {
314         return (_value instanceof Vector) ? ((Vector)_value).size() : 0;
315     }
316
317     public CSSValue item(int index) {
318         return (_value instanceof Vector)
319             ? ((CSSValue)((Vector)_value).elementAt(index))
320             : null;
321     }
322
323     public String JavaDoc toString() {
324         return getCssText();
325     }
326 }
327
Popular Tags