KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jdbclogger > core > util > Tokenizer


1 package net.sourceforge.jdbclogger.core.util;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 import java.util.Iterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20 import java.util.TreeMap JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 /**
24  * @author Martin Marinschek (latest modification by $Author: catalean $)
25  * @version $Revision: 83 $ $Date: 2007-07-08 00:00:58 +0300 (Sun, 08 Jul 2007) $
26  */

27 public class Tokenizer
28 {
29     public static char NO_COMMENT_CHARACTER = '\0';
30     private Vector JavaDoc _indexToTokenEntries;
31     private int _currentIndex;
32     
33     public Tokenizer(String JavaDoc str, String JavaDoc delimiter)
34     {
35         this(str, new String JavaDoc[]{delimiter});
36     }
37
38     public Tokenizer(String JavaDoc str, String JavaDoc delimiters[])
39     {
40         this(str, delimiters, false, false, NO_COMMENT_CHARACTER, NO_COMMENT_CHARACTER);
41     }
42
43     public Tokenizer(String JavaDoc str, String JavaDoc delimiters[], boolean returnDelimiters, boolean returnEmptyStrings, char commentStartCharacter, char commentEndCharacter)
44     {
45         _currentIndex = 0;
46         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str);
47         TreeMap JavaDoc indexToDelimiterMap = new TreeMap JavaDoc();
48         for (int i = 0; i < delimiters.length; i++)
49             getDelimiterIndizes(buf, delimiters[i], commentStartCharacter, commentEndCharacter, indexToDelimiterMap);
50
51         TreeMap JavaDoc indexToTokenMap = new TreeMap JavaDoc();
52         getTokenIndizes(buf, indexToDelimiterMap, indexToTokenMap, returnEmptyStrings);
53         if (returnDelimiters)
54             indexToTokenMap.putAll(indexToDelimiterMap);
55         _indexToTokenEntries = new Vector JavaDoc();
56         _indexToTokenEntries.addAll(indexToTokenMap.entrySet());
57     }
58
59     public boolean hasMoreTokens()
60     {
61         return _currentIndex < _indexToTokenEntries.size();
62     }
63
64     public String JavaDoc nextToken()
65     {
66         if (_currentIndex >= _indexToTokenEntries.size())
67         {
68             throw new NoSuchElementException JavaDoc();
69         }else
70         {
71             java.util.Map.Entry entry = (java.util.Map.Entry) _indexToTokenEntries.get(_currentIndex);
72             String JavaDoc returnString = (String JavaDoc) 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 JavaDoc buf, TreeMap JavaDoc indexToDelimiterMap, TreeMap JavaDoc tokenMap, boolean returnEmptyStrings)
84     {
85         Iterator JavaDoc it = indexToDelimiterMap.entrySet().iterator();
86         int lastStopIndex;
87         int currentStopIndex;
88         String JavaDoc 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 JavaDoc) entry.getKey()).floatValue();
93             putTokenIfNecessary(buf, tokenMap, returnEmptyStrings, lastStopIndex, currentStopIndex);
94             delimiterString = (String JavaDoc) entry.getValue();
95         }
96
97         putTokenIfNecessary(buf, tokenMap, returnEmptyStrings, lastStopIndex, buf.length());
98     }
99
100     private static void putTokenIfNecessary(StringBuffer JavaDoc buf, TreeMap JavaDoc tokenMap, boolean returnEmptyStrings, int lastStopIndex, int currentStopIndex)
101     {
102         String JavaDoc returnString = buf.substring(lastStopIndex, currentStopIndex);
103         if (returnString.trim().equals(""))
104         {
105             if (returnEmptyStrings)
106                 tokenMap.put(new Double JavaDoc((double) lastStopIndex - 0.5D), returnString);
107         }else
108         {
109             tokenMap.put(new Double JavaDoc(lastStopIndex), returnString);
110         }
111     }
112
113     private static void getDelimiterIndizes(StringBuffer JavaDoc buf, String JavaDoc delimiter, char commentStartCharacter, char commentEndCharacter, TreeMap JavaDoc 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 JavaDoc(delimiterStartIndex), delimiter);
135                     delimiterIndex = 0;
136                 }
137             }else
138             {
139                 delimiterIndex = 0;
140             }
141         }
142     }
143 }
144
Popular Tags