KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2002 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.w3c.dom.DOMException JavaDoc;
21 import org.w3c.dom.css.CSSPrimitiveValue;
22
23 /**
24  * This class represents string values.
25  *
26  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
27  * @version $Id: StringValue.java,v 1.4 2004/08/18 07:12:53 vhardy Exp $
28  */

29 public class StringValue extends AbstractValue {
30
31     /**
32      * Returns the CSS text associated with the given type/value pair.
33      */

34     public static String JavaDoc getCssText(short type, String JavaDoc value) {
35     switch (type) {
36     case CSSPrimitiveValue.CSS_URI:
37         return "url(" + value + ")";
38
39     case CSSPrimitiveValue.CSS_STRING:
40         char q = (value.indexOf('"') != -1) ? '\'' : '"';
41         return q + value + q;
42     }
43     return value;
44     }
45     
46     /**
47      * The value of the string
48      */

49     protected String JavaDoc value;
50
51     /**
52      * The unit type
53      */

54     protected short unitType;
55
56     /**
57      * Creates a new StringValue.
58      */

59     public StringValue(short type, String JavaDoc s) {
60         unitType = type;
61         value = s;
62     }
63
64     /**
65      * The type of the value.
66      */

67     public short getPrimitiveType() {
68         return unitType;
69     }
70
71     /**
72      * Indicates whether some other object is "equal to" this one.
73      * @param obj the reference object with which to compare.
74      */

75     public boolean equals(Object JavaDoc obj) {
76     if (obj == null || !(obj instanceof StringValue)) {
77         return false;
78     }
79     StringValue v = (StringValue)obj;
80     if (unitType != v.unitType) {
81         return false;
82     }
83     return value.equals(v.value);
84     }
85
86     /**
87      * A string representation of the current value.
88      */

89     public String JavaDoc getCssText() {
90     return getCssText(unitType, value);
91     }
92
93     /**
94      * This method is used to get the string value.
95      * @exception DOMException
96      * INVALID_ACCESS_ERR: Raised if the value doesn't contain a string
97      * value.
98      */

99     public String JavaDoc getStringValue() throws DOMException JavaDoc {
100         return value;
101     }
102
103     /**
104      * Returns a printable representation of this value.
105      */

106     public String JavaDoc toString() {
107         return getCssText();
108     }
109 }
110
Popular Tags