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