1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.pluto.portalImpl.util;
17
18 import java.util.Iterator;
19 import java.util.LinkedList;
20 import java.util.List;
21
22 import org.apache.pluto.util.StringUtils;
23
24 /***
25 ** This class collects name/value pairs and provides
26 ** convenient methods to access them as different types.
27 **
28 ** @see Properties
29 ** @see Parameters
30 **/
31
32 public abstract class NameValuePairs
33 {
34
35 /***
36 ** Returns the number of name/value pairs. Names
37 ** that have more than one value are counted as one name.
38 **
39 ** @return the number of name/value pairs
40 **/
41
42 public int size ()
43 {
44 return (iEntries.size ());
45 }
46
47 /***
48 ** Returns the value for the given name as string,
49 ** or <CODE>null</CODE> if there is no pair with the given name.
50 **
51 ** @param aName
52 ** the name of a pair
53 **
54 ** @return the value of the pair
55 **/
56
57 public String getString (String aName)
58 {
59 return (findString (aName));
60 }
61
62 /***
63 ** Returns the value for the given name as string,
64 ** or the given default if there is no pair with the given name.
65 **
66 ** @param aName
67 ** the name of a pair
68 ** @param aDefault
69 ** the default value
70 **
71 ** @return the value of the pair
72 **/
73
74 public String getString (String aName, String aDefault)
75 {
76 String result = findString (aName);
77
78 if (result == null)
79 result = aDefault;
80
81 return (result);
82 }
83
84 /***
85 ** Returns all values for the given name as a string array,
86 ** or <CODE>null</CODE> if there is no pair with the given name.
87 **
88 ** @param aName
89 ** the name of a pair
90 **
91 ** @return the values of the pair
92 **/
93
94 public String [] getStrings (String aName)
95 {
96 String [] result = null;
97
98 Entry entry = findEntry (aName);
99
100 if (entry != null)
101 result = entry.iValues;
102
103 return (result);
104
105 }
106
107 public Integer getInteger (String aName)
108 {
109 return (getInteger (aName, null));
110 }
111
112 public Integer getInteger (String aName, Integer aDefault)
113 {
114 Integer result = aDefault;
115
116 String value = findString (aName);
117
118 if (value != null)
119 result = Integer.valueOf (value);
120
121 return (result);
122 }
123
124 public int getInteger (String aName, int aDefault)
125 {
126 int result = aDefault;
127
128 String value = findString (aName);
129
130 if (value != null)
131 result = Integer.parseInt (value);
132
133 return (result);
134 }
135
136 public Boolean getBoolean (String aName)
137 {
138 return (getBoolean (aName, null));
139 }
140
141 public Boolean getBoolean (String aName, Boolean aDefault)
142 {
143 Boolean result = aDefault;
144
145 String value = findString (aName);
146
147 if (value != null)
148 {
149 result = StringUtils.booleanOf (value);
150 }
151
152 return (result);
153 }
154
155 public boolean getBoolean (String aName, boolean aDefault)
156 {
157 return (getBoolean (aName, aDefault ? Boolean.TRUE : Boolean.FALSE).booleanValue ());
158 }
159
160 public Iterator names ()
161 {
162 return (new EntryIterator (this));
163 }
164
165 public final Iterator keys ()
166 {
167 return (names ());
168 }
169
170 public void setParent (NameValuePairs aParent)
171 {
172 iParent = aParent;
173 }
174
175 public String toString ()
176 {
177 return (iEntries.toString ());
178 }
179
180
181
182 protected NameValuePairs ()
183 {
184 }
185
186 protected void add (String aName, String aValue)
187 {
188 add (aName, new String [] { aValue });
189 }
190
191 protected void add (String aName, String [] aValues)
192 {
193 if (aName == null)
194 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aName\" cannot be null."));
195 if (aValues == null)
196 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aValues\" cannot be null."));
197
198 for (int i = 0; i < aValues.length; i++)
199 {
200 if (aValues [i] == null)
201 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aValues\" cannot contain null."));
202 }
203
204 Entry entry = findEntry (aName);
205
206 if (entry == null)
207 {
208 entry = new Entry (aName, aValues);
209
210 iEntries.add (entry);
211 }
212 else
213 {
214 String [] values = new String [entry.iValues.length + aValues.length];
215
216 System.arraycopy (entry.iValues, 0, values, 0, entry.iValues.length);
217 System.arraycopy (aValues, 0, values, entry.iValues.length, aValues.length);
218
219 entry.iValues = values;
220 }
221 }
222
223 protected Entry findEntry (String aName)
224 {
225 if (aName == null)
226 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aName\" cannot be null!"));
227
228 Entry result = null;
229
230 for (Iterator iter = iEntries.iterator (); iter.hasNext (); )
231 {
232 Entry entry = (Entry) iter.next ();
233
234 if (entry.iName.equals (aName))
235 {
236 result = entry;
237 break;
238 }
239 }
240
241 if (result == null && iParent != null)
242 {
243 result = iParent.findEntry (aName);
244 }
245
246 return (result);
247 }
248
249 protected void removeEntry (String aName)
250 {
251 if (aName == null)
252 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aName\" cannot be null!"));
253
254 boolean found = false;
255
256 for (Iterator iter = iEntries.iterator (); iter.hasNext (); )
257 {
258 Entry entry = (Entry) iter.next ();
259
260 if (entry.iName.equals (aName))
261 {
262 iter.remove ();
263
264 found = true;
265 break;
266 }
267 }
268
269 if (! found && iParent != null)
270 {
271 iParent.removeEntry (aName);
272 }
273 }
274
275 private NameValuePairs iParent;
276
277 private List iEntries = new LinkedList ();
278
279 private String findString (String aName)
280 {
281 String result = null;
282
283 Entry entry = findEntry (aName);
284
285 if (entry != null)
286 result = entry.iValues [0];
287
288 return (result);
289 }
290
291 NameValuePairs getParent()
292 {
293 return iParent;
294 }
295
296 List getEntries()
297 {
298 return iEntries;
299 }
300
301 public static class Entry
302 {
303
304 String iName;
305 String [] iValues;
306
307 protected Entry (String aName, String [] aValues)
308 {
309 iName = aName;
310 iValues = aValues;
311 }
312
313 public String toString ()
314 {
315 StringBuffer result = new StringBuffer ();
316
317 result.append (iName);
318 result.append (" = ");
319
320 for (int i = 0; i < iValues.length; i++)
321 {
322 if (i > 0)
323 result.append (", ");
324
325 result.append (iValues [i]);
326 }
327
328 return (result.toString ());
329 }
330 }
331
332 private static class EntryIterator implements Iterator
333 {
334
335 private NameValuePairs iPairs;
336 private Iterator iIterator;
337
338 private EntryIterator (NameValuePairs aPairs)
339 {
340 iPairs = aPairs;
341 iIterator = iPairs.getEntries().iterator();
342 }
343
344
345
346 public boolean hasNext ()
347 {
348 if (!nextParent()) return false;
349 return (iIterator.hasNext ());
350 }
351
352 public Object next ()
353 {
354 if (!nextParent()) return null;
355 return (((Entry) iIterator.next ()).iName);
356 }
357
358 public void remove()
359 {
360 iIterator.remove ();
361 }
362
363
364
365 private boolean nextParent()
366 {
367 while (!iIterator.hasNext ())
368 {
369 iPairs = iPairs.getParent();
370 if (iPairs==null) return false;
371 iIterator = iPairs.getEntries().iterator();
372 }
373 return true;
374 }
375
376 }
377 }