*/
protected Schema parseSchemaImport(XMLElement importEl,
DescriptionElement desc)
throws WSDLException {
ImportedSchemaImpl schema = new ImportedSchemaImpl();
String ns = importEl.getAttributeValue(SchemaConstants.ATTR_NAMESPACE);
if(ns != null) {
schema.setNamespace(getURI(ns));
}
String sloc = importEl.getAttributeValue(SchemaConstants.ATTR_SCHEMA_LOCATION);
if(sloc != null) {
schema.setSchemaLocation(getURI(sloc));
}
if(schema.getNamespace() == null){
//The namespace attribute is REQUIRED on xs:import, so don't continue.
schema.setReferenceable(false);
return schema;
}
if(schema.getSchemaLocation() == null){
//This is a namespace-only import, no schema document to be retrieved so don't continue.
/* TODO investigate whether/how to try to resolve the imported namespace to known schema
* components from that namespace (e.g. via a URI catalog resolver). Currently, any attempt
* to resolve a QName against schema components from this namespace will search ALL
* schemas imported from this namespace (see methods in TypesImpl).
*/
return schema;
}
//Now try to retrieve the schema import using schemaLocation
OMElement importedSchemaDoc = null;
URI contextURI = null;
String schemaLoc = null;
URL url = null;
try{
/*
* For simple resolvers, we resolve the parent (Description) URI
* to be used as the context. This allows for relative locationURIs
* to be resolved implicitly - they are considered to be located
* relative to the resolved parent. Therefore, relative URIs such as these
* need not be listed in the catalog file.
*/
/* TODO
* OASIS-style catalogs have a convenience notation to define root URIs
* thus grouping related URLs together. In this case the context URI here
* should be left alone, but the resultant locationURL resolved instead.
*
* Implement a boolean system property like org.apache.woden.resolver.useRelativeURLs
* (set by the resolver ctor). SimpleURIResolver (et al) should set this to true,
* OASISCatalogResolver should set to false.
*/
// contextURI = desc.getDocumentBaseURI();
contextURI = resolveURI(desc.getDocumentBaseURI());
URL contextURL = (contextURI != null) ? contextURI.toURL() : null;
schemaLoc = schema.getSchemaLocation().toString();
url = StringUtils.getURL(contextURL, schemaLoc);
}
catch (MalformedURLException e) {
String baseLoc = contextURI != null ? contextURI.toString() : null;
getErrorReporter().reportError(
new ErrorLocatorImpl(), //TODO line&col nos.
"WSDL502",
new Object[] {baseLoc, schemaLoc},
ErrorReporter.SEVERITY_ERROR);
//can't continue schema retrieval with a bad URL.
schema.setReferenceable(false);
return schema;
}
String schemaURL = url.toString();
//If the schema has already been imported, reuse it.
XmlSchema schemaDef = (XmlSchema)fImportedSchemas.get(schemaURL);
if(schemaDef == null){
//not previously imported, so retrieve it now.
try {
importedSchemaDoc = OMUtils.getElement(schemaURL);
} catch (URISyntaxException e){
String msg = getErrorReporter().getFormattedMessage(
"WSDL502", new Object[] {null, schemaURL});
throw new WSDLException(WSDLException.PARSER_ERROR, msg, e);
} catch (XMLStreamException e){
String msg = getErrorReporter().getFormattedMessage(
"WSDL500", new Object[] {"XML", schemaURL});
throw new WSDLException(WSDLException.PARSER_ERROR, msg, e);
} catch (IOException e4) {
//schema retrieval failed (e.g. 'not found')
getErrorReporter().reportError(
new ErrorLocatorImpl(), //TODO line&col nos.
"WSDL504",
new Object[] {schemaURL},
ErrorReporter.SEVERITY_WARNING,
e4);
//cannot continue without an imported schema
schema.setReferenceable(false);
return schema;
}
/*
* First get the schema element and serialize that into a byte array.
* This is used in getting an InputSource which is later used as an argument
* to the XMLSchemaCollection object.
*/
String schemaElStr = null;
try {
schemaElStr = importedSchemaDoc.toStringWithConsume();
}
catch (XMLStreamException e) {
e.printStackTrace();
}
byte[] schemaElbytes = schemaElStr.getBytes();
InputSource schemaSource = new InputSource(new ByteArrayInputStream(schemaElbytes));
schemaSource.setSystemId(schemaURL);
try {
XmlSchemaCollection xsc = new XmlSchemaCollection();
// Plug in the selected woden URI Resolver
xsc.setSchemaResolver(new OMSchemaResolverAdapter(getURIResolver(), importEl));
schemaDef = xsc.read(schemaSource, null);
fImportedSchemas.put(schemaURL, schemaDef);
}
catch (XmlSchemaException e){
getErrorReporter().reportError(
new ErrorLocatorImpl(), //TODO line&col nos.
"WSDL522",
new Object[] {schemaURL},
ErrorReporter.SEVERITY_WARNING,
e);
}
}
if(schemaDef != null) {
schema.setSchemaDefinition(schemaDef);
}
else {
schema.setReferenceable(false);
}
return schema;
}