View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
17  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
18  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
19  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
20  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
21  import com.eviware.soapui.model.iface.Interface;
22  import com.eviware.soapui.model.project.Project;
23  import com.eviware.soapui.settings.ToolsSettings;
24  import com.eviware.soapui.settings.ToolsSupport;
25  import com.eviware.soapui.support.StringUtils;
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  import java.io.File;
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", XForm.FieldType.PROJECT_FILE );
93  		
94  		mainForm.addCheckBox( SRCONLY, "(Do not compile JAVA files or jar the output)" );
95  		mainForm.addCheckBox( DOWNLOADS, "(Permit network downloads for imports and includes)" );
96  		mainForm.addCheckBox( NOUPA, "(Do not enforce the unique particle attribution rule)" );
97  		mainForm.addCheckBox( NOPVR, "(Do not enforce the particle valid (restriction) rule)" );
98  		mainForm.addCheckBox( NOANN, "(Ignore annotations)" );
99  		mainForm.addCheckBox( NOVDOC, "(Do not validate contents of <documentation> elements)" );
100 		mainForm.addCheckBox( DEBUG, "(Compile with debug symbols)" );
101 
102 		mainForm.addComboBox( JAVASOURCE, new String[] {"1.5", "1.4" }, "Generate Java source compatible for the specified Java version" );
103 		
104 		mainForm.addTextField( ALLOWMDEF, "Ignore multiple defs in given namespaces. Use  ##local  to specify the no-namespace in that list", XForm.FieldType.TEXT );
105 		mainForm.addTextField( CATALOG, "Catalog file to use for resolving external entities", XForm.FieldType.PROJECT_FILE );
106 		mainForm.addTextField( XSDCONFIG, "Path to .xsdconfig file containing type-mapping information", XForm.FieldType.PROJECT_FILE );
107 
108 		mainForm.addCheckBox( VERBOSE, "(Print more informational messages)" );
109 
110       buildArgsForm( builder, false, "scomp");
111       
112 		return builder.buildDialog( buildDefaultActions(HelpUrls.XMLBEANS_HELP_URL, modelItem ),
113       		"Specify arguments for XmlBeans 2.X scomp", UISupport.TOOL_ICON );
114 	}
115 
116 	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem) throws Exception
117 	{
118 		String xbDir = SoapUI.getSettings().getString( ToolsSettings.XMLBEANS_LOCATION, null );
119 		if( Tools.isEmpty( xbDir ))
120 		{
121 			UISupport.showErrorMessage( "XmlBeans location must be set in global preferences" );
122 			return;
123 		}
124 		
125 		ProcessBuilder builder = new ProcessBuilder();
126       ArgumentBuilder argumentBuilder = buildArgs( modelItem );
127       builder.command( argumentBuilder.getArgs() );
128 		builder.directory( new File( xbDir + File.separatorChar + "bin" ));
129 		
130 		toolHost.run( new ProcessToolRunner( builder, "XmlBeans", modelItem ));
131 	}
132 
133 	private ArgumentBuilder buildArgs(Interface modelItem)
134 	{
135 		StringToStringMap values = dialog.getValues();
136 		ArgumentBuilder builder = new ArgumentBuilder( values );
137 		
138 		values.put( XSBTARGET, Tools.ensureDir( values.get( XSBTARGET ), "" ));
139 		values.put( SRCTARGET, Tools.ensureDir( values.get( SRCTARGET ), "" ));
140 		
141 		builder.startScript( "scomp", ".cmd", "" );
142 		
143 		builder.addString( XSBTARGET, "-d" );
144 		builder.addString( SRCTARGET, "-src" );
145 		builder.addString( JARFILE, "-out" );
146 		
147 		builder.addBoolean( SRCONLY, "-srconly" );
148 		builder.addBoolean( DOWNLOADS, "-dl" );
149 		builder.addBoolean( NOUPA, "-noupa" );
150 		builder.addBoolean( NOPVR, "-nopvr" );
151 		builder.addBoolean( NOANN, "-noann" );
152 		builder.addBoolean( NOVDOC, "-novdoc" );
153 		builder.addBoolean( DEBUG, "-debug" );
154 		
155 		builder.addString( JAVASOURCE, "-javasource" );
156 		builder.addString( ALLOWMDEF, "-allowmdef" );
157 		builder.addString( CATALOG, "-catalog" );
158 		builder.addBoolean( VERBOSE, "-verbose" );
159 		
160       String javac = ToolsSupport.getToolLocator().getJavacLocation(false);
161 
162 		if( StringUtils.hasContent( javac ))
163 		{
164 			builder.addArgs( "-compiler", javac + File.separatorChar + "javac" );
165 		}
166 		addToolArgs( values, builder );
167 
168 		builder.addString( XSDCONFIG, null );
169 		builder.addArgs( getWsdlUrl( values, modelItem ) );
170 		
171 		return builder;
172 	}
173 
174 	public void setOutput(String output)
175 	{
176 		this.output = output;
177 	}
178 }