1 21 22 27 28 package com.sun.activation.registries; 29 30 34 public class MailcapTokenizer { 35 36 public static final int UNKNOWN_TOKEN = 0; 37 public static final int START_TOKEN = 1; 38 public static final int STRING_TOKEN = 2; 39 public static final int EOI_TOKEN = 5; 40 public static final int SLASH_TOKEN = '/'; 41 public static final int SEMICOLON_TOKEN = ';'; 42 public static final int EQUALS_TOKEN = '='; 43 44 49 public MailcapTokenizer(String inputString) { 50 data = inputString; 51 dataIndex = 0; 52 dataLength = inputString.length(); 53 54 currentToken = START_TOKEN; 55 currentTokenValue = ""; 56 57 isAutoquoting = false; 58 autoquoteChar = ';'; 59 } 60 61 71 public void setIsAutoquoting(boolean value) { 72 isAutoquoting = value; 73 } 74 75 78 public void setAutoquoteChar(char value) { 79 autoquoteChar = value; 80 } 81 82 87 public int getCurrentToken() { 88 return currentToken; 89 } 90 91 94 public static String nameForToken(int token) { 95 String name = "really unknown"; 96 97 switch(token) { 98 case UNKNOWN_TOKEN: 99 name = "unknown"; 100 break; 101 case START_TOKEN: 102 name = "start"; 103 break; 104 case STRING_TOKEN: 105 name = "string"; 106 break; 107 case EOI_TOKEN: 108 name = "EOI"; 109 break; 110 case SLASH_TOKEN: 111 name = "'/'"; 112 break; 113 case SEMICOLON_TOKEN: 114 name = "';'"; 115 break; 116 case EQUALS_TOKEN: 117 name = "'='"; 118 break; 119 } 120 121 return name; 122 } 123 124 129 public String getCurrentTokenValue() { 130 return currentTokenValue; 131 } 132 133 138 public int nextToken() { 139 if (dataIndex < dataLength) { 140 while ((dataIndex < dataLength) && 142 (isWhiteSpaceChar(data.charAt(dataIndex)))) { 143 ++dataIndex; 144 } 145 146 if (dataIndex < dataLength) { 147 char c = data.charAt(dataIndex); 149 if (isAutoquoting) { 150 if (!isAutoquoteSpecialChar(c)) { 151 processAutoquoteToken(); 152 } else if ((c == ';') || (c == '=')) { 153 currentToken = c; 154 currentTokenValue = new Character (c).toString(); 155 ++dataIndex; 156 } else { 157 currentToken = UNKNOWN_TOKEN; 158 currentTokenValue = new Character (c).toString(); 159 ++dataIndex; 160 } 161 } else { 162 if (isStringTokenChar(c)) { 163 processStringToken(); 164 } else if ((c == '/') || (c == ';') || (c == '=')) { 165 currentToken = c; 166 currentTokenValue = new Character (c).toString(); 167 ++dataIndex; 168 } else { 169 currentToken = UNKNOWN_TOKEN; 170 currentTokenValue = new Character (c).toString(); 171 ++dataIndex; 172 } 173 } 174 } else { 175 currentToken = EOI_TOKEN; 176 currentTokenValue = null; 177 } 178 } else { 179 currentToken = EOI_TOKEN; 180 currentTokenValue = null; 181 } 182 183 return currentToken; 184 } 185 186 private void processStringToken() { 187 int initialIndex = dataIndex; 189 190 while ((dataIndex < dataLength) && 192 isStringTokenChar(data.charAt(dataIndex))) { 193 ++dataIndex; 194 } 195 196 currentToken = STRING_TOKEN; 197 currentTokenValue = data.substring(initialIndex, dataIndex); 198 } 199 200 private void processAutoquoteToken() { 201 int initialIndex = dataIndex; 203 204 boolean foundTerminator = false; 206 while ((dataIndex < dataLength) && !foundTerminator) { 207 char c = data.charAt(dataIndex); 208 if (c != autoquoteChar) { 209 ++dataIndex; 210 } else { 211 foundTerminator = true; 212 } 213 } 214 215 currentToken = STRING_TOKEN; 216 currentTokenValue = 217 fixEscapeSequences(data.substring(initialIndex, dataIndex)); 218 } 219 220 public static boolean isSpecialChar(char c) { 221 boolean lAnswer = false; 222 223 switch(c) { 224 case '(': 225 case ')': 226 case '<': 227 case '>': 228 case '@': 229 case ',': 230 case ';': 231 case ':': 232 case '\\': 233 case '"': 234 case '/': 235 case '[': 236 case ']': 237 case '?': 238 case '=': 239 lAnswer = true; 240 break; 241 } 242 243 return lAnswer; 244 } 245 246 public static boolean isAutoquoteSpecialChar(char c) { 247 boolean lAnswer = false; 248 249 switch(c) { 250 case ';': 251 case '=': 252 lAnswer = true; 253 break; 254 } 255 256 return lAnswer; 257 } 258 259 public static boolean isControlChar(char c) { 260 return Character.isISOControl(c); 261 } 262 263 public static boolean isWhiteSpaceChar(char c) { 264 return Character.isWhitespace(c); 265 } 266 267 public static boolean isStringTokenChar(char c) { 268 return !isSpecialChar(c) && !isControlChar(c) && !isWhiteSpaceChar(c); 269 } 270 271 private static String fixEscapeSequences(String inputString) { 272 int inputLength = inputString.length(); 273 StringBuffer buffer = new StringBuffer (); 274 buffer.ensureCapacity(inputLength); 275 276 for (int i = 0; i < inputLength; ++i) { 277 char currentChar = inputString.charAt(i); 278 if (currentChar != '\\') { 279 buffer.append(currentChar); 280 } else { 281 if (i < inputLength - 1) { 282 char nextChar = inputString.charAt(i + 1); 283 buffer.append(nextChar); 284 285 ++i; 287 } else { 288 buffer.append(currentChar); 289 } 290 } 291 } 292 293 return buffer.toString(); 294 } 295 296 private String data; 297 private int dataIndex; 298 private int dataLength; 299 private int currentToken; 300 private String currentTokenValue; 301 private boolean isAutoquoting; 302 private char autoquoteChar; 303 304 347 } 348 | Popular Tags |