Search in sources :

Example 36 with QName

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

the class UserDefinedFunction method addVariable.

public void addVariable(final String varName) throws XPathException {
    try {
        final QName qname = QName.parse(context, varName, null);
        addVariable(qname);
    } catch (final QName.IllegalQNameException e) {
        throw new XPathException(ErrorCodes.XPST0081, "No namespace defined for prefix " + varName);
    }
}
Also used : QName(org.exist.dom.QName)

Example 37 with QName

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

the class UserDefinedFunction 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 {
    // context.expressionStart(this);
    context.stackEnter(this);
    // make sure reset state is called after query has finished
    hasBeenReset = false;
    // Save the local variable stack
    final LocalVariable mark = context.markLocalVariables(true);
    if (closureVariables != null) {
        context.restoreStack(closureVariables);
    }
    Sequence result = null;
    try {
        QName varName;
        LocalVariable var;
        int j = 0;
        for (int i = 0; i < parameters.size(); i++, j++) {
            varName = parameters.get(i);
            var = new LocalVariable(varName);
            var.setValue(currentArguments[j]);
            if (contextDocs != null) {
                var.setContextDocs(contextDocs[i]);
            }
            context.declareVariableBinding(var);
            Cardinality actualCardinality;
            if (currentArguments[j].isEmpty()) {
                actualCardinality = Cardinality.EMPTY_SEQUENCE;
            } else if (currentArguments[j].hasMany()) {
                actualCardinality = Cardinality._MANY;
            } else {
                actualCardinality = Cardinality.EXACTLY_ONE;
            }
            if (!getSignature().getArgumentTypes()[j].getCardinality().isSuperCardinalityOrEqualOf(actualCardinality)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "Invalid cardinality for parameter $" + varName + ". Expected " + getSignature().getArgumentTypes()[j].getCardinality().getHumanDescription() + ", got " + currentArguments[j].getItemCount());
            }
        }
        result = body.eval(null, null);
        return result;
    } finally {
        // restore the local variable stack
        context.popLocalVariables(mark, result);
        context.stackLeave(this);
    // context.expressionEnd(this);
    }
}
Also used : QName(org.exist.dom.QName) Sequence(org.exist.xquery.value.Sequence)

Example 38 with QName

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

the class VariableImpl method checkType.

public void checkType() throws XPathException {
    if (type == null) {
        return;
    }
    type.checkCardinality(value);
    if (value.isEmpty()) {
        return;
    }
    final int requiredType = type.getPrimaryType();
    if (Type.subTypeOf(requiredType, Type.ATOMIC)) {
        if (!Type.subTypeOf(value.getItemType(), Type.ATOMIC)) {
            value = Atomize.atomize(value);
        }
        if (requiredType != Type.ATOMIC) {
            value = convert(value);
        }
    }
    if (!type.checkType(value)) {
        final SequenceType valueType = new SequenceType(value.getItemType(), value.getCardinality());
        if ((!value.isEmpty()) && type.getPrimaryType() == Type.DOCUMENT && value.getItemType() == Type.DOCUMENT) {
            // it's a document... we need to get the document element's name
            final NodeValue nvItem = (NodeValue) value.itemAt(0);
            final Document doc;
            if (nvItem instanceof Document) {
                doc = (Document) nvItem;
            } else {
                doc = nvItem.getOwnerDocument();
            }
            if (doc != null) {
                final Element elem = doc.getDocumentElement();
                if (elem != null) {
                    valueType.setNodeName(new QName(elem.getLocalName(), elem.getNamespaceURI()));
                }
            }
        }
        throw new XPathException(Messages.getMessage(Error.VAR_TYPE_MISMATCH, toString(), type.toString(), valueType.toString()));
    }
}
Also used : QName(org.exist.dom.QName) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 39 with QName

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

the class PrologFunctions method getOption.

private Sequence getOption(final Sequence[] args) throws XPathException {
    final String qnameString = args[0].getStringValue();
    try {
        final QName qname = QName.parse(context, qnameString, context.getDefaultFunctionNamespace());
        final Option option = context.getOption(qname);
        if (option != null) {
            return new StringValue(option.getContents());
        } else {
            return Sequence.EMPTY_SEQUENCE;
        }
    } catch (final QName.IllegalQNameException e) {
        throw new XPathException(this, ErrorCodes.XPST0081, "No namespace defined for prefix " + qnameString);
    }
}
Also used : QName(org.exist.dom.QName)

Example 40 with QName

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

the class IndexKeys method eval.

