001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Batik" and "Apache Software Foundation" must not be
026: used to endorse or promote products derived from this software without
027: prior written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation. For more information on the
047: Apache Software Foundation, please see <http://www.apache.org/>.
048:
049: */
050:
051: package org.apache.batik.css.engine.value;
052:
053: import org.apache.batik.css.engine.CSSEngine;
054: import org.apache.batik.css.engine.CSSStylableElement;
055: import org.apache.batik.css.engine.StyleMap;
056: import org.apache.batik.util.CSSConstants;
057: import org.w3c.css.sac.LexicalUnit;
058: import org.w3c.dom.DOMException;
059: import org.w3c.dom.css.CSSPrimitiveValue;
060: import org.w3c.dom.css.CSSValue;
061:
062: /**
063: * This class provides a manager for the property with support for
064: * rect values.
065: *
066: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
067: * @version $Id$
068: */
069: public abstract class RectManager extends LengthManager {
070:
071: /**
072: * The current orientation.
073: */
074: protected int orientation;
075:
076: /**
077: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
078: */
079: public Value createValue(LexicalUnit lu, CSSEngine engine)
080: throws DOMException {
081: switch (lu.getLexicalUnitType()) {
082: case LexicalUnit.SAC_FUNCTION:
083: if (!lu.getFunctionName().equalsIgnoreCase("rect")) {
084: break;
085: }
086: case LexicalUnit.SAC_RECT_FUNCTION:
087: lu = lu.getParameters();
088: Value top = createRectComponent(lu);
089: lu = lu.getNextLexicalUnit();
090: if (lu == null
091: || lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
092: throw createMalformedRectDOMException();
093: }
094: lu = lu.getNextLexicalUnit();
095: Value right = createRectComponent(lu);
096: lu = lu.getNextLexicalUnit();
097: if (lu == null
098: || lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
099: throw createMalformedRectDOMException();
100: }
101: lu = lu.getNextLexicalUnit();
102: Value bottom = createRectComponent(lu);
103: lu = lu.getNextLexicalUnit();
104: if (lu == null
105: || lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
106: throw createMalformedRectDOMException();
107: }
108: lu = lu.getNextLexicalUnit();
109: Value left = createRectComponent(lu);
110: return new RectValue(top, right, bottom, left);
111: }
112: throw createMalformedRectDOMException();
113: }
114:
115: private Value createRectComponent(LexicalUnit lu)
116: throws DOMException {
117: switch (lu.getLexicalUnitType()) {
118: case LexicalUnit.SAC_IDENT:
119: if (lu.getStringValue().equalsIgnoreCase(
120: CSSConstants.CSS_AUTO_VALUE)) {
121: return ValueConstants.AUTO_VALUE;
122: }
123: break;
124: case LexicalUnit.SAC_EM:
125: return new FloatValue(CSSPrimitiveValue.CSS_EMS, lu
126: .getFloatValue());
127: case LexicalUnit.SAC_EX:
128: return new FloatValue(CSSPrimitiveValue.CSS_EXS, lu
129: .getFloatValue());
130: case LexicalUnit.SAC_PIXEL:
131: return new FloatValue(CSSPrimitiveValue.CSS_PX, lu
132: .getFloatValue());
133: case LexicalUnit.SAC_CENTIMETER:
134: return new FloatValue(CSSPrimitiveValue.CSS_CM, lu
135: .getFloatValue());
136: case LexicalUnit.SAC_MILLIMETER:
137: return new FloatValue(CSSPrimitiveValue.CSS_MM, lu
138: .getFloatValue());
139: case LexicalUnit.SAC_INCH:
140: return new FloatValue(CSSPrimitiveValue.CSS_IN, lu
141: .getFloatValue());
142: case LexicalUnit.SAC_POINT:
143: return new FloatValue(CSSPrimitiveValue.CSS_PT, lu
144: .getFloatValue());
145: case LexicalUnit.SAC_PICA:
146: return new FloatValue(CSSPrimitiveValue.CSS_PC, lu
147: .getFloatValue());
148: case LexicalUnit.SAC_INTEGER:
149: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, lu
150: .getIntegerValue());
151: case LexicalUnit.SAC_REAL:
152: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, lu
153: .getFloatValue());
154: case LexicalUnit.SAC_PERCENTAGE:
155: return new FloatValue(CSSPrimitiveValue.CSS_PERCENTAGE, lu
156: .getFloatValue());
157: }
158: throw createMalformedRectDOMException();
159: }
160:
161: /**
162: * Implements {@link
163: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
164: */
165: public Value computeValue(CSSStylableElement elt, String pseudo,
166: CSSEngine engine, int idx, StyleMap sm, Value value) {
167: if (value.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) {
168: return value;
169: }
170: if (value.getPrimitiveType() != CSSPrimitiveValue.CSS_RECT) {
171: return value;
172: }
173: RectValue rect = (RectValue) value;
174:
175: orientation = VERTICAL_ORIENTATION;
176: Value top = super .computeValue(elt, pseudo, engine, idx, sm,
177: rect.getTop());
178: Value bottom = super .computeValue(elt, pseudo, engine, idx, sm,
179: rect.getBottom());
180: orientation = HORIZONTAL_ORIENTATION;
181: Value left = super .computeValue(elt, pseudo, engine, idx, sm,
182: rect.getLeft());
183: Value right = super .computeValue(elt, pseudo, engine, idx, sm,
184: rect.getRight());
185: if (top != rect.getTop() || right != rect.getRight()
186: || bottom != rect.getBottom() || left != rect.getLeft()) {
187: return new RectValue(top, right, bottom, left);
188: } else {
189: return value;
190: }
191: }
192:
193: /**
194: * Indicates the orientation of the property associated with
195: * this manager.
196: */
197: protected int getOrientation() {
198: return orientation;
199: }
200:
201: private DOMException createMalformedRectDOMException() {
202: Object[] p = new Object[] { getPropertyName() };
203: String s = Messages.formatMessage("malformed.rect", p);
204: return new DOMException(DOMException.SYNTAX_ERR, s);
205: }
206: }
|