1 30 31 package com.steadystate.css.dom; 32 33 import java.io.Serializable ; 34 import java.io.StringReader ; 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 54 public class CSSValueImpl implements CSSPrimitiveValue, CSSValueList, Serializable { 55 56 private Object _value = null; 57 58 61 public CSSValueImpl(LexicalUnit value, boolean forcePrimitive) { 62 if (value.getParameters() != null) { 63 if (value.getLexicalUnitType() == LexicalUnit.SAC_RECT_FUNCTION) { 64 _value = new RectImpl(value.getParameters()); 66 } else if (value.getLexicalUnitType() == LexicalUnit.SAC_RGBCOLOR) { 67 _value = new RGBColorImpl(value.getParameters()); 69 } else if (value.getLexicalUnitType() == LexicalUnit.SAC_COUNTER_FUNCTION) { 70 _value = new CounterImpl(false, value.getParameters()); 72 } else if (value.getLexicalUnitType() == LexicalUnit.SAC_COUNTERS_FUNCTION) { 73 _value = new CounterImpl(true, value.getParameters()); 75 } else { 76 _value = value; 77 } 78 } else if (forcePrimitive || (value.getNextLexicalUnit() == null)) { 79 80 _value = value; 82 } else { 83 84 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 getCssText() { 105 if (getCssValueType() == CSS_VALUE_LIST) { 106 107 StringBuffer sb = new StringBuffer (); 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 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 cssText) throws DOMException { 132 try { 133 InputSource is = new InputSource(new StringReader (cssText)); 134 CSSOMParser parser = new CSSOMParser(); 135 CSSValueImpl v2 = (CSSValueImpl) parser.parsePropertyValue(is); 136 _value = v2._value; 137 } catch (Exception 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_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_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 } 238 239 public void setStringValue(short stringType, String 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 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 266 public String 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 toString() { 324 return getCssText(); 325 } 326 } 327 | Popular Tags |