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.WSDLToXMLProcessor;
017:
018: public class WSDLToXML extends AbstractCeltixToolContainer {
019:
020: static final String TOOL_NAME = "wsdl2xml";
021: static final String BINDING_NAME_POSFIX = "_XMLBinding";
022: static final String SERVICE_NAME_POSFIX = "_XMLService";
023: static final String PORT_NAME_POSFIX = "_XMLPort";
024:
025: private static String[] args;
026:
027: public WSDLToXML(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: WSDLToXMLProcessor processor = new WSDLToXMLProcessor();
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_SERVICE)) {
079: env.put(ToolConstants.CFG_SERVICE, env
080: .get(ToolConstants.CFG_PORTTYPE)
081: + SERVICE_NAME_POSFIX);
082: }
083: if (!env.optionSet(ToolConstants.CFG_PORT)) {
084: env.put(ToolConstants.CFG_PORT, env
085: .get(ToolConstants.CFG_PORTTYPE)
086: + PORT_NAME_POSFIX);
087: }
088: }
089:
090: private void validate(ProcessorEnvironment env)
091: throws ToolException {
092: String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
093: if (outdir != null) {
094: File dir = new File(outdir);
095: if (!dir.exists()) {
096: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
097: outdir);
098: throw new ToolException(msg);
099: }
100: if (!dir.isDirectory()) {
101: Message msg = new Message("NOT_A_DIRECTORY", LOG,
102: outdir);
103: throw new ToolException(msg);
104: }
105: }
106: }
107:
108: public static void main(String[] pargs) {
109: args = pargs;
110: try {
111: ToolRunner.runTool(WSDLToXML.class, WSDLToXML.class
112: .getResourceAsStream(ToolConstants.TOOLSPECS_BASE
113: + "wsdl2xml.xml"), false, args);
114: } catch (BadUsageException ex) {
115: getInstance().printUsageException(TOOL_NAME, ex);
116: } catch (Exception ex) {
117: System.err.println("Error : " + ex.getMessage());
118: System.err.println();
119: ex.printStackTrace();
120: }
121: }
122:
123: public void checkParams(ErrorVisitor errors) throws ToolException {
124: CommandDocument doc = super .getCommandDocument();
125:
126: if (!doc.hasParameter("wsdlurl")) {
127: errors.add(new ErrorVisitor.UserError(
128: "WSDL/SCHEMA URL has to be specified"));
129: }
130: if (errors.getErrors().size() > 0) {
131: Message msg = new Message("PARAMETER_MISSING", LOG);
132: throw new ToolException(msg, new BadUsageException(
133: getUsage(), errors));
134: }
135: }
136:
137: }
|