You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2010/09/21 21:11:01 UTC

svn commit: r999550 - in /commons/proper/exec/trunk/src: changes/ main/java/org/apache/commons/exec/ test/java/org/apache/commons/exec/ test/scripts/

Author: sgoeschl
Date: Tue Sep 21 19:11:01 2010
New Revision: 999550

URL: http://svn.apache.org/viewvc?rev=999550&view=rev
Log:
The qouting of command line arguments was previously done  when adding the command line argument. When using command line argument expansion the result argument was therefore not properly quoted. Quoting is now done after expanding the command line arguments.

Modified:
    commons/proper/exec/trunk/src/changes/changes.xml
    commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
    commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
    commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/TutorialTest.java
    commons/proper/exec/trunk/src/test/scripts/acrord32.sh

Modified: commons/proper/exec/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/changes/changes.xml?rev=999550&r1=999549&r2=999550&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/changes/changes.xml (original)
+++ commons/proper/exec/trunk/src/changes/changes.xml Tue Sep 21 19:11:01 2010
@@ -24,6 +24,9 @@
     </properties>
     <body>
         <release version="1.1-SNAPSHOT" date="as in SVN" description="Maintenance Release">
+            <action dev="sgoeschl" type="add" date="2010-09-21" >
+                Adding 'Argument' class and quote the arguments after expansion.                
+            </action>
             <action dev="sgoeschl" type="add" date="2010-09-02" >
                 Reverting changes of [EXEC-41] because the patch does not fix the problem.
                 Also added test case for the broken patch.

Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java?rev=999550&r1=999549&r2=999550&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java (original)
+++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java Tue Sep 21 19:11:01 2010
@@ -146,7 +146,11 @@ public class CommandLine {
         return StringUtils.fixFileSeparatorChar(expandArgument(executable));
     }
 
-    /** @return Was a file being used to set the executable? */
+    /**
+     * Was a file being used to set the executable?
+     *
+     * @return true if a file was used for setting the executable 
+     */
     public boolean isFile(){
         return isFile;
     }
