Search in sources :

Example 21 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class Field method highlightMatches.

/**
 * Highlight matches in field content using the analyzer defined for the field.
 *
 * @param fieldName the name of the field
 * @param proxy node on which the field is defined
 * @param match the lucene match attached to the node
 * @param text the content of the field
 * @return a sequence of exist:field elements containing the field content with matches enclosed in exist:match
 * @throws XPathException in case of error
 * @throws IOException in case of a lucene error
 */
private Sequence highlightMatches(final String fieldName, final NodeProxy proxy, final LuceneMatch match, final Sequence text) throws XPathException, IOException {
    final LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
    final Map<Object, Query> terms = index.getTerms(match.getQuery());
    final NodePath path = LuceneMatchListener.getPath(proxy);
    final LuceneConfig config = index.getLuceneConfig(context.getBroker(), proxy.getDocumentSet());
    LuceneIndexConfig idxConf = config.getConfig(path).next();
    if (idxConf == null) {
        // no lucene index: no fields to highlight
        return Sequence.EMPTY_SEQUENCE;
    }
    final Analyzer analyzer = idxConf.getAnalyzer();
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        final InMemoryNodeSet result = new InMemoryNodeSet(text.getItemCount());
        for (final SequenceIterator si = text.iterate(); si.hasNext(); ) {
            final int nodeNr = builder.startElement(Namespaces.EXIST_NS, "field", "exist:field", null);
            final String content = si.nextItem().getStringValue();
            int currentPos = 0;
            try (final Reader reader = new StringReader(content);
                final TokenStream tokenStream = analyzer.tokenStream(fieldName, reader)) {
                tokenStream.reset();
                final MarkableTokenFilter stream = new MarkableTokenFilter(tokenStream);
                while (stream.incrementToken()) {
                    String token = stream.getAttribute(CharTermAttribute.class).toString();
                    final Query query = terms.get(token);
                    if (query != null) {
                        if (match.getQuery() instanceof PhraseQuery) {
                            final Term[] phraseTerms = ((PhraseQuery) match.getQuery()).getTerms();
                            if (token.equals(phraseTerms[0].text())) {
                                // Scan the following text and collect tokens to see
                                // if they are part of the phrase.
                                stream.mark();
                                int t = 1;
                                OffsetAttribute offset = stream.getAttribute(OffsetAttribute.class);
                                final int startOffset = offset.startOffset();
                                int endOffset = offset.endOffset();
                                while (stream.incrementToken() && t < phraseTerms.length) {
                                    token = stream.getAttribute(CharTermAttribute.class).toString();
                                    if (token.equals(phraseTerms[t].text())) {
                                        offset = stream.getAttribute(OffsetAttribute.class);
                                        endOffset = offset.endOffset();
                                        t++;
                                        if (t == phraseTerms.length) {
                                            break;
                                        }
                                    } else {
                                        break;
                                    }
                                }
                                if (t == phraseTerms.length) {
                                    if (currentPos < startOffset) {
                                        builder.characters(content.substring(currentPos, startOffset));
                                    }
                                    builder.startElement(Namespaces.EXIST_NS, "match", "exist:match", null);
                                    builder.characters(content.substring(startOffset, endOffset));
                                    builder.endElement();
                                    currentPos = endOffset;
                                }
                            }
                        // End of phrase handling
                        } else {
                            final OffsetAttribute offset = stream.getAttribute(OffsetAttribute.class);
                            if (currentPos < offset.startOffset()) {
                                builder.characters(content.substring(currentPos, offset.startOffset()));
                            }
                            builder.startElement(Namespaces.EXIST_NS, "match", "exist:match", null);
                            builder.characters(content.substring(offset.startOffset(), offset.endOffset()));
                            builder.endElement();
                            currentPos = offset.endOffset();
                        }
                    }
                }
            }
            if (currentPos < content.length() - 1) {
                builder.characters(content.substring(currentPos));
            }
            builder.endElement();
            result.add(builder.getDocument().getNode(nodeNr));
        }
        return result;
    } finally {
        context.popDocumentContext();
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) Reader(java.io.Reader) StringReader(java.io.StringReader) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) NodePath(org.exist.storage.NodePath) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) StringReader(java.io.StringReader) OffsetAttribute(org.apache.lucene.analysis.tokenattributes.OffsetAttribute)

Example 22 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class IdFunction method functionId.

/**
 * Returns a document describing the accounts of the executing process
 *
 * @return An in-memory document describing the accounts
 */
