View Javadoc

1   
2   /*
3    * This code is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public 
5    * License as published by the Free Software Foundation; either 
6    * version 2.1 of the License, or (at your option) any later version.
7    *
8    * This code is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public 
14   * License along with this program; if not, write to the Free 
15   * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
16   * MA  02111-1307, USA.
17   */
18  package no.geosoft.cc.util;
19  
20  
21  
22  import java.util.ArrayList;
23  
24  
25  
26  /**
27   * A StringTokenizer class that handle empty tokens.
28   * 
29   * @author <a href="mailto:info@geosoft.no">GeoSoft</a>
30   */   
31  public class SmartTokenizer 
32  {
33    private ArrayList <String>tokens_;
34    private int       current_;
35    
36  
37    
38    public SmartTokenizer (String string, String delimiter)
39    {
40      tokens_  = new ArrayList <String> ();
41      current_ = 0;
42      
43      java.util.StringTokenizer tokenizer =
44        new java.util.StringTokenizer (string, delimiter, true);
45  
46      boolean wasDelimiter = true;
47      boolean isDelimiter  = false;    
48      
49      while (tokenizer.hasMoreTokens()) {
50        String token = tokenizer.nextToken();
51  
52        isDelimiter = token.equals (delimiter);
53        
54        if (wasDelimiter)
55          tokens_.add (isDelimiter ? "" : token);
56        else if (!isDelimiter)
57          tokens_.add (token);
58  
59        wasDelimiter = isDelimiter;
60      }
61  
62      if (isDelimiter) tokens_.add ("");
63    }
64  
65  
66    public int countTokens()
67    {
68      return tokens_.size();
69    }
70  
71    
72    public boolean hasMoreTokens()
73    {
74      return current_ < tokens_.size();
75    }
76  
77  
78    public boolean hasMoreElements()
79    {
80      return hasMoreTokens();
81    }
82  
83  
84    public Object nextElement()
85    {
86      return nextToken();
87    }
88  
89  
90    public String nextToken()
91    {
92      String token = (String) tokens_.get (current_);
93      current_++;
94      return token;
95    }
96    
97    
98  
99    /**
100    * Testing this class.
101    * 
102    * @param args  Not used.
103    */
104   public static void main (String args[])
105   {
106     SmartTokenizer t = new SmartTokenizer ("This,is,a,,test,", ",");
107     while (t.hasMoreTokens()) {
108       String token = t.nextToken();
109       System.out.println ("#" + token + "#");
110     }
111   }
112 }