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      private final static String RAW_ENCODING     = "8859_1";
35      private final static String DEFAULT_ENCODING = "UTF-8";
36  
37      public Parameters (Map params)
38      {
39          Iterator iterator = params.keySet().iterator();
40  
41          while (iterator.hasNext())
42          {
43              String name = (String)iterator.next();
44  
45              super.add (name, (String)params.get(name));
46          }
47      }
48  
49      public Parameters (ServletConfig aConfig)
50      {
51          for (Enumeration e = aConfig.getInitParameterNames (); e.hasMoreElements ();)
52          {
53              String name = (String) e.nextElement ();
54  
55              super.add (name, aConfig.getInitParameter (name));
56          }
57      }
58  
59      public Parameters (ServletContext aContext)
60      {
61          for (Enumeration e = aContext.getInitParameterNames (); e.hasMoreElements ();)
62          {
63              String name = (String) e.nextElement ();
64  
65              super.add (name, aContext.getInitParameter (name));
66          }
67      }
68  
69      public void setString(String aKey, String aValue)
70      {
71          if (aKey == null)
72              throw (new IllegalArgumentException("Parameters: Argument \"aKey\" cannot be null."));
73          if (aValue == null)
74              throw (new IllegalArgumentException("Parameters: Argument \"aValue\" cannot be null."));
75  
76          super.add(aKey, aValue);
77      }
78  
79      /***
80       ** Removes all values with the given name.
81       **
82       ** @param   aName
83       **          the name of a pair
84       **/
85  
86      public void remove (String aName)
87      {
88          super.removeEntry (aName);
89      }
90  
91      /***
92       ** Removes all values with names that start with the given prefix.
93       **
94       ** @param   aPrefix
95       **          the name prefix
96       **/
97  
98      public void removeWithPrefix (String aPrefix)
99      {
100         List deletables = new ArrayList ();
101 
102         for (Iterator iter = names (); iter.hasNext (); )
103         {
104             String name = (String) iter.next ();
105 
106             if (name.startsWith (aPrefix))
107             {
108                 deletables.add (name);
109             }
110         }
111 
112         for (Iterator iter = deletables.iterator (); iter.hasNext (); )
113         {
114             super.removeEntry ((String) iter.next ());
115         }
116     }
117 }