View Javadoc

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