use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class AddMatch method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return args[0];
}
final NodeValue nv = (NodeValue) args[0].itemAt(0);
if (!nv.isPersistentSet()) {
return nv;
}
final NodeProxy node = (NodeProxy) nv;
final int thisLevel = node.getNodeId().getTreeLevel();
String matchStr = null;
NodeId nodeId = null;
try {
for (final XMLStreamReader reader = context.getBroker().getXMLStreamReader(node, true); reader.hasNext(); ) {
final int status = reader.next();
if (status == XMLStreamConstants.CHARACTERS) {
matchStr = reader.getText();
nodeId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
break;
}
final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
final int otherLevel = otherId.getTreeLevel();
if (status == XMLStreamConstants.END_ELEMENT && otherLevel == thisLevel) {
// exit-for
break;
}
}
} catch (IOException | XMLStreamException e) {
throw new XPathException(this, ErrorCodes.FOER0000, "Exception caught while reading document");
}
if (nodeId != null) {
Match match = new NGramMatch(getContextId(), node.getNodeId(), matchStr);
match.addOffset(0, matchStr.length());
node.addMatch(match);
}
return node;
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class FastQSort method IntroSortLoopByNodeId.
private static final void IntroSortLoopByNodeId(NodeProxy[] a, int l, int r, int maxdepth) // ----------------------------------------------------
{
while ((r - l) > M) {
if (maxdepth <= 0) {
HeapSort.sortByNodeId(a, l, r);
return;
}
maxdepth--;
int i = (l + r) / 2;
int j;
NodeProxy partionElement;
// the array.
if (a[l].getNodeId().compareTo(a[i].getNodeId()) > 0) // Tri-Median Methode!
{
SwapVals.swap(a, l, i);
}
if (a[l].getNodeId().compareTo(a[r].getNodeId()) > 0) {
SwapVals.swap(a, l, r);
}
if (a[i].getNodeId().compareTo(a[r].getNodeId()) > 0) {
SwapVals.swap(a, i, r);
}
partionElement = a[i];
// loop through the array until indices cross
i = l + 1;
j = r - 1;
while (i <= j) {
// the partionElement starting from the leftIndex.
while ((i < r) && (partionElement.getNodeId().compareTo(a[i].getNodeId()) > 0)) ++i;
// the partionElement starting from the rightIndex.
while ((j > l) && (partionElement.getNodeId().compareTo(a[j].getNodeId()) < 0)) --j;
// if the indexes have not crossed, swap
if (i <= j) {
SwapVals.swap(a, i, j);
++i;
--j;
}
}
// must now sort the left partition.
if (l < j) {
IntroSortLoopByNodeId(a, l, j, maxdepth);
}
// must now sort the right partition.
if (i >= r) {
break;
}
l = i;
}
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class DocumentNameOrId method getResourceByAbsoluteId.
private Sequence getResourceByAbsoluteId(final IntegerValue ivAbsoluteId) throws XPathException, PermissionDeniedException, LockException, IOException {
final BigInteger absoluteId = ivAbsoluteId.toJavaObject(BigInteger.class);
final Tuple3<Integer, Integer, Byte> decoded = decodeAbsoluteResourceId(absoluteId);
final DocumentImpl doc = context.getBroker().getResourceById(decoded._1, decoded._3, decoded._2);
if (doc != null) {
try (final ManagedDocumentLock docLock = context.getBroker().getBrokerPool().getLockManager().acquireDocumentReadLock(doc.getURI())) {
if (doc instanceof BinaryDocument) {
final BinaryDocument bin = (BinaryDocument) doc;
final InputStream is = context.getBroker().getBinaryResource(bin);
return Base64BinaryDocument.getInstance(context, is);
} else {
return new NodeProxy(doc);
}
}
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class FunDoctype method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
final MutableDocumentSet docs = new DefaultDocumentSet();
for (int i = 0; i < getArgumentCount(); i++) {
final Sequence seq = getArgument(i).eval(contextSequence, contextItem);
for (final SequenceIterator j = seq.iterate(); j.hasNext(); ) {
final String next = j.nextItem().getStringValue();
try {
context.getBroker().getXMLResourcesByDoctype(next, docs);
} catch (final PermissionDeniedException | LockException e) {
LOG.error(e.getMessage(), e);
throw new XPathException(this, e);
}
}
}
final NodeSet result = new ExtArrayNodeSet(1);
for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
result.add(new NodeProxy(i.next(), NodeId.DOCUMENT_NODE));
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class Modification method deepCopy.
protected Sequence deepCopy(Sequence inSeq) throws XPathException {
context.pushDocumentContext();
final MemTreeBuilder builder = context.getDocumentBuilder();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
final Serializer serializer = context.getBroker().borrowSerializer();
serializer.setReceiver(receiver);
try {
final Sequence out = new ValueSequence();
for (final SequenceIterator i = inSeq.iterate(); i.hasNext(); ) {
Item item = i.nextItem();
if (item.getType() == Type.DOCUMENT) {
if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
final NodeHandle root = (NodeHandle) ((NodeProxy) item).getOwnerDocument().getDocumentElement();
item = new NodeProxy(root);
} else {
item = (Item) ((Document) item).getDocumentElement();
}
}
if (Type.subTypeOf(item.getType(), Type.NODE)) {
if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
final int last = builder.getDocument().getLastNode();
final NodeProxy p = (NodeProxy) item;
serializer.toReceiver(p, false, false);
if (p.getNodeType() == Node.ATTRIBUTE_NODE) {
item = builder.getDocument().getLastAttr();
} else {
item = builder.getDocument().getNode(last + 1);
}
} else {
((org.exist.dom.memtree.NodeImpl) item).deepCopy();
}
}
out.add(item);
}
return out;
} catch (final SAXException | DOMException e) {
throw new XPathException(this, e.getMessage(), e);
} finally {
context.getBroker().returnSerializer(serializer);
context.popDocumentContext();
}
}
Aggregations