Search in sources :

Example 1 with SequenceType

use of org.exist.xquery.value.SequenceType in project exist by eXist-db.

the class DescribeFunction method writeSignature.

/**
 * @param signature
 * @param builder
 * @throws XPathException if an internal error occurs
 */
private void writeSignature(FunctionSignature signature, MemTreeBuilder builder) throws XPathException {
    final AttributesImpl attribs = new AttributesImpl();
    attribs.addAttribute("", "arguments", "arguments", "CDATA", Integer.toString(signature.getArgumentCount()));
    builder.startElement("", "prototype", "prototype", attribs);
    attribs.clear();
    builder.startElement("", "signature", "signature", attribs);
    builder.characters(signature.toString());
    builder.endElement();
    writeAnnotations(signature, builder);
    if (signature.getDescription() != null) {
        builder.startElement("", "description", "description", attribs);
        final StringBuilder description = new StringBuilder();
        description.append(signature.getDescription());
        description.append("\n\n");
        final SequenceType[] argumentTypes = signature.getArgumentTypes();
        if (argumentTypes != null && argumentTypes.length > 0) {
            final StringBuilder args = new StringBuilder();
            int noArgs = 0;
            for (final SequenceType argumentType : argumentTypes) {
                if (argumentType instanceof FunctionParameterSequenceType) {
                    noArgs++;
                    final FunctionParameterSequenceType fp = (FunctionParameterSequenceType) argumentType;
                    args.append("$");
                    args.append(fp.getAttributeName());
                    args.append(" : ");
                    args.append(fp.getDescription());
                    args.append("\n");
                }
            }
            // only add if there were good arguments
            if (noArgs > 0) {
                description.append("Parameters:\n");
                description.append(args);
            }
        }
        final SequenceType returnType = signature.getReturnType();
        if (returnType != null) {
            if (returnType instanceof FunctionReturnSequenceType) {
                description.append("\n");
                description.append("Returns ");
                final FunctionReturnSequenceType fp = (FunctionReturnSequenceType) returnType;
                description.append(fp.getDescription());
                description.append("\n");
            }
        }
        builder.characters(description.toString());
        builder.endElement();
    }
    if (signature.getDeprecated() != null) {
        builder.startElement("", "deprecated", "deprecated", attribs);
        builder.characters(signature.getDeprecated());
        builder.endElement();
    }
    builder.endElement();
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType)

Example 2 with SequenceType

use of org.exist.xquery.value.SequenceType in project exist by eXist-db.

the class ResourceFunctionExecutorImpl method convertToExistFunctionArguments.

/**
 * Creates converts function arguments from EXQuery to eXist-db types
 *
 * @param xqueryContext The XQuery Context of the XQuery containing the Function Call
 * @param fn The Function in the XQuery to create a Function Call for
 * @param arguments The arguments to be passed to the Function when its invoked
 *
 * @return The arguments ready to pass to the Function Call when it is invoked
 */
private org.exist.xquery.value.Sequence[] convertToExistFunctionArguments(final XQueryContext xqueryContext, final UserDefinedFunction fn, final Iterable<TypedArgumentValue> arguments) throws XPathException, RestXqServiceException {
    final List<org.exist.xquery.value.Sequence> fnArgs = new ArrayList<>();
    for (final SequenceType argumentType : fn.getSignature().getArgumentTypes()) {
        final FunctionParameterSequenceType fnParameter = (FunctionParameterSequenceType) argumentType;
        org.exist.xquery.value.Sequence fnArg = null;
        boolean found = false;
        for (final TypedArgumentValue argument : arguments) {
            final String argumentName = argument.getArgumentName();
            if (argumentName != null && argumentName.equals(fnParameter.getAttributeName())) {
                fnArg = convertToExistSequence(xqueryContext, argument, fnParameter.getPrimaryType());
                found = true;
                break;
            }
        }
        if (!found) {
            // value is not always provided, e.g. by PathAnnotation, so use empty sequence
            // TODO do we need to check the cardinality of the receiving arg to make sure it permits ZERO?
            // argumentType.getCardinality();
            // create the empty sequence
            fnArg = org.exist.xquery.value.Sequence.EMPTY_SEQUENCE;
        }
        fnArgs.add(fnArg);
    }
    return fnArgs.toArray(new org.exist.xquery.value.Sequence[0]);
}
Also used : org.exist.xquery(org.exist.xquery) ArrayList(java.util.ArrayList) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exquery.xquery.Sequence) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) TypedArgumentValue(org.exquery.xquery.TypedArgumentValue) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType)

