KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > ArgumentTokenizer


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util;
35
36 import java.util.List JavaDoc;
37 import java.util.LinkedList JavaDoc;
38
39 /**
40  * Utility class which can tokenize a String into a list of String arguments,
41  * with behavior similar to parsing command line arguments to a program.
42  * Quoted Strings are treated as single arguments, and escaped characters
43  * are translated so that the tokenized arguments have the same meaning.
44  * Since all methods are static, the class is declared abstract to prevent
45  * instantiation.
46  * @version $Id: ArgumentTokenizer.java 3903 2006-07-05 20:03:06Z rcartwright $
47  */

48 public abstract class ArgumentTokenizer {
49   private static final int NO_TOKEN_STATE = 0;
50   private static final int NORMAL_TOKEN_STATE = 1;
51   private static final int SINGLE_QUOTE_STATE = 2;
52   private static final int DOUBLE_QUOTE_STATE = 3;
53
54   /** Tokenizes the given String into String tokens
55    * @param arguments A String containing one or more command-line style arguments to be tokenized.
56    * @return A list of parsed and properly escaped arguments.
57    */

58   public static List JavaDoc<String JavaDoc> tokenize(String JavaDoc arguments) {
59     return tokenize(arguments, false);
60   }
61
62   /** Tokenizes the given String into String tokens.
63    * @param arguments A String containing one or more command-line style arguments to be tokenized.
64    * @param stringify whether or not to include escape special characters
65    * @return A list of parsed and properly escaped arguments.
66    */

67   public static List JavaDoc<String JavaDoc> tokenize(String JavaDoc arguments, boolean stringify) {
68
69     LinkedList JavaDoc<String JavaDoc> argList = new LinkedList JavaDoc<String JavaDoc>();
70     StringBuilder JavaDoc currArg = new StringBuilder JavaDoc();
71     boolean escaped = false;
72     int state = NO_TOKEN_STATE; // start in the NO_TOKEN_STATE
73
int len = arguments.length();
74
75     // Loop over each character in the string
76
for (int i = 0; i < len; i++) {
77       char c = arguments.charAt(i);
78       if (escaped) {
79         // Escaped state: just append the next character to the current arg.
80
escaped = false;
81         currArg.append(c);
82       }
83       else {
84         switch(state) {
85           case SINGLE_QUOTE_STATE:
86             if (c == '\'') {
87               // Seen the close quote; continue this arg until whitespace is seen
88
state = NORMAL_TOKEN_STATE;
89             }
90             else {
91               currArg.append(c);
92             }
93             break;
94           case DOUBLE_QUOTE_STATE:
95             if (c == '"') {
96               // Seen the close quote; continue this arg until whitespace is seen
97
state = NORMAL_TOKEN_STATE;
98             }
99             else if (c == '\\') {
100               // Look ahead, and only escape quotes or backslashes
101
i++;
102               char next = arguments.charAt(i);
103               if (next == '"' || next == '\\') {
104                 currArg.append(next);
105               }
106               else {
107                 currArg.append(c);
108                 currArg.append(next);
109               }
110             }
111             else {
112               currArg.append(c);
113             }
114             break;
115 // case NORMAL_TOKEN_STATE:
116
// if (Character.isWhitespace(c)) {
117
// // Whitespace ends the token; start a new one
118
// argList.add(currArg.toString());
119
// currArg = new StringBuffer();
120
// state = NO_TOKEN_STATE;
121
// }
122
// else if (c == '\\') {
123
// // Backslash in a normal token: escape the next character
124
// escaped = true;
125
// }
126
// else if (c == '\'') {
127
// state = SINGLE_QUOTE_STATE;
128
// }
129
// else if (c == '"') {
130
// state = DOUBLE_QUOTE_STATE;
131
// }
132
// else {
133
// currArg.append(c);
134
// }
135
// break;
136
case NO_TOKEN_STATE:
137           case NORMAL_TOKEN_STATE:
138             switch(c) {
139               case '\\':
140                 escaped = true;
141                 state = NORMAL_TOKEN_STATE;
142                 break;
143               case '\'':
144                 state = SINGLE_QUOTE_STATE;
145                 break;
146               case '"':
147                 state = DOUBLE_QUOTE_STATE;
148                 break;
149               default:
150                 if (!Character.isWhitespace(c)) {
151                   currArg.append(c);
152                   state = NORMAL_TOKEN_STATE;
153                 }
154                 else if (state == NORMAL_TOKEN_STATE) {
155                   // Whitespace ends the token; start a new one
156
argList.add(currArg.toString());
157                   currArg = new StringBuilder JavaDoc();
158                   state = NO_TOKEN_STATE;
159                 }
160               }
161             break;
162           default:
163             throw new IllegalStateException JavaDoc("ArgumentTokenizer state " + state + " is invalid!");
164         }
165       }
166     }
167
168     // If we're still escaped, put in the backslash
169
if (escaped) {
170       currArg.append('\\');
171       argList.add(currArg.toString());
172     }
173     // Close the last argument if we haven't yet
174
else if (state != NO_TOKEN_STATE) {
175       argList.add(currArg.toString());
176     }
177     // Format each argument if we've been told to stringify them
178
if (stringify) {
179       for (int i = 0; i < argList.size(); i++) {
180         argList.set(i, "\"" + _escapeQuotesAndBackslashes(argList.get(i)) + "\"");
181       }
182     }
183     return argList;
184   }
185
186   /**
187    * Inserts backslashes before any occurrences of a backslash or
188    * quote in the given string. Also converts any special characters
189    * appropriately.
190    */

191   protected static String JavaDoc _escapeQuotesAndBackslashes(String JavaDoc s) {
192     final StringBuilder JavaDoc buf = new StringBuilder JavaDoc(s);
193
194     // Walk backwards, looking for quotes or backslashes.
195
// If we see any, insert an extra backslash into the buffer at
196
// the same index. (By walking backwards, the index into the buffer
197
// will remain correct as we change the buffer.)
198
for (int i = s.length()-1; i >= 0; i--) {
199       char c = s.charAt(i);
200       if ((c == '\\') || (c == '"')) {
201         buf.insert(i, '\\');
202       }
203       // Replace any special characters with escaped versions
204
else if (c == '\n') {
205         buf.deleteCharAt(i);
206         buf.insert(i, "\\n");
207       }
208       else if (c == '\t') {
209         buf.deleteCharAt(i);
210         buf.insert(i, "\\t");
211       }
212       else if (c == '\r') {
213         buf.deleteCharAt(i);
214         buf.insert(i, "\\r");
215       }
216       else if (c == '\b') {
217         buf.deleteCharAt(i);
218         buf.insert(i, "\\b");
219       }
220       else if (c == '\f') {
221         buf.deleteCharAt(i);
222         buf.insert(i, "\\f");
223       }
224     }
225     return buf.toString();
226   }
227 }
228
Popular Tags