Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: a557bf7edbeaac161a45e0ceeca667c8a7e38c1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.wst.wsdl.validation.internal.ui.text;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.wst.wsdl.validation.internal.ClassloaderWSDLValidatorDelegate;
import org.eclipse.wst.wsdl.validation.internal.IValidationMessage;
import org.eclipse.wst.wsdl.validation.internal.IValidationReport;
import org.eclipse.wst.wsdl.validation.internal.WSDLValidationConfiguration;
import org.eclipse.wst.wsdl.validation.internal.WSDLValidator;
import org.eclipse.wst.wsdl.validation.internal.logging.ILogger;
import org.eclipse.wst.wsdl.validation.internal.logging.LoggerFactory;
import org.eclipse.wst.wsdl.validation.internal.resolver.URIResolverDelegate;
import org.eclipse.wst.wsdl.validation.internal.wsdl11.ClassloaderWSDL11ValidatorDelegate;
import org.eclipse.wst.wsdl.validation.internal.wsdl11.WSDL11ValidatorDelegate;
import org.eclipse.wst.wsdl.validation.internal.xml.XMLCatalog;
import org.eclipse.wst.wsdl.validation.internal.xml.XMLCatalogEntityHolder;

import com.ibm.wsdl.util.StringUtils;

/**
 * A commande line tool to run WSDL Validation on a single or multiple files.
 * 
 * Options
 * -schemaDir directory       : a directory of schemas to load into the catalog
 * -schema namespace location : a schema to load into the registry
 * -wsdl11v classname namespace resourcebundle : register a WSDL 1.1 extension validator
 *                              to load for the given namespace with the given resourcebundle
 * -wsiv validatorClass namespace propertiesfile : register a WS-I validator
 * -uriresolver URIResolverClass : register an extension URI resolver
 */
public class WSDLValidate
{
  private final String FILE_PREFIX = "file:///";
  private static final String VALIDATOR_PROPERTIES =
    org.eclipse.wst.wsdl.validation.internal.Constants.WSDL_VALIDATOR_PROPERTIES_FILE;
  
  protected static final String PARAM_WSDL11VAL = "-wsdl11v";
  protected static final String PARAM_EXTVAL = "-extv";
  protected static final String PARAM_SCHEMADIR = "-schemaDir";
  protected static final String PARAM_SCHEMA = "-schema";
  protected static final String PARAM_URIRESOLVER = "-uriresolver";
  protected static final String PARAM_LOGGER = "-logger";
  protected static final String PARAM_PROPERTY = "-D";
  protected static final String PARAM_VERBOSE = "-verbose";
  protected static final String PARAM_VERBOSE_SHORT = "-v";
  
  private static final String STRING_EMPTY = "";
  private static final String STRING_SPACE = " ";
  private static final String STRING_DASH = "-";
  
  String workingdir = System.getProperty("user.dir");
  
  //protected ResourceBundle resourceBundle;

  protected WSDLValidator wsdlValidator = null;
  protected WSDLValidationConfiguration configuration = null;
  protected List wsdlFiles = new ArrayList();
  protected boolean verbose = false;
  protected ResourceBundle validatorRB = null;

  /**
   * Constuctor.
   */
  protected WSDLValidate()
  {
  	wsdlValidator = new WSDLValidator();
  	configuration = new WSDLValidationConfiguration();
  	
    try
    {
      validatorRB = ResourceBundle.getBundle(VALIDATOR_PROPERTIES);
    }
    catch (MissingResourceException e)
    {
      LoggerFactory.getInstance().getLogger().log("Validation failed: Unable to load the WSDL validator properties file.", ILogger.SEV_ERROR, e);
    }
  }
  
