KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > RectManager


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

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 JavaDoc;
26 import org.w3c.dom.css.CSSPrimitiveValue;
27 import org.w3c.dom.css.CSSValue;
28
29 /**
30  * This class provides a manager for the property with support for
31  * rect values.
32  *
33  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
34  * @version $Id: RectManager.java,v 1.5 2004/08/18 07:12:53 vhardy Exp $
35  */

36 public abstract class RectManager extends LengthManager {
37     
38     /**
39      * The current orientation.
40      */

41     protected int orientation;
42
43     /**
44      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
45      */

46     public Value createValue(LexicalUnit lu, CSSEngine engine)
47         throws DOMException JavaDoc {
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 JavaDoc {
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     /**
128      * Implements {@link
129      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
130      */

131     public Value computeValue(CSSStylableElement elt,
132                               String JavaDoc 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     /**
166      * Indicates the orientation of the property associated with
167      * this manager.
168      */

169     protected int getOrientation() {
170         return orientation;
171     }
172
173     private DOMException JavaDoc createMalformedRectDOMException() {
174         Object JavaDoc[] p = new Object JavaDoc[] { getPropertyName() };
175         String JavaDoc s = Messages.formatMessage("malformed.rect", p);
176         return new DOMException JavaDoc(DOMException.SYNTAX_ERR, s);
177     }
178 }
179
Popular Tags