1 10 package org.mmbase.util; 11 12 import java.util.*; 13 14 26 public class StringTagger implements Map { 27 28 31 private Hashtable tokens; 32 35 private Map multitokens; 36 39 private char tagStart; 40 43 private char tagSeparator; 44 47 private char fieldSeparator; 48 51 private char quote; 52 55 private char functionOpen; 56 59 private char functionClose; 60 61 64 private String startline = ""; 65 66 77 public StringTagger(String line, char tagStart, char tagSeparator,char fieldSeparator, char quote, 78 char functionOpen, char functionClose) { 79 this.tagStart = tagStart; 80 this.startline = line; 81 this.tagSeparator = tagSeparator; 82 this.fieldSeparator = fieldSeparator; 83 this.quote = quote; 84 this.functionOpen = functionOpen; 85 this.functionClose = functionClose; 86 tokens = new Hashtable(); multitokens = new HashMap(); 88 createTagger(line); 89 } 90 91 101 public StringTagger(String line, char tagStart, char tagSeparator,char fieldSeparator, char quote) { 102 this(line, tagStart, tagSeparator,fieldSeparator, quote,'(',')'); 103 } 104 105 110 public StringTagger(String line) { 111 this(line,' ','=',',','"','(',')'); 112 } 113 114 120 protected void createTagger(String line) { 121 StringTokenizer tok2 = new StringTokenizer(line+tagStart,""+tagSeparator+tagStart,true); 122 String part,tag,prevtok,tok; 123 boolean isTag,isPart,isQuoted; 124 125 isTag = true; 126 isPart = false; 127 isQuoted = false; 128 prevtok = ""; 129 tag = part = ""; while(tok2.hasMoreTokens()) { 132 tok = tok2.nextToken(); 133 if (tok.equals(""+tagSeparator)) { 135 if (isTag) { 136 tag = prevtok; 137 isTag = false; 138 } else { 139 if (!isQuoted) { 140 splitTag(tag+tagSeparator+part); 141 isTag = true; 142 isPart = false; 143 part = ""; 144 } else { 145 part += tok; 146 } 147 } 148 } else if (tok.equals(""+tagStart)) { 149 if (isPart) { 150 if (isQuoted) { 151 part += tok; 152 } else { 153 if (!prevtok.equals("" + tagStart)) { 154 splitTag(tag + tagSeparator + part); 155 isTag = true; 156 isPart = false; 157 part = ""; 158 } 159 } 160 prevtok = tok; 161 } 162 } else { 163 if (!isTag) isPart = true; 164 if (isPart) { 166 if (isQuoted) { 167 if (tok.charAt(tok.length() - 1) == quote) { 169 isQuoted = false; 170 } 171 part += tok; 172 } else { 173 if (tok.charAt(0) == quote && !(tok.charAt(tok.length() - 1) == quote)) { 174 isQuoted = true; 175 } 176 part += tok; 177 } 178 } 179 prevtok = tok; 181 } 182 } 183 } 184 185 191 protected void splitTag(String tag) { 192 int tagPos = tag.indexOf(tagSeparator); 193 String name = tag.substring(0,tagPos); 194 String result = tag.substring(tagPos+1); 195 197 if (result.length()>1 && result.charAt(0) == quote && result.charAt(result.length() - 1) == quote) { 198 result = result.substring(1, result.length() - 1); 199 } 200 tokens.put(name, result); 201 202 StringTokenizer toks = new StringTokenizer(result, "" + fieldSeparator + functionOpen + functionClose, true); 203 Vector multi = new Vector(); 205 if(toks.hasMoreTokens()) { 206 String tokvalue=""; 207 int nesting = 0; 208 while (toks.hasMoreTokens()) { 209 String tok = toks.nextToken(); 210 if (tok.equals("" + fieldSeparator)) { 211 if (nesting == 0) { 212 multi.add(tokvalue); 213 tokvalue = ""; 214 } else { 215 tokvalue += tok; 216 } 217 } else if (tok.equals("" + functionOpen)) { 218 nesting++; 219 tokvalue += tok; 220 } else if (tok.equals("" + functionClose)) { 221 nesting--; 222 tokvalue += tok; 223 } else { 224 tokvalue += tok; 225 } 226 } 227 multi.add(tokvalue); 228 } 229 multitokens.put(name, multi); 230 } 231 232 233 235 238 public void clear() { 239 tokens.clear(); 240 multitokens.clear(); 241 startline=""; 242 } 243 244 247 public boolean containsKey (Object ob) { 248 return tokens.containsKey(ob); 249 } 250 251 254 public boolean containsValue (Object ob) { 255 return tokens.containsValue(ob); 256 } 257 258 261 public Set entrySet() { 262 return tokens.entrySet(); 263 } 264 265 269 public boolean equals(Object ob) { 270 return (ob instanceof Map) && (ob.hashCode() == this.hashCode()); 271 } 272 273 280 public Object get(Object ob) { 281 return tokens.get(ob); 282 } 283 284 287 public int hashCode() { 288 return multitokens.hashCode(); 289 } 290 291 294 public boolean isEmpty() { 295 return tokens.isEmpty(); 296 } 297 298 301 public Set keySet() { 302 return tokens.keySet(); 303 } 304 305 308 public Object put(Object key, Object value) { 309 Object res = tokens.get(key); 310 setValue((String )key, (String )value); 311 return res; 312 } 313 314 317 public void putAll(Map map) { 318 throw new UnsupportedOperationException (); 319 } 320 321 324 public Object remove(Object key) { 325 Object res = tokens.get(key); 326 tokens.remove(key); 327 multitokens.remove(key); 328 return res; 329 } 330 331 334 public int size() { 335 return tokens.size(); 336 } 337 338 341 public Collection values() { 342 return tokens.values(); 343 } 344 345 347 350 public Enumeration keys() { 351 return tokens.keys(); 352 } 353 354 357 public String toString() { 358 StringBuffer content = new StringBuffer ("["); 359 for (Enumeration e = keys(); e.hasMoreElements();) { 360 String key = (String ) e.nextElement(); 361 content.append('<').append(key); 362 content.append('=').append(Values(key)); 363 content.append('>'); 364 } 365 content.append(']'); 366 return content.toString(); 367 } 368 369 374 public Enumeration elements() { 375 return tokens.elements(); 376 } 377 378 383 public Enumeration multiElements(String token) { 384 Vector tmp = (Vector) multitokens.get(token); 385 if (tmp != null) { 386 return tmp.elements(); 387 } else { 388 return null; 389 } 390 } 391 392 399 public Vector Values(String token) { 400 Vector tmp = (Vector) multitokens.get(token); 401 return tmp; 402 } 403 404 408 public String ValuesString(String token) { 409 return startline; 410 } 411 412 420 public String Value(String token) { 421 String val; 422 Vector tmp=(Vector) multitokens.get(token); 423 if (tmp!=null && tmp.size()>0) { 424 val=(String ) tmp.elementAt(0); 425 if (val != null) { 426 val = Strip.DoubleQuote(val,Strip.BOTH); return val; 428 } else { 429 return null; 430 } 431 } else { 432 return null; 433 } 434 } 435 436 439 public void setValue(String token,String val) { 440 Vector newval = new Vector(); 441 newval.addElement(val); 442 tokens.put(token,newval); 443 multitokens.put(token,newval); 444 } 445 446 449 public void setValues(String token,Vector values) { 450 tokens.put(token,values.toString()); 451 multitokens.put(token,values); 452 } 453 454 457 public static void main(String args[]) { 458 StringTagger tag = new StringTagger(args[0]); 459 } 460 461 } 462 | Popular Tags |