// Copyright (c) 2003-2008 by Leif Frenzel. All rights reserved.
// This code is made available under the terms of the Eclipse Public License,
// version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
package net.sf.eclipsefp.haskell.util;
import java.util.ArrayList;
import java.util.List;
/** <p>parses command line arguments out of one big string into
* a list.</p>
*
* @author Leif Frenzel
*/
public class CommandLineUtil {
private static final char ARG_DELIMITER = ' ';
private static final char ARG_DBL_QUOTE = '"';
/** <p>renders the passed array into one command line string.</p> */
public static String renderCommandLine( final String[] commandLine ) {
StringBuilder buf = new StringBuilder(100);
if(commandLine!=null && commandLine.length > 0 ) {
for (String s:commandLine){
addArg(buf,s);
}
}
return buf.toString();
}
/** <p>renders the passed list into one command line string.</p> */
public static String renderCommandLine( final List<String> commandLine ) {
StringBuilder buf = new StringBuilder(100);
if(commandLine!=null && commandLine.size() > 0 ) {
for (String s:commandLine){
addArg(buf,s);
}
}
return buf.toString();
}
private static void addArg(final StringBuilder sb,final String arg){
if (arg!=null && arg.trim().length()>0){
if (sb.length()>0){
sb.append(' ');
}
sb.append(quoteArg(arg));
}
}
private static String quoteArg(final String arg){
if (arg.indexOf(' ' )>-1 || arg.indexOf( '\t' )>-1 || arg.indexOf( '"' )>-1){
if (arg.charAt( 0 )!='"' && (arg.charAt( arg.length()-1 )!='"')){
StringBuilder sb=new StringBuilder();
sb.append("\""); //$NON-NLS-1$
sb.append(arg.replace( "\"", "\"\"" )); //$NON-NLS-1$ //$NON-NLS-2$
sb.append("\""); //$NON-NLS-1$
return sb.toString();
}
}
return arg;
}
/** <p>parses the argument text into an array of individual arguments using
* the space character as the delimiter.</p>
*
* <p>An individual argument containing spaces must have a double quote (")
* at the start and end. Two double quotes together is taken to mean an
* embedded double quote in the argument text.</p>
*/
public static String[] parse( final String content ) {
String[] result = new String[ 0 ];
if( content != null && content.length() > 0 ) {
List<String> alResult = new ArrayList<>();
boolean inQuotes = false;
int start = 0;
int end = content.length();
StringBuffer buffer = new StringBuffer( end );
while( start < end ) {
char ch = content.charAt( start );
start++;
switch( ch ) {
case ARG_DELIMITER :
if( inQuotes ) {
buffer.append( ch );
} else {
if( buffer.length() > 0 ) {
alResult.add( buffer.toString() );
buffer.setLength( 0 );
}
}
break;
case ARG_DBL_QUOTE :
if( start < end ) {
if( content.charAt( start ) == ARG_DBL_QUOTE ) {
// Two quotes together represents one quote
buffer.append( ch );
start++;
} else {
inQuotes = !inQuotes;
}
} else {
// A lone quote at the end, just drop it.
inQuotes = false;
}
break;
default :
buffer.append( ch );
break;
}
}
if( buffer.length() > 0 ) {
alResult.add( buffer.toString() );
}
result = new String[ alResult.size() ];
alResult.toArray( result );
}
return result;
}
}