use of org.exist.dom.persistent.AVLTreeNodeSet in project exist by eXist-db.
the class OrderedValueSequence method toNodeSet.
@Override
public NodeSet toNodeSet() throws XPathException {
// return early
if (isEmpty()) {
return NodeSet.EMPTY_SET;
}
// for this method to work, all items have to be nodes
if (itemType != Type.ANY_TYPE && Type.subTypeOf(itemType, Type.NODE)) {
// Was ExtArrayNodeset() which orders the nodes in document order
// The order seems to change between different invocations !!!
final NodeSet set = new AVLTreeNodeSet();
// NodeSet set = new ArraySet(100);
for (int i = 0; i < count; i++) {
NodeValue v = (NodeValue) items[i].item;
if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
// found an in-memory document
final org.exist.dom.memtree.DocumentImpl doc = v.getType() == Type.DOCUMENT ? (org.exist.dom.memtree.DocumentImpl) v : ((NodeImpl) v).getOwnerDocument();
if (doc == null) {
continue;
}
// make this document persistent: doc.makePersistent()
// returns a map of all root node ids mapped to the corresponding
// persistent node. We scan the current sequence and replace all
// in-memory nodes with their new persistent node objects.
final DocumentImpl expandedDoc = doc.expandRefs(null);
final org.exist.dom.persistent.DocumentImpl newDoc = expandedDoc.makePersistent();
if (newDoc != null) {
final NodeId rootId = newDoc.getBrokerPool().getNodeFactory().createInstance();
for (int j = i; j < count; j++) {
v = (NodeValue) items[j].item;
if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
NodeImpl node = (NodeImpl) v;
final Document nodeOwnerDoc = node.getNodeType() == Node.DOCUMENT_NODE ? (org.exist.dom.memtree.DocumentImpl) v : ((NodeImpl) v).getOwnerDocument();
if (nodeOwnerDoc == doc) {
node = expandedDoc.getNode(node.getNodeNumber());
NodeId nodeId = node.getNodeId();
if (nodeId == null) {
throw new XPathException("Internal error: nodeId == null");
}
if (node.getNodeType() == Node.DOCUMENT_NODE) {
nodeId = rootId;
} else {
nodeId = rootId.append(nodeId);
}
final NodeProxy p = new NodeProxy(newDoc, nodeId, node.getNodeType());
if (p != null) {
// replace the node by the NodeProxy
items[j].item = p;
}
}
}
}
}
set.add((NodeProxy) items[i].item);
} else {
set.add((NodeProxy) v);
}
}
return set;
} else {
throw new XPathException("Type error: the sequence cannot be converted into" + " a node set. Item type is " + Type.getTypeName(itemType));
}
}
use of org.exist.dom.persistent.AVLTreeNodeSet in project exist by eXist-db.
the class XPathUtil method javaObjectToXPath.
public static final Sequence javaObjectToXPath(Object obj, XQueryContext context, boolean expandChars) throws XPathException {
if (obj == null) {
// return Sequence.EMPTY_SEQUENCE;
return null;
} else if (obj instanceof Sequence) {
return (Sequence) obj;
} else if (obj instanceof String) {
final StringValue v = new StringValue((String) obj);
return (expandChars ? v.expand() : v);
} else if (obj instanceof Boolean) {
return BooleanValue.valueOf(((Boolean) obj));
} else if (obj instanceof Float) {
return new FloatValue(((Float) obj));
} else if (obj instanceof Double) {
return new DoubleValue(((Double) obj));
} else if (obj instanceof Short) {
return new IntegerValue(((Short) obj), Type.SHORT);
} else if (obj instanceof Integer) {
return new IntegerValue(((Integer) obj), Type.INT);
} else if (obj instanceof Long) {
return new IntegerValue(((Long) obj), Type.LONG);
} else if (obj instanceof byte[]) {
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream((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) {
context.pushDocumentContext();
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
streamer.setContentHandler(receiver);
streamer.serialize((Node) obj, false);
if (obj instanceof Document) {
return builder.getDocument();
} else {
return builder.getDocument().getNode(1);
}
} catch (final SAXException e) {
throw new XPathException("Failed to transform node into internal model: " + e.getMessage());
} finally {
context.popDocumentContext();
SerializerPool.getInstance().returnObject(streamer);
}
} else if (obj instanceof List<?>) {
boolean createNodeSequence = true;
for (Object next : ((List<?>) obj)) {
if (!(next instanceof NodeProxy)) {
createNodeSequence = false;
break;
}
}
Sequence seq = createNodeSequence ? new AVLTreeNodeSet() : new ValueSequence();
for (Object o : ((List<?>) obj)) {
seq.add((Item) javaObjectToXPath(o, context, expandChars));
}
return seq;
} else if (obj instanceof NodeList) {
context.pushDocumentContext();
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
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 {
context.popDocumentContext();
SerializerPool.getInstance().returnObject(streamer);
}
} else if (obj instanceof Object[]) {
boolean createNodeSequence = true;
final Object[] array = (Object[]) obj;
for (Object arrayItem : array) {
if (!(arrayItem instanceof NodeProxy)) {
createNodeSequence = false;
break;
}
}
Sequence seq = createNodeSequence ? new AVLTreeNodeSet() : new ValueSequence();
for (Object arrayItem : array) {
seq.add((Item) javaObjectToXPath(arrayItem, context, expandChars));
}
return seq;
} else {
return new JavaObjectValue(obj);
}
}
Aggregations