View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.impl.wsdl.actions.iface.tools.support;
    14
    15  import java.io.File;
    16  import java.util.ArrayList;
    17  import java.util.List;
    18
    19  import com.eviware.soapui.support.UISupport;
    20  import com.eviware.soapui.support.types.StringToStringMap;
    21
    22  /***
    23   * Utility for build commandline arguments from dialog input
    24   *
    25   * @author ole.matzura
    26   */
    27
    28  public class ArgumentBuilder
    29  {
    30  	private final StringToStringMap values;
    31  	private List<String> args = new ArrayList<String>();
    32  	private boolean isUnix;
    33
    34  	public ArgumentBuilder( StringToStringMap values )
    35  	{
    36  		this.values = values;
    37  	}
    38
    39  	public List<String> getArgs()
    40  	{
    41  		if( isUnix )
    42  		{
    43  			// sh -c requires all args in one string..
    44  			StringBuffer buf = new StringBuffer();
    45  			for( int c = 2; c < args.size(); c++ )
    46  			{
    47  				if( c > 2 )
    48  					buf.append( ' ' );
    49  				buf.append( escapeUnixArg( args.get( c )));
    50  			}
    51
    52  			ArrayList<String> result = new ArrayList<String>();
    53  			result.add( args.get( 0 ));
    54  			result.add( args.get( 1 ));
    55  			result.add( buf.toString() );
    56
    57  			return result;
    58  		}
    59  		else
    60  		{
    61  			return new ArrayList<String>( args );
    62  		}
    63  	}
    64
    65  	private String escapeUnixArg( String str )
    66  	{
    67  		StringBuffer buf = new StringBuffer();
    68  		int spaceIndex = str.indexOf( ' ' );
    69  		if( spaceIndex >= 0 )
    70  		{
    71  			buf.append( "//'" );
    72  		}
    73
    74  		for( int c = 0; c < str.length(); c++ )
    75  		{
    76  			char ch = str.charAt( c );
    77  			switch( ch )
    78  			{
    79  				case '\'' : buf.append( "//'" ); break;
    80  				case '//' : buf.append( "////" ); break;
    81  				default : buf.append( ch );
    82  			}
    83  		}
    84
    85  		if( spaceIndex >= 0 )
    86  		{
    87  			buf.append( "//'" );
    88  		}
    89
    90  		return buf.toString();
    91  	}
    92
    93  	public boolean addString(String name, String arg)
    94  	{
    95  		if( !values.containsKey( name ))
    96  			return false;
    97
    98  		String value = values.get( name ).toString();
    99  		if( value == null || value.length() == 0 )
    100 			return false;
    101
    102 		if( arg != null )
    103 			args.add( arg );
    104
    105 		args.add( value);
    106
    107 		return true;
    108 	}
    109
    110 	public ArgumentBuilder addArgs( String... args )
    111 	{
    112 		for( int c = 0; c < args.length; c++ )
    113 			this.args.add( args[c] );
    114
    115 		return this;
    116 	}
    117
    118 	public boolean addBoolean( String name, String arg )
    119 	{
    120 		if( values.containsKey( name ) && Boolean.valueOf( values.get( name ).toString() ))
    121 		{
    122 			args.add( arg );
    123 			return true;
    124 		}
    125
    126 		return false;
    127 	}
    128
    129 	public String toString()
    130 	{
    131 		StringBuffer buf = new StringBuffer();
    132 		for( int c = 0; c < args.size(); c++ )
    133 		{
    134 			if( c > 0 )
    135 				buf.append( ' ' );
    136
    137 			buf.append( args.get( c ));
    138 		}
    139
    140 		return buf.toString();
    141 	}
    142
    143 	public ArgumentBuilder startScript(String script)
    144 	{
    145 		return startScript( script, ".bat", ".sh" );
    146 	}
    147
    148 	public boolean addString(String name, String arg, String separator)
    149 	{
    150 		if( !values.containsKey( name ))
    151 			return false;
    152
    153 		String value = values.get( name ).toString();
    154 		if( value == null || value.length() == 0 )
    155 			return false;
    156
    157 		args.add( arg + separator + value );
    158
    159 		return true;
    160 	}
    161
    162 	public boolean addBoolean(String name, String arg, String trueValue, String falseValue )
    163 	{
    164 		if( !values.containsKey( name ))
    165 			return false;
    166
    167 		args.add( arg );
    168
    169 		if( Boolean.valueOf( values.get( name ).toString() ))
    170 		{
    171 			args.add( trueValue );
    172 			return true;
    173 		}
    174 		else
    175 		{
    176 			args.add( falseValue );
    177 			return false;
    178 		}
    179 	}
    180
    181 	public ArgumentBuilder startScript(String script, String windowsExt, String unixExt )
    182 	{
    183 		if( UISupport.isWindows() && windowsExt != null )
    184 		{
    185 			addArgs( "cmd.exe", "/C", script + windowsExt );
    186 		}
    187 		else
    188 		{
    189 			isUnix = true;
    190
    191 			if( !script.startsWith( "." ) && !script.startsWith( File.separator ))
    192 				script = "./" + script;
    193
    194 			addArgs( "sh", "-c", script + unixExt );
    195 		}
    196
    197 		return this;
    198 	}
    199
    200 	public String[] getStringArgs()
    201 	{
    202 		return args.toArray( new String[args.size()] );
    203 	}
    204 }