25 26 package com.sun.activation.registries; 27 28 /** 29 * A tokenizer for strings in the form of "foo/bar; prop1=val1; ... ". 30 * Useful for parsing MIME content types. 31 */ 32 public class MailcapTokenizer { 33 34 public static final int UNKNOWN_TOKEN = 0; 35 public static final int START_TOKEN = 1; 36 public static final int STRING_TOKEN = 2; 37 public static final int EOI_TOKEN = 5; 38 public static final int SLASH_TOKEN = '/'; 39 public static final int SEMICOLON_TOKEN = ';'; 40 public static final int EQUALS_TOKEN = '='; 41 42 /** 43 * Constructor 44 * 45 * @parameter inputString the string to tokenize 46 */ 47 public MailcapTokenizer(String inputString) { 48 data = inputString; 49 dataIndex = 0; 50 dataLength = inputString.length(); 51 52 currentToken = START_TOKEN; 53 currentTokenValue = ""; 54 55 isAutoquoting = false; 56 autoquoteChar = ';'; 57 } 58 59 /** 60 * Set whether auto-quoting is on or off. 61 * 62 * Auto-quoting means that all characters after the first 63 * non-whitespace, non-control character up to the auto-quote 64 * terminator character or EOI (minus any whitespace immediatley 65 * preceeding it) is considered a token. 66 * 67 * This is required for handling command strings in a mailcap entry. 68 */ 69 public void setIsAutoquoting(boolean value) { 70 isAutoquoting = value; 71 } 72 73 /** 74 * Retrieve current token. 75 * 76 * @returns The current token value 77 */ 78 public int getCurrentToken() { 79 return currentToken; 80 } 81 82 /* 83 * Get a String that describes the given token. 84 */ 85 public static String nameForToken(int token) { 86 String name = "really unknown"; 87 88 switch(token) { 89 case UNKNOWN_TOKEN: 90 name = "unknown"; 91 break; 92 case START_TOKEN: 93 name = "start"; 94 break; 95 case STRING_TOKEN: 96 name = "string"; 98 case EOI_TOKEN: 99 name = "EOI"; 100 break; 101 case SLASH_TOKEN: 102 name = "'/'"; 103 break; 104 case SEMICOLON_TOKEN: 105 name = "';'"; 106 break; 107 case EQUALS_TOKEN: 108 name = "'='"; 109 break; 110 } 111 112 return name; 113 } 114 115 /* 116 * Retrieve current token value. 117 * 118 * @returns A String containing the current token value 119 */ 120 public String getCurrentTokenValue() { 121 return currentTokenValue; 122 } 123 /* 124 * Process the next token. 125 * 126 * @returns the next token 127 */ 128 public int nextToken() { 129 if (dataIndex < dataLength) { 130 // skip white space 131 while ((dataIndex < dataLength) && 132 (isWhiteSpaceChar(data.charAt(dataIndex)))) { 133 ++dataIndex; 134 } 135 136 if (dataIndex < dataLength) { 137 // examine the current character and see what kind of token we have 138 char c = data.charAt(dataIndex); 139 if (isAutoquoting) { 140 if (c == ';' || c == '=') { 141 currentToken = c; 142 currentTokenValue = new Character(c).toString(); 143 ++dataIndex; 144 } else { 145 processAutoquoteToken(); 146 } | 25 26 package com.sun.activation.registries; 27 28 /** 29 * A tokenizer for strings in the form of "foo/bar; prop1=val1; ... ". 30 * Useful for parsing MIME content types. 31 */ 32 public class MailcapTokenizer { 33 34 public static final int UNKNOWN_TOKEN = 0; 35 public static final int START_TOKEN = 1; 36 public static final int STRING_TOKEN = 2; 37 public static final int EOI_TOKEN = 5; 38 public static final int SLASH_TOKEN = '/'; 39 public static final int SEMICOLON_TOKEN = ';'; 40 public static final int EQUALS_TOKEN = '='; 41 42 /** 43 * Constructor 44 * 45 * @param inputString the string to tokenize 46 */ 47 public MailcapTokenizer(String inputString) { 48 data = inputString; 49 dataIndex = 0; 50 dataLength = inputString.length(); 51 52 currentToken = START_TOKEN; 53 currentTokenValue = ""; 54 55 isAutoquoting = false; 56 autoquoteChar = ';'; 57 } 58 59 /** 60 * Set whether auto-quoting is on or off. 61 * 62 * Auto-quoting means that all characters after the first 63 * non-whitespace, non-control character up to the auto-quote 64 * terminator character or EOI (minus any whitespace immediatley 65 * preceeding it) is considered a token. 66 * 67 * This is required for handling command strings in a mailcap entry. 68 */ 69 public void setIsAutoquoting(boolean value) { 70 isAutoquoting = value; 71 } 72 73 /** 74 * Retrieve current token. 75 * 76 * @return The current token value 77 */ 78 public int getCurrentToken() { 79 return currentToken; 80 } 81 82 /* 83 * Get a String that describes the given token. 84 */ 85 public static String nameForToken(int token) { 86 String name = "really unknown"; 87 88 switch(token) { 89 case UNKNOWN_TOKEN: 90 name = "unknown"; 91 break; 92 case START_TOKEN: 93 name = "start"; 94 break; 95 case STRING_TOKEN: 96 name = "string"; 98 case EOI_TOKEN: 99 name = "EOI"; 100 break; 101 case SLASH_TOKEN: 102 name = "'/'"; 103 break; 104 case SEMICOLON_TOKEN: 105 name = "';'"; 106 break; 107 case EQUALS_TOKEN: 108 name = "'='"; 109 break; 110 } 111 112 return name; 113 } 114 115 /* 116 * Retrieve current token value. 117 * 118 * @return A String containing the current token value 119 */ 120 public String getCurrentTokenValue() { 121 return currentTokenValue; 122 } 123 /* 124 * Process the next token. 125 * 126 * @return the next token 127 */ 128 public int nextToken() { 129 if (dataIndex < dataLength) { 130 // skip white space 131 while ((dataIndex < dataLength) && 132 (isWhiteSpaceChar(data.charAt(dataIndex)))) { 133 ++dataIndex; 134 } 135 136 if (dataIndex < dataLength) { 137 // examine the current character and see what kind of token we have 138 char c = data.charAt(dataIndex); 139 if (isAutoquoting) { 140 if (c == ';' || c == '=') { 141 currentToken = c; 142 currentTokenValue = new Character(c).toString(); 143 ++dataIndex; 144 } else { 145 processAutoquoteToken(); 146 } |