001: package org.objectweb.celtix.tools;
002:
003: import java.io.File;
004: import java.util.HashSet;
005: import java.util.Set;
006:
007: import org.objectweb.celtix.common.i18n.Message;
008: import org.objectweb.celtix.tools.common.ProcessorEnvironment;
009: import org.objectweb.celtix.tools.common.ToolConstants;
010: import org.objectweb.celtix.tools.common.ToolException;
011: import org.objectweb.celtix.tools.common.toolspec.ToolRunner;
012: import org.objectweb.celtix.tools.common.toolspec.ToolSpec;
013: import org.objectweb.celtix.tools.common.toolspec.parser.BadUsageException;
014: import org.objectweb.celtix.tools.common.toolspec.parser.CommandDocument;
015: import org.objectweb.celtix.tools.common.toolspec.parser.ErrorVisitor;
016: import org.objectweb.celtix.tools.processors.wsdl2.WSDLToSoapProcessor;
017:
018: public class WSDLToSoap extends AbstractCeltixToolContainer {
019:
020: static final String TOOL_NAME = "wsdl2soap";
021: static final String BINDING_NAME_POSFIX = "_Binding";
022: static final String STYLE_DEF_VALUE = "document";
023: static final String USE_DEF_VALUE = "literal";
024:
025: private static String[] args;
026:
027: public WSDLToSoap(ToolSpec toolspec) throws Exception {
028: super (TOOL_NAME, toolspec);
029: }
030:
031: private Set getArrayKeys() {
032: return new HashSet<String>();
033: }
034:
035: public void execute(boolean exitOnFinish) {
036: WSDLToSoapProcessor processor = new WSDLToSoapProcessor();
037: try {
038: super .execute(exitOnFinish);
039: if (!hasInfoOption()) {
040: ProcessorEnvironment env = new ProcessorEnvironment();
041: env.setParameters(getParametersMap(getArrayKeys()));
042: if (isVerboseOn()) {
043: env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
044: }
045: env.put(ToolConstants.CFG_CMD_ARG, args);
046:
047: validate(env);
048: setEnvParamDefValues(env);
049:
050: processor.setEnvironment(env);
051: processor.process();
052: }
053: } catch (ToolException ex) {
054: System.err.println("Error : " + ex.getMessage());
055: if (ex.getCause() instanceof BadUsageException) {
056: getInstance().printUsageException(TOOL_NAME,
057: (BadUsageException) ex.getCause());
058: }
059: System.err.println();
060: if (isVerboseOn()) {
061: ex.printStackTrace();
062: }
063: } catch (Exception ex) {
064: System.err.println("Error : " + ex.getMessage());
065: System.err.println();
066: if (isVerboseOn()) {
067: ex.printStackTrace();
068: }
069: }
070: }
071:
072: private void setEnvParamDefValues(ProcessorEnvironment env) {
073: if (!env.optionSet(ToolConstants.CFG_BINDING)) {
074: env.put(ToolConstants.CFG_BINDING, env
075: .get(ToolConstants.CFG_PORTTYPE)
076: + BINDING_NAME_POSFIX);
077: }
078: if (!env.optionSet(ToolConstants.CFG_STYLE)) {
079: env.put(ToolConstants.CFG_STYLE, STYLE_DEF_VALUE);
080: }
081: if (!env.optionSet(ToolConstants.CFG_USE)) {
082: env.put(ToolConstants.CFG_USE, USE_DEF_VALUE);
083: }
084: }
085:
086: private void validate(ProcessorEnvironment env)
087: throws ToolException {
088: String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
089: if (outdir != null) {
090: File dir = new File(outdir);
091: if (!dir.exists()) {
092: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
093: outdir);
094: throw new ToolException(msg);
095: }
096: if (!dir.isDirectory()) {
097: Message msg = new Message("NOT_A_DIRECTORY", LOG,
098: outdir);
099: throw new ToolException(msg);
100: }
101: }
102: }
103:
104: public static void main(String[] pargs) {
105: args = pargs;
106: try {
107: ToolRunner.runTool(WSDLToSoap.class, WSDLToSoap.class
108: .getResourceAsStream(ToolConstants.TOOLSPECS_BASE
109: + "wsdl2soap.xml"), false, args);
110: } catch (BadUsageException ex) {
111: getInstance().printUsageException(TOOL_NAME, ex);
112: } catch (Exception ex) {
113: System.err.println("Error : " + ex.getMessage());
114: System.err.println();
115: ex.printStackTrace();
116: }
117: }
118:
119: public void checkParams(ErrorVisitor errors) throws ToolException {
120: CommandDocument doc = super .getCommandDocument();
121:
122: if (!doc.hasParameter("wsdlurl")) {
123: errors.add(new ErrorVisitor.UserError(
124: "WSDL/SCHEMA URL has to be specified"));
125: }
126: if (errors.getErrors().size() > 0) {
127: Message msg = new Message("PARAMETER_MISSING", LOG);
128: throw new ToolException(msg, new BadUsageException(
129: getUsage(), errors));
130: }
131: }
132:
133: }
|