Пример #1
0
  private void convertTypes() throws IOException {
    Types types = definition.getTypes();
    if (types != null) {
      List<ExtensibilityElement> list = types.getExtensibilityElements();
      for (ExtensibilityElement element : list) {
        if (element instanceof SchemaImpl) {
          Element schemaElement = ((SchemaImpl) element).getElement();
          // We need to inject the namespaces declared in parent nodes into the schema element
          Map<String, String> namespaces = definition.getNamespaces();
          for (Entry<String, String> entry : namespaces.entrySet()) {
            if (schemaElement.getAttribute("xmlns:" + entry.getKey()).isEmpty()) {
              schemaElement.setAttribute("xmlns:" + entry.getKey(), entry.getValue());
            }
          }
          parseSchemaElement(schemaElement);
        }
      }

      try {
        XSSchemaSet schemaSet = schemaParser.getResult();
        if (schemaSet == null) {
          throw new IOException("An error occurred while parsing the WSDL types section");
        }
        XsdToJolieConverter schemaConverter = new XsdToJolieConverterImpl(schemaSet, false, null);
        typeDefinitions.addAll(schemaConverter.convert());
      } catch (SAXException e) {
        throw new IOException(e);
      } catch (XsdToJolieConverter.ConversionException e) {
        throw new IOException(e);
      }
    }
  }
Пример #2
0
  /**
   * Loads nested schema type definitions from wsdl.
   *
   * @throws IOException
   * @throws WSDLException
   * @throws TransformerFactoryConfigurationError
   * @throws TransformerException
   * @throws TransformerConfigurationException
   */
  private void loadSchemas()
      throws WSDLException, IOException, TransformerConfigurationException, TransformerException,
          TransformerFactoryConfigurationError {
    Definition definition =
        WSDLFactory.newInstance().newWSDLReader().readWSDL(wsdl.getFile().getAbsolutePath());

    Types types = definition.getTypes();
    List<?> schemaTypes = types.getExtensibilityElements();

    for (Object schemaObject : schemaTypes) {
      if (schemaObject instanceof SchemaImpl) {
        SchemaImpl schema = (SchemaImpl) schemaObject;

        inheritNamespaces(schema, definition);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Source source = new DOMSource(schema.getElement());
        Result result = new StreamResult(bos);

        TransformerFactory.newInstance().newTransformer().transform(source, result);
        Resource schemaResource = new ByteArrayResource(bos.toByteArray());

        schemas.add(schemaResource);

        if (definition
            .getTargetNamespace()
            .equals(schema.getElement().getAttribute("targetNamespace"))) {
          setXsd(schemaResource);
        }
      } else {
        log.warn("Found unsupported schema type implementation " + schemaObject.getClass());
      }
    }
  }
