1 11 12 package org.eclipse.osgi.framework.util; 13 14 17 public class Tokenizer { 18 protected char value[]; 19 protected int max; 20 protected int cursor; 21 22 public Tokenizer(String value) { 23 this.value = value.toCharArray(); 24 max = this.value.length; 25 cursor = 0; 26 } 27 28 private void skipWhiteSpace() { 29 char[] val = value; 30 int cur = cursor; 31 32 for (; cur < max; cur++) { 33 char c = val[cur]; 34 if ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r')) { 35 continue; 36 } 37 break; 38 } 39 cursor = cur; 40 } 41 42 public String getToken(String terminals) { 43 skipWhiteSpace(); 44 char[] val = value; 45 int cur = cursor; 46 47 int begin = cur; 48 for (; cur < max; cur++) { 49 char c = val[cur]; 50 if ((terminals.indexOf(c) != -1)) { 51 break; 52 } 53 } 54 cursor = cur; 55 int count = cur - begin; 56 if (count > 0) { 57 skipWhiteSpace(); 58 while (count > 0 && (val[begin + count - 1] == ' ' || val[begin + count - 1] == '\t')) 59 count--; 60 return (new String (val, begin, count)); 61 } 62 return (null); 63 } 64 65 public String getString(String terminals) { 66 skipWhiteSpace(); 67 char[] val = value; 68 int cur = cursor; 69 70 if (cur < max) { 71 if (val[cur] == '\"') 72 { 73 cur++; 74 char c = '\0'; 75 int begin = cur; 76 for (; cur < max; cur++) { 77 c = val[cur]; 78 if (c == '\"') { 79 break; 80 } 81 } 82 int count = cur - begin; 83 if (c == '\"') { 84 cur++; 85 } 86 cursor = cur; 87 if (count > 0) { 88 skipWhiteSpace(); 89 return (new String (val, begin, count)); 90 } 91 } else 92 { 93 int begin = cur; 94 for (; cur < max; cur++) { 95 char c = val[cur]; 96 if (c == '\"') { 97 cur = cur + skipQuotedString(val, cur); 99 } else if ((terminals.indexOf(c) != -1)) { 100 break; 101 } 102 } 103 cursor = cur; 104 int count = cur - begin; 105 if (count > 0) { 106 skipWhiteSpace(); 107 while (count > 0 && (val[begin + count - 1] == ' ' || val[begin + count - 1] == '\t')) 108 count--; 109 return (new String (val, begin, count)); 110 } 111 } 112 } 113 return (null); 114 } 115 116 private int skipQuotedString(char[] val, int cur) { 117 cur++; 118 char c = '\0'; 119 int begin = cur; 120 for (; cur < max; cur++) { 121 c = val[cur]; 122 if (c == '\"') { 123 break; 124 } 125 } 126 int count = cur - begin; 127 if (c == '\"') { 128 cur++; 129 } 130 cursor = cur; 131 if (count > 0) { 132 skipWhiteSpace(); 133 } 134 return count; 135 } 136 137 public char getChar() { 138 int cur = cursor; 139 if (cur < max) { 140 cursor = cur + 1; 141 return (value[cur]); 142 } 143 return ('\0'); 144 } 145 146 public boolean hasMoreTokens() { 147 if (cursor < max) { 148 return true; 149 } 150 return false; 151 } 152 } | Popular Tags |