if (result == null || result instanceof Undefined) {
return NO_DOCUMENTS;
}
if (result instanceof RhinoDocument) {
final RhinoDocument rhinoDocument = (RhinoDocument) result;
final Document document = rhinoDocument.toDocument(doc.getId(), defaults, database);
return Collections.singleton(document);
}
if (result instanceof NativeArray) {
final NativeArray nativeArray = (NativeArray) result;
final Collection<Document> arrayResult = new ArrayList<Document>((int) nativeArray.getLength());
for (int i = 0; i < (int) nativeArray.getLength(); i++) {
if (nativeArray.get(i, null) instanceof RhinoDocument) {
final RhinoDocument rhinoDocument = (RhinoDocument) nativeArray.get(i, null);
final Document document = rhinoDocument.toDocument(
doc.getId(),
defaults,
database);
arrayResult.add(document);
}