Пример #3
0
  protected void updateDefinition(
      Definition def,
      Map<String, Definition> done,
      Map<String, SchemaReference> doneSchemas,
      String base,
      EndpointInfo ei) {
    OASISCatalogManager catalogs = OASISCatalogManager.getCatalogManager(bus);

    Collection<List<?>> imports = CastUtils.cast((Collection<?>) def.getImports().values());
    for (List<?> lst : imports) {
      List<Import> impLst = CastUtils.cast(lst);
      for (Import imp : impLst) {

        String start = imp.getLocationURI();
        String decodedStart = null;
        // Always use the URL decoded version to ensure that we have a
        // canonical representation of the import URL for lookup.
        try {
          decodedStart = URLDecoder.decode(start, "utf-8");
        } catch (UnsupportedEncodingException e) {
          throw new WSDLQueryException(
              new org.apache.cxf.common.i18n.Message("COULD_NOT_PROVIDE_WSDL", LOG, start), e);
        }

        String resolvedSchemaLocation = resolveWithCatalogs(catalogs, start, base);

        if (resolvedSchemaLocation == null) {
          try {
            // check to see if it's already in a URL format.  If so, leave it.
            new URL(start);
          } catch (MalformedURLException e) {
            if (done.put(decodedStart, imp.getDefinition()) == null) {
              updateDefinition(imp.getDefinition(), done, doneSchemas, base, ei);
            }
          }
        } else {
          if (done.put(decodedStart, imp.getDefinition()) == null) {
            done.put(resolvedSchemaLocation, imp.getDefinition());
            updateDefinition(imp.getDefinition(), done, doneSchemas, base, ei);
          }
        }
      }
    }

    /* This doesn't actually work.   Setting setSchemaLocationURI on the import
     * for some reason doesn't actually result in the new URI being written
     * */
    Types types = def.getTypes();
    if (types != null) {
      for (ExtensibilityElement el :
          CastUtils.cast(types.getExtensibilityElements(), ExtensibilityElement.class)) {
        if (el instanceof Schema) {
          Schema see = (Schema) el;
          updateSchemaImports(see, doneSchemas, base);
        }
      }
    }
  }
  /*
   * Analyze the wsdl document at the given URI, and traverse any relative files that
   * it imports. Can optionally pass in a parsed Definition if one's available so
   * we don't have to parse the wsdl again (otherwise just pass in null).
   */
  private void analyzeWSDL(URI uri, Definition definition)
      throws MalformedURLException, IOException, WSDLException, WWWAuthenticationException {

    uri = uri.normalize();

    // already seen this wsdl, skip
    if (xmlObjectInfos.containsKey(uri.toString())) return;

    // need to parse the wsdl ourselves
    if (definition == null) definition = parser.getWSDLDefinitionVerbose(uri.toString());

    // save a reference to the starting wsdl
    if (this.definition == null) this.definition = definition;

    IPath path = new Path(uri.getPath());

    // a target filename was given, so we need to modify the path with the new name
    if (definition == this.definition && targetFilename != null)
      path = path.removeLastSegments(1).append(targetFilename);

    XMLObjectInfo info = new XMLObjectInfo(path, definition);
    xmlObjectInfos.put(uri.toString(), info);
    updatePathPrefix(info);

    // now look at wsdl imports

    for (Iterator it = definition.getImports().values().iterator(); it.hasNext(); ) {

      List list = (List) it.next();

      for (Iterator listIt = list.iterator(); listIt.hasNext(); ) {

        Import wsdlImport = (Import) listIt.next();
        String wsdlImportLocation = wsdlImport.getLocationURI();

        // analyze any relative imports we find
        if (wsdlImportLocation != null && isRelative(wsdlImportLocation)) {

          // bad form, importing xsd with wsdl:import, but need to handle
          if (wsdlImportLocation.toLowerCase().endsWith(XSD))
            analyzeXSD(uri.resolve(uriCreate(wsdlImportLocation)));
          else analyzeWSDL(uri.resolve(uriCreate(wsdlImportLocation)), null);
        }
      }
    }

    // now look at xsd imports

    Types types = definition.getTypes();

    // there's no wsdl:types, we're done
    if (types == null) return;

    for (Iterator it = types.getExtensibilityElements().iterator(); it.hasNext(); ) {

      ExtensibilityElement extElement = (ExtensibilityElement) it.next();
      Element element;

      // we'll try to parse any UnknownExtensibilityElements and
      // XSDSchemaExtensibilityElements into an XSD schema

      if (extElement instanceof UnknownExtensibilityElement)
        element = ((UnknownExtensibilityElement) extElement).getElement();
      else if (extElement instanceof XSDSchemaExtensibilityElement)
        element = ((XSDSchemaExtensibilityElement) extElement).getElement();
      else if (extElement instanceof Schema) element = ((Schema) extElement).getElement();
      else continue;

      try {
        XSDSchema xsdSchema = XSDSchemaImpl.createSchema(element);

        // analyze the inlined schema at the current uri
        analyzeXSD(uri, xsdSchema);
      } catch (Exception t) {
        // ignore any extensibility elements that cannot be parsed into a
        // XSDSchema instance
      }
    }
  }