1 /*
2
3    Licensed to the Apache Software Foundation (ASF) under one or more
4    contributor license agreements.  See the NOTICE file distributed with
5    this work for additional information regarding copyright ownership.
6    The ASF licenses this file to You under the Apache License, Version 2.0
7    (the "License"); you may not use this file except in compliance with
8    the License.  You may obtain a copy of the License at
9
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 string values.
26  *
27  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: StringValue.java 1733416 2016-03-03 07:07:13Z gadams $
29  */

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

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

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

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

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

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

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

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

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

107     public String toString() {
108         return getCssText();
109     }
110 }
111