1 19 20 package com.maverick.http; 21 22 import java.io.IOException ; 23 import java.text.MessageFormat ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 27 31 class ParameterParser { 32 33 34 private char[] chars = null; 35 36 37 private int pos = 0; 38 39 40 private int len = 0; 41 42 43 private int i1 = 0; 44 45 46 private int i2 = 0; 47 48 49 public ParameterParser() { 50 super(); 51 } 52 53 54 private boolean hasChar() { 55 return this.pos < this.len; 56 } 57 58 59 private String getToken(boolean quoted) { 60 while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) { 62 i1++; 63 } 64 while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) { 66 i2--; 67 } 68 if (quoted) { 70 if (((i2 - i1) >= 2) && (chars[i1] == '"') && (chars[i2 - 1] == '"')) { 71 i1++; 72 i2--; 73 } 74 } 75 String result = null; 76 if (i2 > i1) { 77 result = new String (chars, i1, i2 - i1); 78 } 79 return result; 80 } 81 82 83 private boolean isOneOf(char ch, char[] charray) { 84 boolean result = false; 85 for (int i = 0; i < charray.length; i++) { 86 if (ch == charray[i]) { 87 result = true; 88 break; 89 } 90 } 91 return result; 92 } 93 94 97 private String parseToken(final char[] terminators) { 98 char ch; 99 i1 = pos; 100 i2 = pos; 101 while (hasChar()) { 102 ch = chars[pos]; 103 if (isOneOf(ch, terminators)) { 104 break; 105 } 106 i2++; 107 pos++; 108 } 109 return getToken(false); 110 } 111 112 116 private String parseQuotedToken(final char[] terminators) { 117 char ch; 118 i1 = pos; 119 i2 = pos; 120 boolean quoted = false; 121 while (hasChar()) { 122 ch = chars[pos]; 123 if (!quoted && isOneOf(ch, terminators)) { 124 break; 125 } 126 if (ch == '"') { 127 quoted = !quoted; 128 } 129 i2++; 130 pos++; 131 } 132 return getToken(true); 133 } 134 135 142 public Vector parse(final String str, char separator) { 143 144 if (str == null) { 145 return new Vector (); 146 } 147 return parse(str.toCharArray(), separator); 148 } 149 150 159 public Vector parse(final char[] chars, char separator) { 160 161 if (chars == null) { 162 return new Vector (); 163 } 164 return parse(chars, 0, chars.length, separator); 165 } 166 167 178 public Vector parse(final char[] chars, int offset, int length, char separator) { 179 180 if (chars == null) { 181 return new Vector (); 182 } 183 Vector params = new Vector (); 184 this.chars = chars; 185 this.pos = offset; 186 this.len = length; 187 188 String paramName = null; 189 String paramValue = null; 190 while (hasChar()) { 191 paramName = parseToken(new char[] { '=', separator }); 192 paramValue = null; 193 if (hasChar() && (chars[pos] == '=')) { 194 pos++; paramValue = parseQuotedToken(new char[] { separator }); 196 } 197 if (hasChar() && (chars[pos] == separator)) { 198 pos++; } 200 if ((paramName != null) && (paramName.length() > 0)) { 201 params.addElement(new NameValuePair(paramName, paramValue)); 202 } 203 } 204 return params; 205 } 206 207 public static Hashtable extractParams(String challengeStr) throws IOException { 208 if (challengeStr == null) { 209 throw new IllegalArgumentException (Messages.getString("ParameterParser.challendMayNotBeNull")); } 211 int idx = challengeStr.indexOf(' '); 212 if (idx == -1) { 213 throw new IOException (MessageFormat.format(Messages.getString("ParameterParser.invalidChallenge"), new Object [] { challengeStr })); } 215 Hashtable map = new Hashtable (); 216 ParameterParser parser = new ParameterParser(); 217 Vector params = parser.parse(challengeStr.substring(idx + 1, challengeStr.length()), ','); 218 for (int i = 0; i < params.size(); i++) { 219 NameValuePair param = (NameValuePair) params.elementAt(i); 220 map.put(param.getName().toLowerCase(), param.getValue()); 221 } 222 return map; 223 } 224 } 225 | Popular Tags |