getQuotedArguments() methods added into VcsUtilities to parse BLD200208080100
authormentlicher@netbeans.org
Wed, 07 Aug 2002 12:17:22 +0000
changeset 2509e6ebd2df5448
parent 2508 e73e6bb538fa
child 2510 c612456d13c7
getQuotedArguments() methods added into VcsUtilities to parse
String[] arguments from a single String. It takes into account quotation
marks (") for arguments with spaces.
ExecuteCommand use this instead of the simple StringTokenizer
to parse arguments for class commands execution.
vcscore/src/org/netbeans/modules/vcscore/cmdline/ExecuteCommand.java
vcscore/src/org/netbeans/modules/vcscore/util/VcsUtilities.java
     1.1 --- a/vcscore/src/org/netbeans/modules/vcscore/cmdline/ExecuteCommand.java	Tue Aug 06 10:48:23 2002 +0000
     1.2 +++ b/vcscore/src/org/netbeans/modules/vcscore/cmdline/ExecuteCommand.java	Wed Aug 07 12:17:22 2002 +0000
     1.3 @@ -502,9 +502,9 @@
     1.4      /**
     1.5       * Loads class of given name with some arguments and execute its list() method.
     1.6       * @param className the name of the class to be loaded
     1.7 -     * @param tokens the arguments
     1.8 +     * @param args the arguments
     1.9       */
    1.10 -    private void runClass(String exec, String className, StringTokenizer tokens) {
    1.11 +    private void runClass(String exec, String className, String[] args) {
    1.12  
    1.13          E.deb("runClass: "+className); // NOI18N
    1.14          boolean success = true;
    1.15 @@ -548,11 +548,6 @@
    1.16          }
    1.17          if (success) {
    1.18              E.deb("VcsAdditionalCommand created."); // NOI18N
    1.19 -            String[] args = new String[tokens.countTokens()];
    1.20 -            int i = 0;
    1.21 -            while(tokens.hasMoreTokens()) {
    1.22 -                args[i++] = tokens.nextToken();
    1.23 -            }
    1.24              ExecuteCommand.setAdditionalParams(execCommand, fileSystem);
    1.25              String dataRegex = (String) cmd.getProperty(UserCommand.PROPERTY_DATA_REGEX);
    1.26              String errorRegex = (String) cmd.getProperty(UserCommand.PROPERTY_ERROR_REGEX);
    1.27 @@ -646,16 +641,19 @@
    1.28              filesToRefresh = new ArrayList(); // Only some files (with unmatched status) should be refreshed.
    1.29          }
    1.30          
    1.31 -        StringTokenizer tokens = new StringTokenizer(exec);
    1.32 -        String first = tokens.nextToken();
    1.33 +        String[] allArgs = VcsUtilities.getQuotedArguments(exec);
    1.34 +        String first = allArgs[0];
    1.35          E.deb("first = "+first); // NOI18N
    1.36          boolean disableRefresh = VcsCommandIO.getBooleanProperty(cmd, VcsCommand.PROPERTY_CHECK_FOR_MODIFICATIONS);
    1.37          if (disableRefresh) fileSystem.disableRefresh();
    1.38          try {
    1.39 -            if (first != null && (first.toLowerCase().endsWith(".class"))) // NOI18N
    1.40 -                runClass(exec, first.substring(0, first.length() - ".class".length()), tokens); // NOI18N
    1.41 -            else
    1.42 +            if (first != null && (first.toLowerCase().endsWith(".class"))) {// NOI18N
    1.43 +                String[] args = new String[allArgs.length - 1];
    1.44 +                System.arraycopy(allArgs, 1, args, 0, args.length);
    1.45 +                runClass(exec, first.substring(0, first.length() - ".class".length()), args); // NOI18N
    1.46 +            } else {
    1.47                  runCommand(execs);
    1.48 +            }
    1.49          } finally {
    1.50              if (disableRefresh) fileSystem.enableRefresh();
    1.51              if (tempFile != null) tempFile.delete();
     2.1 --- a/vcscore/src/org/netbeans/modules/vcscore/util/VcsUtilities.java	Tue Aug 06 10:48:23 2002 +0000
     2.2 +++ b/vcscore/src/org/netbeans/modules/vcscore/util/VcsUtilities.java	Wed Aug 07 12:17:22 2002 +0000
     2.3 @@ -306,6 +306,50 @@
     2.4      }
     2.5      
     2.6      /**
     2.7 +     * Get the quoted string.
     2.8 +     * @return the string inside of quotation marks or null when no string found.
     2.9 +     */
    2.10 +    private static String getQuotedArgument(String str, int[] pos) {
    2.11 +        while(pos[0] < str.length() && Character.isWhitespace(str.charAt(pos[0]))) pos[0]++;
    2.12 +        if (pos[0] >= str.length()) return null;
    2.13 +        StringBuffer result = new StringBuffer();
    2.14 +        if (str.charAt(pos[0]) == '"') { // getting quoted string
    2.15 +            pos[0]++;
    2.16 +            while(pos[0] < str.length()) {
    2.17 +                if (str.charAt(pos[0]) != '"') result.append(str.charAt(pos[0]));
    2.18 +                else if (str.charAt(pos[0] - 1) == '\\') result.setCharAt(result.length() - 1, '"'); // replace '\\' with '"' => \" becomes "
    2.19 +                else break;
    2.20 +                pos[0]++;
    2.21 +            }
    2.22 +        } else { // getting not-quoted string
    2.23 +            while(pos[0] < str.length() && !Character.isWhitespace(str.charAt(pos[0]))) {
    2.24 +                result.append(str.charAt(pos[0]));
    2.25 +                pos[0]++;
    2.26 +            }
    2.27 +        }
    2.28 +        return result.toString();
    2.29 +    }
    2.30 +    
    2.31 +    /**
    2.32 +     * Converts a String of quoted values delimited by spaces or any other white
    2.33 +     * characters to an array of String values.
    2.34 +     * If the values are not quoted, only white characters works as delimeters.
    2.35 +     */
    2.36 +    public static String[] getQuotedArguments(String str) {
    2.37 +        LinkedList list = new LinkedList();
    2.38 +        int[] index = new int[] { 0 };
    2.39 +        String element = VcsUtilities.getQuotedArgument(str, index);
    2.40 +        while(element != null) {
    2.41 +            list.add(element);
    2.42 +            //while(index[0] < str.length() && Character.isWhitespace(str.charAt(index[0]))) index[0]++;
    2.43 +            index[0]++;
    2.44 +            element = VcsUtilities.getQuotedArgument(str, index);
    2.45 +        }
    2.46 +        //String element = str.substring(index, end);
    2.47 +        return (String[]) list.toArray(new String[0]);
    2.48 +    }
    2.49 +    
    2.50 +    /**
    2.51       * Find out, whether some string from the field of quoted strings is contained in a set of strings.
    2.52       * @param quotedStr the field of quoted strings, can be <code>null</code>
    2.53       * @param set the set of strings