View Javadoc

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