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