Search in sources :

Example 1 with ProgressURIResolver

use of eu.esdihumboldt.hale.io.xsd.reader.internal.ProgressURIResolver in project hale by halestudio.

the class XmlSchemaReader method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // $NON-NLS-1$
    progress.begin(Messages.getString("ApacheSchemaProvider.21"), ProgressIndicator.UNKNOWN);
    this.reporter = reporter;
    XmlSchema xmlSchema = null;
    XmlSchemaCollection schemaCol = new XmlSchemaCollection();
    // Check if the file is located on web
    URI location = getSource().getLocation();
    if (location.getHost() == null) {
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        schemaCol.setBaseUri(findBaseUri(location));
    } else if (location.getScheme().equals("bundleresource")) {
        // $NON-NLS-1$
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        // $NON-NLS-1$
        schemaCol.setBaseUri(findBaseUri(location) + "/");
    } else {
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        // $NON-NLS-1$
        schemaCol.setBaseUri(findBaseUri(location) + "/");
    }
    InputStream is = null;
    // try resolving using (local) Resources
    InputSupplier<? extends InputStream> input = Resources.tryResolve(location, Resources.RESOURCE_TYPE_XML_SCHEMA);
    if (input != null) {
        try {
            is = input.getInput();
        } catch (Exception e) {
        // ignore
        }
    }
    if (is == null) {
        is = getSource().getInput();
    }
    StreamSource ss = new StreamSource(is);
    ss.setSystemId(location.toString());
    xmlSchema = schemaCol.read(ss, null);
    is.close();
    String namespace = xmlSchema.getTargetNamespace();
    if (namespace == null) {
        namespace = XMLConstants.NULL_NS_URI;
    }
    xmlSchema.setSourceURI(location.toString());
    // create index
    index = new XmlIndex(namespace, location);
    // create group counter
    groupCounter = new TObjectIntHashMap<String>();
    Set<String> imports = new HashSet<String>();
    imports.add(location.toString());
    // load XML Schema schema (for base type definitions)
    try {
        is = XmlSchemaReader.class.getResourceAsStream("/schemas/XMLSchema.xsd");
        ss = new StreamSource(is);
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        schemaCol.setBaseUri(findBaseUri(XmlSchemaReader.class.getResource("/schemas/XMLSchema.xsd").toURI()) + "/");
        XmlSchema xsSchema = schemaCol.read(ss, null);
        is.close();
        xsSchema.setSourceURI("http://www.w3.org/2001/XMLSchema.xsd");
        XmlSchemaImport xmlSchemaImport = new XmlSchemaImport();
        xmlSchemaImport.setSchema(xsSchema);
        // add it to includes as XmlSchemaImport (not XmlSchemaInclude!)
        xmlSchema.getIncludes().add(xmlSchemaImport);
    } catch (Exception e) {
        _log.error("Exception while loading XML Schema schema", e);
    }
    loadSchema(location.toString(), xmlSchema, imports, progress, true);
    groupCounter.clear();
    // post processing
    applyRelevantElements(index);
    applyCustomTypeContent(index);
    reporter.setSuccess(true);
    return reporter;
}
Also used : ProgressURIResolver(eu.esdihumboldt.hale.io.xsd.reader.internal.ProgressURIResolver) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) HumboldtURIResolver(eu.esdihumboldt.hale.io.xsd.reader.internal.HumboldtURIResolver) HashSet(java.util.HashSet)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)1 HumboldtURIResolver (eu.esdihumboldt.hale.io.xsd.reader.internal.HumboldtURIResolver)1 ProgressURIResolver (eu.esdihumboldt.hale.io.xsd.reader.internal.ProgressURIResolver)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 StreamSource (javax.xml.transform.stream.StreamSource)1 XmlSchema (org.apache.ws.commons.schema.XmlSchema)1 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)1 XmlSchemaImport (org.apache.ws.commons.schema.XmlSchemaImport)1