private org.exist.dom.memtree.DocumentImpl functionId() throws XPathException {
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        builder.startElement(new QName("id", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
        builder.startElement(new QName("real", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
        subjectToXml(builder, context.getRealUser());
        builder.endElement();
        if (context.getRealUser().getId() != context.getEffectiveUser().getId()) {
            builder.startElement(new QName("effective", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
            subjectToXml(builder, context.getEffectiveUser());
            builder.endElement();
        }
        builder.endElement();
        builder.endDocument();
        return builder.getDocument();
    } finally {
        context.popDocumentContext();
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName)

Example 23 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class PermissionsFunction method permissionsToXml.

private org.exist.dom.memtree.DocumentImpl permissionsToXml(final Permission permission) {
    context.pushDocumentContext();
    final MemTreeBuilder builder = context.getDocumentBuilder();
    builder.startDocument();
    builder.startElement(new QName("permission", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
    builder.addAttribute(new QName("owner", XMLConstants.NULL_NS_URI), permission.getOwner().getName());
    builder.addAttribute(new QName("group", XMLConstants.NULL_NS_URI), permission.getGroup().getName());
    builder.addAttribute(new QName("mode", XMLConstants.NULL_NS_URI), permission.toString());
    if (permission instanceof SimpleACLPermission) {
        final SimpleACLPermission aclPermission = (SimpleACLPermission) permission;
        builder.startElement(new QName("acl", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
        builder.addAttribute(new QName("entries", XMLConstants.NULL_NS_URI), String.valueOf(aclPermission.getACECount()));
        for (int i = 0; i < aclPermission.getACECount(); i++) {
            builder.startElement(new QName("ace", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
            builder.addAttribute(new QName("index", XMLConstants.NULL_NS_URI), String.valueOf(i));
            builder.addAttribute(new QName("target", XMLConstants.NULL_NS_URI), aclPermission.getACETarget(i).name());
            builder.addAttribute(new QName("who", XMLConstants.NULL_NS_URI), aclPermission.getACEWho(i));
            builder.addAttribute(new QName("access_type", XMLConstants.NULL_NS_URI), aclPermission.getACEAccessType(i).name());
            builder.addAttribute(new QName("mode", XMLConstants.NULL_NS_URI), aclPermission.getACEModeString(i));
            builder.endElement();
        }
        builder.endElement();
    }
    builder.endElement();
    builder.endDocument();
    final org.exist.dom.memtree.DocumentImpl doc = builder.getDocument();
    context.popDocumentContext();
    return doc;
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName)

Example 24 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class FunctionTrace method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    logger.info("Entering " + SystemModule.PREFIX + ":{}", getName().getLocalPart());
    if (isCalledAs("clear-trace")) {
        logger.info("Entering the " + SystemModule.PREFIX + ":clear-trace XQuery function");
        context.getBroker().getBrokerPool().getPerformanceStats().clear();
    } else if (isCalledAs("enable-tracing")) {
        logger.info("Entering the " + SystemModule.PREFIX + ":enable-tracing XQuery function");
        final boolean enable = args[0].effectiveBooleanValue();
        context.getBroker().getBrokerPool().getPerformanceStats().setEnabled(enable);
        if (getArgumentCount() == 2) {
            if (args[1].effectiveBooleanValue()) {
                context.getBroker().getConfiguration().setProperty(Profiler.CONFIG_PROPERTY_TRACELOG, Boolean.TRUE);
            } else {
                context.getBroker().getConfiguration().setProperty(Profiler.CONFIG_PROPERTY_TRACELOG, Boolean.FALSE);
            }
        }
    } else if (isCalledAs("tracing-enabled")) {
        logger.info("Entering the " + SystemModule.PREFIX + ":tracing-enabled XQuery function");
        logger.info("Exiting " + SystemModule.PREFIX + ":{}", getName().getLocalPart());
        return BooleanValue.valueOf(context.getBroker().getBrokerPool().getPerformanceStats().isEnabled());
    } else {
        logger.info("Entering the " + SystemModule.PREFIX + ":trace XQuery function");
        context.getProfiler().reset();
        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            builder.startDocument();
            final BrokerPool brokerPool = context.getBroker().getBrokerPool();
            brokerPool.getPerformanceStats().toXML(builder);
            builder.endDocument();
            logger.info("Exiting " + SystemModule.PREFIX + ":{}", getName().getLocalPart());
            return (NodeValue) builder.getDocument().getDocumentElement();
        } finally {
            context.popDocumentContext();
        }
    }
    logger.info("Exiting " + SystemModule.PREFIX + ":{}", getName().getLocalPart());
    return Sequence.EMPTY_SEQUENCE;
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) BrokerPool(org.exist.storage.BrokerPool)

Example 25 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class GetRunningJobs method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole()) {
        throw (new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to get the list of running jobs"));
    }
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        builder.startElement(new QName("jobs", NAMESPACE_URI, PREFIX), null);
        final BrokerPool brokerPool = context.getBroker().getBrokerPool();
        final ProcessMonitor monitor = brokerPool.getProcessMonitor();
        final ProcessMonitor.JobInfo[] jobs = monitor.runningJobs();
        for (ProcessMonitor.JobInfo job : jobs) {
            final Thread process = job.getThread();
            final Date startDate = new Date(job.getStartTime());
            builder.startElement(new QName("job", NAMESPACE_URI, PREFIX), null);
            builder.addAttribute(new QName("id", null, null), process.getName());
            builder.addAttribute(new QName("action", null, null), job.getAction());
            builder.addAttribute(new QName("start", null, null), new DateTimeValue(startDate).getStringValue());
            builder.addAttribute(new QName("info", null, null), job.getAddInfo().toString());
            builder.endElement();
        }
        builder.endElement();
        builder.endDocument();
        return (NodeValue) builder.getDocument().getDocumentElement();
    } finally {
        context.popDocumentContext();
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) ProcessMonitor(org.exist.storage.ProcessMonitor) BrokerPool(org.exist.storage.BrokerPool) Date(java.util.Date)

Aggregations

MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)81 QName (org.exist.dom.QName)28 XPathException (org.exist.xquery.XPathException)23 Sequence (org.exist.xquery.value.Sequence)19 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)17 SAXException (org.xml.sax.SAXException)16 NodeValue (org.exist.xquery.value.NodeValue)15 IOException (java.io.IOException)14 NodeImpl (org.exist.dom.memtree.NodeImpl)14 Item (org.exist.xquery.value.Item)8 SequenceIterator (org.exist.xquery.value.SequenceIterator)8 Test (org.junit.Test)8 XQueryContext (org.exist.xquery.XQueryContext)7 ValueSequence (org.exist.xquery.value.ValueSequence)7 AttributesImpl (org.xml.sax.helpers.AttributesImpl)7 DocumentImpl (org.exist.dom.memtree.DocumentImpl)6 BrokerPool (org.exist.storage.BrokerPool)6 InputSource (org.xml.sax.InputSource)6 MalformedURLException (java.net.MalformedURLException)5 Path (java.nio.file.Path)5