Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EnhancedStringTokenizer |
|
| 3.2857142857142856;3.286 |
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 | 0 | private StringTokenizer cst = null; |
55 | ||
56 | String cdelim; | |
57 | ||
58 | final boolean cdelimSingleChar; | |
59 | ||
60 | final char cdelimChar; | |
61 | ||
62 | boolean creturnDelims; | |
63 | ||
64 | 0 | String lastToken = null; |
65 | ||
66 | 0 | boolean delimLast = true; |
67 | ||
68 | public EnhancedStringTokenizer( String str ) | |
69 | { | |
70 | 0 | this( str, " \t\n\r\f", false ); |
71 | 0 | } |
72 | ||
73 | public EnhancedStringTokenizer( String str, String delim ) | |
74 | { | |
75 | 0 | this( str, delim, false ); |
76 | 0 | } |
77 | ||
78 | public EnhancedStringTokenizer( String str, String delim, boolean returnDelims ) | |
79 | 0 | { |
80 | 0 | cst = new StringTokenizer( str, delim, true ); |
81 | 0 | cdelim = delim; |
82 | 0 | creturnDelims = returnDelims; |
83 | 0 | cdelimSingleChar = ( delim.length() == 1 ); |
84 | 0 | cdelimChar = delim.charAt( 0 ); |
85 | 0 | } |
86 | ||
87 | public boolean hasMoreTokens() | |
88 | { | |
89 | 0 | return cst.hasMoreTokens(); |
90 | } | |
91 | ||
92 | private String internalNextToken() | |
93 | { | |
94 | 0 | if ( lastToken != null ) |
95 | { | |
96 | 0 | String last = lastToken; |
97 | 0 | lastToken = null; |
98 | 0 | return last; |
99 | } | |
100 | ||
101 | 0 | String token = cst.nextToken(); |
102 | 0 | if ( isDelim( token ) ) |
103 | { | |
104 | 0 | if ( delimLast ) |
105 | { | |
106 | 0 | lastToken = token; |
107 | 0 | return ""; |
108 | } | |
109 | else | |
110 | { | |
111 | 0 | delimLast = true; |
112 | 0 | return token; |
113 | } | |
114 | } | |
115 | else | |
116 | { | |
117 | 0 | delimLast = false; |
118 | 0 | return token; |
119 | } | |
120 | } | |
121 | ||
122 | public String nextToken() | |
123 | { | |
124 | 0 | String token = internalNextToken(); |
125 | 0 | if ( creturnDelims ) |
126 | { | |
127 | 0 | return token; |
128 | } | |
129 | 0 | if ( isDelim( token ) ) |
130 | { | |
131 | 0 | return hasMoreTokens() ? internalNextToken() : ""; |
132 | } | |
133 | else | |
134 | { | |
135 | 0 | return token; |
136 | } | |
137 | } | |
138 | ||
139 | private boolean isDelim( String str ) | |
140 | { | |
141 | 0 | if ( str.length() == 1 ) |
142 | { | |
143 | 0 | char ch = str.charAt( 0 ); |
144 | 0 | if ( cdelimSingleChar ) |
145 | { | |
146 | 0 | if ( cdelimChar == ch ) |
147 | { | |
148 | 0 | return true; |
149 | } | |
150 | } | |
151 | else | |
152 | { | |
153 | 0 | if ( cdelim.indexOf( ch ) >= 0 ) |
154 | { | |
155 | 0 | return true; |
156 | } | |
157 | } | |
158 | } | |
159 | 0 | return false; |
160 | ||
161 | } | |
162 | } |