String schemaLocation = atts.getValue(SchemaNames.SCHEMALOCATION_ATTR);
_schema = schema;
if (schemaLocation == null) {
//-- <redefine/> or <redefine> <annotation>(*) </redefine>
_redefineSchema = new RedefineSchema(schema);
_schema.addRedefineSchema(_redefineSchema);
return;
}
if (schemaLocation.indexOf("\\") != -1) {
String err = "'" + schemaLocation +
"' is not a valid URI as defined by IETF RFC 2396.";
err += "The URI mustn't contain '\\'.";
error(err);
}
try {
String documentBase = locator.getSystemId();
if (documentBase != null) {
if (!documentBase.endsWith("/"))
documentBase = documentBase.substring(0, documentBase.lastIndexOf('/') +1 );
}
uri = getURIResolver().resolve(schemaLocation, documentBase);
if (uri != null) {
schemaLocation = uri.getAbsoluteURI();
}
}
catch (URIException urix) {
throw new XMLException(urix);
}
//-- Schema object to hold import schema
boolean addSchema = false;
_redefineSchema = schema.getRedefineSchema(schemaLocation);
Schema importedSchema = null;
boolean alreadyLoaded = false;
//-- The schema is not yet loaded
if (_redefineSchema == null) {
if (uri instanceof SchemaLocation) {
importedSchema = ((SchemaLocation)uri).getSchema();
//-- set the main schema in order to handle
//-- redefinition at runtime
// importedSchema.addMainSchema(schema);
_redefineSchema = new RedefineSchema(schema, importedSchema);
schema.addRedefineSchema(_redefineSchema);
alreadyLoaded = true;
}
else {
importedSchema = new Schema();
addSchema = true;
}
}
else {
//-- check schema location, if different, allow merge
String tmpLocation = _redefineSchema.getOriginalSchema().getSchemaLocation();
alreadyLoaded = schemaLocation.equals(tmpLocation);
}
state.markAsProcessed(schemaLocation, importedSchema);
if (alreadyLoaded) return;
//-- Parser Schema
Parser parser = null;
try {
parser = getSchemaContext().getParser();
}
catch(RuntimeException rte) {}
if (parser == null) {
throw new SchemaException("Error failed to create parser for import");
}
//-- Create Schema object and setup unmarshaller
SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), state);
schemaUnmarshaller.setURIResolver(getURIResolver());
schemaUnmarshaller.setSchema(importedSchema);
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 import file '"+schemaLocation+"': "+ ioe);
}
catch(org.xml.sax.SAXException sx) {
throw new SchemaException(sx);
}
//-- namespace checking
String namespace = importedSchema.getTargetNamespace();
if ( namespace != null ) {
//-- Make sure targetNamespace is not the same as the
//-- importing schema, see section 4.2.2 in the
//-- XML Schema Recommendation
if (!namespace.equals(schema.getTargetNamespace()) ) {
String err = "The 'namespace' attribute in the <redefine> element must be the same of the targetNamespace of the global schema.\n"
+namespace+" is different from:"+schema.getTargetNamespace();
error (err);
}
} else {
importedSchema.setTargetNamespace(schema.getTargetNamespace());
}
//-- set the main schema in order to handle
//-- redefinition at runtime
// importedSchema.addMainSchema(schema);
_importedSchema = importedSchema;
_redefineSchema = new RedefineSchema(schema, _importedSchema);
//-- Add schema to list of redefine schemas (if not already present)
if (addSchema)
{
importedSchema.setSchemaLocation(schemaLocation);
_schema.addRedefineSchema(_redefineSchema);