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.validator;
019:
020: import java.util.HashSet;
021: import java.util.Set;
022:
023: import org.apache.cxf.common.i18n.Message;
024: import org.apache.cxf.tools.common.AbstractCXFToolContainer;
025: import org.apache.cxf.tools.common.ToolConstants;
026: import org.apache.cxf.tools.common.ToolContext;
027: import org.apache.cxf.tools.common.ToolException;
028: import org.apache.cxf.tools.common.toolspec.ToolRunner;
029: import org.apache.cxf.tools.common.toolspec.ToolSpec;
030: import org.apache.cxf.tools.common.toolspec.parser.BadUsageException;
031: import org.apache.cxf.tools.common.toolspec.parser.CommandDocument;
032: import org.apache.cxf.tools.common.toolspec.parser.ErrorVisitor;
033: import org.apache.cxf.tools.validator.internal.WSDL11Validator;
034:
035: public class WSDLValidator extends AbstractCXFToolContainer {
036:
037: private static final String TOOL_NAME = "wsdlvalidator";
038:
039: public WSDLValidator(ToolSpec toolspec) throws Exception {
040: super (TOOL_NAME, toolspec);
041: }
042:
043: private Set getArrayKeys() {
044: Set<String> set = new HashSet<String>();
045: set.add(ToolConstants.CFG_SCHEMA_URL);
046: return set;
047: }
048:
049: public void execute(boolean exitOnFinish) {
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:
059: env.put(ToolConstants.CFG_CMD_ARG, getArgument());
060:
061: WSDL11Validator wsdlValidator = new WSDL11Validator(
062: null, env);
063: if (wsdlValidator.isValid()) {
064: System.out
065: .println("Passed Validation : Valid WSDL ");
066: }
067: }
068: } catch (ToolException ex) {
069: System.err.println("WSDLValidator Error : "
070: + ex.getMessage());
071: if (ex.getCause() instanceof BadUsageException) {
072: printUsageException(TOOL_NAME, (BadUsageException) ex
073: .getCause());
074: }
075: System.err.println();
076: if (isVerboseOn()) {
077: System.err.println("[+] Verbose turned on");
078: System.err.println();
079: ex.printStackTrace();
080: }
081: } catch (Exception ex) {
082: System.err.println("WSDLValidator Error : "
083: + ex.getMessage());
084: System.err.println();
085: if (isVerboseOn()) {
086: System.err.println("[+] Verbose turned on");
087: System.err.println();
088: ex.printStackTrace();
089: }
090: }
091: }
092:
093: public static void main(String[] pargs) {
094: try {
095: ToolRunner.runTool(WSDLValidator.class, WSDLValidator.class
096: .getResourceAsStream("wsdlvalidator.xml"), false,
097: pargs);
098: } catch (Exception ex) {
099: System.err.println("Error : " + ex.getMessage());
100: System.err.println();
101: ex.printStackTrace();
102: }
103: }
104:
105: public void checkParams(ErrorVisitor errors) throws ToolException {
106: CommandDocument doc = super .getCommandDocument();
107:
108: if (!doc.hasParameter("wsdlurl")) {
109: errors.add(new ErrorVisitor.UserError(
110: "WSDL/SCHEMA URL has to be specified"));
111: }
112: if (errors.getErrors().size() > 0) {
113: Message msg = new Message("PARAMETER_MISSING", LOG);
114: throw new ToolException(msg, new BadUsageException(
115: getUsage(), errors));
116: }
117: }
118: }
|