add Iterable and Iterator support to response writers: SOLR-177

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@514254 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2007-03-03 22:21:27 +00:00
parent f3bd9ebc57
commit cfb9f5f2f2
3 changed files with 34 additions and 28 deletions

View File

@ -532,24 +532,21 @@ class JSONWriter extends TextResponseWriter {
}
}
public void writeArray(String name, Object[] val) throws IOException {
writeArray(name, Arrays.asList(val));
writeArray(name, Arrays.asList(val).iterator());
}
public void writeArray(String name, Collection val) throws IOException {
public void writeArray(String name, Iterator val) throws IOException {
writer.write('[');
int sz = val.size();
incLevel();
boolean first=true;
for (Object o : val) {
if (first) {
first=false;
} else {
while( val.hasNext() ) {
if( !first ) indent();
writeVal(null, val.next());
if( val.hasNext() ) {
writer.write(',');
}
if (sz>1) indent();
writeVal(null, o);
first=false;
}
decLevel();
writer.write(']');

View File

@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@ -140,10 +141,12 @@ public abstract class TextResponseWriter {
writeMap(name, (Map)val, false, true);
} else if (val instanceof NamedList) {
writeNamedList(name, (NamedList)val);
} else if (val instanceof Collection) {
writeArray(name,(Collection)val);
} else if (val instanceof Iterable) {
writeArray(name,((Iterable)val).iterator());
} else if (val instanceof Object[]) {
writeArray(name,(Object[])val);
} else if (val instanceof Iterator) {
writeArray(name,(Iterator)val);
} else {
// default... for debugging only
writeStr(name, val.getClass().getName() + ':' + val.toString(), true);
@ -164,7 +167,7 @@ public abstract class TextResponseWriter {
public abstract void writeArray(String name, Object[] val) throws IOException;
public abstract void writeArray(String name, Collection val) throws IOException;
public abstract void writeArray(String name, Iterator val) throws IOException;
public abstract void writeNull(String name) throws IOException;

View File

@ -25,6 +25,7 @@ import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocSet;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.TextField;
import java.io.Writer;
import java.io.IOException;
@ -290,8 +291,10 @@ final public class XMLWriter {
FieldType ft = schema.getFieldType(fname);
***/
SchemaField sf = schema.getField(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()) {
@ -411,10 +414,12 @@ final public class XMLWriter {
writeMap(name, (Map)val);
} else if (val instanceof NamedList) {
writeNamedList(name, (NamedList)val);
} else if (val instanceof Collection) {
writeArray(name,(Collection)val);
} else if (val instanceof Iterable) {
writeArray(name,((Iterable)val).iterator());
} else if (val instanceof Object[]) {
writeArray(name,(Object[])val);
} else if (val instanceof Iterator) {
writeArray(name,(Iterator)val);
} else {
// default...
writeStr(name, val.getClass().getName() + ':' + val.toString());
@ -468,22 +473,23 @@ final public class XMLWriter {
}
public void writeArray(String name, Object[] val) throws IOException {
writeArray(name, Arrays.asList(val));
writeArray(name, Arrays.asList(val).iterator());
}
public void writeArray(String name, Collection val) throws IOException {
int sz = val.size();
startTag("arr", name, sz<=0);
incLevel();
for (Object o : val) {
// if (sz<indentThreshold) indent();
writeVal(null, o);
}
decLevel();
if (sz > 0) {
public void writeArray(String name, Iterator iter) throws IOException {
if( iter.hasNext() ) {
startTag("arr", name, false );
incLevel();
while( iter.hasNext() ) {
writeVal(null, iter.next());
}
decLevel();
if (doIndent) indent();
writer.write("</arr>");
}
else {
startTag("arr", name, true );
}
}
//