001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.misc;
019:
020: import java.io.File;
021: import java.util.HashSet;
022: import java.util.Set;
023:
024: import org.apache.cxf.common.i18n.Message;
025: import org.apache.cxf.tools.common.AbstractCXFToolContainer;
026: import org.apache.cxf.tools.common.ToolConstants;
027: import org.apache.cxf.tools.common.ToolContext;
028: import org.apache.cxf.tools.common.ToolException;
029: import org.apache.cxf.tools.common.toolspec.ToolRunner;
030: import org.apache.cxf.tools.common.toolspec.ToolSpec;
031: import org.apache.cxf.tools.common.toolspec.parser.BadUsageException;
032: import org.apache.cxf.tools.common.toolspec.parser.CommandDocument;
033: import org.apache.cxf.tools.common.toolspec.parser.ErrorVisitor;
034: import org.apache.cxf.tools.misc.processor.XSDToWSDLProcessor;
035:
036: public class XSDToWSDL extends AbstractCXFToolContainer {
037:
038: static final String TOOL_NAME = "xsd2wsdl";
039:
040: public XSDToWSDL(ToolSpec toolspec) throws Exception {
041: super (TOOL_NAME, toolspec);
042: }
043:
044: private Set getArrayKeys() {
045: return new HashSet<String>();
046: }
047:
048: public void execute(boolean exitOnFinish) {
049: XSDToWSDLProcessor processor = new XSDToWSDLProcessor();
050: try {
051: super .execute(exitOnFinish);
052: if (!hasInfoOption()) {
053: ToolContext env = new ToolContext();
054: env.setParameters(getParametersMap(getArrayKeys()));
055: if (isVerboseOn()) {
056: env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
057: }
058: env.put(ToolConstants.CFG_CMD_ARG, getArgument());
059: validate(env);
060: processor.setEnvironment(env);
061: processor.process();
062: }
063: } catch (ToolException ex) {
064: if (ex.getCause() instanceof BadUsageException) {
065: printUsageException(TOOL_NAME, (BadUsageException) ex
066: .getCause());
067: }
068: System.err.println();
069: System.err.println("XSDToWSDL Error: " + ex.getMessage());
070: if (isVerboseOn()) {
071: ex.printStackTrace();
072: }
073: } catch (Exception ex) {
074: System.err.println();
075: System.err.println("XSDToWSDL Error: " + ex.getMessage());
076: if (isVerboseOn()) {
077: ex.printStackTrace();
078: }
079: }
080: }
081:
082: private void validate(ToolContext env) throws ToolException {
083: String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
084: if (outdir != null) {
085: File dir = new File(outdir);
086: if (!dir.exists()) {
087: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
088: outdir);
089: throw new ToolException(msg);
090: }
091: if (!dir.isDirectory()) {
092: Message msg = new Message("NOT_A_DIRECTORY", LOG,
093: outdir);
094: throw new ToolException(msg);
095: }
096: }
097: }
098:
099: public static void main(String[] pargs) {
100: try {
101: ToolRunner.runTool(XSDToWSDL.class, XSDToWSDL.class
102: .getResourceAsStream("xsd2wsdl.xml"), false, pargs);
103: } catch (Exception ex) {
104: System.err.println("Error : " + ex.getMessage());
105: System.err.println();
106: ex.printStackTrace();
107: }
108: }
109:
110: public void checkParams(ErrorVisitor errors) throws ToolException {
111: CommandDocument doc = super .getCommandDocument();
112:
113: if (!doc.hasParameter("xsdurl")) {
114: errors.add(new ErrorVisitor.UserError(
115: "XSD URL has to be specified"));
116: }
117: if (errors.getErrors().size() > 0) {
118: Message msg = new Message("PARAMETER_MISSING", LOG);
119: throw new ToolException(msg, new BadUsageException(
120: getUsage(), errors));
121: }
122: }
123:
124: }
|