int offset = 0;
SchemaElement[] schemas = new SchemaElement[count];
for (Iterator iter = resolves.iterator(); iter.hasNext();) {
// unmarshal document to construct schema structure
ISchemaResolver resolver = (ISchemaResolver)iter.next();
ictx.setDocument(resolver.getContent(), resolver.getName(), null);
ictx.setUserContext(vctx);
SchemaElement schema = new SchemaElement();
((IUnmarshallable)schema).unmarshal(ictx);
// set resolver for use during schema processing
schemas[offset++] = schema;
schema.setResolver(resolver);
String id = resolver.getId();
vctx.setSchema(id, schema);
// verify schema roundtripping if debug enabled
if (s_logger.isDebugEnabled()) {
try {
// determine encoding of input document
String enc = ((UnmarshallingContext)ictx).getInputEncoding();
if (enc == null) {
enc = "UTF-8";
}
// marshal root object back out to document in memory
IMarshallingContext mctx = factory.createMarshallingContext();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mctx.setIndent(2);
mctx.marshalDocument(schema, "UTF-8", null, bos);
// compare with original input document
InputStreamReader brdr =
new InputStreamReader(new ByteArrayInputStream(bos.toByteArray()), "UTF-8");
InputStreamReader frdr = new InputStreamReader(resolver.getContent(), enc);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream pstream = new PrintStream(baos);
DocumentComparator comp = new DocumentComparator(pstream);
if (comp.compare(frdr, brdr)) {
// report schema roundtripped successfully
s_logger.debug("Successfully roundtripped schema " + id);
} else {
// report problems in roundtripping schema
s_logger.debug("Errors in roundtripping schema " + id);
pstream.flush();
s_logger.debug(baos.toString());
}
} catch (XmlPullParserException e) {
s_logger.debug("Error during schema roundtripping", e);
}
}
}
// to correctly handle namespaces for includes, process namespaced schemas first
ArrayList ordereds = new ArrayList();
vctx.clearTraversed();
for (int i = 0; i < count; i++) {
SchemaElement schema = schemas[i];
if (schema.getTargetNamespace() != null) {
ordereds.add(schema);
}
}
// check for no namespaced schemas, but effective namespace supplied
if (ordereds.size() == 0 && uri != null) {
// generate a schema to include all the no-namespace schemas into namespace
SchemaElement schema = new SchemaElement();
schema.setTargetNamespace(uri);
SyntheticSchemaResolver resolver = new SyntheticSchemaResolver();
for (int i = 0; i < count; i++) {
SchemaElement inclschema = schemas[i];
inclschema.setEffectiveNamespace(uri);
IncludeElement include = new IncludeElement();
ISchemaResolver inclresolver = inclschema.getResolver();
include.setLocation(inclresolver.getId());
resolver.addResolver(inclresolver);
schema.getSchemaChildren().add(include);
}
schema.setResolver(resolver);
ordereds.add(schema);