1 16 package net.jforum.util.legacy.commons.fileupload; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 33 34 public class ParameterParser { 35 38 private char[] chars = null; 39 40 43 private int pos = 0; 44 45 48 private int len = 0; 49 50 53 private int i1 = 0; 54 55 58 private int i2 = 0; 59 60 63 private boolean lowerCaseNames = false; 64 65 68 public ParameterParser() { 69 super(); 70 } 71 72 78 private boolean hasChar() { 79 return this.pos < this.len; 80 } 81 82 91 private String getToken(boolean quoted) { 92 while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) { 94 i1++; 95 } 96 while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) { 98 i2--; 99 } 100 if (quoted) { 102 if (((i2 - i1) >= 2) 103 && (chars[i1] == '"') 104 && (chars[i2 - 1] == '"')) { 105 i1++; 106 i2--; 107 } 108 } 109 String result = null; 110 if (i2 > i1) { 111 result = new String (chars, i1, i2 - i1); 112 } 113 return result; 114 } 115 116 125 private boolean isOneOf(char ch, final char[] charray) { 126 boolean result = false; 127 for (int i = 0; i < charray.length; i++) { 128 if (ch == charray[i]) { 129 result = true; 130 break; 131 } 132 } 133 return result; 134 } 135 136 145 private String parseToken(final char[] terminators) { 146 char ch; 147 i1 = pos; 148 i2 = pos; 149 while (hasChar()) { 150 ch = chars[pos]; 151 if (isOneOf(ch, terminators)) { 152 break; 153 } 154 i2++; 155 pos++; 156 } 157 return getToken(false); 158 } 159 160 170 private String parseQuotedToken(final char[] terminators) { 171 char ch; 172 i1 = pos; 173 i2 = pos; 174 boolean quoted = false; 175 boolean charEscaped = false; 176 while (hasChar()) { 177 ch = chars[pos]; 178 if (!quoted && isOneOf(ch, terminators)) { 179 break; 180 } 181 if (!charEscaped && ch == '"') { 182 quoted = !quoted; 183 } 184 charEscaped = (!charEscaped && ch == '\\'); 185 i2++; 186 pos++; 187 188 } 189 return getToken(true); 190 } 191 192 200 public boolean isLowerCaseNames() { 201 return this.lowerCaseNames; 202 } 203 204 212 public void setLowerCaseNames(boolean b) { 213 this.lowerCaseNames = b; 214 } 215 216 225 public Map parse(final String str, char separator) { 226 if (str == null) { 227 return new HashMap (); 228 } 229 return parse(str.toCharArray(), separator); 230 } 231 232 242 public Map parse(final char[] chars, char separator) { 243 if (chars == null) { 244 return new HashMap (); 245 } 246 return parse(chars, 0, chars.length, separator); 247 } 248 249 261 public Map parse( 262 final char[] chars, 263 int offset, 264 int length, 265 char separator) { 266 267 if (chars == null) { 268 return new HashMap (); 269 } 270 HashMap params = new HashMap (); 271 this.chars = chars; 272 this.pos = offset; 273 this.len = length; 274 275 String paramName = null; 276 String paramValue = null; 277 while (hasChar()) { 278 paramName = parseToken(new char[] { 279 '=', separator }); 280 paramValue = null; 281 if (hasChar() && (chars[pos] == '=')) { 282 pos++; paramValue = parseQuotedToken(new char[] { 284 separator }); 285 } 286 if (hasChar() && (chars[pos] == separator)) { 287 pos++; } 289 if ((paramName != null) && (paramName.length() > 0)) { 290 if (this.lowerCaseNames) { 291 paramName = paramName.toLowerCase(); 292 } 293 params.put(paramName, paramValue); 294 } 295 } 296 return params; 297 } 298 } 299 | Popular Tags |