@@ -201,8 +205,8 @@ public class CommandLine {
      */
     public CommandLine addArguments(final String arguments, boolean handleQuoting) {
         if (arguments != null) {
-            String[] argmentsArray = translateCommandline(arguments);
-            addArguments(argmentsArray, handleQuoting);
+            String[] argumentsArray = translateCommandline(arguments);
+            addArguments(argumentsArray, handleQuoting);
         }
 
         return this;
@@ -227,29 +231,41 @@ public class CommandLine {
     * @return The command line itself
     */
    public CommandLine addArgument(final String argument, boolean handleQuoting) {
-        if (argument == null) {
+
+       if (argument == null)
+       {
            return this;
-        }
+       }
 
-        if(handleQuoting) {
-            arguments.add(StringUtils.quoteArgument(argument));
-        }
-        else {
-            arguments.add(argument);
-        }
+       // check if we can really quote the argument - if not throw an
+       // IllegalArgumentException
+       if (handleQuoting)
+       {
+           StringUtils.quoteArgument(argument);
+       }
 
-        return this;
+       arguments.add(new Argument(argument, handleQuoting));
+       return this;
    }
 
     /**
-     * Returns the quoted arguments.
+     * Returns the expanded and quoted command line arguments.
      *  
      * @return The quoted arguments
      */
     public String[] getArguments() {
+
+        Argument currArgument;
+        String expandedArgument;
         String[] result = new String[arguments.size()];
-        result = (String[]) arguments.toArray(result);
-        return this.expandArguments(result);
+
+        for(int i=0; i<result.length; i++) {
+            currArgument = (Argument) arguments.get(i);
+            expandedArgument = expandArgument(currArgument.getValue());
+            result[i] = (currArgument.isHandleQuoting() ? StringUtils.quoteArgument(expandedArgument) : expandedArgument);
+        }
+
+        return result;
     }
 
     /**
@@ -289,26 +305,7 @@ public class CommandLine {
      * @return the command line as single string
      */
     public String toString() {
-        StringBuffer result = new StringBuffer();
-        String[] currArguments = this.getArguments();
-
-        result.append(StringUtils.quoteArgument(this.getExecutable()));
-        result.append(' ');
-
-        for(int i=0; i<currArguments.length; i++) {
-            String currArgument = currArguments[i];
-            if( StringUtils.isQuoted(currArgument)) {
-                result.append(currArgument);
-            }
-            else {
-                result.append(StringUtils.quoteArgument(currArgument));
-            }
-            if(i<currArguments.length-1) {
-                result.append(' ');
-            }
-        }
-        
-        return result.toString().trim();
+        return StringUtils.toString(toStrings(), " ");
     }
 
     // --- Implementation ---------------------------------------------------
@@ -325,21 +322,6 @@ public class CommandLine {
     }
 
     /**
-     * Expand variables in a command line arguments.
-     *
-     * @param arguments the arguments to be expadedn
-     * @return the expanded string
-     */
-    private String[] expandArguments(final String[] arguments) {
-        String[] result = new String[arguments.length];
-        for(int i=0; i<result.length; i++) {
-            result[i] = this.expandArgument(arguments[i]);
-        }
-        return result;
-    }
-
-
-    /**
      * Crack a command line.
      *
      * @param toProcess
@@ -431,4 +413,29 @@ public class CommandLine {
             return StringUtils.fixFileSeparatorChar(executable);
         }
     }
+
+    /**
+     * Encapsulates a command line argument.
+     */
+    class Argument {
+
+        private String value;
+        private boolean handleQuoting;
+
+        private Argument(String value, boolean handleQuoting)
+        {
+            this.value = value.trim();
+            this.handleQuoting = handleQuoting;
+        }
+
+        private String getValue()
+        {
+            return value;
+        }
+
+        private boolean isHandleQuoting()
+        {
+            return handleQuoting;
+        }
+    }
 }

Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java?rev=999550&r1=999549&r2=999550&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java (original)
+++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java Tue Sep 21 19:11:01 2010
@@ -388,7 +388,7 @@ public class CommandLineTest extends Tes
         assertEquals(StringUtils.fixFileSeparatorChar("C:\\Programme\\jdk1.5.0_12\\bin\\java"), result[0]);
         assertEquals("-class", result[1]);
         assertEquals("foo.bar.Main", result[2]);
-        assertEquals("C:\\Document And Settings\\documents\\432431.pdf", result[3]);
+        assertEquals("\"C:\\Document And Settings\\documents\\432431.pdf\"", result[3]);
 
         // verify the first command line again but by
         // accessing the executable and arguments directly
@@ -397,7 +397,7 @@ public class CommandLineTest extends Tes
         assertEquals(StringUtils.fixFileSeparatorChar("C:\\Programme\\jdk1.5.0_12\\bin\\java"), executable);
         assertEquals("-class", arguments[0]);
         assertEquals("foo.bar.Main", arguments[1]);
-        assertEquals("C:\\Document And Settings\\documents\\432431.pdf", arguments[2]);
+        assertEquals("\"C:\\Document And Settings\\documents\\432431.pdf\"", arguments[2]);
 
         // build the second command line with updated parameters resulting in  a different command line
         substitutionMap.put("file", "C:\\Document And Settings\\documents\\432432.pdf");        
@@ -405,14 +405,14 @@ public class CommandLineTest extends Tes
         assertEquals(StringUtils.fixFileSeparatorChar("C:\\Programme\\jdk1.5.0_12\\bin\\java"), result[0]);
         assertEquals("-class", result[1]);
         assertEquals("foo.bar.Main", result[2]);
-        assertEquals("C:\\Document And Settings\\documents\\432432.pdf", result[3]);                
+        assertEquals("\"C:\\Document And Settings\\documents\\432432.pdf\"", result[3]);                
     }
 
     public void testCommandLineParsingWithExpansion3(){
         CommandLine cmdl = CommandLine.parse("AcroRd32.exe");
         cmdl.addArgument("/p");
         cmdl.addArgument("/h");
-        cmdl.addArgument("${file}");
+        cmdl.addArgument("${file}", false);
         HashMap params = new HashMap();
         params.put("file", "C:\\Document And Settings\\documents\\432432.pdf");
         cmdl.setSubstitutionMap(params);

Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/TutorialTest.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/TutorialTest.java?rev=999550&r1=999549&r2=999550&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/TutorialTest.java (original)
+++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/TutorialTest.java Tue Sep 21 19:11:01 2010
@@ -44,7 +44,7 @@ public class TutorialTest extends TestCa
 
         long printJobTimeout = 15000;
         boolean printInBackground = false;
-        File pdfFile = new File("./pom.xml");
+        File pdfFile = new File("/Documents and Settings/foo.pdf");
 
         PrintResultHandler printResult;
 

Modified: commons/proper/exec/trunk/src/test/scripts/acrord32.sh
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/scripts/acrord32.sh?rev=999550&r1=999549&r2=999550&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/test/scripts/acrord32.sh (original)
+++ commons/proper/exec/trunk/src/test/scripts/acrord32.sh Tue Sep 21 19:11:01 2010
@@ -19,7 +19,7 @@
 
 # Simulate printing a PDF document
 
-echo "[acrord32] Printing the following document : '$3'"
+echo "[acrord32] Printing the following document : $3"
 for i in {1..10}
 do
    sleep 1