View Javadoc
1   package org.codehaus.plexus.util.cli;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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  
19  import java.util.StringTokenizer;
20  
21  /**
22   * The java.util.StringTokenizer is horribly broken. Given the string 1,,,3,,4 (, delim) It will return 1,3,4 Which is
23   * clearly wrong - 1,EMPTY,EMPTY,3,EMPTY,4 is what it should return
24   * 
25   * @version $Id$
26   */
27  public final class EnhancedStringTokenizer
28  {
29      private StringTokenizer cst = null;
30  
31      String cdelim;
32  
33      final boolean cdelimSingleChar;
34  
35      final char cdelimChar;
36  
37      boolean creturnDelims;
38  
39      String lastToken = null;
40  
41      boolean delimLast = true;
42  
43      public EnhancedStringTokenizer( String str )
44      {
45          this( str, " \t\n\r\f", false );
46      }
47  
48      public EnhancedStringTokenizer( String str, String delim )
49      {
50          this( str, delim, false );
51      }
52  
53      public EnhancedStringTokenizer( String str, String delim, boolean returnDelims )
54      {
55          cst = new StringTokenizer( str, delim, true );
56          cdelim = delim;
57          creturnDelims = returnDelims;
58          cdelimSingleChar = ( delim.length() == 1 );
59          cdelimChar = delim.charAt( 0 );
60      }
61  
62      public boolean hasMoreTokens()
63      {
64          return cst.hasMoreTokens();
65      }
66  
67      private String internalNextToken()
68      {
69          if ( lastToken != null )
70          {
71              String last = lastToken;
72              lastToken = null;
73              return last;
74          }
75  
76          String token = cst.nextToken();
77          if ( isDelim( token ) )
78          {
79              if ( delimLast )
80              {
81                  lastToken = token;
82                  return "";
83              }
84              else
85              {
86                  delimLast = true;
87                  return token;
88              }
89          }
90          else
91          {
92              delimLast = false;
93              return token;
94          }
95      }
96  
97      public String nextToken()
98      {
99          String token = internalNextToken();
100         if ( creturnDelims )
101         {
102             return token;
103         }
104         if ( isDelim( token ) )
105         {
106             return hasMoreTokens() ? internalNextToken() : "";
107         }
108         else
109         {
110             return token;
111         }
112     }
113 
114     private boolean isDelim( String str )
115     {
116         if ( str.length() == 1 )
117         {
118             char ch = str.charAt( 0 );
119             if ( cdelimSingleChar )
120             {
121                 if ( cdelimChar == ch )
122                 {
123                     return true;
124                 }
125             }
126             else
127             {
128                 if ( cdelim.indexOf( ch ) >= 0 )
129                 {
130                     return true;
131                 }
132             }
133         }
134         return false;
135 
136     }
137 }