1 16 package org.apache.cocoon.util; 17 18 import java.util.Enumeration ; 19 import java.util.NoSuchElementException ; 20 21 28 public class Tokenizer implements Enumeration { 29 30 43 public Tokenizer(String str, String delim, boolean returnTokens) { 44 this.str = str; 45 this.delim = delim; 46 this.returnTokens = returnTokens; 47 48 max = str.length(); 49 } 50 51 59 public Tokenizer(String str, String delim) { 60 this(str, delim, false); 61 } 62 63 71 public Tokenizer(String str, char delim) { 72 this(str, String.valueOf(delim), false); 73 } 74 75 84 public Tokenizer(String str) { 85 this(str, DEFAULT_DELIMITERS, false); 86 } 87 88 96 public boolean hasMoreTokens() { 97 return ((current < max) ? (true) : 98 (((current == max) && (max == 0 99 || (returnTokens && delim.indexOf(str.charAt(previous)) >= 0))))); 100 } 101 102 110 public String nextToken() throws NoSuchElementException { 111 if (current == max 112 && (max == 0 113 || (returnTokens && delim.indexOf(str.charAt(previous)) >= 0))) { 114 115 current++; 116 return ""; 117 } 118 119 if (current >= max) 120 throw new NoSuchElementException (); 121 122 int start = current; 123 String result = null; 124 125 if (delim.indexOf(str.charAt(start)) >= 0) { 126 if (previous == -1 || (returnTokens && previous != current 127 && delim.indexOf(str.charAt(previous)) >= 0)) { 128 129 result = ""; 130 } 131 else if (returnTokens) 132 result = str.substring(start, ++current); 133 134 if (!returnTokens) 135 current++; 136 } 137 138 previous = start; 139 start = current; 140 141 if (result == null) 142 while (current < max && delim.indexOf(str.charAt(current)) < 0) 143 current++; 144 145 return result == null ? str.substring(start, current) : result; 146 } 147 148 163 public String nextToken(String delim) throws NoSuchElementException { 164 this.delim = delim; 165 return nextToken(); 166 } 167 168 174 public boolean hasMoreElements() { 175 return hasMoreTokens(); 176 } 177 178 188 public Object nextElement() { 189 return nextToken(); 190 } 191 192 200 public int countTokens() { 201 int curr = current; 202 int count = 0; 203 204 for (int i = curr; i < max; i++) { 205 if (delim.indexOf(str.charAt(i)) >= 0) 206 count++; 207 208 curr++; 209 } 210 211 return count + (returnTokens ? count : 0) + 1; 212 } 213 214 217 public void reset() { 218 previous = -1; 219 current = 0; 220 } 221 222 238 public static String [] tokenize(String str, String delim, 239 boolean returnTokens) { 240 241 Tokenizer tokenizer = new Tokenizer(str, delim, returnTokens); 242 String [] tokens = new String [tokenizer.countTokens()]; 243 244 int i = 0; 245 while (tokenizer.hasMoreTokens()) { 246 tokens[i] = tokenizer.nextToken(); 247 i++; 248 } 249 250 return tokens; 251 } 252 253 258 public static final String DEFAULT_DELIMITERS = " \t\n\r\f"; 259 260 263 private String str = null; 264 265 268 private String delim = null; 269 270 273 private boolean returnTokens = false; 274 275 278 private int previous = -1; 279 280 283 private int current = 0; 284 285 288 private int max = 0; 289 } 290 | Popular Tags |