001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.core;
028:
029: /**
030: * Generic structure for storing name-value pairs.
031: *
032: * @version JAIN-SIP-1.1
033: *
034: *
035: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
036: *
037: */
038: public class NameValue extends GenericObject {
039: /** FLag to indicate the value is a quoted string. */
040: protected boolean isQuotedString;
041: /** Field separator for name value encoding. */
042: protected String separator;
043: /** Characters used for quoting strings. */
044: protected String quotes;
045: /** The label for the name value pair. */
046: protected String name;
047: /** The value for this data element. */
048: protected Object value;
049:
050: /** Default constructor. */
051: public NameValue() {
052: name = null;
053: value = null;
054: separator = Separators.EQUALS;
055: this .quotes = "";
056: }
057:
058: /**
059: * Constructs a name value pair from initial strings.
060: * @param n the name of the key
061: * @param v the value for the pair
062: */
063: public NameValue(String n, Object v) {
064: name = n;
065: separator = Separators.EQUALS;
066: quotes = "";
067: setValue(v);
068: }
069:
070: /**
071: * Sets the separator for the encoding method below.
072: * @param sep the field separator for the encoded string
073: */
074: public void setSeparator(String sep) {
075: separator = sep;
076: }
077:
078: /**
079: * Sets the type of the field to be a quoted string.
080: * A flag that indicates that doublequotes should be put around the
081: * value when encoded
082: * (for example name=value when value is doublequoted).
083: */
084: public void setQuotedValue() {
085: isQuotedString = true;
086: this .quotes = Separators.DOUBLE_QUOTE;
087: }
088:
089: /**
090: * Returns true if the value is quoted in doublequotes.
091: * @return true if the value is of type quoted string
092: */
093: public boolean isValueQuoted() {
094: return isQuotedString;
095: }
096:
097: /**
098: * Gets the name.
099: * @return the name
100: */
101: public String getName() {
102: return name;
103: }
104:
105: /**
106: * Gets the value.
107: * @return the value
108: */
109: public Object getValue() {
110: if (isValueQuoted()) {
111: return "\"" + value + "\"";
112: } else {
113: return value;
114: }
115: }
116:
117: /**
118: * Gets the unquoted value.
119: * @return the value
120: */
121: public Object getUnquotedValue() {
122: return value;
123: }
124:
125: /**
126: * Sets the name member.
127: * @param n the name for the key
128: */
129: public void setName(String n) {
130: name = n;
131: }
132:
133: /**
134: * Sets the value member.
135: * @param v the value for the pair
136: */
137: public void setValue(Object v) {
138: value = v;
139: if (value != null) {
140: if (value instanceof String) {
141: String str = (String) value;
142: if (str.startsWith("\"") && str.endsWith("\"")) {
143: setQuotedValue();
144: str = str.substring(1, str.length() - 1);
145: value = (Object) str;
146: }
147: }
148: }
149: }
150:
151: /**
152: * Gets the encoded representation of this namevalue object.
153: * Added doublequote for encoding doublequoted values
154: * @since 1.0
155: * @return an encoded name value (eg. name=value) string.
156: */
157: public String encode() {
158: if (name != null && value != null) {
159: return name + separator + quotes + value.toString()
160: + quotes;
161: } else if (name == null && value != null) {
162: return quotes + value.toString() + quotes;
163: } else if (name != null && value == null) {
164: return name;
165: } else
166: return "";
167: }
168:
169: /**
170: * Makes a copy of the current instance.
171: * @return copy of current object
172: */
173: public Object clone() {
174: NameValue retval = new NameValue();
175: retval.separator = this .separator;
176: retval.isQuotedString = this .isQuotedString;
177: retval.quotes = this .quotes;
178: retval.name = this .name;
179: if (value != null && value instanceof GenericObject) {
180: retval.value = ((GenericObject) this .value).clone();
181: } else
182: retval.value = this .value;
183: return retval;
184: }
185:
186: /**
187: * Equality comparison predicate.
188: * @param other the object for comparison
189: * @return true if the instances are equivalent
190: */
191: public boolean equals(Object other) {
192: if (!other.getClass().equals(this .getClass()))
193: return false;
194: NameValue that = (NameValue) other;
195: if (this == that)
196: return true;
197: if (this .name == null && that.name != null || this .name != null
198: && that.name == null)
199: return false;
200: if (this .name != null
201: && that.name != null
202: && this .name.toLowerCase().compareTo(
203: that.name.toLowerCase()) != 0)
204: return false;
205: if (this .value != null && that.value == null
206: || this .value == null && that.value != null)
207: return false;
208: if (this .value == that.value)
209: return true;
210: if (value instanceof String) {
211: // Quoted string comparisions are case sensitive.
212: if (isQuotedString)
213: return this .value.equals(that.value);
214: String val = (String) this .value;
215: String val1 = (String) that.value;
216: return val.toLowerCase().equals(val1.toLowerCase());
217: } else
218: return this .value.equals(that.value);
219: }
220:
221: /**
222: * Converts the contents to a string.
223: * @return the encoded string contenets
224: */
225: public String toString() {
226: return this.encode();
227: }
228:
229: }
|