KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > util > ArgumentScanner


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22 package org.aspectj.util;
23
24 import java.util.*;
25
26 public class ArgumentScanner {
27
28     private final static char quote = '"';
29
30     public List scanToList(String JavaDoc str) {
31         str = (str == null ? "" : str.trim()) + " ";
32         List args = new Vector();
33         boolean inQuotes = false;
34         boolean wasInQuotes = false;
35         boolean add = false;
36         int start = 0;
37         int i = -1;
38         try {
39             for (i = start; i < str.length(); i++) {
40                 char c = str.charAt(i);
41                 if (Character.isWhitespace(c) && !inQuotes) {
42                     args.add(str.substring(start,i-(wasInQuotes?1:0)));
43                     start = i+1;
44                 } else if (c == quote && (inQuotes = !(wasInQuotes = inQuotes))) {
45                     start = i+1;
46                 } else if (c == '\\') {
47                     i++;
48                 }
49             }
50         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
51             ioobe.printStackTrace();
52             throw new RuntimeException JavaDoc("at " + i);
53         }
54         if (inQuotes) {
55             throw new RuntimeException JavaDoc("Unmatched quotes");
56         }
57         return args;
58     }
59
60     public String JavaDoc[] scanToArray(String JavaDoc str) {
61         List list = scanToList(str);
62         String JavaDoc[] args = new String JavaDoc[list.size()];
63         for (int i = 0; i < args.length; i++) {
64             args[i] = list.get(i) + "";
65         }
66         return args;
67     }
68
69     static String JavaDoc[] strings = {
70         " a a \"asdf\"",
71     };
72     public static void main(String JavaDoc[] args) {
73         for (int i = 0; i < strings.length; i++) {
74             test(strings[i]);
75         }
76     }
77     static void test(String JavaDoc argsStr) {
78         List newList = new ArgumentScanner().scanToList(argsStr);
79         System.out.println("argsStr=" + argsStr);
80         System.out.println("newList=" + newList);
81     }
82 }
83
Popular Tags