/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.yoko.tools;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import org.apache.cxf.common.i18n.Message;
import org.apache.cxf.tools.common.AbstractCXFToolContainer;
import org.apache.cxf.tools.common.ToolConstants;
import org.apache.cxf.tools.common.ToolException;
import org.apache.cxf.tools.common.toolspec.ToolRunner;
import org.apache.cxf.tools.common.toolspec.ToolSpec;
import org.apache.cxf.tools.common.toolspec.parser.BadUsageException;
import org.apache.cxf.tools.common.toolspec.parser.CommandDocument;
import org.apache.cxf.tools.common.toolspec.parser.ErrorVisitor;
import org.apache.yoko.tools.common.ProcessorEnvironment;
import org.apache.yoko.tools.common.ToolCorbaConstants;
import org.apache.yoko.tools.processors.idl.IDLToWSDLProcessor;
/**
* This class can converts an IDL to a WSDL with CORBA binding information
*/
public class IDLToWSDL extends AbstractCXFToolContainer {
static final String TOOL_NAME = "idltowsdl";
private static String[] args;
public IDLToWSDL(ToolSpec toolspec) throws Exception {
super(TOOL_NAME, toolspec);
}
private Set getArrayKeys() {
return new HashSet<String>();
}
public void execute(boolean exitOnFinish) {
IDLToWSDLProcessor idlProcessor = new IDLToWSDLProcessor();
ProcessorEnvironment env = null;
try {
super.execute(exitOnFinish);
if (!hasInfoOption()) {
env = new ProcessorEnvironment();
env.setParameters(getParametersMap(getArrayKeys()));
if (isVerboseOn()) {
env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
}
env.put(ToolConstants.CFG_CMD_ARG, args);
initialise(env);
validate(env);
idlProcessor.setEnvironment(env);
idlProcessor.process();
}
} catch (ToolException ex) {
System.err.println("Error : " + ex.getMessage());
if (ex.getCause() instanceof BadUsageException) {
getInstance().printUsageException(TOOL_NAME, (BadUsageException)ex.getCause());
}
System.err.println();
if (isVerboseOn()) {
ex.printStackTrace();
}
throw ex;
} catch (Exception ex) {
System.err.println("Error : " + ex.getMessage());
System.err.println();
if (isVerboseOn()) {
ex.printStackTrace();
}
throw new ToolException(ex.getMessage(), ex.getCause());
}
}
private void initialise(ProcessorEnvironment env) throws ToolException {
CommandDocument doc = super.getCommandDocument();
if (env.optionSet(ToolCorbaConstants.CFG_IDLFILE)) {
String idl = doc.getParameter(ToolCorbaConstants.CFG_IDLFILE);
env.put(ToolCorbaConstants.CFG_IDLFILE, idl);
}
if (env.optionSet(ToolCorbaConstants.CFG_TNS)) {
env.put(ToolCorbaConstants.CFG_TNS, doc.getParameter(ToolCorbaConstants.CFG_TNS));
}
if (env.optionSet(ToolConstants.CFG_OUTPUTDIR)) {
env.put(ToolConstants.CFG_OUTPUTDIR, doc.getParameter(ToolConstants.CFG_OUTPUTDIR));
}
if (env.optionSet(ToolCorbaConstants.CFG_ADDRESS)) {
env.put(ToolCorbaConstants.CFG_ADDRESS, doc.getParameter(ToolCorbaConstants.CFG_ADDRESS));
}
//need to add all the other options
}
public static void main(String[] arguments) {
try {
ToolRunner.runTool(IDLToWSDL.class, IDLToWSDL.class
.getResourceAsStream(ToolCorbaConstants.TOOLSPECS_BASE + "idl2wsdl.xml"), false, arguments);
} catch (BadUsageException ex) {
getInstance().printUsageException(TOOL_NAME, ex);
if (getInstance().isVerboseOn()) {
ex.printStackTrace();
}
System.exit(1);
} catch (Exception ex) {
System.err.println("Error : " + ex.getMessage());
System.err.println();
if (getInstance().isVerboseOn()) {
ex.printStackTrace();
}
System.exit(1);
}
}
private void validate(ProcessorEnvironment env) throws ToolException {
String outdir = (String)env.get(ToolConstants.CFG_OUTPUTDIR);
if (outdir != null) {
File dir = new File(outdir);
if (!dir.exists()) {
dir.mkdir();
}
}
}
public void checkParams(ErrorVisitor errors) throws ToolException {
CommandDocument doc = super.getCommandDocument();
if (!doc.hasParameter(ToolCorbaConstants.CFG_IDLFILE)) {
errors.add(new ErrorVisitor.UserError("IDL file has to be specified"));
}
if ((doc.hasParameter(ToolCorbaConstants.CFG_SCHEMA))
&& (doc.hasParameter(ToolCorbaConstants.CFG_IMPORTSCHEMA))) {
errors.add(new ErrorVisitor.UserError("Options -n & -T cannot be used together"));
}
if (errors.getErrors().size() > 0) {
Message msg = new Message("PARAMETER_MISSING", LOG);
throw new ToolException(msg, new BadUsageException(getUsage(), errors));
}
}
}