1 2 12 package com.versant.core.util; 13 14 import java.util.Map ; 15 16 22 public final class StringListParser { 23 24 public static final char DEFAULT_DELIM = ','; 25 26 private String s; 27 private int slen; 28 private char delim; 29 private int pos; 30 31 public StringListParser(char delim) { 32 this.delim = delim; 33 } 34 35 public StringListParser(String s, char delim) { 36 this.delim = delim; 37 setString(s); 38 } 39 40 public StringListParser(String s) { 41 this(s, DEFAULT_DELIM); 42 } 43 44 public StringListParser() { 45 this(DEFAULT_DELIM); 46 } 47 48 51 public void setString(String s) { 52 this.s = s; 53 pos = 0; 54 slen = s.length(); 55 } 56 57 60 public String getString() { 61 return s; 62 } 63 64 69 public String nextString() throws IllegalStateException { 70 if (!hasNext()) { 71 throw new IllegalStateException ("Expected String at end: " + s); 72 } 73 int start = pos; 74 for (; pos < slen;) { 75 char c = s.charAt(pos++); 76 if (c == delim) return s.substring(start, pos - 1); 77 } 78 return s.substring(start); 79 } 80 81 86 public String nextQuotedString() throws IllegalStateException { 87 if (!hasNext()) { 88 throw new IllegalStateException ( 89 "Expected quoted String at end: " + s); 90 } 91 if (s.charAt(pos) == '-') { 92 if (++pos < slen) ++pos; return null; 94 } 95 StringBuffer a = new StringBuffer (); 96 ++pos; for (; pos < slen;) { 98 char c = s.charAt(pos++); 99 if (c == '"') { 100 if (pos >= slen) break; 101 c = s.charAt(pos++); 102 if (c != '"') break; 103 } 104 a.append(c); 105 } 106 return a.toString(); 107 } 108 109 114 public Class nextClass(ClassLoader cl) 115 throws IllegalStateException , ClassNotFoundException { 116 if (!hasNext()) { 117 throw new IllegalStateException ("Expected class name at end: " + s); 118 } 119 if (s.charAt(pos) == '-') { 120 if (++pos < slen) ++pos; return null; 122 } 123 int start = pos; 124 for (; pos < slen;) { 125 char c = s.charAt(pos++); 126 if (c == delim) break; 127 } 128 String name = s.substring(start, pos - 1); 129 return BeanUtils.loadClass(name, true, cl); 130 } 131 132 137 public int nextInt() throws IllegalStateException { 138 if (!hasNext()) { 139 throw new IllegalStateException ("Expected int at end: " + s); 140 } 141 int ibuf = 0; 142 boolean neg = false; 143 if (pos < slen && s.charAt(pos) == '-') { 144 neg = true; 145 pos++; 146 } 147 for (; pos < slen;) { 148 char c = s.charAt(pos++); 149 if (c >= '0' && c <= '9') { 150 ibuf = ibuf * 10 + (c - '0'); 151 } else if (c == delim) { 153 break; 154 } else { 155 throw new IllegalStateException ( 156 "Expected int at pos " + (pos - 1) + ": " + s); 157 } 158 } 159 return neg ? -ibuf : ibuf; 160 } 161 162 167 public boolean nextBoolean() { 168 if (!hasNext()) { 169 throw new IllegalStateException ("Expected boolean at end: " + s); 170 } 171 char c = s.charAt(pos++); 172 if (pos < slen) pos++; if (c == 'Y') { 174 return true; 175 } else if (c == 'N') return false; 176 throw new IllegalStateException ("Invalid boolean character '" + c + 177 "' in " + s); 178 } 179 180 185 public double nextDouble() throws IllegalStateException { 186 if (!hasNext()) { 187 throw new IllegalStateException ("Expected double at end: " + s); 188 } 189 double dbuf = 0.0; 190 boolean neg = false; 191 if (pos < slen && s.charAt(pos) == '-') { 192 neg = true; 193 pos++; 194 } 195 for (; pos < slen;) { 196 char c = s.charAt(pos++); 197 if (c >= '0' && c <= '9') { 198 dbuf = dbuf * 10.0 + (c - '0'); 199 } else if (c == delim) { 200 return neg ? -dbuf : dbuf; 201 } else if (c == '.') { 202 break; 203 } else { 204 throw new IllegalStateException ( 205 "Expected double at pos " + (pos - 1) + ": " + s); 206 } 207 } 208 double m = 10.0; 209 double f = 0.0; 210 for (; pos < slen;) { 211 char c = s.charAt(pos++); 212 if (c >= '0' && c <= '9') { 213 f = f * 10.0 + (c - '0'); 214 m = m * 10.0; 215 } else if (c == delim) { 216 break; 217 } else { 218 throw new IllegalStateException ( 219 "Expected double at pos " + (pos - 1) + ": " + s); 220 } 221 } 222 dbuf = dbuf + f / m; 223 return neg ? -dbuf : dbuf; 224 } 225 226 229 public void nextProperties(Map map) { 230 for (; hasNext();) { 231 String key = nextString(); 232 String value = nextQuotedString(); 233 map.put(key, value); 234 } 235 } 236 237 240 public final boolean hasNext() { 241 return pos < slen; 242 } 243 244 public void nextProperties(IntObjectHashMap map) { 245 for (; hasNext();) { 246 int key = nextInt(); 247 String value = nextQuotedString(); 248 map.put(key, value); 249 } 250 } 251 } 252 | Popular Tags |