  /**
   * Validate the files specified.
   */
  protected void validate()
  {
	ILogger logger = LoggerFactory.getInstance().getLogger();
	
	int numInvalid = 0;

	Iterator filesIter = wsdlFiles.iterator();
    while (filesIter.hasNext())
	{
	  String wsdlFile = (String)filesIter.next();
	  IValidationReport valReport = validateFile(wsdlFile);

	  if(valReport.hasErrors())
	  {
	    numInvalid++;
	    logger.log(MessageFormat.format(WSDLValidateTextUIMessages._UI_FILE_INVALID, new Object[]{wsdlFile}), ILogger.SEV_INFO);
	    //logger.log(WSDLValidateTextUIMessages._UI_INFORMATION_DELIMITER, ILogger.SEV_ERROR);
	    logger.log(getMessages(valReport.getValidationMessages()), ILogger.SEV_INFO);
	  }
	  else if(verbose)
	  {
	    logger.log(MessageFormat.format(WSDLValidateTextUIMessages._UI_FILE_VALID, new Object[]{wsdlFile}), ILogger.SEV_VERBOSE);
	  }
	}
    
    // Log the summary.
    logger.log(MessageFormat.format(WSDLValidateTextUIMessages._UI_VALIDATION_SUMMARY, new Object[]{new Integer(wsdlFiles.size()), new Integer(numInvalid)}), ILogger.SEV_INFO);
  }

  /**
   * Run WSDL validation on a given file.
   * 
   * @param filename 
   * 		The name of the file to validate.
   * @throws Exception
   */
  protected IValidationReport validateFile(String filename)
  {	
    // Resolve the location of the file.
    String filelocation = null;
    try
    {
      URL fileURL = StringUtils.getURL(new URL(FILE_PREFIX + workingdir + "/"), filename);
      filelocation = fileURL.toExternalForm();
    }
    catch (MalformedURLException e)
    {
      // Do nothing. The WSDL validator will handle the error.
      //LoggerFactory.getInstance().getLogger().log(MessageFormat.format(WSDLValidateTextUIMessages._ERROR_UNABLE_TO_READ_FILE, new Object[]{filename}), ILogger.SEV_ERROR);
    }
    
    // Run validation on the file.
    IValidationReport valReport = wsdlValidator.validate(filelocation, null, configuration);
    return valReport;
  }

  /**
   * Returns a String with formatted output for a list of messages.
   * 
   * @param messages 
   * 		The messages to get.
   * @return 
   * 		A string with a formatted list of the messages.
   */
  protected String getMessages(IValidationMessage[] messages)
  {
    StringBuffer outputBuffer = new StringBuffer();
    if (messages != null)
    {
      // create a list of messages that looks like
      // ERROR 1:1 Error message content
      int numMessages = messages.length;
      String marker = null;
      for (int i = 0; i < numMessages; i++)
      {
        IValidationMessage message = messages[i];
        int severity = message.getSeverity();
        if (severity == IValidationMessage.SEV_ERROR)
        {
          marker = WSDLValidateTextUIMessages._UI_ERROR_MARKER;
        }
        else if (severity == IValidationMessage.SEV_WARNING)
        {
          marker = WSDLValidateTextUIMessages._UI_WARNING_MARKER;
        }
        else
        {
          marker = STRING_EMPTY;
        }
        outputBuffer
          .append(marker)
          .append(STRING_SPACE)
          .append(message.getLine())
          .append(":")
          .append(message.getColumn())
          .append(STRING_SPACE)
          .append(message.getMessage());
        if(i != numMessages -1)
        {
          outputBuffer.append("\n");
        }
      }
    }
    return outputBuffer.toString();
  }

  /**
   * The main entry point into the command line tool. 
   * Checks the command line arguments, registers the default validators and runs validation on the
   * list of files.
   * 
   * @param args - the arguments to the validator
   */
  public static void main(String[] args)
  {
    // No arguments specified. Print usage.
	if (args.length < 1)
    {
	  System.err.println(WSDLValidateTextUIMessages._ERROR_WRONG_ARGUMENTS);
	  System.exit(0);
	}
	
	WSDLValidate wsdlValidate = new WSDLValidate();
	wsdlValidate.parseArguments(args);
	wsdlValidate.validate();
  }
  
