001: package org.objectweb.celtix.tools;
002:
003: import java.util.HashSet;
004: import java.util.Set;
005:
006: import org.objectweb.celtix.common.i18n.Message;
007: import org.objectweb.celtix.tools.common.ProcessorEnvironment;
008: import org.objectweb.celtix.tools.common.ToolConstants;
009: import org.objectweb.celtix.tools.common.ToolException;
010: import org.objectweb.celtix.tools.common.toolspec.ToolRunner;
011: import org.objectweb.celtix.tools.common.toolspec.ToolSpec;
012: import org.objectweb.celtix.tools.common.toolspec.parser.BadUsageException;
013: import org.objectweb.celtix.tools.common.toolspec.parser.CommandDocument;
014: import org.objectweb.celtix.tools.common.toolspec.parser.ErrorVisitor;
015: import org.objectweb.celtix.tools.processors.wsdl2.validators.WSDL11Validator;
016:
017: public class WSDLValidator extends AbstractCeltixToolContainer {
018:
019: private static final String TOOL_NAME = "wsdlvalidator";
020: private static String[] args;
021:
022: public WSDLValidator(ToolSpec toolspec) throws Exception {
023: super (TOOL_NAME, toolspec);
024: }
025:
026: private Set getArrayKeys() {
027: Set<String> set = new HashSet<String>();
028: set.add(ToolConstants.CFG_SCHEMA_URL);
029: return set;
030: }
031:
032: public void execute(boolean exitOnFinish) {
033: try {
034: super .execute(exitOnFinish);
035: if (!hasInfoOption()) {
036: ProcessorEnvironment env = new ProcessorEnvironment();
037: env.setParameters(getParametersMap(getArrayKeys()));
038: if (isVerboseOn()) {
039: env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
040: }
041:
042: env.put(ToolConstants.CFG_CMD_ARG, args);
043:
044: String schemaDir = (String) env
045: .get(ToolConstants.CFG_SCHEMA_DIR);
046: if (schemaDir == null) {
047: throw new ToolException(
048: "Schema search directory should "
049: + "be defined before validating wsdl.");
050: }
051:
052: WSDL11Validator wsdlValidator = new WSDL11Validator(
053: null, env);
054: if (wsdlValidator.isValid()) {
055: System.out
056: .println("Passed Validation : Valid WSDL ");
057: }
058: }
059: } catch (ToolException ex) {
060: System.err.println("Error : " + ex.getMessage());
061: if (ex.getCause() instanceof BadUsageException) {
062: getInstance().printUsageException(TOOL_NAME,
063: (BadUsageException) ex.getCause());
064: }
065: System.err.println();
066: if (isVerboseOn()) {
067: ex.printStackTrace();
068: }
069: } catch (Exception ex) {
070: System.err.println("Error : " + ex.getMessage());
071: System.err.println();
072: if (isVerboseOn()) {
073: ex.printStackTrace();
074: }
075: }
076: }
077:
078: public static void main(String[] pargs) {
079: args = pargs;
080:
081: try {
082: ToolRunner.runTool(WSDLValidator.class, WSDLValidator.class
083: .getResourceAsStream(ToolConstants.TOOLSPECS_BASE
084: + "wsdlvalidator.xml"), false, args);
085: } catch (BadUsageException ex) {
086: getInstance().printUsageException(TOOL_NAME, ex);
087: } catch (Exception ex) {
088: System.err.println("Error : " + ex.getMessage());
089: System.err.println();
090: ex.printStackTrace();
091: }
092: }
093:
094: public void checkParams(ErrorVisitor errors) throws ToolException {
095: CommandDocument doc = super .getCommandDocument();
096:
097: if (!doc.hasParameter("wsdlurl")) {
098: errors.add(new ErrorVisitor.UserError(
099: "WSDL/SCHEMA URL has to be specified"));
100: }
101: if (errors.getErrors().size() > 0) {
102: Message msg = new Message("PARAMETER_MISSING", LOG);
103: throw new ToolException(msg, new BadUsageException(
104: getUsage(), errors));
105: }
106: }
107: }
|