1 package org.codehaus.plexus.util.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.StringTokenizer;
20
21
22
23
24
25
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 }