View Javadoc

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  package org.apache.pluto.portalImpl.util;
17  
18  import java.util.ArrayList;
19  import java.util.Enumeration;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletContext;
26  
27  public class Parameters extends NameValuePairs
28  {
29  
30      public Parameters (Map params)
31      {
32          Iterator iterator = params.keySet().iterator();
33  
34          while (iterator.hasNext())
35          {
36              String name = (String)iterator.next();
37  
38              super.add (name, (String)params.get(name));
39          }
40      }
41  
42      public Parameters (ServletConfig aConfig)
43      {
44          for (Enumeration e = aConfig.getInitParameterNames (); e.hasMoreElements ();)
45          {
46              String name = (String) e.nextElement ();
47  
48              super.add (name, aConfig.getInitParameter (name));
49          }
50      }
51  
52      public Parameters (ServletContext aContext)
53      {
54          for (Enumeration e = aContext.getInitParameterNames (); e.hasMoreElements ();)
55          {
56              String name = (String) e.nextElement ();
57  
58              super.add (name, aContext.getInitParameter (name));
59          }
60      }
61  
62      public void setString(String aKey, String aValue)
63      {
64          if (aKey == null)
65              throw (new IllegalArgumentException("Parameters: Argument \"aKey\" cannot be null."));
66          if (aValue == null)
67              throw (new IllegalArgumentException("Parameters: Argument \"aValue\" cannot be null."));
68  
69          super.add(aKey, aValue);
70      }
71  
72      /***
73       ** Removes all values with the given name.
74       **
75       ** @param   aName
76       **          the name of a pair
77       **/
78  
79      public void remove (String aName)
80      {
81          super.removeEntry (aName);
82      }
83  
84      /***
85       ** Removes all values with names that start with the given prefix.
86       **
87       ** @param   aPrefix
88       **          the name prefix
89       **/
90  
91      public void removeWithPrefix (String aPrefix)
92      {
93          List deletables = new ArrayList ();
94  
95          for (Iterator iter = names (); iter.hasNext (); )
96          {
97              String name = (String) iter.next ();
98  
99              if (name.startsWith (aPrefix))
100             {
101                 deletables.add (name);
102             }
103         }
104 
105         for (Iterator iter = deletables.iterator (); iter.hasNext (); )
106         {
107             super.removeEntry ((String) iter.next ());
108         }
109     }
110 }