KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > ParamHelper


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.util;
19
20 import java.util.Map JavaDoc;
21 import java.util.List JavaDoc;
22 import java.lang.reflect.Array JavaDoc;
23 import org.apache.beehive.netui.util.logging.Logger;
24
25 /**
26  * This class is used by NetUI tags that use parameters.
27  */

28 public class ParamHelper
29 {
30     private static final Logger logger = Logger.getInstance(ParamHelper.class);
31
32     /**
33      * Add a new parameter or update an existing parameter's list of values.
34      * <p/>
35      * <em>Implementation Note:</em> in the case that a Map was provided for
36      * the <code>value</code> parameter, the this returns without doing
37      * anything; in any other case, params is updated (even in
38      * <code>value</code> is null).
39      * </p>
40      * <p/>
41      * If value is some object (not an array or list), the string
42      * representation of that object is added as a value for name. If the
43      * value is a list (or array) of objects, then the string representation
44      * of each element is added as a value for name. When there are multiple
45      * values for a name, then an array of Strings is used in Map.
46      * </p>
47      *
48      * @param params an existing Map of names and values to update
49      * @param name the name of the parameter to add or update
50      * @param value an item or list of items to put into the map
51      * @throws IllegalArgumentException in the case that either the params
52      * <p/>
53      * or name given was null
54      */

55     public static void addParam(Map JavaDoc params, String JavaDoc name, Object JavaDoc value)
56     {
57
58
59         if (params == null)
60             throw new IllegalArgumentException JavaDoc("Parameter map cannot be null");
61         if (name == null)
62             throw new IllegalArgumentException JavaDoc("Parameter name cannot be null");
63
64         if (value instanceof Map JavaDoc) {
65             logger.warn(Bundle.getString("Tags_BadParameterType", name));
66             return;
67         }
68
69         if (value == null)
70             value = "";
71
72         // check to see if we are adding a new element
73
// or if this is an existing element
74
Object JavaDoc o = params.get(name);
75         int length = 0;
76
77         if (o != null) {
78             assert (o instanceof String JavaDoc ||
79                     o instanceof String JavaDoc[]);
80
81             if (o.getClass().isArray()) {
82                 length = Array.getLength(o);
83             }
84             else {
85                 length++;
86             }
87         }
88
89         // check how much size the output needs to be
90
if (value.getClass().isArray()) {
91             length += Array.getLength(value);
92         }
93         else if (value instanceof List JavaDoc) {
94             length += ((List JavaDoc) value).size();
95         }
96         else {
97             length++;
98         }
99
100         if (length == 0)
101             return;
102
103         //System.err.println("Number of vaues:" + length);
104
// if there is only a single value push it to the parameter table
105
if (length == 1) {
106             if (value.getClass().isArray()) {
107                 Object JavaDoc val = Array.get(value, 0);
108                 if (val != null)
109                     params.put(name,val.toString());
110                 else
111                     params.put(name,"");
112             }
113             else if (value instanceof List JavaDoc) {
114                 List JavaDoc list = (List JavaDoc) value;
115                 Object JavaDoc val = list.get(0);
116                 if (val != null)
117                     params.put(name,val.toString());
118                 else
119                     params.put(name,"");
120             }
121             else
122                 params.put(name,value.toString());
123             return;
124         }
125
126         // allocate the string for the multiple values
127
String JavaDoc[] values = new String JavaDoc[length];
128         int offset = 0;
129
130         // if we had old values, push them to the new array
131
if (o != null) {
132             if (o.getClass().isArray()) {
133                 String JavaDoc[] obs = (String JavaDoc[]) o;
134                 for (;offset<obs.length;offset++) {
135                     values[offset] = obs[offset];
136                 }
137             }
138             else {
139                 values[0] = o.toString();
140                 offset = 1;
141             }
142         }
143
144         // now move the new values to the array starting at the offset
145
// position
146
if (value.getClass().isArray())
147         {
148             //need to convert this array into a String[]
149
int size = Array.getLength(value);
150             for (int i=0; i < size; i++)
151             {
152                 Object JavaDoc val = Array.get(value, i);
153                 if (val != null)
154                     values[i+offset] = val.toString();
155                 else
156                    values[i+offset] = "";
157             }
158         }
159         else if (value instanceof List JavaDoc)
160         {
161             List JavaDoc list = (List JavaDoc) value;
162             int size = list.size();
163             for (int i=0; i < size; i++)
164             {
165                 if (list.get(i) != null)
166                     values[i+offset] = list.get(i).toString();
167                 else
168                     values[i+offset] = "";
169             }
170         }
171         else {
172             values[offset] = value.toString();
173         }
174         // store the new values array
175
params.put(name, values);
176     }
177 }
178
Popular Tags