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: import java.util.*;
030: import java.util.Hashtable;
031:
032: /**
033: * Implements a simple NameValue association with a quick lookup.
034: *
035: * @version JAIN-SIP-1.1
036: *
037: *
038: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
039: *
040: */
041: public class NameValueList extends GenericObject {
042: /** Internal name value list. */
043: private Hashtable nvList;
044: /** Separator character. */
045: private String separator;
046:
047: /**
048: * Constructs a new list.
049: * @param listName label for this list
050: */
051: public NameValueList(String listName) {
052: nvList = new Hashtable();
053: this .separator = Separators.SEMICOLON;
054: }
055:
056: /**
057: * Gets a list of key names.
058: * @return list of key names.
059: */
060: public Vector getNames() {
061: Vector names = new Vector();
062: Enumeration enumNames = nvList.keys();
063:
064: while (enumNames.hasMoreElements()) {
065: names.addElement(enumNames.nextElement());
066: }
067:
068: return names;
069: }
070:
071: /**
072: * Gets the enumeration of key names.
073: * @return enumeration of key names.
074: */
075: public Enumeration getKeys() {
076: return nvList.keys();
077: }
078:
079: /**
080: * Adds a name value pair to the list.
081: * @param nv the data to be stored
082: */
083: public void add(NameValue nv) {
084: if (nv == null)
085: throw new NullPointerException("null nv");
086: nvList.put(nv.getName(), nv.getValue());
087: }
088:
089: /**
090: * Sets a namevalue object in this list.
091: * @param nv the data to be updated
092: */
093: public void set(NameValue nv) {
094: this .add(nv);
095: }
096:
097: /**
098: * Sets a namevalue object in this list.
099: * @param name the label for the data element
100: * @param value the value for the element
101: */
102: public void set(String name, Object value) {
103: NameValue nv = new NameValue(name, value);
104: this .set(nv);
105: }
106:
107: /**
108: * Adds a name value record to this list.
109: * @param name the label for the data element
110: * @param obj the value for the data element
111: */
112: public void add(String name, Object obj) {
113: if (name == null)
114: throw new NullPointerException("name in null ! ");
115: NameValue nv = new NameValue(name, obj);
116: add(nv);
117: }
118:
119: /**
120: * Compares if two NameValue lists are equal.
121: * @param otherObject is the object to compare to.
122: * @return true if the two objects compare for equality.
123: */
124: public boolean equals(Object otherObject) {
125: if (!otherObject.getClass().equals(this .getClass())) {
126: return false;
127: }
128: NameValueList other = (NameValueList) otherObject;
129:
130: if (this .nvList.size() != other.nvList.size()) {
131: return false;
132: }
133: Enumeration enumNames = nvList.keys();
134: String currKey;
135: Object currValue, currValueOther;
136: while (enumNames.hasMoreElements()) {
137: currKey = (String) enumNames.nextElement();
138: currValue = this .nvList.get(currKey);
139: currValueOther = other.nvList.get(currKey);
140: if ((currValueOther == null)
141: || !currValue.equals(currValueOther)) {
142: return false;
143: }
144: }
145: return true;
146: }
147:
148: /**
149: * Do a lookup on a given name and return value associated with it.
150: * @param name to be looked up
151: * @return the object that was found or null if not found
152: */
153: public Object getValue(String name) {
154: return nvList.get(name.toLowerCase());
155: }
156:
157: /**
158: * Do a lookup on a given name and return value associated with it.
159: * @param name to be looked up
160: * @param nameDefault to be returned when name is not found
161: * @return the object that was found or default if not found
162: */
163: public String getValueDefault(String name, String nameDefault) {
164: String returnValue = (String) nvList.get(name.toLowerCase());
165: if (returnValue == null)
166: returnValue = nameDefault;
167: return returnValue;
168: }
169:
170: /**
171: * Gets the NameValue record given a name.
172: * @param name the data element laebl to find
173: * @return the name value found or null if not found
174: * @since 1.0
175: */
176: public NameValue getNameValue(String name) {
177: if (name == null)
178: throw new NullPointerException("null arg!");
179: String name1 = name.toLowerCase();
180: NameValue returnValue = null;
181: Object value = getValue(name1);
182: if (value != null)
183: returnValue = new NameValue(name1, value);
184: return returnValue;
185: }
186:
187: /**
188: * Returns a boolean telling if this NameValueList
189: * has a record with this name.
190: * @param name the label to find
191: * @return true if the element includes a value
192: * @since 1.0
193: */
194: public boolean hasNameValue(String name) {
195: return nvList.containsKey(name.toLowerCase());
196: }
197:
198: /**
199: * Removes the element corresponding to this name.
200: * @param name the label to find
201: * @return true if successfully removed
202: * @since 1.0
203: */
204: public boolean delete(String name) {
205: if (name == null) {
206: return true;
207: }
208:
209: String name1 = name.toLowerCase();
210: nvList.remove(name1);
211:
212: return true;
213: }
214:
215: /**
216: * Default constructor.
217: */
218: public NameValueList() {
219: nvList = new Hashtable();
220: this .separator = Separators.SEMICOLON;
221: }
222:
223: /**
224: * Makes a copy of the current instance.
225: * @return copy of current object
226: */
227: public Object clone() {
228: NameValueList retval = new NameValueList();
229: retval.separator = this .separator;
230: Enumeration enumNames = nvList.keys();
231: String currKey;
232: while (enumNames.hasMoreElements()) {
233: currKey = (String) enumNames.nextElement();
234: retval.add(currKey, nvList.get(currKey));
235: }
236: return retval;
237:
238: }
239:
240: /**
241: * Gets the parameter as a String.
242: * @param name the label to find
243: * @return the parameter as a string.
244: */
245: public String getParameter(String name) {
246: Object val = this .getValue(name);
247: if (val == null)
248: return null;
249: if (val instanceof GenericObject)
250: return ((GenericObject) val).encode();
251: else
252: return val.toString();
253: }
254:
255: /**
256: * Encodes the contenst as a string.
257: * @return the encoded text string.
258: */
259: public String encode() {
260: if (nvList.size() == 0)
261: return "";
262:
263: StringBuffer encoding = new StringBuffer();
264: Enumeration enumNames = nvList.keys();
265: String currKey;
266: Object currValue;
267:
268: while (enumNames.hasMoreElements()) {
269: currKey = (String) enumNames.nextElement();
270: encoding.append(currKey);
271: currValue = nvList.get(currKey);
272:
273: if (currValue != null) {
274: if (currValue instanceof GenericObject) {
275: GenericObject gobj = (GenericObject) currValue;
276: encoding.append(Separators.EQUALS + gobj.encode());
277: } else {
278: String s = currValue.toString();
279:
280: if (s.length() > 0) {
281: encoding.append(Separators.EQUALS + s);
282: }
283: }
284: }
285:
286: if (enumNames.hasMoreElements()) // not last
287: encoding.append(separator);
288: }
289:
290: return encoding.toString();
291: }
292:
293: /**
294: * Encodes the contenst as a string followd by separator.
295: * @return the encoded text string.
296: */
297: public String encodeWithSep() {
298: String retVal = encode();
299: if (retVal.length() > 0) {
300: retVal = separator + retVal;
301: }
302: return retVal;
303: }
304:
305: /**
306: * Converts contenets to a string.
307: * @return contenets encoded in a text string
308: */
309: public String toString() {
310: return this .encode();
311: }
312:
313: /**
314: * Sets the separator string to be used in formatted contents.
315: * @param separator string to use between fields
316: */
317: public void setSeparator(String separator) {
318: this .separator = separator;
319: }
320:
321: /**
322: * Checks if the listis empty.
323: * @return true if the size of the list is zero
324: */
325: public boolean isEmpty() {
326: return this .nvList.size() == 0;
327: }
328:
329: /**
330: * Gets the size of the list.
331: * @return the count of elements in the list
332: */
333: public int size() {
334: return nvList.size();
335: }
336: }
|