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.w3c.dom.DOMException;
022: import org.w3c.dom.css.CSSPrimitiveValue;
023:
024: /**
025: * This class represents string values.
026: *
027: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
028: * @version $Id: StringValue.java 478160 2006-11-22 13:35:06Z dvholten $
029: */
030: public class StringValue extends AbstractValue {
031:
032: /**
033: * Returns the CSS text associated with the given type/value pair.
034: */
035: public static String getCssText(short type, String value) {
036: switch (type) {
037: case CSSPrimitiveValue.CSS_URI:
038: return "url(" + value + ')';
039:
040: case CSSPrimitiveValue.CSS_STRING:
041: char q = (value.indexOf('"') != -1) ? '\'' : '"';
042: return q + value + q;
043: }
044: return value;
045: }
046:
047: /**
048: * The value of the string
049: */
050: protected String value;
051:
052: /**
053: * The unit type
054: */
055: protected short unitType;
056:
057: /**
058: * Creates a new StringValue.
059: */
060: public StringValue(short type, String s) {
061: unitType = type;
062: value = s;
063: }
064:
065: /**
066: * The type of the value.
067: */
068: public short getPrimitiveType() {
069: return unitType;
070: }
071:
072: /**
073: * Indicates whether some other object is "equal to" this one.
074: * @param obj the reference object with which to compare.
075: */
076: public boolean equals(Object obj) {
077: if (obj == null || !(obj instanceof StringValue)) {
078: return false;
079: }
080: StringValue v = (StringValue) obj;
081: if (unitType != v.unitType) {
082: return false;
083: }
084: return value.equals(v.value);
085: }
086:
087: /**
088: * A string representation of the current value.
089: */
090: public String getCssText() {
091: return getCssText(unitType, value);
092: }
093:
094: /**
095: * This method is used to get the string value.
096: * @exception DOMException
097: * INVALID_ACCESS_ERR: Raised if the value doesn't contain a string
098: * value.
099: */
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: }
|