Search in sources :

Example 76 with QName

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

the class LuceneIndexTest method configuration.

@Test
public void configuration() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG4, XML4, "test.xml");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        checkIndex(docs, broker, new QName[] { new QName("a") }, "x", 1);
        checkIndex(docs, broker, new QName[] { new QName("c") }, "x", 1);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "/test[ft:query(a, 'x')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/test[ft:query(.//c, 'x')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/test[ft:query(b, 'x')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) QName(org.exist.dom.QName) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 77 with QName

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

the class Query method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (contextItem != null)
        contextSequence = contextItem.toSequence();
    if (contextSequence != null && !contextSequence.isPersistentSet())
        // in-memory docs won't have an index
        return Sequence.EMPTY_SEQUENCE;
    NodeSet result;
    if (preselectResult == null) {
        long start = System.currentTimeMillis();
        Sequence input = getArgument(0).eval(contextSequence);
        if (!(input instanceof VirtualNodeSet) && input.isEmpty())
            result = NodeSet.EMPTY_SET;
        else {
            NodeSet inNodes = input.toNodeSet();
            DocumentSet docs = inNodes.getDocumentSet();
            LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
            Item key = getKey(contextSequence, contextItem);
            List<QName> qnames = null;
            if (contextQName != null) {
                qnames = new ArrayList<>(1);
                qnames.add(contextQName);
            }
            QueryOptions options = parseOptions(this, contextSequence, contextItem, 3);
            try {
                if (key != null && Type.subTypeOf(key.getType(), Type.ELEMENT)) {
                    final Element queryXML = (Element) ((NodeValue) key).getNode();
                    result = index.query(getExpressionId(), docs, inNodes, qnames, queryXML, NodeSet.ANCESTOR, options);
                } else {
                    final String query = key == null ? null : key.getStringValue();
                    result = index.query(getExpressionId(), docs, inNodes, qnames, query, NodeSet.ANCESTOR, options);
                }
            } catch (IOException | org.apache.lucene.queryparser.classic.ParseException e) {
                throw new XPathException(this, e.getMessage());
            }
        }
        if (context.getProfiler().traceFunctions()) {
            context.getProfiler().traceIndexUsage(context, "lucene", this, PerformanceStats.BASIC_INDEX, System.currentTimeMillis() - start);
        }
    } else {
        // DW: contextSequence can be null
        contextStep.setPreloadedData(contextSequence.getDocumentSet(), preselectResult);
        result = getArgument(0).eval(contextSequence).toNodeSet();
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) VirtualNodeSet(org.exist.dom.persistent.VirtualNodeSet) QName(org.exist.dom.QName) Element(org.w3c.dom.Element) IOException(java.io.IOException) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) VirtualNodeSet(org.exist.dom.persistent.VirtualNodeSet) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 78 with QName

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

the class RpcConnection method getIndexedElements.

private List<List> getIndexedElements(final XmldbURI collUri, final boolean inclusive) throws EXistException, PermissionDeniedException {
    return this.<List<List>>readCollection(collUri).apply((collection, broker, transaction) -> {
        final Occurrences[] occurrences = broker.getElementIndex().scanIndexedElements(collection, inclusive);
        final List<List> result = new ArrayList<>(occurrences.length);
        for (final Occurrences occurrence : occurrences) {
            final QName qname = (QName) occurrence.getTerm();
            final List temp = new ArrayList(4);
            temp.add(qname.getLocalPart());
            temp.add(qname.getNamespaceURI());
            temp.add(qname.getPrefix() == null ? "" : qname.getPrefix());
            temp.add(occurrence.getOccurrences());
            result.add(temp);
        }
        return result;
    });
}
Also used : QName(org.exist.dom.QName)

Example 79 with QName

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

the class FunctionFunction method lookupFunction.

private FunctionCall lookupFunction(String funcName, int arity) throws XPathException {
    // try to parse the qname
    QName qname;
    try {
        qname = QName.parse(context, funcName, context.getDefaultFunctionNamespace());
    } catch (final QName.IllegalQNameException e) {
        throw new XPathException(this, ErrorCodes.XPST0081, "No namespace defined for prefix " + funcName);
    }
    // check if the function is from a module
    final Module[] modules = context.getModules(qname.getNamespaceURI());
    UserDefinedFunction func = null;
    if (isEmpty(modules)) {
        func = context.resolveFunction(qname, arity);
    } else {
        for (final Module module : modules) {
            func = ((ExternalModule) module).getFunction(qname, arity, context);
            if (func != null) {
                if (module.isInternalModule()) {
                    logger.error("Cannot create a reference to an internal Java function");
                    throw new XPathException(this, "Cannot create a reference to an internal Java function");
                }
                break;
            }
        }
    }
    if (func == null) {
        throw new XPathException(this, Messages.getMessage(Error.FUNC_NOT_FOUND, qname, Integer.toString(arity)));
    }
    final FunctionCall funcCall = new FunctionCall(context, func);
    funcCall.setLocation(line, column);
    return funcCall;
}
Also used : QName(org.exist.dom.QName) Module(org.exist.xquery.Module)

Example 80 with QName

use of org.exist.dom.QName 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)

Aggregations

QName (org.exist.dom.QName)271 Test (org.junit.Test)54 Sequence (org.exist.xquery.value.Sequence)39 DBBroker (org.exist.storage.DBBroker)31 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)28 IOException (java.io.IOException)23 Document (org.w3c.dom.Document)23 DocumentSet (org.exist.dom.persistent.DocumentSet)20 Text (org.w3c.dom.Text)20 AttributesImpl (org.xml.sax.helpers.AttributesImpl)18 NameTest (org.exist.xquery.NameTest)17 XPathException (org.exist.xquery.XPathException)17 BrokerPool (org.exist.storage.BrokerPool)15 IllegalQNameException (org.exist.dom.QName.IllegalQNameException)13 Node (org.w3c.dom.Node)12 ReentrantLock (java.util.concurrent.locks.ReentrantLock)11 NodeSet (org.exist.dom.persistent.NodeSet)11 SAXException (org.xml.sax.SAXException)11 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)10 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10