Search in sources :

Example 1 with FunctionParameterSequenceType

use of org.exist.xquery.value.FunctionParameterSequenceType 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 FunctionParameterSequenceType

use of org.exist.xquery.value.FunctionParameterSequenceType 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 FunctionParameterSequenceType

use of org.exist.xquery.value.FunctionParameterSequenceType 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 4 with FunctionParameterSequenceType

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

the class InspectFunctionHelper method writeParameters.

private static void writeParameters(final FunctionSignature sig, final MemTreeBuilder builder) {
    final SequenceType[] arguments = sig.getArgumentTypes();
    if (arguments != null) {
        final AttributesImpl attribs = new AttributesImpl();
        for (final SequenceType type : arguments) {
            attribs.clear();
            attribs.addAttribute("", "type", "type", "CDATA", Type.getTypeName(type.getPrimaryType()));
            attribs.addAttribute("", "cardinality", "cardinality", "CDATA", type.getCardinality().getHumanDescription());
            if (type instanceof FunctionParameterSequenceType) {
                attribs.addAttribute("", "var", "var", "CDATA", ((FunctionParameterSequenceType) type).getAttributeName());
            }
            builder.startElement(ARGUMENT_QNAME, attribs);
            if (type instanceof FunctionParameterSequenceType) {
                builder.characters(((FunctionParameterSequenceType) type).getDescription());
            }
            builder.endElement();
        }
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType)

Aggregations

FunctionParameterSequenceType (org.exist.xquery.value.FunctionParameterSequenceType)4 SequenceType (org.exist.xquery.value.SequenceType)4 FunctionReturnSequenceType (org.exist.xquery.value.FunctionReturnSequenceType)3 AttributesImpl (org.xml.sax.helpers.AttributesImpl)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 org.exist.xquery (org.exist.xquery)1 ValueSequence (org.exist.xquery.value.ValueSequence)1 Sequence (org.exquery.xquery.Sequence)1 TypedArgumentValue (org.exquery.xquery.TypedArgumentValue)1