Search in sources :

Example 96 with QName

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

the class FunctionFactory method wrap.

/**
 * Wrap a function call into a user defined function.
 * This is used to handle dynamic function calls or partial
 * function applications on built in functions.
 *
 * @param context current context
 * @param call the function call to be wrapped
 * @return a new function call referencing an inline function
 * @throws XPathException in case of a static error
 */
public static FunctionCall wrap(XQueryContext context, Function call) throws XPathException {
    final int argCount = call.getArgumentCount();
    final QName[] variables = new QName[argCount];
    final List<Expression> innerArgs = new ArrayList<>(argCount);
    final List<Expression> wrapperArgs = new ArrayList<>(argCount);
    final FunctionSignature signature = call.getSignature();
    // the parameters of the newly created inline function:
    final List<SequenceType> newParamTypes = new ArrayList<>();
    final SequenceType[] paramTypes = signature.getArgumentTypes();
    for (int i = 0; i < argCount; i++) {
        final Expression param = call.getArgument(i);
        wrapperArgs.add(param);
        QName varName = new QName("vp" + i, XMLConstants.NULL_NS_URI);
        variables[i] = varName;
        final VariableReference ref = new VariableReference(context, varName);
        innerArgs.add(ref);
        // overloaded functions like concat may have an arbitrary number of arguments
        if (i < paramTypes.length) {
            newParamTypes.add(paramTypes[i]);
        } else // overloaded function: add last sequence type
        {
            newParamTypes.add(paramTypes[paramTypes.length - 1]);
        }
    }
    final SequenceType[] newParamArray = newParamTypes.toArray(new SequenceType[0]);
    final FunctionSignature newSignature = new FunctionSignature(signature);
    newSignature.setArgumentTypes(newParamArray);
    final UserDefinedFunction func = new UserDefinedFunction(context, newSignature);
    for (final QName varName : variables) {
        func.addVariable(varName);
    }
    call.setArguments(innerArgs);
    func.setFunctionBody(call);
    final FunctionCall wrappedCall = new FunctionCall(context, func);
    wrappedCall.setArguments(wrapperArgs);
    return wrappedCall;
}
Also used : QName(org.exist.dom.QName) ArrayList(java.util.ArrayList) SequenceType(org.exist.xquery.value.SequenceType)

Example 97 with QName

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

the class CastExpression method toFunction.

public Function toFunction() throws XPathException {
    final String typeName = Type.getTypeName(CastExpression.this.requiredType);
    try {
        final QName qname = QName.parse(context, typeName);
        final FunctionSignature signature = new FunctionSignature(qname);
        final SequenceType argType = new SequenceType(Type.ITEM, Cardinality.EXACTLY_ONE);
        signature.setArgumentTypes(new SequenceType[] { argType });
        signature.setReturnType(new SequenceType(CastExpression.this.requiredType, CastExpression.this.cardinality));
        return new FunctionWrapper(this, signature);
    } catch (final QName.IllegalQNameException e) {
        throw new XPathException(ErrorCodes.XPST0081, "No namespace defined for prefix " + typeName);
    }
}
Also used : QName(org.exist.dom.QName)

Example 98 with QName

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

the class ElementConstructor method addNamespaceDecl.

private void addNamespaceDecl(final QName qn) throws XPathException {
    if (namespaceDecls == null) {
        namespaceDecls = new QName[1];
        namespaceDecls[0] = qn;
    } else {
        for (QName namespaceDecl : namespaceDecls) {
            if (qn.equals(namespaceDecl)) {
                throw new XPathException(this, ErrorCodes.XQST0071, "duplicate definition for '" + qn + "'");
            }
        }
        final QName[] decls = new QName[namespaceDecls.length + 1];
        System.arraycopy(namespaceDecls, 0, decls, 0, namespaceDecls.length);
        decls[namespaceDecls.length] = qn;
        namespaceDecls = decls;
    }
// context.inScopeNamespaces.put(qn.getLocalPart(), qn.getNamespaceURI());
}
Also used : QName(org.exist.dom.QName)

Example 99 with QName

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

the class FunAnalyzeString method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        builder.startElement(new QName("analyze-string-result", Function.BUILTIN_FUNCTION_NS), null);
        String input = "";
        if (!args[0].isEmpty()) {
            input = args[0].itemAt(0).getStringValue();
        }
        if (input != null && !input.isEmpty()) {
            final String pattern = args[1].itemAt(0).getStringValue();
            String flags = "";
            if (args.length == 3) {
                flags = args[2].itemAt(0).getStringValue();
            }
            analyzeString(builder, input, pattern, flags);
        }
        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) QName(org.exist.dom.QName)

Example 100 with QName

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

the class XQueryContext method declareFunction.

@Override
public void declareFunction(final UserDefinedFunction function) throws XPathException {
    // TODO: redeclaring functions should be forbidden. however, throwing an
    // exception will currently break util:eval.
    final QName name = function.getSignature().getName();
    if (XML_NS.equals(name.getNamespaceURI())) {
        throw new XPathException(function, ErrorCodes.XQST0045, "Function '" + name + "' is in the forbidden namespace '" + XML_NS + "'");
    }
    if (Namespaces.SCHEMA_NS.equals(name.getNamespaceURI())) {
        throw new XPathException(function, ErrorCodes.XQST0045, "Function '" + name + "' is in the forbidden namespace '" + Namespaces.SCHEMA_NS + "'");
    }
    if (Namespaces.SCHEMA_INSTANCE_NS.equals(name.getNamespaceURI())) {
        throw new XPathException(function, ErrorCodes.XQST0045, "Function '" + name + "' is in the forbidden namespace '" + Namespaces.SCHEMA_INSTANCE_NS + "'");
    }
    if (Namespaces.XPATH_FUNCTIONS_NS.equals(name.getNamespaceURI())) {
        throw new XPathException(function, ErrorCodes.XQST0045, "Function '" + name + "' is in the forbidden namespace '" + Namespaces.XPATH_FUNCTIONS_NS + "'");
    }
    if (name.getNamespaceURI().isEmpty()) {
        throw new XPathException(function, ErrorCodes.XQST0060, "Every declared function name must have a non-null namespace URI, but function '" + name + "' does not meet this requirement.");
    }
    final FunctionSignature signature = function.getSignature();
    final FunctionId functionKey = signature.getFunctionId();
    if (declaredFunctions.containsKey(functionKey)) {
        throw new XPathException(ErrorCodes.XQST0034, "Function " + signature.getName().toURIQualifiedName() + '#' + signature.getArgumentCount() + " is already defined.");
    } else {
        declaredFunctions.put(functionKey, function);
    }
}
Also used : QName(org.exist.dom.QName)

Aggregations

QName (org.exist.dom.QName)260 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 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 PermissionDeniedException (org.exist.security.PermissionDeniedException)10