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: b104c6d31a1142b60c1eb8428c959b3e1f464d51 (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
/*******************************************************************************
 * Copyright (c) 2002-2005 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 - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.profile.validator.impl.wsdl;

import javax.wsdl.Definition;
import javax.wsdl.Import;
import javax.xml.namespace.QName;

import org.eclipse.wst.wsi.internal.core.WSIConstants;
import org.eclipse.wst.wsi.internal.core.WSIException;
import org.eclipse.wst.wsi.internal.core.WSITag;
import org.eclipse.wst.wsi.internal.core.profile.TestAssertion;
import org.eclipse.wst.wsi.internal.core.profile.validator.EntryContext;
import org.eclipse.wst.wsi.internal.core.profile.validator.impl.AssertionProcessVisitor;
import org.eclipse.wst.wsi.internal.core.report.AssertionResult;
import org.eclipse.wst.wsi.internal.core.util.ErrorList;
import org.eclipse.wst.wsi.internal.core.wsdl.traversal.WSDLTraversal;
import org.eclipse.wst.wsi.internal.core.wsdl.traversal.WSDLTraversalContext;
import org.eclipse.wst.wsi.internal.core.xml.XMLUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


/**
  * BP2105.
  * <context>For a candidate wsdl:definitions</context>
  * <assertionDescription>For the referenced definitions, as well as all imported descriptions, in the WSDL namespace under the wsdl:defintitions element, the wsdl:import element is either directly after the wsdl:documentation element or is the first child element if the wsdl:documentation element is not present.</assertionDescription>
  */
public class BP2105 extends AssertionProcessVisitor implements WSITag
{
  private final WSDLValidatorImpl validator;

  /**
   * @param WSDLValidatorImpl
   */
  public BP2105(WSDLValidatorImpl impl)
  {
    super(impl);
    this.validator = impl;
  }

  private ErrorList errors = new ErrorList();

  private boolean importFound = false;

  /* 
   * Check wsld import element is either directly after the wsdl:documentation element 
   * or is the first child element if the wsdl:documentation element is not present.
   * @see org.wsi.wsdl.traversal.WSDLVisitor#visit(javax.wsdl.Import, java.lang.Object, org.wsi.wsdl.traversal.WSDLTraversalContext)
   */
  public void visit(Import im, Object parent, WSDLTraversalContext ctx)
  {
    importFound = true;
    if (im.getDefinition() != null && im.getLocationURI() != null)
      processWSDL(im.getDefinition().getDocumentBaseURI());
  }

  /*
   * Check element is extensibility.
   * @param el - xml element
   * @return boolean
  */
  private boolean isExtensibilityElement(Element el)
  {
    boolean isEx = true;
    isEx = isEx && !XMLUtils.equals(el, WSDL_BINDING);
    isEx = isEx && !XMLUtils.equals(el, WSDL_DEFINITIONS);
    isEx = isEx && !XMLUtils.equals(el, WSDL_DOCUMENTATION);
    isEx = isEx && !XMLUtils.equals(el, WSDL_FAULT);
    isEx = isEx && !XMLUtils.equals(el, WSDL_IMPORT);
    isEx = isEx && !XMLUtils.equals(el, WSDL_INPUT);
    isEx = isEx && !XMLUtils.equals(el, WSDL_MESSAGE);
    isEx = isEx && !XMLUtils.equals(el, WSDL_OPERATION);
    isEx = isEx && !XMLUtils.equals(el, WSDL_OUTPUT);
    isEx = isEx && !XMLUtils.equals(el, WSDL_PART);
    isEx = isEx && !XMLUtils.equals(el, WSDL_PORT);
    isEx = isEx && !XMLUtils.equals(el, WSDL_PORTTYPE);
    isEx = isEx && !XMLUtils.equals(el, WSDL_SERVICE);
    isEx = isEx && !XMLUtils.equals(el, WSDL_TYPES);

    return isEx;
  }

  /* Validates the test assertion.
   * @see org.wsi.test.profile.validator.impl.BaseValidatorImpl.AssertionProcess#validate(org.wsi.test.profile.TestAssertion, org.wsi.test.profile.validator.EntryContext)
   */
  public AssertionResult validate(
    TestAssertion testAssertion,
    EntryContext entryContext)
    throws WSIException
  {
    result = AssertionResult.RESULT_FAILED;

    // Get the definition element that will be analyzed       
    Definition definition =
      (Definition) entryContext.getEntry().getEntryDetail();

    // If it doesn't contain any imports, then the result is notApplicable
    if ((definition.getImports() == null)
      || (definition.getImports().size() == 0))
    {
      result = AssertionResult.RESULT_NOT_APPLICABLE;
    }

    // Else analyze the 
    else
    {
      processWSDL(definition.getDocumentBaseURI());

      WSDLTraversal traversal = new WSDLTraversal();
      //VisitorAdaptor.adapt(this);
      traversal.setVisitor(this);
      traversal.visitImport(true);

      traversal.ignoreReferences();
      traversal.ignoreImport();
      traversal.traverse(
        (Definition) entryContext.getEntry().getEntryDetail());

      if (!errors.isEmpty())
      {
        result = AssertionResult.RESULT_FAILED;
        failureDetail = this.validator.createFailureDetail(errors.toString(), entryContext);
      }

      else if (!importFound)
      {
        result = AssertionResult.RESULT_NOT_APPLICABLE;
      }

      else
      {
        result = AssertionResult.RESULT_PASSED;
      }
    }

    return validator.createAssertionResult(testAssertion, result, failureDetail);
  }

  /*
   * the method loads WSDL and check structure
   * @param location
   */
  private void processWSDL(String location)
  {
    try
    {
      // parses WSDL
      Document doc = validator.parseXMLDocumentURL(location, null);
      checkWSDL(doc.getDocumentElement()); // get definition
    }
    catch (Throwable t)
    {
      // ADD: add exception handling
      //System.err.println("2105 - WSDL load failed");
      errors.add(t.toString());
    }
  }

  /*
   * Create falure report contains information of node.
   * @param root
   */
  private void createFailed(Node root)
  {
    Attr a = XMLUtils.getAttribute((Element) root, ATTR_WSDL_LOCATION);
    String schemaLocation = (a != null) ? a.getValue() : "";
    a = XMLUtils.getAttribute((Element) root, ATTR_WSDL_NAMESPACE);
    String namespace = (a != null) ? a.getValue() : "";
    errors.add(new QName(namespace, schemaLocation));
  }

  /*
   * Check wsdl correspons assertion description.
  * @param root - definition
   */
  private void checkWSDL(Node root)
  {
    if (root != null)
    {
      // Find the first import element
      Element im = XMLUtils.findChildElement((Element) root, WSDL_IMPORT);

      // Determine if a documentation element is present in the WSDL document
      boolean documentIsPresent =
        (XMLUtils.findChildElement((Element) root, WSDL_DOCUMENTATION)
          != null);

      // Verify that all import elements occur after a documentation element
      while (im != null)
      {
        // Find elements that are previous to the import element
        Element el = XMLUtils.findPreviousSibling(im);
        if (el != null)
          while (isExtensibilityElement(el) && el != null)
            if (el != null)
              el = XMLUtils.findPreviousSibling(el);

        // If the wsdl:document elemnt is present
        if (documentIsPresent && el != null)
        {
          if (!XMLUtils.equals(el, WSDL_DOCUMENTATION) && !XMLUtils.equals(el, WSDL_IMPORT))
            createFailed(root);
        }

        // Else check if it is another wsdl: element
        else if (el != null)
        {
          // If not the definitions element, then check for other wsdl: element
          if (!XMLUtils.equals(el, WSDL_DEFINITIONS) && !XMLUtils.equals(el, WSDL_IMPORT))
          {
            if (el.getNamespaceURI().equals(WSIConstants.NS_URI_WSDL))
              createFailed(root);
          }
        }

        // Get the next import element
        im = XMLUtils.findElement(im, WSDL_IMPORT);
      }
    }
  }
}

Back to the top