/*
     * (non-Javadoc)
     * 
     * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
     *      org.exist.xquery.value.Sequence)
     */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    NodeSet nodes = null;
    DocumentSet docs = null;
    Sequence qnames = null;
    if (isCalledAs("index-keys-by-qname")) {
        qnames = args[0];
        docs = contextSequence == null ? context.getStaticallyKnownDocuments() : contextSequence.getDocumentSet();
    } else {
        nodes = args[0].toNodeSet();
        docs = nodes.getDocumentSet();
    }
    final Sequence result = new ValueSequence();
    try (final FunctionReference ref = (FunctionReference) args[2].itemAt(0)) {
        int max = -1;
        if (args[3].hasOne()) {
            max = ((IntegerValue) args[3].itemAt(0)).getInt();
        }
        // if we have 5 arguments, query the user-specified index
        if (this.getArgumentCount() == 5) {
            final IndexWorker indexWorker = context.getBroker().getIndexController().getWorkerByIndexName(args[4].itemAt(0).getStringValue());
            // IndexWorker indexWorker = context.getBroker().getBrokerPool().getIndexManager().getIndexByName(args[4].itemAt(0).getStringValue()).getWorker();
            if (indexWorker == null) {
                throw new XPathException(this, "Unknown index: " + args[4].itemAt(0).getStringValue());
            }
            final Map<String, Object> hints = new HashMap<>();
            if (max != -1) {
                hints.put(IndexWorker.VALUE_COUNT, new IntegerValue(max));
            }
            if (indexWorker instanceof OrderedValuesIndex) {
                hints.put(OrderedValuesIndex.START_VALUE, args[1].getStringValue());
            } else {
                logger.warn("{} isn't an instance of org.exist.indexing.OrderedValuesIndex. Start value '{}' ignored.", indexWorker.getClass().getName(), args[1]);
            }
            if (qnames != null) {
                final List<QName> qnameList = new ArrayList<>(qnames.getItemCount());
                for (final SequenceIterator i = qnames.iterate(); i.hasNext(); ) {
                    final QNameValue qv = (QNameValue) i.nextItem();
                    qnameList.add(qv.getQName());
                }
                hints.put(QNamedKeysIndex.QNAMES_KEY, qnameList);
            }
            final Occurrences[] occur = indexWorker.scanIndex(context, docs, nodes, hints);
            // TODO : add an extra argument to pass the END_VALUE ?
            final int len = (max != -1 && occur.length > max ? max : occur.length);
            final Sequence[] params = new Sequence[2];
            ValueSequence data = new ValueSequence();
            for (int j = 0; j < len; j++) {
                params[0] = new StringValue(occur[j].getTerm().toString());
                data.add(new IntegerValue(occur[j].getOccurrences(), Type.UNSIGNED_INT));
                data.add(new IntegerValue(occur[j].getDocuments(), Type.UNSIGNED_INT));
                data.add(new IntegerValue(j + 1, Type.UNSIGNED_INT));
                params[1] = data;
                result.addAll(ref.evalFunction(Sequence.EMPTY_SEQUENCE, null, params));
                data.clear();
            }
        // no index specified: use the range index
        } else {
            final Indexable indexable = (Indexable) args[1].itemAt(0);
            ValueOccurrences[] occur = null;
            // First check for indexes defined on qname
            final QName[] allQNames = getDefinedIndexes(context.getBroker(), docs);
            if (allQNames.length > 0) {
                occur = context.getBroker().getValueIndex().scanIndexKeys(docs, nodes, allQNames, indexable);
            }
            // Also check if there's an index defined by path
            ValueOccurrences[] occur2 = context.getBroker().getValueIndex().scanIndexKeys(docs, nodes, indexable);
            // Merge the two results
            if (occur == null || occur.length == 0) {
                occur = occur2;
            } else {
                ValueOccurrences[] t = new ValueOccurrences[occur.length + occur2.length];
                System.arraycopy(occur, 0, t, 0, occur.length);
                System.arraycopy(occur2, 0, t, occur.length, occur2.length);
                occur = t;
            }
            final int len = (max != -1 && occur.length > max ? max : occur.length);
            final Sequence[] params = new Sequence[2];
            ValueSequence data = new ValueSequence();
            for (int j = 0; j < len; j++) {
                params[0] = occur[j].getValue();
                data.add(new IntegerValue(occur[j].getOccurrences(), Type.UNSIGNED_INT));
                data.add(new IntegerValue(occur[j].getDocuments(), Type.UNSIGNED_INT));
                data.add(new IntegerValue(j + 1, Type.UNSIGNED_INT));
                params[1] = data;
                result.addAll(ref.evalFunction(Sequence.EMPTY_SEQUENCE, null, params));
                data.clear();
            }
        }
    }
    logger.debug("Returning: {}", result.getItemCount());
    return result;
}
Also used : OrderedValuesIndex(org.exist.indexing.OrderedValuesIndex) Occurrences(org.exist.util.Occurrences) ValueOccurrences(org.exist.util.ValueOccurrences) IndexWorker(org.exist.indexing.IndexWorker) Indexable(org.exist.storage.Indexable) NodeSet(org.exist.dom.persistent.NodeSet) QName(org.exist.dom.QName) ValueOccurrences(org.exist.util.ValueOccurrences) DocumentSet(org.exist.dom.persistent.DocumentSet)

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