KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > util > NameValuePairs


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17
18  */

19
20 package org.apache.pluto.portalImpl.util;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.pluto.util.StringUtils;
27
28 /**
29  ** This class collects name/value pairs and provides
30  ** convenient methods to access them as different types.
31  **
32  ** @see Properties
33  ** @see Parameters
34  **/

35
36 public abstract class NameValuePairs
37 {
38
39     /**
40      ** Returns the number of name/value pairs. Names
41      ** that have more than one value are counted as one name.
42      **
43      ** @return the number of name/value pairs
44      **/

45
46     public int size ()
47     {
48         return (iEntries.size ());
49     }
50
51     /**
52      ** Returns the value for the given name as string,
53      ** or <CODE>null</CODE> if there is no pair with the given name.
54      **
55      ** @param aName
56      ** the name of a pair
57      **
58      ** @return the value of the pair
59      **/

60
61     public String JavaDoc getString (String JavaDoc aName)
62     {
63         return (findString (aName));
64     }
65
66     /**
67      ** Returns the value for the given name as string,
68      ** or the given default if there is no pair with the given name.
69      **
70      ** @param aName
71      ** the name of a pair
72      ** @param aDefault
73      ** the default value
74      **
75      ** @return the value of the pair
76      **/

77
78     public String JavaDoc getString (String JavaDoc aName, String JavaDoc aDefault)
79     {
80         String JavaDoc result = findString (aName);
81
82         if (result == null)
83             result = aDefault;
84
85         return (result);
86     }
87
88     /**
89      ** Returns all values for the given name as a string array,
90      ** or <CODE>null</CODE> if there is no pair with the given name.
91      **
92      ** @param aName
93      ** the name of a pair
94      **
95      ** @return the values of the pair
96      **/

97
98     public String JavaDoc [] getStrings (String JavaDoc aName)
99     {
100         String JavaDoc [] result = null;
101
102         Entry entry = findEntry (aName);
103
104         if (entry != null)
105             result = entry.iValues;
106
107         return (result);
108
109     }
110
111     public Integer JavaDoc getInteger (String JavaDoc aName)
112     {
113         return (getInteger (aName, null));
114     }
115
116     public Integer JavaDoc getInteger (String JavaDoc aName, Integer JavaDoc aDefault)
117     {
118         Integer JavaDoc result = aDefault;
119
120         String JavaDoc value = findString (aName);
121
122         if (value != null)
123             result = Integer.valueOf (value);
124
125         return (result);
126     }
127
128     public int getInteger (String JavaDoc aName, int aDefault)
129     {
130         int result = aDefault;
131
132         String JavaDoc value = findString (aName);
133
134         if (value != null)
135             result = Integer.parseInt (value);
136
137         return (result);
138     }
139
140     public Boolean JavaDoc getBoolean (String JavaDoc aName)
141     {
142         return (getBoolean (aName, null));
143     }
144
145     public Boolean JavaDoc getBoolean (String JavaDoc aName, Boolean JavaDoc aDefault)
146     {
147         Boolean JavaDoc result = aDefault;
148
149         String JavaDoc value = findString (aName);
150
151         if (value != null)
152         {
153             result = StringUtils.booleanOf (value);
154         }
155
156         return (result);
157     }
158
159     public boolean getBoolean (String JavaDoc aName, boolean aDefault)
160     {
161         return (getBoolean (aName, aDefault ? Boolean.TRUE : Boolean.FALSE).booleanValue ());
162     }
163
164     public Iterator JavaDoc names ()
165     {
166         return (new EntryIterator (this));
167     }
168
169     public final Iterator JavaDoc keys ()
170     {
171         return (names ());
172     }
173
174     public void setParent (NameValuePairs aParent)
175     {
176         iParent = aParent;
177     }
178
179     public String JavaDoc toString ()
180     {
181         return (iEntries.toString ());
182     }
183
184     // --- PROTECTED MEMBERS --- //
185

186     protected NameValuePairs ()
187     {
188     }
189
190     protected void add (String JavaDoc aName, String JavaDoc aValue)
191     {
192         add (aName, new String JavaDoc [] { aValue });
193     }
194
195     protected void add (String JavaDoc aName, String JavaDoc [] aValues)
196     {
197         if (aName == null)
198             throw (new IllegalArgumentException JavaDoc ("NameValuePairs: Argument \"aName\" cannot be null."));
199         if (aValues == null)
200             throw (new IllegalArgumentException JavaDoc ("NameValuePairs: Argument \"aValues\" cannot be null."));
201
202         for (int i = 0; i < aValues.length; i++)
203         {
204             if (aValues [i] == null)
205                 throw (new IllegalArgumentException JavaDoc ("NameValuePairs: Argument \"aValues\" cannot contain null."));
206         }
207
208         Entry entry = findEntry (aName);
209
210         if (entry == null)
211         {
212             entry = new Entry (aName, aValues);
213
214             iEntries.add (entry);
215         }
216         else
217         {
218             String JavaDoc [] values = new String JavaDoc [entry.iValues.length + aValues.length];
219
220             System.arraycopy (entry.iValues, 0, values, 0, entry.iValues.length);
221             System.arraycopy (aValues, 0, values, entry.iValues.length, aValues.length);
222
223             entry.iValues = values;
224         }
225     }
226
227     protected Entry findEntry (String JavaDoc aName)
228     {
229         if (aName == null)
230             throw (new IllegalArgumentException JavaDoc ("NameValuePairs: Argument \"aName\" cannot be null!"));
231
232         Entry result = null;
233
234         for (Iterator JavaDoc iter = iEntries.iterator (); iter.hasNext (); )
235         {
236             Entry entry = (Entry) iter.next ();
237
238             if (entry.iName.equals (aName))
239             {
240                 result = entry;
241                 break;
242             }
243         }
244
245         if (result == null && iParent != null)
246         {
247             result = iParent.findEntry (aName);
248         }
249
250         return (result);
251     }
252
253     protected void removeEntry (String JavaDoc aName)
254     {
255         if (aName == null)
256             throw (new IllegalArgumentException JavaDoc ("NameValuePairs: Argument \"aName\" cannot be null!"));
257
258         boolean found = false;
259
260         for (Iterator JavaDoc iter = iEntries.iterator (); iter.hasNext (); )
261         {
262             Entry entry = (Entry) iter.next ();
263
264             if (entry.iName.equals (aName))
265             {
266                 iter.remove ();
267
268                 found = true;
269                 break;
270             }
271         }
272
273         if (! found && iParent != null)
274         {
275             iParent.removeEntry (aName);
276         }
277     }
278
279     private NameValuePairs iParent;
280
281     private List JavaDoc iEntries = new LinkedList JavaDoc ();
282
283     private String JavaDoc findString (String JavaDoc aName)
284     {
285         String JavaDoc result = null;
286
287         Entry entry = findEntry (aName);
288
289         if (entry != null)
290             result = entry.iValues [0];
291
292         return (result);
293     }
294
295     NameValuePairs getParent()
296     {
297         return iParent;
298     }
299
300     List JavaDoc getEntries()
301     {
302         return iEntries;
303     }
304
305     public static class Entry
306     {
307
308         String JavaDoc iName;
309         String JavaDoc [] iValues;
310
311         protected Entry (String JavaDoc aName, String JavaDoc [] aValues)
312         {
313             iName = aName;
314             iValues = aValues;
315         }
316
317         public String JavaDoc toString ()
318         {
319             StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
320
321             result.append (iName);
322             result.append (" = ");
323
324             for (int i = 0; i < iValues.length; i++)
325             {
326                 if (i > 0)
327                     result.append (", ");
328
329                 result.append (iValues [i]);
330             }
331
332             return (result.toString ());
333         }
334     }
335
336     private static class EntryIterator implements Iterator JavaDoc
337     {
338
339         private NameValuePairs iPairs;
340         private Iterator JavaDoc iIterator;
341
342         private EntryIterator (NameValuePairs aPairs)
343         {
344             iPairs = aPairs;
345             iIterator = iPairs.getEntries().iterator();
346         }
347
348         // Iterator implementation.
349

350         public boolean hasNext ()
351         {
352             if (!nextParent()) return false;
353             return (iIterator.hasNext ());
354         }
355
356         public Object JavaDoc next ()
357         {
358             if (!nextParent()) return null;
359             return (((Entry) iIterator.next ()).iName);
360         }
361
362         public void remove()
363         {
364             iIterator.remove ();
365         }
366
367         // additional methods.
368

369         private boolean nextParent()
370         {
371             while (!iIterator.hasNext ())
372             {
373                 iPairs = iPairs.getParent();
374                 if (iPairs==null) return false;
375                 iIterator = iPairs.getEntries().iterator();
376             }
377             return true;
378         }
379
380     }
381 }
382
Popular Tags