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.jaxb;
    14
    15  import java.io.File;
    16
    17  import com.eviware.soapui.SoapUI;
    18  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
    19  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
    20  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
    21  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
    22  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    23  import com.eviware.soapui.model.iface.Interface;
    24  import com.eviware.soapui.model.project.Project;
    25  import com.eviware.soapui.settings.ToolsSettings;
    26  import com.eviware.soapui.support.Tools;
    27  import com.eviware.soapui.support.UISupport;
    28  import com.eviware.soapui.support.types.StringToStringMap;
    29  import com.eviware.x.form.XForm;
    30  import com.eviware.x.form.XFormDialog;
    31  import com.eviware.x.form.XFormDialogBuilder;
    32  import com.eviware.x.form.XFormFactory;
    33
    34  /***
    35   * Generates JAXB classes for given interface
    36   *
    37   * @author Ole.Matzura
    38   */
    39
    40  public class JaxbXjcAction extends AbstractToolsAction<Interface>
    41  {
    42  	private final static String PACKAGE = "package";
    43  	private final static String OUTPUT = "output";
    44  	private final static String NOVALIDATION = "no validation";
    45  	private final static String BINDINGS = "binding files";
    46  	private final static String CLASSPATH = "classpath";
    47  	private final static String CATALOG = "catalog";
    48  	private final static String HTTPPROXY = "http proxy";
    49  	private final static String READONLY = "read-only";
    50  	private final static String NPA = "npa";
    51  	private final static String VERBOSE = "verbose";
    52  	public static final String SOAPUI_ACTION_ID = "JaxbXjcAction";
    53
    54     // Configure the behavior of this action:
    55     private String output = null;
    56
    57     public JaxbXjcAction()
    58     {
    59        super("JAXB 2.0 Artifacts", "Generates JAXB artifacts");
    60     }
    61
    62     @Override
    63     public boolean applies( Interface target )
    64     {
    65        Interface iface = (Interface) target;
    66        return !iface.getProject().hasNature(Project.JBOSSWS_NATURE_ID);
    67     }
    68
    69     /***
    70      * Set this to predefine the output directory instead of letting the user select.
    71      */
    72     public void setOutput(String output)
    73     {
    74        this.output = output;
    75     }
    76
    77  	protected StringToStringMap initValues(Interface modelItem, Object param )
    78  	{
    79  		StringToStringMap values = super.initValues(modelItem, param);
    80
    81  		if( output != null )
    82  			values.put( OUTPUT, output );
    83
    84  		return values;
    85  	}
    86
    87  	protected XFormDialog buildDialog(Interface modelItem )
    88  	{
    89        XFormDialogBuilder builder = XFormFactory.createDialogBuilder("JAXB Artifacts");
    90
    91        XForm mainForm = builder.createForm( "Basic" );
    92        addWSDLFields( mainForm, modelItem );
    93
    94        mainForm.addTextField( OUTPUT, "generated files will go into this directory", XForm.FieldType.PROJECT_FOLDER );
    95  		mainForm.addTextField( PACKAGE, "the target package", XForm.FieldType.JAVA_PACKAGE );
    96
    97  		mainForm.addTextField( BINDINGS, "external bindings file(s), comma-separated", XForm.FieldType.PROJECT_FILE );
    98  		mainForm.addTextField( CATALOG, "catalog files to resolve external entity references", XForm.FieldType.PROJECT_FILE );
    99  		mainForm.addTextField( CLASSPATH, "where to find user class files", XForm.FieldType.PROJECT_FOLDER );
    100
    101 		mainForm.addTextField( HTTPPROXY, "set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost[:proxyPort]", XForm.FieldType.TEXT );
    102 		mainForm.addCheckBox( READONLY, "(generated files will be in read-only mode)" );
    103 		mainForm.addCheckBox( NOVALIDATION, "(do not perform strict validation of the input schema(s))" );
    104 		mainForm.addCheckBox( NPA, "(suppress generation of package level annotations (**/package-info.java))" );
    105
    106 		mainForm.addCheckBox( VERBOSE, "(be extra verbose)" );
    107
    108       buildArgsForm( builder, false, "xjc");
    109
    110 		return builder.buildDialog( buildDefaultActions( HelpUrls.JABXJC_HELP_URL, modelItem ),
    111       		"Specify arguments for the JAXB 2 xjc compiler", UISupport.TOOL_ICON );
    112 	}
    113
    114 	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
    115 	{
    116 		String jaxbDir = SoapUI.getSettings().getString( ToolsSettings.JAXB_LOCATION, null );
    117 		if( Tools.isEmpty( jaxbDir ))
    118 		{
    119 			UISupport.showErrorMessage( "JAXB location must be set in global preferences" );
    120 			return;
    121 		}
    122
    123 		ProcessBuilder builder = new ProcessBuilder();
    124 		builder.command( buildArgs( modelItem ).getArgs() );
    125 		builder.directory( new File( jaxbDir + File.separatorChar + "bin" ));
    126
    127 		toolHost.run( new ProcessToolRunner( builder, "JAXB xjc", modelItem ));
    128 	}
    129
    130 	private ArgumentBuilder buildArgs(Interface modelItem )
    131 	{
    132 		StringToStringMap values = dialog.getValues();
    133 		ArgumentBuilder builder = new ArgumentBuilder( values );
    134
    135 		builder.startScript( "xjc", ".bat", ".sh" );
    136
    137       String outputValue = (output != null ? output : values.get(OUTPUT));
    138       values.put( OUTPUT, Tools.ensureDir( outputValue, "" ));
    139
    140 		builder.addString( OUTPUT, "-d" );
    141 		builder.addString( PACKAGE, "-p" );
    142 		builder.addString( CLASSPATH, "-classpath" );
    143 		builder.addString( CATALOG, "-catalog" );
    144 		builder.addString( HTTPPROXY, "-httpproxy " );
    145
    146 		builder.addBoolean( NOVALIDATION, "-nv" );
    147 		builder.addBoolean( NPA, "-npa" );
    148 		builder.addBoolean( READONLY, "-readOnly" );
    149 		builder.addBoolean( VERBOSE, "-verbose" );
    150
    151 		addToolArgs( values, builder );
    152
    153 		builder.addArgs( "-wsdl", getWsdlUrl( values, modelItem  ) );
    154
    155 		String[] bindings = values.get( BINDINGS ).split( "," );
    156 		for( String binding : bindings )
    157 		{
    158 			if( binding.trim().length() > 0 )
    159 				builder.addArgs( "-b", binding.trim() );
    160 		}
    161
    162 		return builder;
    163 	}
    164 }