001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.actions.iface.tools.jaxb;
014:
015: import java.io.File;
016:
017: import com.eviware.soapui.SoapUI;
018: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
019: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
020: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
021: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
022: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
023: import com.eviware.soapui.model.ModelItem;
024: import com.eviware.soapui.model.iface.Interface;
025: import com.eviware.soapui.model.project.Project;
026: import com.eviware.soapui.settings.ToolsSettings;
027: import com.eviware.soapui.support.Tools;
028: import com.eviware.soapui.support.UISupport;
029: import com.eviware.soapui.support.types.StringToStringMap;
030: import com.eviware.x.form.XForm;
031: import com.eviware.x.form.XFormDialog;
032: import com.eviware.x.form.XFormDialogBuilder;
033: import com.eviware.x.form.XFormFactory;
034:
035: /**
036: * Generates JAXB classes for given interface
037: *
038: * @author Ole.Matzura
039: */
040:
041: public class JaxbXjcAction extends AbstractToolsAction<Interface> {
042: private final static String PACKAGE = "package";
043: private final static String OUTPUT = "output";
044: private final static String NOVALIDATION = "no validation";
045: private final static String BINDINGS = "binding files";
046: private final static String CLASSPATH = "classpath";
047: private final static String CATALOG = "catalog";
048: private final static String HTTPPROXY = "http proxy";
049: private final static String READONLY = "read-only";
050: private final static String NPA = "npa";
051: private final static String VERBOSE = "verbose";
052: public static final String SOAPUI_ACTION_ID = "JaxbXjcAction";
053:
054: // Configure the behavior of this action:
055: private String output = null;
056:
057: public JaxbXjcAction() {
058: super ("JAXB 2.0 Artifacts", "Generates JAXB artifacts");
059: }
060:
061: @Override
062: public boolean applies(ModelItem target) {
063: Interface iface = (Interface) target;
064: return !iface.getProject().hasNature(Project.JBOSSWS_NATURE_ID);
065: }
066:
067: /**
068: * Set this to predefine the output directory instead of letting the user select.
069: */
070: public void setOutput(String output) {
071: this .output = output;
072: }
073:
074: protected StringToStringMap initValues(Interface modelItem) {
075: StringToStringMap values = super .initValues(modelItem);
076:
077: if (output != null)
078: values.put(OUTPUT, output);
079:
080: return values;
081: }
082:
083: protected XFormDialog buildDialog(Interface modelItem) {
084: XFormDialogBuilder builder = XFormFactory
085: .createDialogBuilder("JAXB Artifacts");
086:
087: XForm mainForm = builder.createForm("Basic");
088: addWSDLFields(mainForm, modelItem);
089:
090: mainForm.addTextField(OUTPUT,
091: "generated files will go into this directory",
092: XForm.FieldType.PROJECT_FOLDER);
093: mainForm.addTextField(PACKAGE, "the target package",
094: XForm.FieldType.JAVA_PACKAGE);
095:
096: mainForm.addTextField(BINDINGS,
097: "external bindings file(s), comma-separated",
098: XForm.FieldType.PROJECT_FILE);
099: mainForm.addTextField(CATALOG,
100: "catalog files to resolve external entity references",
101: XForm.FieldType.PROJECT_FILE);
102: mainForm.addTextField(CLASSPATH,
103: "where to find user class files",
104: XForm.FieldType.PROJECT_FOLDER);
105:
106: mainForm
107: .addTextField(
108: HTTPPROXY,
109: "set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost[:proxyPort]",
110: XForm.FieldType.TEXT);
111: mainForm.addCheckBox(READONLY,
112: "(generated files will be in read-only mode)");
113: mainForm
114: .addCheckBox(NOVALIDATION,
115: "(do not perform strict validation of the input schema(s))");
116: mainForm
117: .addCheckBox(NPA,
118: "(suppress generation of package level annotations (**/package-info.java))");
119:
120: mainForm.addCheckBox(VERBOSE, "(be extra verbose)");
121:
122: buildArgsForm(builder, false, "xjc");
123:
124: return builder.buildDialog(buildDefaultActions(
125: HelpUrls.JABXJC_HELP_URL, modelItem),
126: "Specify arguments for the JAXB 2 xjc compiler",
127: UISupport.TOOL_ICON);
128: }
129:
130: protected void generate(StringToStringMap values,
131: ToolHost toolHost, Interface modelItem) throws Exception {
132: String jaxbDir = SoapUI.getSettings().getString(
133: ToolsSettings.JAXB_LOCATION, null);
134: if (Tools.isEmpty(jaxbDir)) {
135: UISupport
136: .showErrorMessage("JAXB location must be set in global preferences");
137: return;
138: }
139:
140: ProcessBuilder builder = new ProcessBuilder();
141: builder.command(buildArgs(modelItem).getArgs());
142: builder
143: .directory(new File(jaxbDir + File.separatorChar
144: + "bin"));
145:
146: toolHost.run(new ProcessToolRunner(builder, "JAXB xjc",
147: modelItem));
148: }
149:
150: private ArgumentBuilder buildArgs(Interface modelItem) {
151: StringToStringMap values = dialog.getValues();
152: ArgumentBuilder builder = new ArgumentBuilder(values);
153:
154: builder.startScript("xjc", ".bat", ".sh");
155:
156: String outputValue = (output != null ? output : values
157: .get(OUTPUT));
158: values.put(OUTPUT, Tools.ensureDir(outputValue, ""));
159:
160: builder.addString(OUTPUT, "-d");
161: builder.addString(PACKAGE, "-p");
162: builder.addString(CLASSPATH, "-classpath");
163: builder.addString(CATALOG, "-catalog");
164: builder.addString(HTTPPROXY, "-httpproxy ");
165:
166: builder.addBoolean(NOVALIDATION, "-nv");
167: builder.addBoolean(NPA, "-npa");
168: builder.addBoolean(READONLY, "-readOnly");
169: builder.addBoolean(VERBOSE, "-verbose");
170:
171: addToolArgs(values, builder);
172:
173: builder.addArgs("-wsdl", getWsdlUrl(values, modelItem));
174:
175: String[] bindings = values.get(BINDINGS).split(",");
176: for (String binding : bindings) {
177: if (binding.trim().length() > 0)
178: builder.addArgs("-b", binding.trim());
179: }
180:
181: return builder;
182: }
183: }
|