1 16 package org.apache.cocoon.environment.wrapper; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 26 34 public final class RequestParameters 35 implements Serializable { 36 37 38 private Map names; 39 40 43 private String parseName(String s) { 44 StringBuffer sb = new StringBuffer (); 45 for (int i = 0; i < s.length(); i++) { 46 char c = s.charAt(i); 47 switch (c) { 48 case '+': 49 sb.append(' '); 50 break; 51 case '%': 52 try { 53 sb.append((char) Integer.parseInt(s.substring(i+1, i+3), 54 16)); 55 i += 2; 56 } catch (NumberFormatException e) { 57 throw new IllegalArgumentException (); 58 } catch (StringIndexOutOfBoundsException e) { 59 String rest = s.substring(i); 60 sb.append(rest); 61 if (rest.length()==2) 62 i++; 63 } 64 65 break; 66 default: 67 sb.append(c); 68 break; 69 } 70 } 71 return sb.toString(); 72 } 73 74 77 public RequestParameters(String queryString) { 78 this.names = new HashMap (5); 79 if (queryString != null) { 80 StringTokenizer st = new StringTokenizer (queryString, "&"); 81 while (st.hasMoreTokens()) { 82 String pair = st.nextToken(); 83 int pos = pair.indexOf('='); 84 if (pos != -1) { 85 this.setParameter(this.parseName(pair.substring(0, pos)), 86 this.parseName(pair.substring(pos+1, pair.length()))); 87 } 88 } 89 } 90 } 91 92 98 private void setParameter(String name, String value) { 99 ArrayList list; 100 if (names.containsKey(name)) { 101 list = (ArrayList )names.get(name); 102 } else { 103 list = new ArrayList (3); 104 names.put(name, list); 105 } 106 list.add(value); 107 } 108 109 115 public String getParameter(String name) { 116 if (names.containsKey(name)) { 117 return (String )((ArrayList )names.get(name)).get(0); 118 } 119 return null; 120 } 121 122 129 public String getParameter(String name, String defaultValue) { 130 if (names.containsKey(name)) { 131 return (String )((ArrayList )names.get(name)).get(0); 132 } 133 return defaultValue; 134 } 135 136 142 public String [] getParameterValues(String name) { 143 if (names.containsKey(name)) { 144 String values[] = null; 145 ArrayList list = (ArrayList )names.get(name); 146 Iterator iter = list.iterator(); 147 while (iter.hasNext()) { 148 if (values == null) { 149 values = new String [1]; 150 } else { 151 String [] copy = new String [values.length+1]; 152 System.arraycopy(values, 0, copy, 0, values.length); 153 values = copy; 154 } 155 values[values.length-1] = (String )iter.next(); 156 } 157 return values; 158 } 159 return null; 160 } 161 162 166 public Enumeration getParameterNames() { 167 return new EnumerationFromIterator(names.keySet().iterator()); 168 } 169 170 final static class EnumerationFromIterator implements Enumeration { 171 private Iterator iter; 172 EnumerationFromIterator(Iterator iter) { 173 this.iter = iter; 174 } 175 176 public boolean hasMoreElements() { 177 return iter.hasNext(); 178 } 179 public Object nextElement() { return iter.next(); } 180 } 181 182 } 183 | Popular Tags |