01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
10: http://www.apache.org/licenses/LICENSE-2.0
11:
12: Unless required by applicable law or agreed to in writing, software
13: distributed under the License is distributed on an "AS IS" BASIS,
14: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: See the License for the specific language governing permissions and
16: limitations under the License.
17:
18: */
19: package org.apache.batik.css.engine.value;
20:
21: import org.w3c.dom.DOMException;
22: import org.w3c.dom.css.CSSPrimitiveValue;
23:
24: /**
25: * This class represents float values.
26: *
27: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
28: * @version $Id: FloatValue.java 478160 2006-11-22 13:35:06Z dvholten $
29: */
30: public class FloatValue extends AbstractValue {
31:
32: /**
33: * Returns the CSS text associated with the given type/value pair.
34: */
35: public static String getCssText(short unit, float value) {
36: if (unit < 0 || unit >= UNITS.length) {
37: throw new DOMException(DOMException.SYNTAX_ERR, "");
38: }
39: String s = String.valueOf(value);
40: if (s.endsWith(".0")) {
41: s = s.substring(0, s.length() - 2);
42: }
43: return s + UNITS[unit - CSSPrimitiveValue.CSS_NUMBER];
44: }
45:
46: /**
47: * The unit types representations
48: */
49: protected static final String[] UNITS = { "", "%", "em", "ex",
50: "px", "cm", "mm", "in", "pt", "pc", "deg", "rad", "grad",
51: "ms", "s", "Hz", "kHz", "" };
52:
53: /**
54: * The float value
55: */
56: protected float floatValue;
57:
58: /**
59: * The unit type
60: */
61: protected short unitType;
62:
63: /**
64: * Creates a new value.
65: */
66: public FloatValue(short unitType, float floatValue) {
67: this .unitType = unitType;
68: this .floatValue = floatValue;
69: }
70:
71: /**
72: * The type of the value.
73: */
74: public short getPrimitiveType() {
75: return unitType;
76: }
77:
78: /**
79: * Returns the float value.
80: */
81: public float getFloatValue() {
82: return floatValue;
83: }
84:
85: /**
86: * A string representation of the current value.
87: */
88: public String getCssText() {
89: return getCssText(unitType, floatValue);
90: }
91:
92: /**
93: * Returns a printable representation of this value.
94: */
95: public String toString() {
96: return getCssText();
97: }
98: }
|