1 18 package org.apache.beehive.netui.util; 19 20 import java.util.Map ; 21 import java.util.List ; 22 import java.lang.reflect.Array ; 23 import org.apache.beehive.netui.util.logging.Logger; 24 25 28 public class ParamHelper 29 { 30 private static final Logger logger = Logger.getInstance(ParamHelper.class); 31 32 55 public static void addParam(Map params, String name, Object value) 56 { 57 58 59 if (params == null) 60 throw new IllegalArgumentException ("Parameter map cannot be null"); 61 if (name == null) 62 throw new IllegalArgumentException ("Parameter name cannot be null"); 63 64 if (value instanceof Map ) { 65 logger.warn(Bundle.getString("Tags_BadParameterType", name)); 66 return; 67 } 68 69 if (value == null) 70 value = ""; 71 72 Object o = params.get(name); 75 int length = 0; 76 77 if (o != null) { 78 assert (o instanceof String || 79 o instanceof String []); 80 81 if (o.getClass().isArray()) { 82 length = Array.getLength(o); 83 } 84 else { 85 length++; 86 } 87 } 88 89 if (value.getClass().isArray()) { 91 length += Array.getLength(value); 92 } 93 else if (value instanceof List ) { 94 length += ((List ) value).size(); 95 } 96 else { 97 length++; 98 } 99 100 if (length == 0) 101 return; 102 103 if (length == 1) { 106 if (value.getClass().isArray()) { 107 Object 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 ) { 114 List list = (List ) value; 115 Object 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 String [] values = new String [length]; 128 int offset = 0; 129 130 if (o != null) { 132 if (o.getClass().isArray()) { 133 String [] obs = (String []) 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 if (value.getClass().isArray()) 147 { 148 int size = Array.getLength(value); 150 for (int i=0; i < size; i++) 151 { 152 Object 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 ) 160 { 161 List list = (List ) 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 params.put(name, values); 176 } 177 } 178 | Popular Tags |