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