1 16 package net.myvietnam.mvncore.web.fileupload; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 35 36 public class ParameterParser { 37 40 private char[] chars = null; 41 42 45 private int pos = 0; 46 47 50 private int len = 0; 51 52 55 private int i1 = 0; 56 57 60 private int i2 = 0; 61 62 65 private boolean lowerCaseNames = false; 66 67 70 public ParameterParser() { 71 super(); 72 } 73 74 80 private boolean hasChar() { 81 return this.pos < this.len; 82 } 83 84 93 private String getToken(boolean quoted) { 94 while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) { 96 i1++; 97 } 98 while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) { 100 i2--; 101 } 102 if (quoted) { 104 if (((i2 - i1) >= 2) 105 && (chars[i1] == '"') 106 && (chars[i2 - 1] == '"')) { 107 i1++; 108 i2--; 109 } 110 } 111 String result = null; 112 if (i2 > i1) { 113 result = new String (chars, i1, i2 - i1); 114 } 115 return result; 116 } 117 118 127 private boolean isOneOf(char ch, final char[] charray) { 128 boolean result = false; 129 for (int i = 0; i < charray.length; i++) { 130 if (ch == charray[i]) { 131 result = true; 132 break; 133 } 134 } 135 return result; 136 } 137 138 147 private String parseToken(final char[] terminators) { 148 char ch; 149 i1 = pos; 150 i2 = pos; 151 while (hasChar()) { 152 ch = chars[pos]; 153 if (isOneOf(ch, terminators)) { 154 break; 155 } 156 i2++; 157 pos++; 158 } 159 return getToken(false); 160 } 161 162 172 private String parseQuotedToken(final char[] terminators) { 173 char ch; 174 i1 = pos; 175 i2 = pos; 176 boolean quoted = false; 177 boolean charEscaped = false; 178 while (hasChar()) { 179 ch = chars[pos]; 180 if (!quoted && isOneOf(ch, terminators)) { 181 break; 182 } 183 if (!charEscaped && ch == '"') { 184 quoted = !quoted; 185 } 186 charEscaped = (!charEscaped && ch == '\\'); 187 i2++; 188 pos++; 189 190 } 191 return getToken(true); 192 } 193 194 202 public boolean isLowerCaseNames() { 203 return this.lowerCaseNames; 204 } 205 206 214 public void setLowerCaseNames(boolean b) { 215 this.lowerCaseNames = b; 216 } 217 218 227 public Map parse(final String str, char separator) { 228 if (str == null) { 229 return new HashMap (); 230 } 231 return parse(str.toCharArray(), separator); 232 } 233 234 244 public Map parse(final char[] chars, char separator) { 245 if (chars == null) { 246 return new HashMap (); 247 } 248 return parse(chars, 0, chars.length, separator); 249 } 250 251 263 public Map parse( 264 final char[] chars, 265 int offset, 266 int length, 267 char separator) { 268 269 if (chars == null) { 270 return new HashMap (); 271 } 272 HashMap params = new HashMap (); 273 this.chars = chars; 274 this.pos = offset; 275 this.len = length; 276 277 String paramName = null; 278 String paramValue = null; 279 while (hasChar()) { 280 paramName = parseToken(new char[] { 281 '=', separator }); 282 paramValue = null; 283 if (hasChar() && (chars[pos] == '=')) { 284 pos++; paramValue = parseQuotedToken(new char[] { 286 separator }); 287 } 288 if (hasChar() && (chars[pos] == separator)) { 289 pos++; } 291 if ((paramName != null) && (paramName.length() > 0)) { 292 if (this.lowerCaseNames) { 293 paramName = paramName.toLowerCase(); 294 } 295 params.put(paramName, paramValue); 296 } 297 } 298 return params; 299 } 300 } 301 | Popular Tags |