001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.util;
020:
021: import java.util.Map;
022: import java.util.List;
023: import java.lang.reflect.Array;
024: import org.apache.beehive.netui.util.logging.Logger;
025:
026: /**
027: * This class is used by NetUI tags that use parameters.
028: */
029: public class ParamHelper {
030: private static final Logger logger = Logger
031: .getInstance(ParamHelper.class);
032:
033: /**
034: * Add a new parameter or update an existing parameter's list of values.
035: * <p/>
036: * <em>Implementation Note:</em> in the case that a Map was provided for
037: * the <code>value</code> parameter, the this returns without doing
038: * anything; in any other case, params is updated (even in
039: * <code>value</code> is null).
040: * </p>
041: * <p/>
042: * If value is some object (not an array or list), the string
043: * representation of that object is added as a value for name. If the
044: * value is a list (or array) of objects, then the string representation
045: * of each element is added as a value for name. When there are multiple
046: * values for a name, then an array of Strings is used in Map.
047: * </p>
048: *
049: * @param params an existing Map of names and values to update
050: * @param name the name of the parameter to add or update
051: * @param value an item or list of items to put into the map
052: * @throws IllegalArgumentException in the case that either the params
053: * <p/>
054: * or name given was null
055: */
056: public static void addParam(Map params, String name, Object value) {
057:
058: if (params == null)
059: throw new IllegalArgumentException(
060: "Parameter map cannot be null");
061: if (name == null)
062: throw new IllegalArgumentException(
063: "Parameter name cannot be null");
064:
065: if (value instanceof Map) {
066: logger
067: .warn(Bundle.getString("Tags_BadParameterType",
068: name));
069: return;
070: }
071:
072: if (value == null)
073: value = "";
074:
075: // check to see if we are adding a new element
076: // or if this is an existing element
077: Object o = params.get(name);
078: int length = 0;
079:
080: if (o != null) {
081: assert (o instanceof String || o instanceof String[]);
082:
083: if (o.getClass().isArray()) {
084: length = Array.getLength(o);
085: } else {
086: length++;
087: }
088: }
089:
090: // check how much size the output needs to be
091: if (value.getClass().isArray()) {
092: length += Array.getLength(value);
093: } else if (value instanceof List) {
094: length += ((List) value).size();
095: } else {
096: length++;
097: }
098:
099: if (length == 0)
100: return;
101:
102: //System.err.println("Number of vaues:" + length);
103: // if there is only a single value push it to the parameter table
104: if (length == 1) {
105: if (value.getClass().isArray()) {
106: Object val = Array.get(value, 0);
107: if (val != null)
108: params.put(name, val.toString());
109: else
110: params.put(name, "");
111: } else if (value instanceof List) {
112: List list = (List) value;
113: Object val = list.get(0);
114: if (val != null)
115: params.put(name, val.toString());
116: else
117: params.put(name, "");
118: } else
119: params.put(name, value.toString());
120: return;
121: }
122:
123: // allocate the string for the multiple values
124: String[] values = new String[length];
125: int offset = 0;
126:
127: // if we had old values, push them to the new array
128: if (o != null) {
129: if (o.getClass().isArray()) {
130: String[] obs = (String[]) o;
131: for (; offset < obs.length; offset++) {
132: values[offset] = obs[offset];
133: }
134: } else {
135: values[0] = o.toString();
136: offset = 1;
137: }
138: }
139:
140: // now move the new values to the array starting at the offset
141: // position
142: if (value.getClass().isArray()) {
143: //need to convert this array into a String[]
144: int size = Array.getLength(value);
145: for (int i = 0; i < size; i++) {
146: Object val = Array.get(value, i);
147: if (val != null)
148: values[i + offset] = val.toString();
149: else
150: values[i + offset] = "";
151: }
152: } else if (value instanceof List) {
153: List list = (List) value;
154: int size = list.size();
155: for (int i = 0; i < size; i++) {
156: if (list.get(i) != null)
157: values[i + offset] = list.get(i).toString();
158: else
159: values[i + offset] = "";
160: }
161: } else {
162: values[offset] = value.toString();
163: }
164: // store the new values array
165: params.put(name, values);
166: }
167: }
|