Search in sources :

Example 1 with MatchListener

use of org.exist.indexing.MatchListener in project exist by eXist-db.

the class HighlightMatches method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    context.pushDocumentContext();
    final Serializer serializer = context.getBroker().borrowSerializer();
    try (FunctionReference func = (FunctionReference) args[1].itemAt(0)) {
        MemTreeBuilder builder = context.getDocumentBuilder();
        NGramIndexWorker index = (NGramIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(NGramIndex.ID);
        DocumentBuilderReceiver docBuilder = new DocumentBuilderReceiver(builder);
        MatchCallback matchCb = new MatchCallback(func, docBuilder);
        ValueSequence result = new ValueSequence();
        for (SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
            NodeValue v = (NodeValue) i.nextItem();
            try {
                int nodeNr = builder.getDocument().getLastNode();
                if (v.getImplementationType() == NodeValue.IN_MEMORY_NODE) {
                    ((NodeImpl) v).copyTo(context.getBroker(), docBuilder);
                } else {
                    NodeProxy p = (NodeProxy) v;
                    MatchListener ml = index.getMatchListener(context.getBroker(), p, matchCb);
                    Receiver receiver;
                    if (ml == null)
                        receiver = docBuilder;
                    else {
                        ml.setNextInChain(docBuilder);
                        receiver = ml;
                    }
                    serializer.setReceiver(receiver);
                    serializer.toReceiver((NodeProxy) v, false);
                }
                result.add(builder.getDocument().getNode(++nodeNr));
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
                throw new XPathException(this, e.getMessage());
            }
        }
        return result;
    } finally {
        context.getBroker().returnSerializer(serializer);
        context.popDocumentContext();
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) NGramIndexWorker(org.exist.indexing.ngram.NGramIndexWorker) NGramMatchCallback(org.exist.indexing.ngram.NGramMatchCallback) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Receiver(org.exist.util.serializer.Receiver) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) NodeProxy(org.exist.dom.persistent.NodeProxy) SAXException(org.xml.sax.SAXException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence) FunctionReference(org.exist.xquery.value.FunctionReference) MatchListener(org.exist.indexing.MatchListener) Serializer(org.exist.storage.serializers.Serializer)

Example 2 with MatchListener

use of org.exist.indexing.MatchListener in project exist by eXist-db.

the class Serializer method setPrettyPrinter.

protected void setPrettyPrinter(Writer writer, boolean xmlDecl, NodeProxy root, boolean applyFilters) {
    outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, xmlDecl ? "no" : "yes");
    xmlout = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
    xmlout.setOutput(writer, outputProperties);
    if ("yes".equals(getProperty(EXistOutputKeys.EXPAND_XINCLUDES, "yes"))) {
        xinclude.setReceiver(xmlout);
        receiver = xinclude;
    } else {
        receiver = xmlout;
    }
    if (root != null && getHighlightingMode() != TAG_NONE) {
        final IndexController controller = broker.getIndexController();
        MatchListener listener = controller.getMatchListener(root);
        if (listener != null) {
            final MatchListener last = (MatchListener) listener.getLastInChain();
            last.setNextInChain(receiver);
            receiver = listener;
        }
    }
    if (root == null && applyFilters && customMatchListeners.getFirst() != null) {
        customMatchListeners.getLast().setNextInChain(receiver);
        receiver = customMatchListeners.getFirst();
    }
}
Also used : IndexController(org.exist.indexing.IndexController) MatchListener(org.exist.indexing.MatchListener)

Example 3 with MatchListener

use of org.exist.indexing.MatchListener in project exist by eXist-db.

the class Serializer method setupMatchListeners.

protected Receiver setupMatchListeners(NodeProxy p) {
    final Receiver oldReceiver = receiver;
    if (getHighlightingMode() != TAG_NONE) {
        final IndexController controller = broker.getIndexController();
        MatchListener listener = controller.getMatchListener(p);
        if (listener != null) {
            final MatchListener last = (MatchListener) listener.getLastInChain();
            last.setNextInChain(receiver);
            receiver = listener;
        }
    }
    return oldReceiver;
}
Also used : IndexController(org.exist.indexing.IndexController) Receiver(org.exist.util.serializer.Receiver) MatchListener(org.exist.indexing.MatchListener)

Example 4 with MatchListener

use of org.exist.indexing.MatchListener in project exist by eXist-db.

the class Serializer method setXSLHandler.

protected void setXSLHandler(NodeProxy root, boolean applyFilters) {
    if (templates != null && xslHandler != null) {
        final SAXResult result = new SAXResult();
        boolean processXInclude = "yes".equals(getProperty(EXistOutputKeys.EXPAND_XINCLUDES, "yes"));
        final ReceiverToSAX filter;
        if (processXInclude) {
            final Receiver xincludeReceiver = xinclude.getReceiver();
            if (xincludeReceiver != null && xincludeReceiver instanceof SAXSerializer) {
                filter = new ReceiverToSAX((SAXSerializer) xincludeReceiver);
            } else {
                filter = (ReceiverToSAX) xincludeReceiver;
            }
        } else {
            filter = (ReceiverToSAX) receiver;
        }
        result.setHandler(filter.getContentHandler());
        result.setLexicalHandler(filter.getLexicalHandler());
        filter.setLexicalHandler(xslHandler);
        filter.setContentHandler(xslHandler);
        xslHandler.setResult(result);
        if (processXInclude) {
            xinclude.setReceiver(new ReceiverToSAX(xslHandler));
            receiver = xinclude;
        } else {
            receiver = new ReceiverToSAX(xslHandler);
        }
    }
    if (root != null && getHighlightingMode() != TAG_NONE) {
        final IndexController controller = broker.getIndexController();
        MatchListener listener = controller.getMatchListener(root);
        if (listener != null) {
            final MatchListener last = (MatchListener) listener.getLastInChain();
            last.setNextInChain(receiver);
            receiver = listener;
        }
    }
    if (applyFilters && root == null && customMatchListeners.getFirst() != null) {
        customMatchListeners.getLast().setNextInChain(receiver);
        receiver = customMatchListeners.getFirst();
    }
}
Also used : SAXResult(javax.xml.transform.sax.SAXResult) ReceiverToSAX(org.exist.util.serializer.ReceiverToSAX) IndexController(org.exist.indexing.IndexController) Receiver(org.exist.util.serializer.Receiver) SAXSerializer(org.exist.util.serializer.SAXSerializer) MatchListener(org.exist.indexing.MatchListener)

Aggregations

MatchListener (org.exist.indexing.MatchListener)4 IndexController (org.exist.indexing.IndexController)3 Receiver (org.exist.util.serializer.Receiver)3 SAXResult (javax.xml.transform.sax.SAXResult)1 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1 NodeImpl (org.exist.dom.memtree.NodeImpl)1 NodeProxy (org.exist.dom.persistent.NodeProxy)1 NGramIndexWorker (org.exist.indexing.ngram.NGramIndexWorker)1 NGramMatchCallback (org.exist.indexing.ngram.NGramMatchCallback)1 Serializer (org.exist.storage.serializers.Serializer)1 ReceiverToSAX (org.exist.util.serializer.ReceiverToSAX)1 SAXSerializer (org.exist.util.serializer.SAXSerializer)1 XPathException (org.exist.xquery.XPathException)1 FunctionReference (org.exist.xquery.value.FunctionReference)1 NodeValue (org.exist.xquery.value.NodeValue)1 SequenceIterator (org.exist.xquery.value.SequenceIterator)1 ValueSequence (org.exist.xquery.value.ValueSequence)1 SAXException (org.xml.sax.SAXException)1