11
11
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
12
12
*/
13
13
public class CliTokenImpl implements CliToken {
14
- private static final String PIPE = "|" ;
15
- private static final String REDIRECT = ">" ;
16
- private static final String REDIRECT_APPEND = ">>" ;
17
14
18
15
final boolean text ;
19
16
final String raw ;
@@ -74,7 +71,7 @@ public static List<CliToken> tokenize(String s) {
74
71
75
72
tokenize (s , 0 , tokens );
76
73
77
- tokens = adjustTokensForSpecialSymbols (tokens );
74
+ tokens = correctPipeChar (tokens );
78
75
return tokens ;
79
76
80
77
}
@@ -90,53 +87,35 @@ public static List<CliToken> tokenize(String s) {
90
87
* unsupported:
91
88
* 3) thread|grep xxx
92
89
* 4) trace -E classA|classB methodA|methodB|grep classA
93
- *
94
- * Also handles redirection of '>' and '>>' in the similar way
95
- *
96
- * @param tokens original tokens
97
- * @return adjusted tokens
90
+ * @param tokens
91
+ * @return
98
92
*/
99
- private static List <CliToken > adjustTokensForSpecialSymbols (List <CliToken > tokens ) {
100
- return separateLeadingAndTailingSymbol (tokens , PIPE , REDIRECT_APPEND , REDIRECT );
101
- }
102
-
103
- private static List <CliToken > separateLeadingAndTailingSymbol (List <CliToken > tokens , String ... symbols ) {
104
- List <CliToken > adjustedTokens = new ArrayList <>();
93
+ private static List <CliToken > correctPipeChar (List <CliToken > tokens ) {
94
+ List <CliToken > newTokens = new ArrayList <CliToken >(tokens .size ()+4 );
105
95
for (CliToken token : tokens ) {
106
- String value = token .value ();
107
- String raw = token .raw ();
108
- boolean handled = false ;
109
- for (String symbol : symbols ) {
110
- if (value .equals (symbol )) {
111
- break ;
112
- } else if (value .endsWith (symbol )) {
113
- handled = true ;
114
- int lastIndexOfSymbol = raw .lastIndexOf (symbol );
115
- adjustedTokens .add (new CliTokenImpl (
116
- token .isText (),
117
- raw .substring (0 , lastIndexOfSymbol ),
118
- value .substring (0 , value .length () - symbol .length ())
119
- ));
120
- adjustedTokens .add (new CliTokenImpl (true , raw .substring (lastIndexOfSymbol ), symbol ));
121
- break ;
122
- } else if (value .startsWith (symbol )) {
123
- handled = true ;
124
- int firstIndexOfSymbol = raw .indexOf (symbol );
125
- adjustedTokens .add (new CliTokenImpl (true ,
126
- raw .substring (0 , firstIndexOfSymbol + symbol .length ()), symbol ));
127
- adjustedTokens .add (new CliTokenImpl (
128
- token .isText (),
129
- raw .substring (firstIndexOfSymbol + symbol .length ()),
130
- value .substring (symbol .length ())
131
- ));
132
- break ;
133
- }
134
- }
135
- if (!handled ) {
136
- adjustedTokens .add (token );
96
+ String tokenValue = token .value ();
97
+ if (tokenValue .length ()>1 && tokenValue .endsWith ("|" )) {
98
+ //split last char '|'
99
+ tokenValue = tokenValue .substring (0 , tokenValue .length ()-1 );
100
+ String rawValue = token .raw ();
101
+ rawValue = rawValue .substring (0 , rawValue .length ()-1 );
102
+ newTokens .add (new CliTokenImpl (token .isText (), rawValue , tokenValue ));
103
+ //add '|' char
104
+ newTokens .add (new CliTokenImpl (true , "|" , "|" ));
105
+
106
+ } else if (tokenValue .length ()>1 && tokenValue .startsWith ("|" )) {
107
+ //add '|' char
108
+ newTokens .add (new CliTokenImpl (true , "|" , "|" ));
109
+ //remove first char '|'
110
+ tokenValue = tokenValue .substring (1 );
111
+ String rawValue = token .raw ();
112
+ rawValue = rawValue .substring (1 );
113
+ newTokens .add (new CliTokenImpl (token .isText (), rawValue , tokenValue ));
114
+ } else {
115
+ newTokens .add (token );
137
116
}
138
117
}
139
- return adjustedTokens ;
118
+ return newTokens ;
140
119
}
141
120
142
121
private static void tokenize (String s , int index , List <CliToken > builder ) {
0 commit comments