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 2008/07/18 22:25:50 UTC
svn commit: r678024 - in /commons/proper/exec/trunk/src:
main/java/org/apache/commons/exec/util/StringUtils.java
test/java/org/apache/commons/exec/CommandLineTest.java
Author: sgoeschl
Date: Fri Jul 18 13:25:50 2008
New Revision: 678024
URL: http://svn.apache.org/viewvc?rev=678024&view=rev
Log:
[EXEC-25] Found another problem and took the time to do some refactoring and documentation fixes.
Modified:
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java?rev=678024&r1=678023&r2=678024&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java (original)
+++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java Fri Jul 18 13:25:50 2008
@@ -28,12 +28,21 @@
/**
* Supplement of commons-lang, the stringSubstitution() was in a simpler
* implementation available in an older commons-lang implementation.
+ *
+ * Furthermore a place to put reusable and/or ugly code.
+ *
* This class is not part of the public API and could change without
* warning.
*
* @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
*/
public class StringUtils {
+
+ private static final String SINGLE_QUOTE = "\'";
+ private static final String DOUBLE_QUOTE = "\"";
+ private static final char SLASH_CHAR = '/';
+ private static final char BACKSLASH_CHAR = '\\';
+
/**
* Perform a series of substitutions. The substitions
* are performed by replacing ${variable} in the target
@@ -89,7 +98,7 @@
} else {
if (isLenient) {
// just append the unresolved variable declaration
- argBuf.append("${" + nameBuf.toString() + "}");
+ argBuf.append("${").append(nameBuf.toString()).append("}");
} else {
// complain that no variable was found
throw new RuntimeException("No value found for : " + nameBuf);
@@ -122,7 +131,8 @@
}
/**
- * Split a string into an array of strings
+ * Split a string into an array of strings based
+ * on a a seperator.
*
* @param input what to split
* @param splitChar what to split on
@@ -134,7 +144,7 @@
while (tokens.hasMoreTokens()) {
strList.add(tokens.nextToken());
}
- return (String[]) strList.toArray(new String[0]);
+ return (String[]) strList.toArray(new String[strList.size()]);
}
/**
@@ -144,12 +154,72 @@
* <ul>
* <li> '/' ==> File.separatorChar
* <li> '\\' ==> File.separatorChar
- * </ul>
+ * </ul>
+ *
+ * @param arg the argument to fix
+ * @return the transformed argument
*/
public static String fixFileSeperatorChar(String arg) {
- return arg.replace('/', File.separatorChar).replace(
- '\\', File.separatorChar);
+ return arg.replace(SLASH_CHAR, File.separatorChar).replace(
+ BACKSLASH_CHAR, File.separatorChar);
+ }
+
+ /**
+ * Concatenates an array of string using a seperator.
+ *
+ * @param strings the strings to concatenate
+ * @param seperator the seperator between two strings
+ * @return the concatened strings
+ */
+ public static String toString(String[] strings, String seperator) {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < strings.length; i++) {
+ if (i > 0) {
+ sb.append(seperator);
+ }
+ sb.append(strings[i]);
+ }
+ return sb.toString();
}
+ /**
+ * Put quotes around the given String if necessary.
+ * <p>
+ * If the argument doesn't include spaces or quotes, return it as is. If it
+ * contains double quotes, use single quotes - else surround the argument by
+ * double quotes.
+ * </p>
+ *
+ * @param argument the argument to be quoted
+ * @return the quoted argument
+ * @throws IllegalArgumentException If argument contains both types of quotes
+ */
+ public static String quoteArgument(final String argument) {
+
+ String cleanedArgument = argument.trim();
+ while(cleanedArgument.startsWith(SINGLE_QUOTE) || cleanedArgument.startsWith(DOUBLE_QUOTE)) {
+ cleanedArgument = cleanedArgument.substring(1);
+ }
+ while(cleanedArgument.endsWith(SINGLE_QUOTE) || cleanedArgument.endsWith(DOUBLE_QUOTE)) {
+ cleanedArgument = cleanedArgument.substring(0, cleanedArgument.length() - 1);
+ }
+
+ final StringBuffer buf = new StringBuffer();
+ if (cleanedArgument.indexOf(DOUBLE_QUOTE) > -1) {
+ if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1) {
+ throw new IllegalArgumentException(
+ "Can't handle single and double quotes in same argument");
+ } else {
+ return buf.append(SINGLE_QUOTE).append(cleanedArgument).append(
+ SINGLE_QUOTE).toString();
+ }
+ } else if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1
+ || cleanedArgument.indexOf(" ") > -1) {
+ return buf.append(DOUBLE_QUOTE).append(cleanedArgument).append(
+ DOUBLE_QUOTE).toString();
+ } else {
+ return cleanedArgument;
+ }
+ }
}
\ No newline at end of file
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=678024&r1=678023&r2=678024&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 Fri Jul 18 13:25:50 2008
@@ -327,11 +327,24 @@
substitutionMap.put("file", "C:\\Document And Settings\\documents\\432431.pdf");
cmdl.setSubstitutionMap(substitutionMap);
result = cmdl.toStrings();
+
+ // verify the first command line
+ // please note - the executable argument is changed to using platform specific file sperator char
+ // whereas all other variable substitution are not touched
assertEquals(StringUtils.fixFileSeperatorChar("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]);
+ // verify the first command line again but by
+ // accessing the executable and arguments directly
+ String executable = cmdl.getExecutable();
+ String[] arguments = cmdl.getArguments();
+ assertEquals(StringUtils.fixFileSeperatorChar("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]);
+
// build the second command line with updated parameters resulting in a different command line
substitutionMap.put("file", "C:\\Document And Settings\\documents\\432432.pdf");
cmdl.setSubstitutionMap(substitutionMap);
@@ -339,6 +352,6 @@
assertEquals(StringUtils.fixFileSeperatorChar("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]);
}
}