Search in sources :

Example 16 with NodeProxy

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;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XMLStreamReader(javax.xml.stream.XMLStreamReader) ExtendedXMLStreamReader(org.exist.stax.ExtendedXMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XPathException(org.exist.xquery.XPathException) NodeId(org.exist.numbering.NodeId) NGramMatch(org.exist.indexing.ngram.NGramMatch) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match) NGramMatch(org.exist.indexing.ngram.NGramMatch)

Example 17 with NodeProxy

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;
    }
}
Also used : NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 18 with NodeProxy

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;
}
Also used : BigInteger(java.math.BigInteger) BinaryDocument(org.exist.dom.persistent.BinaryDocument) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) InputStream(java.io.InputStream) BigInteger(java.math.BigInteger) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 19 with NodeProxy

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;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 20 with NodeProxy

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();
    }
}
Also used : ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Document(org.w3c.dom.Document) NodeProxy(org.exist.dom.persistent.NodeProxy) SAXException(org.xml.sax.SAXException) Item(org.exist.xquery.value.Item) DOMException(org.w3c.dom.DOMException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeHandle(org.exist.dom.persistent.NodeHandle) ValueSequence(org.exist.xquery.value.ValueSequence) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

NodeProxy (org.exist.dom.persistent.NodeProxy)79 DocumentImpl (org.exist.dom.persistent.DocumentImpl)18 Sequence (org.exist.xquery.value.Sequence)17 NodeSet (org.exist.dom.persistent.NodeSet)16 NodeId (org.exist.numbering.NodeId)16 XPathException (org.exist.xquery.XPathException)16 IOException (java.io.IOException)12 NewArrayNodeSet (org.exist.dom.persistent.NewArrayNodeSet)10 PermissionDeniedException (org.exist.security.PermissionDeniedException)10 LockException (org.exist.util.LockException)10 NodeValue (org.exist.xquery.value.NodeValue)10 Node (org.w3c.dom.Node)9 Document (org.w3c.dom.Document)8 SAXException (org.xml.sax.SAXException)8 NodeImpl (org.exist.dom.memtree.NodeImpl)7 ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)7 SequenceIterator (org.exist.xquery.value.SequenceIterator)7 ReentrantLock (java.util.concurrent.locks.ReentrantLock)6 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)6 Match (org.exist.dom.persistent.Match)6