1 23 24 28 29 package com.sun.enterprise.web.util; 30 31 import java.lang.System ; 32 import java.lang.String ; 33 import java.lang.IndexOutOfBoundsException ; 34 import java.lang.ArrayIndexOutOfBoundsException ; 35 36 39 public class CookieTokenizer 40 { 41 47 private static final int MAX_COOKIE_TOKENS = 4 * 20 * 2; 48 49 53 private String tokens[] = new String [MAX_COOKIE_TOKENS]; 54 55 58 private int numTokens = 0; 59 60 66 private int parseNameValue(String cookies, int beginIndex) { 67 int length = cookies.length(); 68 int index = beginIndex; 69 70 while (index < length) { 71 switch (cookies.charAt(index)) { 72 case ';': 73 case ',': 74 tokens[numTokens] = cookies.substring(beginIndex, index).trim(); 76 if (tokens[numTokens].length() > 0) { 77 numTokens++; 78 tokens[numTokens] = null; 79 numTokens++; 80 } 81 return index + 1; 82 83 case '=': 84 tokens[numTokens] = cookies.substring(beginIndex, index).trim(); 86 numTokens++; 87 return parseValue(cookies, index + 1); 88 89 case '"': 90 do index++; while (cookies.charAt(index) != '"'); 92 break; 93 } 94 95 index++; 96 } 97 98 if (index > beginIndex) { 99 tokens[numTokens] = cookies.substring(beginIndex, index).trim(); 101 if (tokens[numTokens].length() > 0) { 102 numTokens++; 103 tokens[numTokens] = null; 104 numTokens++; 105 } 106 } 107 108 return index; 109 } 110 111 116 public int tokenize(String cookies) { 117 numTokens = 0; 118 119 if (cookies != null) { 120 try { 121 int length = cookies.length(); 123 int index = 0; 124 while (index < length) 125 index = parseNameValue(cookies, index); 126 } 127 catch (ArrayIndexOutOfBoundsException e) { 128 } 130 catch (IndexOutOfBoundsException e) { 131 } 133 } 134 135 return numTokens; 136 } 137 138 141 public int getNumTokens() { 142 return numTokens; 143 } 144 145 150 public String tokenAt(int index) { 151 return tokens[index]; 152 } 153 154 160 private int parseValue(String cookies, int beginIndex) { 161 int length = cookies.length(); 162 int index = beginIndex; 163 164 while (index < length) { 165 switch (cookies.charAt(index)) { 166 case ';': 167 case ',': 168 tokens[numTokens] = cookies.substring(beginIndex, index).trim(); 170 numTokens++; 171 return index + 1; 172 173 case '"': 174 do index++; while (cookies.charAt(index) != '"'); 176 break; 177 } 178 179 index++; 180 } 181 182 tokens[numTokens] = cookies.substring(beginIndex, index).trim(); 184 numTokens++; 185 186 return index; 187 } 188 } 189 | Popular Tags |