KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > ParameterWrapper


1 /*
2  * Copyright 1999-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 /* $Id: ParameterWrapper.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.task;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.lenya.util.NamespaceMap;
28 import org.apache.log4j.Category;
29
30 public abstract class ParameterWrapper {
31     
32     private static Category log = Category.getInstance(ParameterWrapper.class);
33     private NamespaceMap parameters;
34     
35     /**
36      * Returns the un-prefixed parameters.
37      * @return A map.
38      */

39     public Map JavaDoc getMap() {
40         return parameters.getMap();
41     }
42
43     /**
44      * Ctor.
45      * @param prefixedParameters The prefixed parameters to wrap.
46      */

47     public ParameterWrapper(Map JavaDoc prefixedParameters) {
48         parameters = new NamespaceMap(prefixedParameters, getPrefix());
49     }
50     
51     /**
52      * Returns the namespace prefix.
53      * @return A string.
54      */

55     public abstract String JavaDoc getPrefix();
56     
57     /**
58      * Adds a key-value pair. If the value is null, no pair is added.
59      * @param key The key.
60      * @param value The value.
61      */

62     public void put(String JavaDoc key, String JavaDoc value) {
63         if (value != null) {
64             log.debug("Setting parameter: [" + key + "] = [" + value + "]");
65             parameters.put(key, value);
66         }
67         else {
68             log.debug("Not setting parameter: [" + key + "] = [" + value + "]");
69         }
70     }
71     
72     /**
73      * Returns the value for a key.
74      * @param key The key.
75      * @return The value.
76      */

77     public String JavaDoc get(String JavaDoc key) {
78         return (String JavaDoc) parameters.get(key);
79     }
80     
81     /**
82      * Returns the required keys.
83      * @return A string array.
84      */

85     protected abstract String JavaDoc[] getRequiredKeys();
86     
87     /**
88      * Checks if this parameters object contains all necessary parameters.
89      * @return A boolean value.
90      */

91     public boolean isComplete() {
92         boolean complete = true;
93         Map JavaDoc parameterMap = getMap();
94         String JavaDoc[] requiredKeys = getRequiredKeys();
95         int i = 0;
96         while (complete && i < requiredKeys.length) {
97             log.debug("Checking parameter: [" + requiredKeys[i] + "]");
98             complete = complete && parameterMap.containsKey(requiredKeys[i]);
99             log.debug("OK: [" + complete + "]");
100             i++;
101         }
102         return complete;
103     }
104
105     /**
106      * Returns the missing parameters parameters.
107      * @return A string array.
108      */

109     public String JavaDoc[] getMissingKeys() {
110         String JavaDoc[] requiredKeys = getRequiredKeys();
111         Map JavaDoc parameterMap = getMap();
112         List JavaDoc keyList = new ArrayList JavaDoc();
113         for (int i = 0; i < requiredKeys.length; i++) {
114             if (!parameterMap.containsKey(requiredKeys[i])) {
115                 keyList.add(requiredKeys[i]);
116             }
117         }
118         return (String JavaDoc[]) keyList.toArray(new String JavaDoc[keyList.size()]);
119     }
120     
121     /**
122      * Parameterizes this wrapper with un-prefixed parameters.
123      * @param parameters A parameters object.
124      */

125     public void parameterize(Parameters parameters) {
126         String JavaDoc[] keys = parameters.getNames();
127         for (int i = 0; i < keys.length; i++) {
128             put(keys[i], parameters.getParameter(keys[i], null));
129         }
130     }
131     
132 }
133
Popular Tags