View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   ** This class collects name/value pairs and provides
45   ** convenient methods to access them as different types.
46   **
47   ** @see   Properties
48   ** @see   Parameters
49   **/
50  public abstract class NameValuePairs
51  {
52      private NameValuePairs iParent;
53      private List iEntries = new LinkedList(  );
54  
55      // --- PROTECTED MEMBERS --- //
56      protected NameValuePairs(  )
57      {
58      }
59  
60      /**
61       ** Returns the number of name/value pairs. Names
62       ** that have more than one value are counted as one name.
63       **
64       ** @return   the number of name/value pairs
65       **/
66      public int size(  )
67      {
68          return ( iEntries.size(  ) );
69      }
70  
71      /**
72       ** Returns the value for the given name as string,
73       ** or <CODE>null</CODE> if there is no pair with the given name.
74       **
75       ** @param   aName
76       **          the name of a pair
77       **
78       ** @return   the value of the pair
79       **/
80      public String getString( String aName )
81      {
82          return ( findString( aName ) );
83      }
84  
85      /**
86       ** Returns the value for the given name as string,
87       ** or the given default if there is no pair with the given name.
88       **
89       ** @param   aName
90       **          the name of a pair
91       ** @param   aDefault
92       **          the default value
93       **
94       ** @return   the value of the pair
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      ** Returns all values for the given name as a string array,
110      ** or <CODE>null</CODE> if there is no pair with the given name.
111      **
112      ** @param   aName
113      **          the name of a pair
114      **
115      ** @return   the values of the pair
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     // XXX LUTECE
209     public void add( String aName, String aValue )
210     {
211         add( aName, new String[] { aValue } );
212     }
213 
214     // XXX LUTECE
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         // Iterator implementation.
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         // additional methods.
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 }