1 package net.sourceforge.jdbclogger.core.util; 2 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 import java.util.TreeMap ; 21 import java.util.Vector ; 22 23 27 public class Tokenizer 28 { 29 public static char NO_COMMENT_CHARACTER = '\0'; 30 private Vector _indexToTokenEntries; 31 private int _currentIndex; 32 33 public Tokenizer(String str, String delimiter) 34 { 35 this(str, new String []{delimiter}); 36 } 37 38 public Tokenizer(String str, String delimiters[]) 39 { 40 this(str, delimiters, false, false, NO_COMMENT_CHARACTER, NO_COMMENT_CHARACTER); 41 } 42 43 public Tokenizer(String str, String delimiters[], boolean returnDelimiters, boolean returnEmptyStrings, char commentStartCharacter, char commentEndCharacter) 44 { 45 _currentIndex = 0; 46 StringBuffer buf = new StringBuffer (str); 47 TreeMap indexToDelimiterMap = new TreeMap (); 48 for (int i = 0; i < delimiters.length; i++) 49 getDelimiterIndizes(buf, delimiters[i], commentStartCharacter, commentEndCharacter, indexToDelimiterMap); 50 51 TreeMap indexToTokenMap = new TreeMap (); 52 getTokenIndizes(buf, indexToDelimiterMap, indexToTokenMap, returnEmptyStrings); 53 if (returnDelimiters) 54 indexToTokenMap.putAll(indexToDelimiterMap); 55 _indexToTokenEntries = new Vector (); 56 _indexToTokenEntries.addAll(indexToTokenMap.entrySet()); 57 } 58 59 public boolean hasMoreTokens() 60 { 61 return _currentIndex < _indexToTokenEntries.size(); 62 } 63 64 public String nextToken() 65 { 66 if (_currentIndex >= _indexToTokenEntries.size()) 67 { 68 throw new NoSuchElementException (); 69 }else 70 { 71 java.util.Map.Entry entry = (java.util.Map.Entry) _indexToTokenEntries.get(_currentIndex); 72 String returnString = (String ) entry.getValue(); 73 _currentIndex++; 74 return returnString; 75 } 76 } 77 78 public int countTokens() 79 { 80 return _indexToTokenEntries.size() - _currentIndex; 81 } 82 83 private static void getTokenIndizes(StringBuffer buf, TreeMap indexToDelimiterMap, TreeMap tokenMap, boolean returnEmptyStrings) 84 { 85 Iterator it = indexToDelimiterMap.entrySet().iterator(); 86 int lastStopIndex; 87 int currentStopIndex; 88 String delimiterString; 89 for (lastStopIndex = 0; it.hasNext(); lastStopIndex = currentStopIndex + delimiterString.length()) 90 { 91 java.util.Map.Entry entry = (java.util.Map.Entry) it.next(); 92 currentStopIndex = (int) ((Double ) entry.getKey()).floatValue(); 93 putTokenIfNecessary(buf, tokenMap, returnEmptyStrings, lastStopIndex, currentStopIndex); 94 delimiterString = (String ) entry.getValue(); 95 } 96 97 putTokenIfNecessary(buf, tokenMap, returnEmptyStrings, lastStopIndex, buf.length()); 98 } 99 100 private static void putTokenIfNecessary(StringBuffer buf, TreeMap tokenMap, boolean returnEmptyStrings, int lastStopIndex, int currentStopIndex) 101 { 102 String returnString = buf.substring(lastStopIndex, currentStopIndex); 103 if (returnString.trim().equals("")) 104 { 105 if (returnEmptyStrings) 106 tokenMap.put(new Double ((double) lastStopIndex - 0.5D), returnString); 107 }else 108 { 109 tokenMap.put(new Double (lastStopIndex), returnString); 110 } 111 } 112 113 private static void getDelimiterIndizes(StringBuffer buf, String delimiter, char commentStartCharacter, char commentEndCharacter, TreeMap indexToDelimiterMap) 114 { 115 int delimiterIndex = 0; 116 boolean isComment = false; 117 if (commentEndCharacter == NO_COMMENT_CHARACTER) 118 commentEndCharacter = commentStartCharacter; 119 for (int i = 0; i < buf.length(); i++) 120 { 121 if (buf.charAt(i) == commentStartCharacter && !isComment || buf.charAt(i) == commentEndCharacter && isComment) 122 { 123 isComment = !isComment; 124 delimiterIndex = 0; 125 continue; 126 } 127 if (isComment) 128 continue; 129 if (buf.charAt(i) == delimiter.charAt(delimiterIndex)) 130 { 131 if (++delimiterIndex == delimiter.length()) 132 { 133 int delimiterStartIndex = (i - delimiterIndex) + 1; 134 indexToDelimiterMap.put(new Double (delimiterStartIndex), delimiter); 135 delimiterIndex = 0; 136 } 137 }else 138 { 139 delimiterIndex = 0; 140 } 141 } 142 } 143 } 144 | Popular Tags |