001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: StringProperty.java 554251 2007-07-07 20:13:41Z adelmelle $ */
019:
020: package org.apache.fop.fo.properties;
021:
022: import org.apache.fop.fo.FObj;
023: import org.apache.fop.fo.PropertyList;
024:
025: /**
026: * Exists primarily as a container for its Maker inner class, which is
027: * extended by many string-based FO property classes.
028: */
029: public class StringProperty extends Property {
030:
031: /**
032: * Inner class for making instances of StringProperty
033: */
034: public static class Maker extends PropertyMaker {
035:
036: /**
037: * @param propId the id of the property for which a Maker should be created
038: */
039: public Maker(int propId) {
040: super (propId);
041: }
042:
043: /**
044: * Make a new StringProperty object
045: * @param propertyList not used
046: * @param value String value of the new object
047: * @param fo not used
048: * @return the StringProperty object
049: */
050: public Property make(PropertyList propertyList, String value,
051: FObj fo) {
052: // Work around the fact that most String properties are not
053: // specified as actual String literals (with "" or '') since
054: // the attribute values themselves are Strings!
055: // If the value starts with ' or ", make sure it also ends with
056: // this character
057: // Otherwise, just take the whole value as the String
058: int vlen = value.length() - 1;
059: if (vlen > 0) {
060: char q1 = value.charAt(0);
061: if (q1 == '"' || q1 == '\'') {
062: if (value.charAt(vlen) == q1) {
063: return new StringProperty(value.substring(1,
064: vlen));
065: }
066: log.warn("String-valued property starts with quote"
067: + " but doesn't end with quote: " + value);
068: // fall through and use the entire value, including first quote
069: }
070: String str = checkValueKeywords(value);
071: if (str != null) {
072: value = str;
073: }
074: }
075: return new StringProperty(value);
076: }
077:
078: } // end String.Maker
079:
080: /** cache containing all canonical StringProperty instances */
081: private static final PropertyCache cache = new PropertyCache();
082:
083: private final String str;
084:
085: /**
086: * Constructor
087: * @param str String value to place in this object
088: */
089: private StringProperty(String str) {
090: this .str = str;
091: }
092:
093: /**
094: * Return the canonical StringProperty instance
095: * corresponding to the given string value
096: * @param str the base String
097: * @return the canonical instance
098: */
099: public static StringProperty getInstance(String str) {
100: return (StringProperty) cache.fetch(new StringProperty(str));
101: }
102:
103: /**
104: * @return the Object equivalent of this property
105: */
106: public Object getObject() {
107: return this .str;
108: }
109:
110: /**
111: * @return the String equivalent of this property
112: */
113: public String getString() {
114: return this .str;
115: }
116:
117: /**
118: * @see java.lang.Object#hashCode()
119: */
120: public boolean equals(Object obj) {
121: if (obj instanceof StringProperty) {
122: StringProperty sp = (StringProperty) obj;
123: return (sp.str == this .str || sp.str.equals(this .str));
124: } else {
125: return false;
126: }
127: }
128:
129: /**
130: * @see java.lang.Object#hashCode()
131: */
132: public int hashCode() {
133: return str.hashCode();
134: }
135: }
|