  /**
   * Parse the arguments specified for this WSDLValidate task and
   * configure validation.
   * 
   * @param args
   * 		The arguments specified for this task.
   */
  protected void parseArguments(String[] args)
  {
	int numargs = args.length;

	for (int i = 0; i < numargs; i++)
	{
	  String param = args[i];

	  // Registering an extension validator or WSDL 1.1 extension validator.
	  if (param.equals(WSDLValidate.PARAM_WSDL11VAL) 
			  || param.equals(WSDLValidate.PARAM_EXTVAL))
	  {
	    String namespace = args[++i];
	    if (!namespace.startsWith(WSDLValidate.STRING_DASH))
	    {
	      String validatorClass = args[++i];

	      if (!validatorClass.startsWith(WSDLValidate.STRING_DASH))
	      {
	        if(param.equalsIgnoreCase(WSDLValidate.PARAM_WSDL11VAL))
	        {  
	          WSDL11ValidatorDelegate delegate = new ClassloaderWSDL11ValidatorDelegate(validatorClass);
	          wsdlValidator.registerWSDL11Validator(namespace, delegate);
	        }
	        else if(param.equalsIgnoreCase(WSDLValidate.PARAM_EXTVAL))
	        {
	          ClassloaderWSDLValidatorDelegate delegate = new ClassloaderWSDLValidatorDelegate(validatorClass);
	          wsdlValidator.registerWSDLExtensionValidator(namespace, delegate);
	        }
	      }
	      else
	      {
	        i--;
	      }
	    }
	    else
	    {
	      i--;
	    }  
	  }
	  // Register a directory with schemas.
	  else if (param.equalsIgnoreCase(WSDLValidate.PARAM_SCHEMADIR))
	  {
	    String xsdDir = args[++i];
	    XMLCatalog.addSchemaDir(xsdDir);
	  }
	  // Register a schema.
	  // TODO: Replace this with an XML catalog.
	  else if (param.equalsIgnoreCase(WSDLValidate.PARAM_SCHEMA))
	  {
	    String publicid = args[++i];
	    String systemid = args[++i];
	    XMLCatalog.addEntity(new XMLCatalogEntityHolder(publicid, systemid));
	  }
	  // Register a URI resolver. 
	  // TODO: Determine if this is appropriate for the text UI.
	  else if(param.equalsIgnoreCase(PARAM_URIRESOLVER))
	  {
	    String resolverClass = args[++i];
	    wsdlValidator.addURIResolver(new URIResolverDelegate(resolverClass, null).getURIResolver());
	  }
	  // Configure the logger.
	  else if(param.equals(PARAM_LOGGER))
	  {
	    String loggerClassString = args[++i];
	    try
	    {
	      Class loggerClass = WSDLValidate.class.getClassLoader().loadClass(loggerClassString);
	      ILogger logger = (ILogger)loggerClass.newInstance();
	      LoggerFactory.getInstance().setLogger(logger);
	    }
	    catch(Exception e)
	    {
	      System.err.println(MessageFormat.format(WSDLValidateTextUIMessages._ERROR_LOADING_LOGGER, new Object[]{loggerClassString}));
	    }
	  }
	  // Set properties.
	  else if(param.startsWith(PARAM_PROPERTY))
	  {
	    int separator = param.indexOf('=');
	    String name = param.substring(2, separator);
	    String value = param.substring(separator+1);
	    configuration.setProperty(name, value);
	  }
	  // Set verbose;
	  else if(param.equals(PARAM_VERBOSE) || param.equals( PARAM_VERBOSE_SHORT))
	  {
		verbose = true;
	  }
	  // a file to validate
	  else
	  {
	    if(!param.startsWith(WSDLValidate.STRING_DASH))
	    {  
	      wsdlFiles.add(param);
	    }
	  }
    }
  }
}

Back to the top