else if (obj instanceof Long)
{return new IntegerValue(((Long) obj).longValue(), Type.LONG);}
else if (obj instanceof byte[])
{return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new ByteArrayInputStream((byte[]) obj));}
else if (obj instanceof ResourceSet) {
final Sequence seq = new AVLTreeNodeSet();
try {
final DBBroker broker = context.getBroker();
for(final ResourceIterator it = ((ResourceSet)obj).getIterator(); it.hasMoreResources();) {
seq.add(getNode(broker, (XMLResource)it.nextResource()));
}
} catch (final XMLDBException xe) {
throw new XPathException("Failed to convert ResourceSet to node: " + xe.getMessage());
}
return seq;
} else if (obj instanceof XMLResource) {
return getNode(context.getBroker(), (XMLResource)obj);
} else if (obj instanceof Node) {
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
final MemTreeBuilder builder = new MemTreeBuilder(context);
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(
builder);
streamer.setContentHandler(receiver);
streamer.serialize((Node) obj, false);
return builder.getDocument().getNode(1);
} catch (final SAXException e) {
throw new XPathException(
"Failed to transform node into internal model: "
+ e.getMessage());
} finally {
SerializerPool.getInstance().returnObject(streamer);
}
} else if (obj instanceof List<?>) {
boolean createNodeSequence = true;
Object next;
for (final Iterator<?> i = ((List<?>) obj).iterator(); i.hasNext();) {
next = i.next();
if (!(next instanceof NodeProxy))
{createNodeSequence = false;}
}
Sequence seq = null;
if (createNodeSequence)
{seq = new AVLTreeNodeSet();}
else
{seq = new ValueSequence();}
for (final Iterator<?> i = ((List<?>) obj).iterator(); i.hasNext();) {
seq.add((Item) javaObjectToXPath(i.next(), context, expandChars));
}
return seq;
} else if (obj instanceof NodeList) {
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
final MemTreeBuilder builder = new MemTreeBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(
builder);
streamer.setContentHandler(receiver);
final ValueSequence seq = new ValueSequence();
final NodeList nl = (NodeList) obj;
int last = builder.getDocument().getLastNode();
for (int i = 0; i < nl.getLength(); i++) {
final Node n = nl.item(i);
streamer.serialize(n, false);
final NodeImpl created = builder.getDocument().getNode(last + 1);
seq.add(created);
last = builder.getDocument().getLastNode();
}
return seq;
} catch (final SAXException e) {
throw new XPathException(
"Failed to transform node into internal model: "
+ e.getMessage());
} finally {
SerializerPool.getInstance().returnObject(streamer);
}
} else if (obj instanceof Object[]) {
boolean createNodeSequence = true;
final Object[] array = (Object[]) obj;
for (int i = 0; i < array.length; i++) {
if (!(array[i] instanceof NodeProxy))
{createNodeSequence = false;}
}
Sequence seq = null;
if (createNodeSequence)
{seq = new AVLTreeNodeSet();}
else
{seq = new ValueSequence();}
for (int i = 0; i < array.length; i++) {
seq.add((Item) javaObjectToXPath(array[i], context, expandChars));
}
return seq;
} else
{return new JavaObjectValue(obj);}
}