KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.ServletConfig JavaDoc;
29 import javax.servlet.ServletContext JavaDoc;
30
31 public class Parameters extends NameValuePairs
32 {
33
34     public Parameters (Map JavaDoc params)
35     {
36         Iterator JavaDoc iterator = params.keySet().iterator();
37
38         while (iterator.hasNext())
39         {
40             String JavaDoc name = (String JavaDoc)iterator.next();
41
42             super.add (name, (String JavaDoc)params.get(name));
43         }
44     }
45
46     public Parameters (ServletConfig JavaDoc aConfig)
47     {
48         for (Enumeration JavaDoc e = aConfig.getInitParameterNames (); e.hasMoreElements ();)
49         {
50             String JavaDoc name = (String JavaDoc) e.nextElement ();
51
52             super.add (name, aConfig.getInitParameter (name));
53         }
54     }
55
56     public Parameters (ServletContext JavaDoc aContext)
57     {
58         for (Enumeration JavaDoc e = aContext.getInitParameterNames (); e.hasMoreElements ();)
59         {
60             String JavaDoc name = (String JavaDoc) e.nextElement ();
61
62             super.add (name, aContext.getInitParameter (name));
63         }
64     }
65
66     public void setString(String JavaDoc aKey, String JavaDoc aValue)
67     {
68         if (aKey == null)
69             throw (new IllegalArgumentException JavaDoc("Parameters: Argument \"aKey\" cannot be null."));
70         if (aValue == null)
71             throw (new IllegalArgumentException JavaDoc("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 JavaDoc 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 JavaDoc aPrefix)
96     {
97         List JavaDoc deletables = new ArrayList JavaDoc ();
98
99         for (Iterator JavaDoc iter = names (); iter.hasNext (); )
100         {
101             String JavaDoc name = (String JavaDoc) iter.next ();
102
103             if (name.startsWith (aPrefix))
104             {
105                 deletables.add (name);
106             }
107         }
108
109         for (Iterator JavaDoc iter = deletables.iterator (); iter.hasNext (); )
110         {
111             super.removeEntry ((String JavaDoc) iter.next ());
112         }
113     }
114 }
115
Popular Tags