1 55 package org.lateralnz.c3d.util; 56 57 62 public class Tokenizer { 63 private static final String EMPTY = ""; 64 65 private String delimiters; 66 private boolean rtnDelims; 67 private String returnDelimiters; 68 69 private char chars[]; 70 71 int currentPos; 72 73 private int maxPos; 74 75 int[] mark = new int[100]; 76 int markpos = 0; 77 78 public Tokenizer(String text, String delimiters, String returnDelimiters) { 79 setString(text); 80 this.delimiters = delimiters; 81 this.returnDelimiters = returnDelimiters; 82 this.rtnDelims = (returnDelimiters != null && !returnDelimiters.equals(EMPTY)); 83 } 84 85 89 public final void setString(String text) { 90 this.maxPos = text.length(); 91 this.currentPos = 0; 92 93 this.chars = new char[maxPos]; 94 text.getChars(0,maxPos,this.chars,0); 95 } 96 97 100 public final void destroy() { 101 chars = null; 102 } 103 104 107 public final void mark() { 108 markpos++; 109 if (markpos >= mark.length) { 110 int[] newmark = new int[mark.length * 2]; 111 System.arraycopy(mark, 0, newmark, 0, mark.length); 112 mark = newmark; 113 } 114 mark[markpos] = currentPos; 115 } 116 117 120 public final void release() { 121 currentPos = mark[markpos]; 122 markpos--; 123 } 124 125 128 public final void reset() { 129 currentPos = 0; 130 } 131 132 135 public final boolean hasMoreTokens() { 136 boolean rtn = false; 137 mark(); 138 String s = nextToken(); 139 if (s != null && !s.equals(EMPTY)) { 140 rtn = true; 141 } 142 release(); 143 144 return rtn; 145 } 146 147 150 public final String nextToken() { 151 StringBuffer rtn = new StringBuffer (); 152 153 while (currentPos < maxPos && delimiters.indexOf(chars[currentPos]) >= 0) { 154 if (rtnDelims && returnDelimiters.indexOf(chars[currentPos]) >= 0) { 155 rtn.append(chars[currentPos]); 156 currentPos++; 157 return rtn.toString(); 158 } 159 currentPos++; 160 } 161 162 while (currentPos < maxPos && delimiters.indexOf(chars[currentPos]) < 0) { 163 rtn.append(chars[currentPos]); 164 currentPos++; 165 } 166 167 177 return rtn.toString(); 178 } 179 180 184 public final String peekNextToken() { 185 mark(); 186 String rtn = nextToken(); 187 release(); 188 return rtn; 189 } 190 191 } | Popular Tags |