}
Sequence currentSequence = seq;
// if the current element exists, use it; otherwise create a new one
Element currentElement = elementExistsInSequence(frag.getLocalName(), frag.getShortName(), currentSequence);
boolean currentElementExists = (currentElement != null);
if (currentElement == null) {
currentElement = new Element();
// don't set the element name yet, as it may end up being a ref
ComplexType cType = new ComplexType();
Sequence sequence = new Sequence();
cType.setSequence(sequence);
currentElement.setComplexType(cType);
}
Element globalElement = null;
String fragUri = frag.getNamespaceURI();
if (fragUri != null) {
Schema s = getSchema(fragUri, null, schemaForNamespace, properties);
String targetNS = workingSchema.getTargetNamespace();
if ((s.isElementFormDefault() && !fragUri.equals(targetNS)) || (!s.isElementFormDefault() && fragUri.length() > 0)) {
// must generate a global element are create a reference to it
// if the global element exists, use it; otherwise create a new one
globalElement = (Element) s.getTopLevelElements().get(frag.getLocalName());
if (globalElement == null) {
globalElement = new Element();
globalElement.setName(frag.getLocalName());
ComplexType gCType = new ComplexType();
Sequence gSequence = new Sequence();
gCType.setSequence(gSequence);
globalElement.setComplexType(gCType);
s.addTopLevelElement(globalElement);
}
// if the current element doesn't exist set a ref and add it to the sequence
if (!currentElementExists) {
// ref won't have a complex type
currentElement.setComplexType(null);
currentElement.setRef(frag.getShortName());
currentSequence.addElement(currentElement);
currentElementExists = true;
}
// make the global element current
currentElement = globalElement;
}
}
// if we didn't process a global element, and the current element isn't already in the sequence, add it
if (!currentElementExists && globalElement == null) {
currentElement.setName(frag.getLocalName());
currentSequence.addElement(currentElement);
}
// set the correct sequence to use/return
currentSequence = currentElement.getComplexType().getSequence();
// don't process the last element in the path - let the calling mapping process it
frag = frag.getNextFragment();
if (frag.getNextFragment() != null && !frag.getNextFragment().nameIsText()) {
Element childElt = buildElement(frag, null, Occurs.ZERO, null);
currentSequence.addElement(childElt);
}
// call back into this method to process the next path element
return buildSchemaComponentsForXPath(frag, currentSequence, schemaForNamespace, workingSchema, properties);
}