// each item is checked if it is a duplicate entry and copied only if it isn't
Iterator itemsIterator = aSchema.getItems().getIterator();
Object schemaObject = null;
XmlSchemaElement schemaElement = null;
XmlSchemaType schemaType = null;
XmlSchemaInclude schemaInclude = null;
QName qName = null;
List existingIncludes = getExistingIncludes(existingSchema);
while (itemsIterator.hasNext()) {
schemaObject = itemsIterator.next();
if (schemaObject instanceof XmlSchemaElement) {
schemaElement = (XmlSchemaElement) schemaObject;
qName = schemaElement.getQName();
// if the element does not exist in the existing schema
if (existingSchema.getElementByName(qName) == null) {
// add it to the existing schema
existingSchema.getElements().add(qName, schemaElement);
existingSchema.getItems().add(schemaElement);
}
} else if (schemaObject instanceof XmlSchemaType) {
schemaType = (XmlSchemaType) itemsIterator.next();
qName = schemaType.getQName();
// if the element does not exist in the existing schema
if (existingSchema.getElementByName(qName) == null) {
// add it to the existing schema
existingSchema.getSchemaTypes().add(qName, schemaType);
existingSchema.getItems().add(schemaType);
// add imports
addImports(existingSchema, qName);
}
} else if (schemaObject instanceof XmlSchemaInclude) {
schemaInclude = (XmlSchemaInclude) itemsIterator.next();
if (!existingIncludes.contains(schemaInclude.getSchemaLocation())) {
existingSchema.getIncludes().add(schemaInclude);
existingSchema.getItems().add(schemaInclude);
}
}
}