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