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.WSDLToXMLProcessor;
035:
036: public class WSDLToXML extends AbstractCXFToolContainer {
037:
038: static final String TOOL_NAME = "wsdl2xml";
039: static final String BINDING_NAME_POSFIX = "_XMLBinding";
040: static final String SERVICE_NAME_POSFIX = "_XMLService";
041: static final String PORT_NAME_POSFIX = "_XMLPort";
042:
043: public WSDLToXML(ToolSpec toolspec) throws Exception {
044: super (TOOL_NAME, toolspec);
045: }
046:
047: private Set getArrayKeys() {
048: return new HashSet<String>();
049: }
050:
051: public void execute(boolean exitOnFinish) {
052: WSDLToXMLProcessor processor = new WSDLToXMLProcessor();
053: try {
054: super .execute(exitOnFinish);
055: if (!hasInfoOption()) {
056: ToolContext env = new ToolContext();
057: env.setParameters(getParametersMap(getArrayKeys()));
058: if (isVerboseOn()) {
059: env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
060: }
061: env.put(ToolConstants.CFG_CMD_ARG, getArgument());
062:
063: validate(env);
064: setEnvParamDefValues(env);
065:
066: processor.setEnvironment(env);
067: processor.process();
068: }
069: } catch (ToolException ex) {
070: if (ex.getCause() instanceof BadUsageException) {
071: printUsageException(TOOL_NAME, (BadUsageException) ex
072: .getCause());
073: }
074: System.err.println();
075: System.err.println("WSDLToXML Error: " + ex.getMessage());
076: if (isVerboseOn()) {
077: ex.printStackTrace();
078: }
079: } catch (Exception ex) {
080: System.err.println();
081: System.err.println("WSDLToXML Error: " + ex.getMessage());
082: if (isVerboseOn()) {
083: ex.printStackTrace();
084: }
085: }
086: }
087:
088: private void setEnvParamDefValues(ToolContext env) {
089: if (!env.optionSet(ToolConstants.CFG_BINDING)) {
090: env.put(ToolConstants.CFG_BINDING, env
091: .get(ToolConstants.CFG_PORTTYPE)
092: + BINDING_NAME_POSFIX);
093: }
094: if (!env.optionSet(ToolConstants.CFG_SERVICE)) {
095: env.put(ToolConstants.CFG_SERVICE, env
096: .get(ToolConstants.CFG_PORTTYPE)
097: + SERVICE_NAME_POSFIX);
098: }
099: if (!env.optionSet(ToolConstants.CFG_PORT)) {
100: env.put(ToolConstants.CFG_PORT, env
101: .get(ToolConstants.CFG_PORTTYPE)
102: + PORT_NAME_POSFIX);
103: }
104: }
105:
106: private void validate(ToolContext env) throws ToolException {
107: String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
108: if (outdir != null) {
109: File dir = new File(outdir);
110: if (!dir.exists()) {
111: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
112: outdir);
113: throw new ToolException(msg);
114: }
115: if (!dir.isDirectory()) {
116: Message msg = new Message("NOT_A_DIRECTORY", LOG,
117: outdir);
118: throw new ToolException(msg);
119: }
120: }
121: }
122:
123: public static void main(String[] pargs) {
124: try {
125: ToolRunner.runTool(WSDLToXML.class, WSDLToXML.class
126: .getResourceAsStream("wsdl2xml.xml"), false, pargs);
127: } catch (Exception ex) {
128: System.err.println("Error : " + ex.getMessage());
129: System.err.println();
130: ex.printStackTrace();
131: }
132: }
133:
134: public void checkParams(ErrorVisitor errors) throws ToolException {
135: CommandDocument doc = super .getCommandDocument();
136:
137: if (!doc.hasParameter("wsdlurl")) {
138: errors.add(new ErrorVisitor.UserError(
139: "WSDL/SCHEMA URL has to be specified"));
140: }
141: if (errors.getErrors().size() > 0) {
142: Message msg = new Message("PARAMETER_MISSING", LOG);
143: throw new ToolException(msg, new BadUsageException(
144: getUsage(), errors));
145: }
146: }
147:
148: }
|