View Javadoc

1   package org.apache.maven.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.StringTokenizer;
22  
23  /**
24   * The java.util.StringTokenizer is horribly broken.
25   * Given the string  1,,,3,,4      (, delim)
26   * It will return 1,3,4
27   * Which is clearly wrong - 1,EMPTY,EMPTY,3,EMPTY,4 is what it should return
28   */
29  public final class EnhancedStringTokenizer
30  {
31      private StringTokenizer cst = null;
32  
33      String cdelim;
34  
35      final boolean cdelimSingleChar;
36  
37      final char cdelimChar;
38  
39      boolean creturnDelims;
40  
41      public EnhancedStringTokenizer( String str )
42      {
43          this( str, " \t\n\r\f", false );
44      }
45  
46      public EnhancedStringTokenizer( String str, String delim )
47      {
48          this( str, delim, false );
49      }
50  
51      public EnhancedStringTokenizer( String str, String delim, boolean returnDelims )
52      {
53          cst = new StringTokenizer( str, delim, true );
54          cdelim = delim;
55          creturnDelims = returnDelims;
56          cdelimSingleChar = ( delim.length() == 1 );
57          cdelimChar = delim.charAt( 0 );
58      }
59  
60      public boolean hasMoreTokens()
61      {
62          return cst.hasMoreTokens();
63      }
64  
65      String lastToken = null;
66  
67      boolean delimLast = true;
68  
69      private String internalNextToken()
70      {
71          if ( lastToken != null )
72          {
73              String last = lastToken;
74              lastToken = null;
75              return last;
76          }
77  
78          String token = cst.nextToken();
79          if ( isDelim( token ) )
80          {
81              if ( delimLast )
82              {
83                  lastToken = token;
84                  return "";
85              }
86              else
87              {
88                  delimLast = true;
89                  return token;
90              }
91          }
92          else
93          {
94              delimLast = false;
95              return token;
96          }
97      }
98  
99      public String nextToken()
100     {
101         String token = internalNextToken();
102         if ( creturnDelims )
103         {
104             return token;
105         }
106         if ( isDelim( token ) )
107         {
108             return hasMoreTokens() ? internalNextToken() : "";
109         }
110         else
111         {
112             return token;
113         }
114     }
115 
116     private boolean isDelim( String str )
117     {
118         if ( str.length() == 1 )
119         {
120             char ch = str.charAt( 0 );
121             if ( cdelimSingleChar )
122             {
123                 if ( cdelimChar == ch )
124                 {
125                     return true;
126                 }
127             }
128             else
129             {
130                 if ( cdelim.indexOf( ch ) >= 0 )
131                 {
132                     return true;
133                 }
134             }
135         }
136         return false;
137     }
138 }