KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > CLITokenizer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: CLITokenizer.java,v 1.3 2005/12/25 03:46:55 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.cli.framework;
29
30 import java.util.ListIterator JavaDoc;
31
32 /**
33  * This CLI Tokenizer class allows CLI command to break strings into tokens.
34  * The tokenizer checks for the escape characters and the quotes to determine
35  * the tokens.
36  * Consider the following examples:
37  * <li>
38  * string is <code>name1=value1:name2=value2</code> and the delimiter is :
39  * CLI tokenizer will tokenized the string to:
40  * <blockquote><pre>
41  * name1=value1
42  * name2=value2
43  * </pre></blockquote>
44  * </li>
45  * <li>
46  * string is <code>name1=abc\:def:name2=value2</code> and the delimiter is :
47  * CLI tokenizer will tokenized the string to:
48  * <blockquote><pre>
49  * name1=abc:def
50  * name2=value2
51  * </pre></blockquote>
52  * notice that abc\:def is not tokenized since it contains an escape character
53  * before the :.
54  * </li>
55  * <li>
56  * string is <code>name1="abc:def":name2=value2</code> and the delimiter is :
57  * CLI tokenizer will tokenized the string to:
58  * <blockquote><pre>
59  * name1=abc:def
60  * name2=value2
61  * </pre></blockquote>
62  * notice that "abc:def" is not not tokenized since it's in the quotes
63  * </li>
64  * <li>
65  * string is <code>name1="abc\:def":name2=value2</code> and the delimiter is :
66  * CLI tokenizer will tokenized the string to:
67  * <blockquote><pre>
68  * name1=abc\:def
69  * name2=value2
70  * </pre></blockquote>
71  * </li>
72  * @author Jane Young
73  */

