// Build the Map of substitution groups.
Map substitutionGroups = new HashMap();
for (int i = 0; i < myChilds.length; i++) {
if (myChilds[i] instanceof XSElement) {
XSElement element = (XSElement) myChilds[i];
XsQName qName = element.getSubstitutionGroupName();
if (qName != null) {
SubstitutionGroup group = (SubstitutionGroup) substitutionGroups.get(qName);
if (group == null) {
XSElement head = pSchema.getElement(qName);
if (head == null) {
throw new LocSAXException("The substituted element " + qName + " is missing in the schema.",
element.getLocator());
}
if (head.isBlockedForSubstitution()) {
throw new LocSAXException("The substituted element " + qName + " is blocked for substitution.",
element.getLocator());
}
group = new SubstitutionGroup(head);
if (!head.isAbstract()) {
group.addMember(head);
}
substitutionGroups.put(qName, group);
}
group.addMember(element);
}
}
}
// For any substitution group: Build an implicit choice group, which
// may be used to replace the substitution groups head, if required.
for (Iterator iter = substitutionGroups.values().iterator(); iter.hasNext(); ) {
SubstitutionGroup group = (SubstitutionGroup) iter.next();
XSElementImpl head = (XSElementImpl) group.getHead();
XsObject object = head.getXsObject();
XsESchema syntaxSchema = object.getXsESchema();
// Find a name for the group
String namespace = syntaxSchema.getTargetNamespace().toString();
String localName = head.getName().getLocalName() + "Group";
XsQName suggestion = new XsQName(namespace, localName);
if (pSchema.getGroup(suggestion) != null) {
for (int i = 0; ; i++) {
suggestion = new XsQName(namespace, localName + i);
if (pSchema.getGroup(suggestion) == null) {
break;
}
}
}
XsTNamedGroup namedGroup = object.getObjectFactory().newXsTNamedGroup(syntaxSchema);
namedGroup.setName(new XsNCName(suggestion.getLocalName()));
XsTSimpleExplicitGroup choice = namedGroup.createChoice();
XSElement[] members = group.getMembers();
for (int j = 0; j < members.length; j++) {
XSElement member = members[j];
XsTLocalElement memberElement = choice.createElement();
memberElement.setRef(member.getName());
}
XSGroupImpl xsGroup = (XSGroupImpl) getSchema().getXSObjectFactory().newXSGroup(pSchema, namedGroup);
pSchema.add(xsGroup);
head.setSubstitutionGroup(xsGroup);