1 8 package org.apache.avalon.excalibur.source; 9 10 import org.apache.avalon.framework.parameters.Parameters; 11 import java.io.*; 12 import java.util.*; 13 14 22 public final class SourceParameters 23 implements Serializable { 24 25 26 private Map names = new HashMap(5); 27 28 31 private String parseName(String s) { 32 StringBuffer sb = new StringBuffer (); 33 for (int i = 0; i < s.length(); i++) { 34 char c = s.charAt(i); 35 switch (c) { 36 case '+': 37 sb.append(' '); 38 break; 39 case '%': 40 try { 41 sb.append((char) Integer.parseInt(s.substring(i+1, i+3), 42 16)); 43 i += 2; 44 } catch (NumberFormatException e) { 45 throw new IllegalArgumentException (); 46 } catch (StringIndexOutOfBoundsException e) { 47 String rest = s.substring(i); 48 sb.append(rest); 49 if (rest.length()==2) 50 i++; 51 } 52 53 break; 54 default: 55 sb.append(c); 56 break; 57 } 58 } 59 return sb.toString(); 60 } 61 62 65 public SourceParameters() { 66 } 67 68 71 public SourceParameters(String queryString) { 72 if (queryString != null) { 73 StringTokenizer st = new StringTokenizer(queryString, "&"); 74 while (st.hasMoreTokens()) { 75 String pair = (String )st.nextToken(); 76 int pos = pair.indexOf('='); 77 if (pos != -1) { 78 this.setParameter(this.parseName(pair.substring(0, pos)), 79 this.parseName(pair.substring(pos+1, pair.length()))); 80 } 81 } 82 } 83 } 84 85 91 private void setParameter(String name, String value) { 92 ArrayList list; 93 if (names.containsKey(name) == true) { 94 list = (ArrayList)names.get(name); 95 } else { 96 list = new ArrayList(3); 97 names.put(name, list); 98 } 99 list.add(value); 100 } 101 102 108 public String getParameter(String name) { 109 if (names.containsKey(name) == true) { 110 return (String )((ArrayList)names.get(name)).get(0); 111 } 112 return null; 113 } 114 115 122 public String getParameter(String name, String defaultValue) { 123 if (names.containsKey(name) == true) { 124 return (String )((ArrayList)names.get(name)).get(0); 125 } 126 return defaultValue; 127 } 128 129 136 public int getParameterAsInteger(String name, int defaultValue) { 137 if (names.containsKey(name) == true) { 138 return new Integer ((String )((ArrayList)names.get(name)).get(0)).intValue(); 139 } 140 return defaultValue; 141 } 142 143 150 public boolean getParameterAsBoolean(String name, boolean defaultValue) { 151 if (names.containsKey(name) == true) { 152 return new Boolean ((String )((ArrayList)names.get(name)).get(0)).booleanValue(); 153 } 154 return defaultValue; 155 } 156 157 162 public boolean containsParameter(String name) { 163 return names.containsKey(name); 164 } 165 166 172 public Iterator getParameterValues(String name) { 173 if (names.containsKey(name) == true) { 174 ArrayList list = (ArrayList)names.get(name); 175 return list.iterator(); 176 } 177 return null; 178 } 179 183 public Iterator getParameterNames() { 184 return names.keySet().iterator(); 185 } 186 187 193 public Parameters getFirstParameters() { 194 Parameters result = new Parameters(); 195 Iterator iter = this.getParameterNames(); 196 String parName; 197 while (iter.hasNext()) { 198 parName = (String )iter.next(); 199 result.setParameter(parName, this.getParameter(parName)); 200 } 201 return result; 202 } 203 204 211 public String getQueryString() { 212 StringBuffer result = new StringBuffer (); 213 Iterator iter = this.names.keySet().iterator(); 214 Iterator listIterator; 215 String key; 216 String value; 217 boolean first = true; 218 while (iter.hasNext() == true) { 219 key = (String )iter.next(); 220 listIterator = ((ArrayList)names.get(key)).iterator(); 221 while (listIterator.hasNext() == true) { 222 if (first == false) result.append('&'); 223 value = (String )listIterator.next(); 224 result.append(key).append('=').append(value); 225 first = false; 226 } 227 } 228 return (result.length() == 0 ? null : result.toString()); 229 } 230 231 238 public String getEncodedQueryString() { 239 StringBuffer result = new StringBuffer (); 240 Iterator iter = this.names.keySet().iterator(); 241 Iterator listIterator; 242 String key; 243 String value; 244 boolean first = true; 245 while (iter.hasNext() == true) { 246 key = (String )iter.next(); 247 listIterator = ((ArrayList)names.get(key)).iterator(); 248 while (listIterator.hasNext() == true) { 249 if (first == false) result.append('&'); 250 value = (String )listIterator.next(); 251 result.append(key).append('=').append(SourceUtil.encode(value)); 252 first = false; 253 } 254 } 255 return (result.length() == 0 ? null : result.toString()); 256 } 257 258 261 public void add(SourceParameters parameters) { 262 Iterator names = parameters.getParameterNames(); 263 Iterator values; 264 String name; 265 String value; 266 while (names.hasNext() == true) { 267 name = (String )names.next(); 268 values = parameters.getParameterValues(name); 269 while (values.hasNext() == true) { 270 value = (String )values.next(); 271 this.setParameter(name, value); 272 } 273 } 274 } 275 276 279 public String toString() { 280 StringBuffer buffer = new StringBuffer ("SourceParameters: {"); 281 Iterator names = this.getParameterNames(); 282 String name; 283 boolean firstName = true; 284 Iterator values; 285 String value; 286 boolean firstValue; 287 while (names.hasNext() == true) { 288 name = (String )names.next(); 289 if (firstName == false) { 290 buffer.append(", "); 291 } else { 292 firstName = false; 293 } 294 buffer.append(name).append(" = ("); 295 values = this.getParameterValues(name); 296 firstValue = true; 297 while (values.hasNext() == true) { 298 value = (String )values.next(); 299 if (firstValue == false) { 300 buffer.append(", "); 301 } else { 302 firstValue = false; 303 } 304 buffer.append(value); 305 } 306 buffer.append(')'); 307 } 308 buffer.append('}'); 309 return buffer.toString(); 310 } 311 312 315 public Object clone() { 316 SourceParameters newObject = new SourceParameters(); 317 Iterator names = this.getParameterNames(); 318 Iterator values; 319 String name, value; 320 while (names.hasNext() == true) { 321 name = (String )names.next(); 322 values = this.getParameterValues(name); 323 while (values.hasNext() == true) { 324 value = (String )values.next(); 325 newObject.setParameter(name, value); 326 } 327 } 328 return newObject; 329 } 330 331 334 public boolean hasParameters() { 335 return (this.names.size() > 0); 336 } 337 338 342 public void setSingleParameterValue(String name, String value) { 343 this.removeParameter(name); 344 this.setParameter(name, value); 345 } 346 347 350 public void removeParameter(String name) { 351 if (this.names.containsKey(name) == true) { 352 this.names.remove(name); 353 } 354 } 355 356 } 357 | Popular Tags |