74 public class CLITokenizer
75 {
76     private final static char ESCAPE_CHAR = '\\';
77     private final static String JavaDoc QUOTE_STRING = "\"";
78     private int size = 0;
79     private ListIterator JavaDoc tokenIterator = null;
80
81
82     /**
83      * constructor that calls popluateList to create the tokeIterator
84      * and size variables.
85      * @param stringToken - the string to tokenize.
86      * @param delimiter - the delimiter to tokenize.
87      */

88     public CLITokenizer(String JavaDoc stringToken, String JavaDoc delimiter)
89         throws CommandException
90     {
91         if (!checkForMatchingQuotes(stringToken))
92             throw new CommandException(getLocalizedString("UnclosedString"));
93
94         if (stringToken != null && delimiter != null)
95             tokenIterator = populateList(stringToken, delimiter);
96         else
97             throw new CommandException(getLocalizedString("CouldNotCreateCliTokenizer"));
98     }
99
100     /**
101      * returns the number of tokens in the string.
102      * @return number of tokens
103      */

104     public int countTokens()
105     {
106         return size;
107     }
108
109     /**
110      * returns true is there are more token in the list
111      * @return true if there are more tokens else false.
112      */

113     public boolean hasMoreTokens()
114     {
115         return tokenIterator.hasNext();
116     }
117
118     /**
119      * returns the token string without the escape characters
120      * @return the next string token without the escape characters.
121      */

122     public String JavaDoc nextTokenWithoutEscapeAndQuoteChars() throws CommandException
123     {
124         final String JavaDoc strWOEscape = removeEscapeChars((String JavaDoc)tokenIterator.next());
125         final String JavaDoc strWOQuotes = removeQuoteChars(strWOEscape);
126         return removeEscapeCharsFromQuotes(strWOQuotes);
127     }
128
129
130     /**
131      * returns the next token string
132      * @return the next string token
133      */

134     public String JavaDoc nextToken() throws CommandException
135     {
136         return (String JavaDoc)tokenIterator.next();
137     }
138
139
140     /**
141      * This method will check for matching quotes. If quotes do not match then
142      * return false else return true.
143      * @param str - string to check for matching quotes
144      * @return boolean - true if quotes match else false.
145      */

146     private boolean checkForMatchingQuotes(String JavaDoc str) throws CommandException
147     {
148         //get index of the first quote in the string
149
int beginQuote = getStringDelimiterIndex(str, QUOTE_STRING, 0);
150
151         while (beginQuote != -1)
152         {
153             int endQuote = getStringDelimiterIndex(str, QUOTE_STRING, beginQuote+1);
154             if (endQuote == -1) return false;
155             beginQuote = getStringDelimiterIndex(str, QUOTE_STRING, endQuote+1);
156         }
157         return true;
158     }
159
160
161     /**
162      * this methos calls the getStringDelimiterIndex to determine the index
163      * of the delimiter and use that to populate the tokenIterator.
164      * @param strToken - string to tokenize
165      * @param delimiter - delimiter to tokenize the string
166      * @return ListIterator
167      */

168     private ListIterator JavaDoc populateList(String JavaDoc strToken, String JavaDoc delimiter)
169         throws CommandException
170     {
171         java.util.List JavaDoc tokenList = new java.util.Vector JavaDoc();
172         int endIndex = getStringDelimiterIndex(strToken, delimiter, 0);
173         if (endIndex == -1) tokenList.add(strToken);
174         else
175         {
176             int beginIndex = 0;
177             while (endIndex > -1)
178             {
179                     //do not want to add to the list if the string is empty
180
if (beginIndex != endIndex)
181                     tokenList.add(strToken.substring(beginIndex, endIndex));
182                 beginIndex = endIndex + 1;
183                 endIndex = getStringDelimiterIndex(strToken, delimiter, beginIndex);
184             }
185                 //do not want to add to the list if the begindIndex is the last index
186
if (beginIndex != strToken.length())
187                 tokenList.add(strToken.substring(beginIndex));
188         }
189         size = tokenList.size();
190         try {
191             return tokenList.listIterator();
192         }
193         catch (java.lang.IndexOutOfBoundsException JavaDoc ioe) {
194             throw new CommandException(ioe);
195         }
196     }
197
198
199     /**
200      * Removes the escape characters from the property value
201      * @param strValue - string value to remove the escape character
202      * @return the string with escape character removed
203      */

204     private String JavaDoc removeEscapeChars(String JavaDoc strValue)
205         throws CommandException
206     {
207         int prefixIndex = 0;
208         java.lang.StringBuffer JavaDoc strbuff = new java.lang.StringBuffer JavaDoc();
209
210         while (prefixIndex < strValue.length())
211         {
212             int delimeterIndex = getStringDelimiterIndex(strValue,
213                                                          String.valueOf(ESCAPE_CHAR), prefixIndex);
214             if (delimeterIndex == -1)
215             {
216                 strbuff.append(strValue.substring(prefixIndex));
217                 break;
218             }
219
220             //if a quote is follow by an esacpe then keep the escape character
221
if (delimeterIndex+1 < strValue.length() &&
222                 String.valueOf(strValue.charAt(delimeterIndex+1)).equals(QUOTE_STRING))
223                 strbuff.append(strValue.substring(prefixIndex, delimeterIndex+1));
224             else
225                 strbuff.append(strValue.substring(prefixIndex, delimeterIndex));
226             
227             prefixIndex = delimeterIndex+1;
228         }
229         return strbuff.toString();
230     }
231
232     /**
233      * Removes escape characters that precedes quotes
234      * @param strValue - the string value to remove the escape characters
235      * @return string value with escape characters removed
236      */

237     private String JavaDoc removeEscapeCharsFromQuotes(String JavaDoc strValue) throws CommandException
238     {
239         int prefixIndex = 0;
240         java.lang.StringBuffer JavaDoc strbuff = new java.lang.StringBuffer JavaDoc();
241
242         while (prefixIndex < strValue.length())
243         {
244             int delimeterIndex = strValue.indexOf(String.valueOf(ESCAPE_CHAR), prefixIndex);
245             if (delimeterIndex == -1)
246             {
247                 strbuff.append(strValue.substring(prefixIndex));
248                 break;
249             }
250             //if a quote is follow by an esacpe then remove the escape character
251
if (String.valueOf(strValue.charAt(delimeterIndex+1)).equals(QUOTE_STRING))
252                 strbuff.append(strValue.substring(prefixIndex, delimeterIndex));
253             else
254                 strbuff.append(strValue.substring(prefixIndex, delimeterIndex+1));
255             
256             prefixIndex = delimeterIndex+1;
257         }
258         return strbuff.toString();
259     }
260
261
262     /**
263      * Removes the quote characters from the property value
264      * @return string value with quotes removed
265      */

266     private String JavaDoc removeQuoteChars(String JavaDoc strValue)
267         throws CommandException
268     {
269         int prefixIndex = 0;
270         java.lang.StringBuffer JavaDoc strbuff = new java.lang.StringBuffer JavaDoc();
271
272         while (prefixIndex < strValue.length())
273         {
274             int delimeterIndex = getStringDelimiterIndex(strValue,
275                                                          QUOTE_STRING, prefixIndex);
276             if (delimeterIndex == -1)
277             {
278                 strbuff.append(strValue.substring(prefixIndex));
279                 break;
280             }
281             strbuff.append(strValue.substring(prefixIndex, delimeterIndex));
282             prefixIndex = delimeterIndex+1;
283         }
284         return strbuff.toString();
285     }
286
287
288     /**
289      * This method returns the index of the delimiter. It will factor out the
290      * escape and quote characters.
291      * @param strToken - string to token
292      * @param delimiter - the delimiter to tokenize
293      * @param fromIndex - the index to start the tokenize
294      * @return index - index of the delimiter in the strToken
295      * @throw CommandException if the end quote do not match.
296      */

297     private int getStringDelimiterIndex(String JavaDoc strToken, String JavaDoc delimiter,
298                                         int fromIndex)
299         throws CommandException
300     {
301         if (fromIndex > strToken.length()-1) return -1;
302         
303             //get index of the delimiter
304
final int hasDelimiter = strToken.indexOf(delimiter, fromIndex);
305
306             //get index of the first quote in the string token
307
final int quoteBeginIndex = strToken.indexOf(QUOTE_STRING, fromIndex);
308
309             // ex: set server.ias1.jdbcurl="jdbc://oracle"
310
// if there's is a quote and a delimiter, then find the end quote
311
if ((quoteBeginIndex != -1) && (hasDelimiter != -1) &&
312             (quoteBeginIndex < hasDelimiter))
313         {
314             //get index of the end quote in the string token
315
final int quoteEndIndex = strToken.indexOf(QUOTE_STRING, quoteBeginIndex+1);
316             
317             if (quoteEndIndex == -1)
318                 throw new CommandException(getLocalizedString("UnclosedString"));
319             if (quoteEndIndex != (strToken.length()-1))
320             {
321                 return getStringDelimiterIndex(strToken, delimiter, quoteEndIndex + 1);
322             }
323             else
324             {
325                 return -1;
326             }
327         }
328         if ((hasDelimiter > 0) && (strToken.charAt(hasDelimiter-1) == ESCAPE_CHAR))
329         {
330             return getStringDelimiterIndex(strToken, delimiter, hasDelimiter+1);
331         }
332         else
333         {
334             return hasDelimiter;
335         }
336     }
337
338
339     /**
340      * returns the localized string from the properties file as defined in the
341      * CommandProperties element of CLIDescriptor.xml file
342      * Calls the LocalStringsManagerFactory.getCommandLocalStringsManager()
343      * method, returns "Key not found" if it cannot find the key
344      * @param key, the string to be localized
345      * @return key value
346      */

347     private String JavaDoc getLocalizedString(String JavaDoc key)
348     {
349         LocalStringsManager lsm = null;
350         try
351         {
352             lsm = LocalStringsManagerFactory.getCommandLocalStringsManager();
353         }
354         catch (CommandValidationException cve)
355         {
356             return LocalStringsManager.DEFAULT_STRING_VALUE;
357         }
358         return lsm.getString(key);
359     }
360
361
362     public static void main(String JavaDoc[] args)
363     {
364         try {
365             final CLITokenizer ct = new CLITokenizer(args[0], ":");
366             while (ct.hasMoreTokens()) {
367                 final String JavaDoc nameAndvalue = ct.nextToken();
368                 final CLITokenizer ct2 = new CLITokenizer(nameAndvalue, "=");
369                 System.out.println("+++++ ct2 tokens = " + ct2.countTokens() + " +++++");
370                 if (ct2.countTokens() == 1)
371                 {
372                     System.out.println(ct2.nextTokenWithoutEscapeAndQuoteChars());
373                 }
374                 else if (ct2.countTokens() == 2)
375                 {
376                     System.out.println(ct2.nextTokenWithoutEscapeAndQuoteChars() + " " +
377                                        ct2.nextTokenWithoutEscapeAndQuoteChars());
378                 }
379                 System.out.println("+++++ " + nameAndvalue + " +++++");
380             }
381             System.out.println("***** the end *****");
382         }
383         catch (Exception JavaDoc e) {
384             e.printStackTrace();
385         }
386
387     }
388
389
390
391 }
392
Popular Tags