Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9580b63138bcbaa6a3e6047466adc76f8a7a66cf (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*******************************************************************************
 * Copyright (c) 2001, 2004 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;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.xerces.impl.XMLErrorReporter;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.StandardParserConfiguration;
import org.apache.xerces.xni.XNIException;
import org.eclipse.wst.wsdl.validation.internal.exception.ValidateWSDLException;
import org.eclipse.wst.wsdl.validation.internal.resolver.URIResolver;
import org.eclipse.wst.wsdl.validation.internal.util.MessageGenerator;
import org.eclipse.wst.wsdl.validation.internal.xml.AbstractXMLConformanceFactory;
import org.eclipse.wst.wsdl.validation.internal.xml.DefaultXMLValidator;
import org.eclipse.wst.wsdl.validation.internal.xml.IXMLValidator;
import org.eclipse.wst.wsdl.validation.internal.xml.LineNumberDOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

/**
 * This is the main entrypoint to the WSDL Validator. The controller is
 * responsible for calling the reader, the XML conformance check, the WSDL
 * validation and the WS-I validation if selected. The controller contains any
 * errors and warnings generated as well.
 */
public class ValidationController
{
  protected final String _ERROR_PROBLEM_WSDL_VALIDATOR = "_ERROR_PROBLEM_WSDL_VALIDATOR";
  protected final String _ERROR_NO_WSDL_VALIDATOR = "_ERROR_NO_WSDL_VALIDATOR";
  protected final String _ERROR_PROBLEM_EXT_VALIDATOR = "_ERROR_PROBLEM_EXT_VALIDATOR";

  protected ValidatorRegistry ver;
  protected ResourceBundle resourcebundle;
  protected MessageGenerator messagegenerator;
  protected URIResolver uriResolver;
  protected Hashtable attributes = new Hashtable();

  //protected String wsdlNamespace = null;

  /**
   * The ResourceBundle needs to be set so it can be passed to the reader.
   * 
   * @param rb
   *            The resource bundle for this validator.
   */
  public ValidationController(ResourceBundle rb, URIResolver uriResolver)
  {
    resourcebundle = rb;
    messagegenerator = new MessageGenerator(resourcebundle);
    this.uriResolver = uriResolver;

    ver = ValidatorRegistry.getInstance();
  }
  
  /**
   * Add the attributes specified to the validation.
   * 
   * @param attributes The attributes to add to the validation.
   */
  public void setAttributes(Hashtable attributes)
  {
  	this.attributes.putAll(attributes);
  }

  /**
   * Validate the WSDL file. Check the file for XML conformance. If it is XML
   * conformant, read the file and check it for WSDL conformance. If it is WSDL
   * conformant and WS-I conformance is set to suggest or require, check the
   * file for WS-I conformance.
   * 
   * @param uri
   *            The uri where the WSDL document is located.
   * @param wsiLevel
   *            The level of WS-I conformance to use for validation.
   * @return A validation report with the validation info for the file.
   */
//  public IValidationReport validate(String uri)
//  { return validate(uri, null);
//  }
  
  /**
   * Validate the WSDL file with the stream. This method will run the check of the 
   * WSDL document. The validation is broken up into three stages: XML conformance,
   * WSDL semantic, and post validation.
   * 
 * @param uri The URI of the WSDL document to be validated.
 * @param inputStream The contents of the WSDL document to be validated.
 * @return A validation report with the validation info for the file.
 */
  public IValidationReport validate(String uri, InputStream inputStream)
  {
    
    InputStream xmlValidateStream = null;
    InputStream wsdlValidateStream = null;
    if (inputStream != null)
    { //copy the inputStream so we can use it more than once
      String contents = createStringForInputStream(inputStream);
      xmlValidateStream = new ByteArrayInputStream(contents.getBytes());
      wsdlValidateStream = new ByteArrayInputStream(contents.getBytes());
    }
    
    ControllerValidationInfo valInfo = new ValidationInfoImpl(uri, messagegenerator);
    ((ValidationInfoImpl)valInfo).setURIResolver(uriResolver);
    ((ValidationInfoImpl)valInfo).setAttributes(attributes);

    if (validateXML(valInfo, xmlValidateStream))
    {
      Document wsdldoc = getDocument(uri, wsdlValidateStream);
      String wsdlns = getWSDLNamespace(wsdldoc);
      if (validateWSDL(wsdldoc, valInfo, wsdlns))
      {
        validateExtensionValidators(wsdldoc, valInfo, wsdlns);
      }
    }
    return (IValidationReport)valInfo;
  }

  /**
   * Validate the file for XML conformance.
	 * @param valInfo information about the validation
   * @param inputStream the inputStream to validate
   * @return True if the file is conformant, false otherwise.
   */
  protected boolean validateXML(ControllerValidationInfo valInfo, InputStream inputStream)
  {
    IXMLValidator xmlValidator = AbstractXMLConformanceFactory.getInstance().getXMLValidator();
    xmlValidator.setURIResolver(uriResolver);
    xmlValidator.setFile(valInfo.getFileURI());
    if (xmlValidator instanceof DefaultXMLValidator)
    {
        ((DefaultXMLValidator)xmlValidator).setInputStream(inputStream);
    }
    //xmlValidator.setValidationInfo(valInfo);
    xmlValidator.run();
    // if there are no xml conformance problems go on to check the wsdl stuff
    if (xmlValidator.hasErrors())
    {
      // temp handling of XML errors until validator is updated.
      List errors = xmlValidator.getErrors();
      Iterator errorsIter = errors.iterator();
      while (errorsIter.hasNext())
      {
        IValidationMessage valMes = (IValidationMessage)errorsIter.next();
        
        if (valMes instanceof ValidationMessageImpl && valInfo instanceof ValidationInfoImpl)
        {   String errorKey = ((ValidationMessageImpl)valMes).getErrorKey();
            Object[] messageArgs = ((ValidationMessageImpl)valMes).getMessageArguments();
            ((ValidationInfoImpl)valInfo).addError(valMes.getMessage(), valMes.getLine(), valMes.getColumn(), valMes.getURI(), errorKey, messageArgs);
        }
        else
        {
            valInfo.addError(valMes.getMessage(), valMes.getLine(), valMes.getColumn(), valMes.getURI());
        }
      }
      return false;
    }
    //wsdlNamespace = xmlValidator.getWSDLNamespace();
    return true;
  }
  
  /**
   * Validate the file for XML conformance.
	 * @param valInfo information about the validation
   * @return True if the file is conformant, false otherwise.
   */
  protected boolean validateXML(ControllerValidationInfo valInfo)
  { return validateXML(valInfo, null);
  }

  /**
   * Validate the WSDL file. Set the errors and warning appropriately.
   * 
   * @param wsdldoc A W3C document representation of the WSDL document.
   * @param valInfo The current validation information.
   * @param wsdlNamespace The WSDL namespace to validate.
   * @return True if the file is valid, false otherwise.
   */
  protected boolean validateWSDL(Document wsdldoc, ControllerValidationInfo valInfo, String wsdlNamespace)
  {
    WSDLValidatorDelegate[] wsdlVals = ver.queryValidatorRegistry(wsdlNamespace, ValidatorRegistry.WSDL_VALIDATOR);
    if (wsdlVals != null)
    {
      for (int i = 0; i < wsdlVals.length; i++)
      {
        WSDLValidatorDelegate wsdlvaldel = wsdlVals[i];
        IWSDLValidator wsdlVal = wsdlvaldel.getValidator();

        // If the wsdl validator isn't null, validate.
        if (wsdlVal != null)
        {
          try
          {
            wsdlVal.validate(wsdldoc, valInfo);
          }
          catch (ValidateWSDLException e)
          {
            valInfo.addError(messagegenerator.getString(_ERROR_PROBLEM_WSDL_VALIDATOR, wsdlNamespace), 1, 1, valInfo.getFileURI());
          }
        }
        // If the validator is null and the namespace isn't create an error.
        // If the namespace is null the file is empty (and the XML validator
        // has let it go)
        // so it is valid but does not need to be validated.
        else
        {
          valInfo.addError(
            messagegenerator.getString(_ERROR_NO_WSDL_VALIDATOR, wsdlNamespace),
            1,
            1,
            valInfo.getFileURI());
        }
      }
    }
    // No validators registered.
    else
    {
      valInfo.addError(messagegenerator.getString(_ERROR_NO_WSDL_VALIDATOR, wsdlNamespace), 1, 1, valInfo.getFileURI());
    }
    valInfo.completeWSDLValidation();

    return valInfo.isWSDLValid();
  }

  /**
   * Validate the WSDL file with the extension validator. Set the errors and warning appropriately.
   * 
   * @param wsdldoc A W3C document representation of the WSDL document.
   * @param valInfo The current validation information.
   * @param wsdlNamespace The WSDL namespace to validate.
   * @return True if the file is valid, false otherwise.
   */
  protected void validateExtensionValidators(Document wsdldoc, ControllerValidationInfo valInfo, String wsdlNamespace)
  {
    WSDLValidatorDelegate[] extVals = ver.queryValidatorRegistry(wsdlNamespace, ValidatorRegistry.EXT_VALIDATOR);
    if(extVals != null)
    {
      int numvals = extVals.length;
      for(int i = 0; i < numvals; i++)
      {
        WSDLValidatorDelegate extvaldel = extVals[i];
        IWSDLValidator extval = extvaldel.getValidator();
        if(extval != null)
        {
          try
          {
          extval.validate(wsdldoc, valInfo);
          }
          catch(Throwable  t)
          {
            valInfo.addWarning(messagegenerator.getString(_ERROR_PROBLEM_EXT_VALIDATOR,  extvaldel.getValidatorClassName(), wsdlNamespace), 1, 1, valInfo.getFileURI());
            // This error should be logged and not displayed to the user.
          }
        }
      }
    }
  }

  /**
   * Set the ResourceBundle for this ValidatorManager.
   * 
   * @param rb
   *            The resource bundle to set.
   * @see #getResourceBundle
   */
  public void setResourceBundle(ResourceBundle rb)
  {
    resourcebundle = rb;
  }

  /**
   * Get the ResourceBundle for this ValidationController.
   * 
   * @return The resource bundle set for this ValidationController.
   * @see #setResourceBundle
   */
  public ResourceBundle getResourceBundle()
  {
    return resourcebundle;
  }
  
  /**
   * Get a DOM document representation of the WSDL document.
   * 
   * @param uri The uri of the file to read
   * @return The DOM model of the WSDL document or null if the document can't be read.
   */
  private Document getDocument(String uri, InputStream inputStream)
  {
    try
    {
      // Catch a premature EOF error to allow empty WSDL files to be considered valid.
      StandardParserConfiguration configuration = new StandardParserConfiguration()
      {
        protected XMLErrorReporter createErrorReporter()
        {
          return new XMLErrorReporter()
          {
            public void reportError(String domain, String key, Object[] arguments, short severity) throws XNIException
            {
              boolean reportError = true;
              if (key.equals("PrematureEOF"))
              {         
                reportError = false;
              }

              if (reportError)
              {
                super.reportError(domain, key, arguments, severity);
              }
            }
          };
        }
      };
      
      InputSource inputSource = null;
      if (inputStream != null)
      { //then we want to create a DOM from the inputstream
        inputSource = new InputSource(inputStream);
      }
      else
      { inputSource = new InputSource(uri);
      }
      
      DOMParser builder = new LineNumberDOMParser(configuration);
      builder.parse(inputSource);
      Document doc = builder.getDocument();

      return doc;
    }
    catch (Throwable t)
    {
      //System.out.println(t);
    }
    return null;
  }
  
  private String getWSDLNamespace(Document doc)
  {
    String wsdlns = null;
    if(doc != null)
    {
      Element definitions = doc.getDocumentElement();
      if(definitions.getLocalName().equals("definitions"))
      {
        wsdlns = definitions.getNamespaceURI();
      }
    }
    return wsdlns;
  }
  
  
  
  private final String createStringForInputStream(InputStream inputStream)
  {
    // Here we are reading the file and storing to a stringbuffer.
    StringBuffer fileString = new StringBuffer();
    try
    {
      InputStreamReader inputReader = new InputStreamReader(inputStream);
      BufferedReader reader = new BufferedReader(inputReader);
      char[] chars = new char[1024];
      int numberRead = reader.read(chars);
      while (numberRead != -1)
      {
        fileString.append(chars, 0, numberRead);
        numberRead = reader.read(chars);
      }
    }
    catch (Exception e)
    {
      //TODO: log error message
      //e.printStackTrace();
    }
    return fileString.toString();
  }
  
}

Back to the top