1 18 package org.apache.batik.css.engine.value; 19 20 import org.apache.batik.css.engine.CSSEngine; 21 import org.apache.batik.css.engine.CSSStylableElement; 22 import org.apache.batik.css.engine.StyleMap; 23 import org.apache.batik.util.CSSConstants; 24 import org.w3c.css.sac.LexicalUnit; 25 import org.w3c.dom.DOMException ; 26 import org.w3c.dom.css.CSSPrimitiveValue; 27 import org.w3c.dom.css.CSSValue; 28 29 36 public abstract class RectManager extends LengthManager { 37 38 41 protected int orientation; 42 43 46 public Value createValue(LexicalUnit lu, CSSEngine engine) 47 throws DOMException { 48 switch (lu.getLexicalUnitType()) { 49 case LexicalUnit.SAC_FUNCTION: 50 if (!lu.getFunctionName().equalsIgnoreCase("rect")) { 51 break; 52 } 53 case LexicalUnit.SAC_RECT_FUNCTION: 54 lu = lu.getParameters(); 55 Value top = createRectComponent(lu); 56 lu = lu.getNextLexicalUnit(); 57 if (lu == null || 58 lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) { 59 throw createMalformedRectDOMException(); 60 } 61 lu = lu.getNextLexicalUnit(); 62 Value right = createRectComponent(lu); 63 lu = lu.getNextLexicalUnit(); 64 if (lu == null || 65 lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) { 66 throw createMalformedRectDOMException(); 67 } 68 lu = lu.getNextLexicalUnit(); 69 Value bottom = createRectComponent(lu); 70 lu = lu.getNextLexicalUnit(); 71 if (lu == null || 72 lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) { 73 throw createMalformedRectDOMException(); 74 } 75 lu = lu.getNextLexicalUnit(); 76 Value left = createRectComponent(lu); 77 return new RectValue(top, right, bottom, left); 78 } 79 throw createMalformedRectDOMException(); 80 } 81 82 private Value createRectComponent(LexicalUnit lu) throws DOMException { 83 switch (lu.getLexicalUnitType()) { 84 case LexicalUnit.SAC_IDENT: 85 if (lu.getStringValue().equalsIgnoreCase 86 (CSSConstants.CSS_AUTO_VALUE)) { 87 return ValueConstants.AUTO_VALUE; 88 } 89 break; 90 case LexicalUnit.SAC_EM: 91 return new FloatValue(CSSPrimitiveValue.CSS_EMS, 92 lu.getFloatValue()); 93 case LexicalUnit.SAC_EX: 94 return new FloatValue(CSSPrimitiveValue.CSS_EXS, 95 lu.getFloatValue()); 96 case LexicalUnit.SAC_PIXEL: 97 return new FloatValue(CSSPrimitiveValue.CSS_PX, 98 lu.getFloatValue()); 99 case LexicalUnit.SAC_CENTIMETER: 100 return new FloatValue(CSSPrimitiveValue.CSS_CM, 101 lu.getFloatValue()); 102 case LexicalUnit.SAC_MILLIMETER: 103 return new FloatValue(CSSPrimitiveValue.CSS_MM, 104 lu.getFloatValue()); 105 case LexicalUnit.SAC_INCH: 106 return new FloatValue(CSSPrimitiveValue.CSS_IN, 107 lu.getFloatValue()); 108 case LexicalUnit.SAC_POINT: 109 return new FloatValue(CSSPrimitiveValue.CSS_PT, 110 lu.getFloatValue()); 111 case LexicalUnit.SAC_PICA: 112 return new FloatValue(CSSPrimitiveValue.CSS_PC, 113 lu.getFloatValue()); 114 case LexicalUnit.SAC_INTEGER: 115 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 116 lu.getIntegerValue()); 117 case LexicalUnit.SAC_REAL: 118 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 119 lu.getFloatValue()); 120 case LexicalUnit.SAC_PERCENTAGE: 121 return new FloatValue(CSSPrimitiveValue.CSS_PERCENTAGE, 122 lu.getFloatValue()); 123 } 124 throw createMalformedRectDOMException(); 125 } 126 127 131 public Value computeValue(CSSStylableElement elt, 132 String pseudo, 133 CSSEngine engine, 134 int idx, 135 StyleMap sm, 136 Value value) { 137 if (value.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) { 138 return value; 139 } 140 if (value.getPrimitiveType() != CSSPrimitiveValue.CSS_RECT) { 141 return value; 142 } 143 RectValue rect = (RectValue)value; 144 145 orientation = VERTICAL_ORIENTATION; 146 Value top = super.computeValue(elt, pseudo, engine, idx, sm, 147 rect.getTop()); 148 Value bottom = super.computeValue(elt, pseudo, engine, idx, sm, 149 rect.getBottom()); 150 orientation = HORIZONTAL_ORIENTATION; 151 Value left = super.computeValue(elt, pseudo, engine, idx, sm, 152 rect.getLeft()); 153 Value right = super.computeValue(elt, pseudo, engine, idx, sm, 154 rect.getRight()); 155 if (top != rect.getTop() || 156 right != rect.getRight() || 157 bottom != rect.getBottom() || 158 left != rect.getLeft()) { 159 return new RectValue(top, right, bottom, left); 160 } else { 161 return value; 162 } 163 } 164 165 169 protected int getOrientation() { 170 return orientation; 171 } 172 173 private DOMException createMalformedRectDOMException() { 174 Object [] p = new Object [] { getPropertyName() }; 175 String s = Messages.formatMessage("malformed.rect", p); 176 return new DOMException (DOMException.SYNTAX_ERR, s); 177 } 178 } 179 | Popular Tags |