Search in sources :

Example 1 with FunctionReturnSequenceType

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

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

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

the class InspectFunctionHelper method generateDocs.

/**
 * Generate an XML fragment containing information about the function identified by its signature.
 *
 * @param sig the signature of the function to describe
 * @param func the function implementation. If provided, the method will also inspect the function body
 *             and list all functions called from the current function.
 * @param builder builder used to create the XML
 * @return nodeNr of the generated element
 * @throws XPathException in case of dynamic error
 */
public static int generateDocs(final FunctionSignature sig, final UserDefinedFunction func, final MemTreeBuilder builder) throws XPathException {
    XQDocHelper.parse(sig);
    final AttributesImpl attribs = new AttributesImpl();
    attribs.addAttribute("", "name", "name", "CDATA", sig.getName().toString());
    attribs.addAttribute("", "module", "module", "CDATA", sig.getName().getNamespaceURI());
    final int nodeNr = builder.startElement(FUNCTION_QNAME, attribs);
    writeParameters(sig, builder);
    final SequenceType returnType = sig.getReturnType();
    if (returnType != null) {
        attribs.clear();
        attribs.addAttribute("", "type", "type", "CDATA", Type.getTypeName(returnType.getPrimaryType()));
        attribs.addAttribute("", "cardinality", "cardinality", "CDATA", returnType.getCardinality().getHumanDescription());
        builder.startElement(RETURN_QNAME, attribs);
        if (returnType instanceof FunctionReturnSequenceType) {
            final FunctionReturnSequenceType type = (FunctionReturnSequenceType) returnType;
            builder.characters(type.getDescription());
        }
        builder.endElement();
    }
    writeAnnotations(sig, builder);
    if (sig.getDescription() != null) {
        builder.startElement(DESCRIPTION_QNAME, null);
        builder.characters(sig.getDescription());
        builder.endElement();
    }
    final Map<String, String> metadata = sig.getMetadata();
    if (metadata != null) {
        for (final Map.Entry<String, String> meta : metadata.entrySet()) {
            builder.startElement(new QName(meta.getKey(), XMLConstants.NULL_NS_URI), null);
            builder.characters(meta.getValue());
            builder.endElement();
        }
    }
    if (sig.isDeprecated()) {
        builder.startElement(DEPRECATED_QNAME, null);
        builder.characters(sig.getDeprecated());
        builder.endElement();
    }
    if (func != null) {
        generateDependencies(func, builder);
    }
    builder.endElement();
    return nodeNr;
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) QName(org.exist.dom.QName) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) Map(java.util.Map)

Aggregations

FunctionParameterSequenceType (org.exist.xquery.value.FunctionParameterSequenceType)3 FunctionReturnSequenceType (org.exist.xquery.value.FunctionReturnSequenceType)3 SequenceType (org.exist.xquery.value.SequenceType)3 Map (java.util.Map)2 AttributesImpl (org.xml.sax.helpers.AttributesImpl)2 HashMap (java.util.HashMap)1 QName (org.exist.dom.QName)1