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.xmlbeans;
    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.settings.ToolsSupport;
    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 XMLBeans for given interface
    37   *
    38   * @author Ole.Matzura
    39   */
    40
    41  public class XmlBeans2Action extends AbstractToolsAction<Interface>
    42  {
    43  	private final static String XSBTARGET = "class/xsb target";
    44  	private final static String SRCTARGET = "src target";
    45  	private final static String SRCONLY = "src only";
    46  	private final static String JARFILE = "out jar";
    47  	private final static String DOWNLOADS = "downloads";
    48  	private final static String NOUPA = "noupa";
    49  	private final static String NOPVR = "nopvr";
    50  	private final static String NOANN = "noann";
    51  	private final static String NOVDOC = "novdoc";
    52  	private final static String VERBOSE = "verbose";
    53  	private final static String JAVASOURCE = "javasource";
    54  	private final static String DEBUG = "debug";
    55  	private final static String ALLOWMDEF = "allowmdef";
    56  	private final static String CATALOG = "catalog file";
    57  	private final static String XSDCONFIG = "xsdconfig";
    58  	public static final String SOAPUI_ACTION_ID = "XmlBeans2Action";
    59  	private String output;
    60
    61     public XmlBeans2Action()
    62     {
    63        super( "XmlBeans Classes", "Generates XmlBeans classes");
    64     }
    65
    66     @Override
    67     public boolean applies( Interface target )
    68     {
    69        Interface iface = (Interface) target;
    70        return !iface.getProject().hasNature(Project.JBOSSWS_NATURE_ID);
    71     }
    72
    73  	protected StringToStringMap initValues(Interface modelItem, Object param)
    74  	{
    75  		StringToStringMap values = super.initValues(modelItem, param);
    76  		if( output != null )
    77  			values.put( SRCTARGET, output );
    78
    79  		return values;
    80  	}
    81
    82  	protected XFormDialog buildDialog(Interface modelItem)
    83  	{
    84        XFormDialogBuilder builder = XFormFactory.createDialogBuilder("XmlBeans Classes");
    85
    86  		XForm mainForm = builder.createForm( "Basic" );
    87  		addWSDLFields( mainForm, modelItem  );
    88
    89  		mainForm.addTextField( XSBTARGET, "Target directory for CLASS and XSB files", XForm.FieldType.PROJECT_FOLDER );
    90  		mainForm.addTextField( SRCTARGET, "Target directory for generated JAVA files", XForm.FieldType.PROJECT_FOLDER );
    91  		mainForm.addTextField( JARFILE, "The name of the output JAR that will contain the result of compilation", XForm.FieldType.PROJECT_FILE );
    92
    93  		mainForm.addCheckBox( SRCONLY, "(Do not compile JAVA files or jar the output)" );
    94  		mainForm.addCheckBox( DOWNLOADS, "(Permit network downloads for imports and includes)" );
    95  		mainForm.addCheckBox( NOUPA, "(Do not enforce the unique particle attribution rule)" );
    96  		mainForm.addCheckBox( NOPVR, "(Do not enforce the particle valid (restriction) rule)" );
    97  		mainForm.addCheckBox( NOANN, "(Ignore annotations)" );
    98  		mainForm.addCheckBox( NOVDOC, "(Do not validate contents of <documentation> elements)" );
    99  		mainForm.addCheckBox( DEBUG, "(Compile with debug symbols)" );
    100
    101 		mainForm.addComboBox( JAVASOURCE, new String[] {"1.5", "1.4" }, "Generate Java source compatible for the specified Java version" );
    102
    103 		mainForm.addTextField( ALLOWMDEF, "Ignore multiple defs in given namespaces. Use  ##local  to specify the no-namespace in that list", XForm.FieldType.TEXT );
    104 		mainForm.addTextField( CATALOG, "Catalog file to use for resolving external entities", XForm.FieldType.PROJECT_FILE );
    105 		mainForm.addTextField( XSDCONFIG, "Path to .xsdconfig file containing type-mapping information", XForm.FieldType.PROJECT_FILE );
    106
    107 		mainForm.addCheckBox( VERBOSE, "(Print more informational messages)" );
    108
    109       buildArgsForm( builder, false, "scomp");
    110
    111 		return builder.buildDialog( buildDefaultActions(HelpUrls.XMLBEANS_HELP_URL, modelItem ),
    112       		"Specify arguments for XmlBeans 2.X scomp", UISupport.TOOL_ICON );
    113 	}
    114
    115 	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem) throws Exception
    116 	{
    117 		String xbDir = SoapUI.getSettings().getString( ToolsSettings.XMLBEANS_LOCATION, null );
    118 		if( Tools.isEmpty( xbDir ))
    119 		{
    120 			UISupport.showErrorMessage( "XmlBeans 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( xbDir + File.separatorChar + "bin" ));
    127
    128 		toolHost.run( new ProcessToolRunner( builder, "XmlBeans", modelItem ));
    129 	}
    130
    131 	private ArgumentBuilder buildArgs(Interface modelItem)
    132 	{
    133 		StringToStringMap values = dialog.getValues();
    134 		ArgumentBuilder builder = new ArgumentBuilder( values );
    135
    136 		values.put( XSBTARGET, Tools.ensureDir( values.get( XSBTARGET ), "" ));
    137 		values.put( SRCTARGET, Tools.ensureDir( values.get( SRCTARGET ), "" ));
    138
    139 		builder.startScript( "scomp", ".cmd", "" );
    140
    141 		builder.addString( XSBTARGET, "-d" );
    142 		builder.addString( SRCTARGET, "-src" );
    143 		builder.addString( JARFILE, "-out" );
    144
    145 		builder.addBoolean( SRCONLY, "-srconly" );
    146 		builder.addBoolean( DOWNLOADS, "-dl" );
    147 		builder.addBoolean( NOUPA, "-noupa" );
    148 		builder.addBoolean( NOPVR, "-nopvr" );
    149 		builder.addBoolean( NOANN, "-noann" );
    150 		builder.addBoolean( NOVDOC, "-novdoc" );
    151 		builder.addBoolean( DEBUG, "-debug" );
    152
    153 		builder.addString( JAVASOURCE, "-javasource" );
    154 		builder.addString( ALLOWMDEF, "-allowmdef" );
    155 		builder.addString( CATALOG, "-catalog" );
    156 		builder.addBoolean( VERBOSE, "-verbose" );
    157
    158       String javac = ToolsSupport.getToolLocator().getJavacLocation(false);
    159
    160 		if( javac != null )
    161 		{
    162 			builder.addArgs( "-compiler", javac + File.separatorChar + "javac" );
    163 		}
    164 		addToolArgs( values, builder );
    165
    166 		builder.addString( XSDCONFIG, null );
    167 		builder.addArgs( getWsdlUrl( values, modelItem ) );
    168
    169 		return builder;
    170 	}
    171
    172 	public void setOutput(String output)
    173 	{
    174 		this.output = output;
    175 	}
    176 }