setURIResolver(uriResolver);
URILocation uri = null;
//-- Get schemaLocation
String include = atts.getValue("schemaLocation");
if (include == null)
throw new SchemaException("'schemaLocation' attribute missing on 'include'");
if (include.indexOf("\\") != -1) {
String err = include+" is not a valid URI as defined by IETF RFC 2396.";
err += "The URI must not contain '\\'.";
throw new SchemaException(err);
}
try {
String documentBase = locator.getSystemId();
if (documentBase != null) {
if (!documentBase.endsWith("/"))
documentBase = documentBase.substring(0, documentBase.lastIndexOf("/") +1 );
}
uri = getURIResolver().resolve(include, documentBase);
} catch (URIException ure) {
throw new XMLException(ure);
}
if (uri != null)
include = uri.getAbsoluteURI();
//-- Has this schema location been included yet?
if (schema.includeProcessed(include)) {
return;
}
else if (include.equals(schema.getSchemaLocation())) {
return;
}
Schema includedSchema = null;
boolean alreadyLoaded = false;
//-- caching is on
if (state.cacheIncludedSchemas) {
if (uri instanceof SchemaLocation) {
includedSchema = ((SchemaLocation)uri).getSchema();
schema.cacheIncludedSchema(includedSchema);
alreadyLoaded = true;
}
//-- Have we already imported this XML Schema file?
if (state.processed(include)) {
includedSchema = state.getSchema(include);
schema.cacheIncludedSchema(includedSchema);
alreadyLoaded = true;
}
}
if (includedSchema == null)
includedSchema = new Schema();
else
state.markAsProcessed(include, includedSchema);
//-- keep track of the schemaLocation
schema.addInclude(include);
if (alreadyLoaded)
return;
Parser parser = null;
try {
parser = getSchemaContext().getParser();
}
catch(RuntimeException rte) {}
if (parser == null) {
throw new SchemaException("Error failed to create parser for include");
}
SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), true, state, getURIResolver());
if (state.cacheIncludedSchemas)
schemaUnmarshaller.setSchema(includedSchema);
else
schemaUnmarshaller.setSchema(schema);
Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
parser.setDocumentHandler(handler);
parser.setErrorHandler(handler);
try {
InputSource source = new InputSource(uri.getReader());
source.setSystemId(uri.getAbsoluteURI());
parser.parse(source);
}
catch(java.io.IOException ioe) {
throw new SchemaException("Error reading include file '"+include+"'");
}
catch(org.xml.sax.SAXException sx) {
throw new SchemaException(sx);
}
if (state.cacheIncludedSchemas) {
String ns = includedSchema.getTargetNamespace();
if (ns == null || ns == "")
includedSchema.setTargetNamespace(schema.getTargetNamespace());
else if (!ns.equals(schema.getTargetNamespace()))
throw new SchemaException("The target namespace of the included components must be the same as the target namespace of the including schema");
schema.cacheIncludedSchema(includedSchema);
}
}