View Javadoc

1   package org.apache.maven.it.util.cli;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  /* ====================================================================
28   * Copyright 2003-2004 The Apache Software Foundation.
29   *
30   * Licensed under the Apache License, Version 2.0 (the "License");
31   * you may not use this file except in compliance with the License.
32   * You may obtain a copy of the License at
33   *
34   *      http://www.apache.org/licenses/LICENSE-2.0
35   *
36   * Unless required by applicable law or agreed to in writing, software
37   * distributed under the License is distributed on an "AS IS" BASIS,
38   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39   * See the License for the specific language governing permissions and
40   * limitations under the License.
41   * ====================================================================
42   */
43  
44  import java.util.StringTokenizer;
45  
46  /**
47   * The java.util.StringTokenizer is horribly broken.
48   * Given the string  1,,,3,,4      (, delim)
49   * It will return 1,3,4
50   * Which is clearly wrong - 1,EMPTY,EMPTY,3,EMPTY,4 is what it should return
51   */
52  public final class EnhancedStringTokenizer
53  {
54      private StringTokenizer cst = null;
55  
56      String cdelim;
57  
58      final boolean cdelimSingleChar;
59  
60      final char cdelimChar;
61  
62      boolean creturnDelims;
63  
64      String lastToken = null;
65  
66      boolean delimLast = true;
67  
68      public EnhancedStringTokenizer( String str )
69      {
70          this( str, " \t\n\r\f", false );
71      }
72  
73      public EnhancedStringTokenizer( String str, String delim )
74      {
75          this( str, delim, false );
76      }
77  
78      public EnhancedStringTokenizer( String str, String delim, boolean returnDelims )
79      {
80          cst = new StringTokenizer( str, delim, true );
81          cdelim = delim;
82          creturnDelims = returnDelims;
83          cdelimSingleChar = ( delim.length() == 1 );
84          cdelimChar = delim.charAt( 0 );
85      }
86  
87      public boolean hasMoreTokens()
88      {
89          return cst.hasMoreTokens();
90      }
91  
92      private String internalNextToken()
93      {
94          if ( lastToken != null )
95          {
96              String last = lastToken;
97              lastToken = null;
98              return last;
99          }
100 
101         String token = cst.nextToken();
102         if ( isDelim( token ) )
103         {
104             if ( delimLast )
105             {
106                 lastToken = token;
107                 return "";
108             }
109             else
110             {
111                 delimLast = true;
112                 return token;
113             }
114         }
115         else
116         {
117             delimLast = false;
118             return token;
119         }
120     }
121 
122     public String nextToken()
123     {
124         String token = internalNextToken();
125         if ( creturnDelims )
126         {
127             return token;
128         }
129         if ( isDelim( token ) )
130         {
131             return hasMoreTokens() ? internalNextToken() : "";
132         }
133         else
134         {
135             return token;
136         }
137     }
138 
139     private boolean isDelim( String str )
140     {
141         if ( str.length() == 1 )
142         {
143             char ch = str.charAt( 0 );
144             if ( cdelimSingleChar )
145             {
146                 if ( cdelimChar == ch )
147                 {
148                     return true;
149                 }
150             }
151             else
152             {
153                 if ( cdelim.indexOf( ch ) >= 0 )
154                 {
155                     return true;
156                 }
157             }
158         }
159         return false;
160 
161     }
162 }