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   *
26   */
27  public final class EnhancedStringTokenizer {
28      private StringTokenizer cst = null;
29  
30      String cdelim;
31  
32      final boolean cdelimSingleChar;
33  
34      final char cdelimChar;
35  
36      boolean creturnDelims;
37  
38      String lastToken = null;
39  
40      boolean delimLast = true;
41  
42      public EnhancedStringTokenizer(String str) {
43          this(str, " \t\n\r\f", false);
44      }
45  
46      public EnhancedStringTokenizer(String str, String delim) {
47          this(str, delim, false);
48      }
49  
50      public EnhancedStringTokenizer(String str, String delim, boolean returnDelims) {
51          cst = new StringTokenizer(str, delim, true);
52          cdelim = delim;
53          creturnDelims = returnDelims;
54          cdelimSingleChar = (delim.length() == 1);
55          cdelimChar = delim.charAt(0);
56      }
57  
58      public boolean hasMoreTokens() {
59          return cst.hasMoreTokens();
60      }
61  
62      private String internalNextToken() {
63          if (lastToken != null) {
64              String last = lastToken;
65              lastToken = null;
66              return last;
67          }
68  
69          String token = cst.nextToken();
70          if (isDelim(token)) {
71              if (delimLast) {
72                  lastToken = token;
73                  return "";
74              } else {
75                  delimLast = true;
76                  return token;
77              }
78          } else {
79              delimLast = false;
80              return token;
81          }
82      }
83  
84      public String nextToken() {
85          String token = internalNextToken();
86          if (creturnDelims) {
87              return token;
88          }
89          if (isDelim(token)) {
90              return hasMoreTokens() ? internalNextToken() : "";
91          } else {
92              return token;
93          }
94      }
95  
96      private boolean isDelim(String str) {
97          if (str.length() == 1) {
98              char ch = str.charAt(0);
99              if (cdelimSingleChar) {
100                 if (cdelimChar == ch) {
101                     return true;
102                 }
103             } else {
104                 if (cdelim.indexOf(ch) >= 0) {
105                     return true;
106                 }
107             }
108         }
109         return false;
110     }
111 }