// using global tlst here, so we shouldn't call any other
// function that uses it until we are done.
tlst.clear();
for (Object obj : doc.getFields()) {
Fieldable ff = (Fieldable)obj;
// skip this field if it is not a field to be returned.
if (returnFields!=null && !returnFields.contains(ff.name())) {
continue;
}
tlst.add(ff);
}
Collections.sort(tlst, fieldnameComparator);
int sz = tlst.size();
int fidx1 = 0, fidx2 = 0;
while (fidx1 < sz) {
Fieldable f1 = (Fieldable)tlst.get(fidx1);
String fname = f1.name();
// find the end of fields with this name
fidx2 = fidx1+1;
while (fidx2 < sz && fname.equals(((Fieldable)tlst.get(fidx2)).name()) ) {
fidx2++;
}
/***
// more efficient to use getFieldType instead of
// getField since that way dynamic fields won't have
// to create a SchemaField on the fly.
FieldType ft = schema.getFieldType(fname);
***/
SchemaField sf = schema.getFieldOrNull(fname);
if( sf == null ) {
sf = new SchemaField( fname, new TextField() );
}
if (fidx1+1 == fidx2) {
// single field value
if (version>=2100 && sf.multiValued()) {
startTag("arr",fname,false);
doIndent=false;
sf.write(this, null, f1);
writer.write("</arr>");
doIndent=defaultIndent;
} else {
sf.write(this, f1.name(), f1);
}
} else {
// multiple fields with same name detected
startTag("arr",fname,false);