private Element generateElement (String parentName, Span span, List childSpans, THashMap tree)
{
Element parentElt = new Element (parentName);
if (childSpans == null || childSpans.isEmpty ()) {
parentElt.setContent (new Text (span.getText ()));
} else {
List childElts = new ArrayList (childSpans.size());
int start = span.getStartIdx ();
int current = 0;
for (int i = 0; i < childSpans.size(); i++) {
LabeledSpan childSpan = (LabeledSpan) childSpans.get (i);
Label childLabel = childSpan.getLabel();
int childStart = childSpan.getStartIdx () - start;
if (childStart > current) {
childElts.add (new Text (span.getText().substring (current, childStart)));
}
if (childLabel == backgroundTag) {
childElts.add (new Text (childSpan.getText()));
} else {
String name = childLabel.getEntry ().toString();
List grandchildren = (List) tree.get (childSpan);
childElts.add (generateElement (name, childSpan, grandchildren, tree));
}
current = childSpan.getEndIdx () - start;
}
if (current < span.getEndIdx ())
childElts.add (new Text (span.getText().substring (current)));
parentElt.addContent (childElts);
}
return parentElt;