Example 3 with SequenceType

use of org.exist.xquery.value.SequenceType in project exist by eXist-db.

the class FunReverse method analyze.

public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
    inPredicate = (contextInfo.getFlags() & IN_PREDICATE) > 0;
    contextId = contextInfo.getContextId();
    contextInfo.setParent(this);
    final SequenceType[] argumentTypes = getSignature().getArgumentTypes();
    for (int i = 0; i < getArgumentCount(); i++) {
        final Expression arg = getArgument(i);
        // call analyze for each argument
        final AnalyzeContextInfo argContextInfo = new AnalyzeContextInfo(contextInfo);
        arg.analyze(argContextInfo);
        if (i == 0) {
            contextInfo.setStaticReturnType(argContextInfo.getStaticReturnType());
        }
        if (!argumentsChecked) {
            // statically check the argument
            SequenceType argType = null;
            if (argumentTypes != null && i < argumentTypes.length) {
                argType = argumentTypes[i];
            }
            checkArgument(arg, argType, argContextInfo, i + 1);
        }
    }
    argumentsChecked = true;
}
Also used : SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType)

Example 4 with SequenceType

use of org.exist.xquery.value.SequenceType in project exist by eXist-db.

the class XQDocHelper method enhance.

protected void enhance(final FunctionSignature signature) {
    signature.setDescription(description.toString().trim());
    if (returnValue != null) {
        final SequenceType returnType = signature.getReturnType();
        final FunctionReturnSequenceType newType = new FunctionReturnSequenceType(returnType.getPrimaryType(), returnType.getCardinality(), returnValue);
        signature.setReturnType(newType);
    }
    final SequenceType[] args = signature.getArgumentTypes();
    for (final SequenceType type : args) {
        if (type instanceof FunctionParameterSequenceType) {
            final FunctionParameterSequenceType argType = (FunctionParameterSequenceType) type;
            final String desc = parameters.get(argType.getAttributeName());
            if (desc != null) {
                argType.setDescription(desc);
            }
        }
    }
    for (final Map.Entry<String, String> entry : meta.entrySet()) {
        String key = entry.getKey();
        if (key.length() > 1 && key.charAt(0) == '@') {
            key = key.substring(1);
        }
        signature.addMetadata(key, entry.getValue());
    }
}
Also used : FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with SequenceType

use of org.exist.xquery.value.SequenceType in project exist by eXist-db.

the class Function method analyze.

@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
    inPredicate = (contextInfo.getFlags() & IN_PREDICATE) > 0;
    unordered = (contextInfo.getFlags() & UNORDERED) > 0;
    contextId = contextInfo.getContextId();
    contextInfo.setParent(this);
    final SequenceType[] argumentTypes = mySignature.getArgumentTypes();
    for (int i = 0; i < getArgumentCount(); i++) {
        final Expression arg = getArgument(i);
        if (arg != null) {
            // call analyze for each argument
            final AnalyzeContextInfo argContextInfo = new AnalyzeContextInfo(contextInfo);
            arg.analyze(argContextInfo);
            if (!argumentsChecked) {
                // statically check the argument
                SequenceType argType = null;
                if (argumentTypes != null && i < argumentTypes.length) {
                    argType = argumentTypes[i];
                }
                checkArgument(arg, argType, argContextInfo, i + 1);
            }
        }
    }
    argumentsChecked = true;
}
Also used : SequenceType(org.exist.xquery.value.SequenceType)

Aggregations

SequenceType (org.exist.xquery.value.SequenceType)11 FunctionParameterSequenceType (org.exist.xquery.value.FunctionParameterSequenceType)6 FunctionReturnSequenceType (org.exist.xquery.value.FunctionReturnSequenceType)5 ArrayList (java.util.ArrayList)3 QName (org.exist.dom.QName)3 Sequence (org.exist.xquery.value.Sequence)3 AttributesImpl (org.xml.sax.helpers.AttributesImpl)3 Map (java.util.Map)2 HashMap (java.util.HashMap)1 org.exist.xquery (org.exist.xquery)1 FunctionReference (org.exist.xquery.value.FunctionReference)1 Item (org.exist.xquery.value.Item)1 ValueSequence (org.exist.xquery.value.ValueSequence)1 Sequence (org.exquery.xquery.Sequence)1 TypedArgumentValue (org.exquery.xquery.TypedArgumentValue)1 Test (org.junit.Test)1