001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.css.engine.value;
020:
021: import org.apache.batik.css.engine.CSSEngine;
022: import org.apache.batik.css.engine.CSSStylableElement;
023: import org.apache.batik.css.engine.StyleMap;
024: import org.apache.batik.util.CSSConstants;
025: import org.w3c.css.sac.LexicalUnit;
026: import org.w3c.dom.DOMException;
027: import org.w3c.dom.css.CSSPrimitiveValue;
028: import org.w3c.dom.css.CSSValue;
029:
030: /**
031: * This class provides a manager for the property with support for
032: * rect values.
033: *
034: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
035: * @version $Id: RectManager.java 475685 2006-11-16 11:16:05Z cam $
036: */
037: public abstract class RectManager extends LengthManager {
038:
039: /**
040: * The current orientation.
041: */
042: protected int orientation;
043:
044: /**
045: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
046: */
047: public Value createValue(LexicalUnit lu, CSSEngine engine)
048: throws DOMException {
049: switch (lu.getLexicalUnitType()) {
050: case LexicalUnit.SAC_FUNCTION:
051: if (!lu.getFunctionName().equalsIgnoreCase("rect")) {
052: break;
053: }
054: case LexicalUnit.SAC_RECT_FUNCTION:
055: lu = lu.getParameters();
056: Value top = createRectComponent(lu);
057: lu = lu.getNextLexicalUnit();
058: if (lu == null
059: || lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
060: throw createMalformedRectDOMException();
061: }
062: lu = lu.getNextLexicalUnit();
063: Value right = createRectComponent(lu);
064: lu = lu.getNextLexicalUnit();
065: if (lu == null
066: || lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
067: throw createMalformedRectDOMException();
068: }
069: lu = lu.getNextLexicalUnit();
070: Value bottom = createRectComponent(lu);
071: lu = lu.getNextLexicalUnit();
072: if (lu == null
073: || lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
074: throw createMalformedRectDOMException();
075: }
076: lu = lu.getNextLexicalUnit();
077: Value left = createRectComponent(lu);
078: return new RectValue(top, right, bottom, left);
079: }
080: throw createMalformedRectDOMException();
081: }
082:
083: private Value createRectComponent(LexicalUnit lu)
084: throws DOMException {
085: switch (lu.getLexicalUnitType()) {
086: case LexicalUnit.SAC_IDENT:
087: if (lu.getStringValue().equalsIgnoreCase(
088: CSSConstants.CSS_AUTO_VALUE)) {
089: return ValueConstants.AUTO_VALUE;
090: }
091: break;
092: case LexicalUnit.SAC_EM:
093: return new FloatValue(CSSPrimitiveValue.CSS_EMS, lu
094: .getFloatValue());
095: case LexicalUnit.SAC_EX:
096: return new FloatValue(CSSPrimitiveValue.CSS_EXS, lu
097: .getFloatValue());
098: case LexicalUnit.SAC_PIXEL:
099: return new FloatValue(CSSPrimitiveValue.CSS_PX, lu
100: .getFloatValue());
101: case LexicalUnit.SAC_CENTIMETER:
102: return new FloatValue(CSSPrimitiveValue.CSS_CM, lu
103: .getFloatValue());
104: case LexicalUnit.SAC_MILLIMETER:
105: return new FloatValue(CSSPrimitiveValue.CSS_MM, lu
106: .getFloatValue());
107: case LexicalUnit.SAC_INCH:
108: return new FloatValue(CSSPrimitiveValue.CSS_IN, lu
109: .getFloatValue());
110: case LexicalUnit.SAC_POINT:
111: return new FloatValue(CSSPrimitiveValue.CSS_PT, lu
112: .getFloatValue());
113: case LexicalUnit.SAC_PICA:
114: return new FloatValue(CSSPrimitiveValue.CSS_PC, lu
115: .getFloatValue());
116: case LexicalUnit.SAC_INTEGER:
117: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, lu
118: .getIntegerValue());
119: case LexicalUnit.SAC_REAL:
120: return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, lu
121: .getFloatValue());
122: case LexicalUnit.SAC_PERCENTAGE:
123: return new FloatValue(CSSPrimitiveValue.CSS_PERCENTAGE, lu
124: .getFloatValue());
125: }
126: throw createMalformedRectDOMException();
127: }
128:
129: /**
130: * Implements {@link
131: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
132: */
133: public Value computeValue(CSSStylableElement elt, String pseudo,
134: CSSEngine engine, int idx, StyleMap sm, Value value) {
135: if (value.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) {
136: return value;
137: }
138: if (value.getPrimitiveType() != CSSPrimitiveValue.CSS_RECT) {
139: return value;
140: }
141: RectValue rect = (RectValue) value;
142:
143: orientation = VERTICAL_ORIENTATION;
144: Value top = super .computeValue(elt, pseudo, engine, idx, sm,
145: rect.getTop());
146: Value bottom = super .computeValue(elt, pseudo, engine, idx, sm,
147: rect.getBottom());
148: orientation = HORIZONTAL_ORIENTATION;
149: Value left = super .computeValue(elt, pseudo, engine, idx, sm,
150: rect.getLeft());
151: Value right = super .computeValue(elt, pseudo, engine, idx, sm,
152: rect.getRight());
153: if (top != rect.getTop() || right != rect.getRight()
154: || bottom != rect.getBottom() || left != rect.getLeft()) {
155: return new RectValue(top, right, bottom, left);
156: } else {
157: return value;
158: }
159: }
160:
161: /**
162: * Indicates the orientation of the property associated with
163: * this manager.
164: */
165: protected int getOrientation() {
166: return orientation;
167: }
168:
169: private DOMException createMalformedRectDOMException() {
170: Object[] p = new Object[] { getPropertyName() };
171: String s = Messages.formatMessage("malformed.rect", p);
172: return new DOMException(DOMException.SYNTAX_ERR, s);
173: }
174: }
|