diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/FileUtils.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/FileUtils.java index 5d66b2d1a..cca54dad4 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/util/FileUtils.java @@ -17,6 +17,7 @@ import java.io.FileWriter; import java.io.IOException; import java.util.Stack; import java.util.StringTokenizer; +import java.util.ArrayList; import org.apache.myrmidon.api.TaskException; import org.apache.tools.ant.types.FilterSetCollection; @@ -245,5 +246,121 @@ public class FileUtils } return new File( path ); } + + /** + * Put quotes around the given String if necessary.

+ * + * 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.

+ * + * @param argument Description of Parameter + * @return Description of the Returned Value + */ + public static String quoteArgument( String argument ) + throws TaskException + { + if( argument.indexOf( "\"" ) > -1 ) + { + if( argument.indexOf( "\'" ) > -1 ) + { + throw new TaskException( "Can\'t handle single and double quotes in same argument" ); + } + else + { + return '\'' + argument + '\''; + } + } + else if( argument.indexOf( "\'" ) > -1 || argument.indexOf( " " ) > -1 ) + { + return '\"' + argument + '\"'; + } + else + { + return argument; + } + } + + public static String[] translateCommandline( String to_process ) + throws TaskException + { + if( to_process == null || to_process.length() == 0 ) + { + return new String[ 0 ]; + } + + // parse with a simple finite state machine + + final int normal = 0; + final int inQuote = 1; + final int inDoubleQuote = 2; + int state = normal; + StringTokenizer tok = new StringTokenizer( to_process, "\"\' ", true ); + ArrayList v = new ArrayList(); + StringBuffer current = new StringBuffer(); + + while( tok.hasMoreTokens() ) + { + String nextTok = tok.nextToken(); + switch( state ) + { + case inQuote: + if( "\'".equals( nextTok ) ) + { + state = normal; + } + else + { + current.append( nextTok ); + } + break; + case inDoubleQuote: + if( "\"".equals( nextTok ) ) + { + state = normal; + } + else + { + current.append( nextTok ); + } + break; + default: + if( "\'".equals( nextTok ) ) + { + state = inQuote; + } + else if( "\"".equals( nextTok ) ) + { + state = inDoubleQuote; + } + else if( " ".equals( nextTok ) ) + { + if( current.length() != 0 ) + { + v.add( current.toString() ); + current.setLength( 0 ); + } + } + else + { + current.append( nextTok ); + } + break; + } + } + + if( current.length() != 0 ) + { + v.add( current.toString() ); + } + + if( state == inQuote || state == inDoubleQuote ) + { + throw new TaskException( "unbalanced quotes in " + to_process ); + } + + final String[] args = new String[ v.size() ]; + return (String[])v.toArray( args ); + } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/FileUtils.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/FileUtils.java index 5d66b2d1a..cca54dad4 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/FileUtils.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/FileUtils.java @@ -17,6 +17,7 @@ import java.io.FileWriter; import java.io.IOException; import java.util.Stack; import java.util.StringTokenizer; +import java.util.ArrayList; import org.apache.myrmidon.api.TaskException; import org.apache.tools.ant.types.FilterSetCollection; @@ -245,5 +246,121 @@ public class FileUtils } return new File( path ); } + + /** + * Put quotes around the given String if necessary.

+ * + * 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.

+ * + * @param argument Description of Parameter + * @return Description of the Returned Value + */ + public static String quoteArgument( String argument ) + throws TaskException + { + if( argument.indexOf( "\"" ) > -1 ) + { + if( argument.indexOf( "\'" ) > -1 ) + { + throw new TaskException( "Can\'t handle single and double quotes in same argument" ); + } + else + { + return '\'' + argument + '\''; + } + } + else if( argument.indexOf( "\'" ) > -1 || argument.indexOf( " " ) > -1 ) + { + return '\"' + argument + '\"'; + } + else + { + return argument; + } + } + + public static String[] translateCommandline( String to_process ) + throws TaskException + { + if( to_process == null || to_process.length() == 0 ) + { + return new String[ 0 ]; + } + + // parse with a simple finite state machine + + final int normal = 0; + final int inQuote = 1; + final int inDoubleQuote = 2; + int state = normal; + StringTokenizer tok = new StringTokenizer( to_process, "\"\' ", true ); + ArrayList v = new ArrayList(); + StringBuffer current = new StringBuffer(); + + while( tok.hasMoreTokens() ) + { + String nextTok = tok.nextToken(); + switch( state ) + { + case inQuote: + if( "\'".equals( nextTok ) ) + { + state = normal; + } + else + { + current.append( nextTok ); + } + break; + case inDoubleQuote: + if( "\"".equals( nextTok ) ) + { + state = normal; + } + else + { + current.append( nextTok ); + } + break; + default: + if( "\'".equals( nextTok ) ) + { + state = inQuote; + } + else if( "\"".equals( nextTok ) ) + { + state = inDoubleQuote; + } + else if( " ".equals( nextTok ) ) + { + if( current.length() != 0 ) + { + v.add( current.toString() ); + current.setLength( 0 ); + } + } + else + { + current.append( nextTok ); + } + break; + } + } + + if( current.length() != 0 ) + { + v.add( current.toString() ); + } + + if( state == inQuote || state == inDoubleQuote ) + { + throw new TaskException( "unbalanced quotes in " + to_process ); + } + + final String[] args = new String[ v.size() ]; + return (String[])v.toArray( args ); + } }