}
}
}
// Here we've matched all subparts, sorted in matches
Iterator<Match> it = matches.iterator();
Match next = null;
if (it.hasNext()) next = it.next();
else {
// No substructure matched. It's all text.
addText(result,s,start,end);
//result.addContent(s.substring(start, end).trim());
return result;
}
Match curr = null;
int used = start;
do {
curr = next;
next = (it.hasNext())? it.next():null;
if (used<curr.start) {
addText(result,s,used,curr.start);
//result.addContent(s.substring(used, curr.start).trim());
used = curr.start;
} else if (used>curr.start) {
System.err.println("Overlapped matches among parts of "
+name+": "+used+" vs. "+curr.start);
}
SM mod = curr.matched;
if (curr.matched.isPattern()) {
Element patElt = new Element(mod.name);
int nb = curr.numBindings();
if (nb<0) patElt.addContent(curr.vals.get(0));
else for (int i=0; i<nb; i++) {
patElt.addContent(curr.bindingToXML(i));
}
result.addContent(patElt);
used = curr.end;
} else { // Start element, not pattern
used = curr.end;