Search in sources :

Example 6 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class CacheFunctions method extractCacheConfig.

private CacheConfig extractCacheConfig(final MapType configMap) throws XPathException {
    final Sequence permsSeq = configMap.get(new StringValue("permissions"));
    final Optional<CacheConfig.Permissions> permissions;
    if (permsSeq != null && permsSeq.getItemCount() > 0) {
        final MapType permsMap = (MapType) permsSeq.itemAt(0);
        final Optional<String> putGroup = getStringValue("put-group", permsMap);
        final Optional<String> getGroup = getStringValue("get-group", permsMap);
        final Optional<String> removeGroup = getStringValue("remove-group", permsMap);
        final Optional<String> clearGroup = getStringValue("clear-group", permsMap);
        permissions = Optional.of(new CacheConfig.Permissions(putGroup, getGroup, removeGroup, clearGroup));
    } else {
        permissions = Optional.empty();
    }
    final Sequence maximumSizeSeq = configMap.get(new StringValue("maximumSize"));
    final Optional<Long> maximumSize;
    if (maximumSizeSeq != null && maximumSizeSeq.getItemCount() == 1) {
        final long l = maximumSizeSeq.itemAt(0).toJavaObject(Long.class);
        maximumSize = Optional.of(l);
    } else {
        maximumSize = Optional.empty();
    }
    final Sequence expireAfterAccessSeq = configMap.get(new StringValue("expireAfterAccess"));
    final Optional<Long> expireAfterAccess;
    if (expireAfterAccessSeq != null && expireAfterAccessSeq.getItemCount() == 1) {
        final long l = expireAfterAccessSeq.itemAt(0).toJavaObject(Long.class);
        expireAfterAccess = Optional.of(l);
    } else {
        expireAfterAccess = Optional.empty();
    }
    return new CacheConfig(permissions, maximumSize, expireAfterAccess);
}
Also used : AbstractMapType(org.exist.xquery.functions.map.AbstractMapType) MapType(org.exist.xquery.functions.map.MapType)

Example 7 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class JSON method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getXQueryVersion() < 31) {
        throw new XPathException(this, ErrorCodes.EXXQDY0004, "json functions only available in XQuery 3.1, but version declaration states " + context.getXQueryVersion());
    }
    // process options if present
    // TODO: jackson does not allow access to raw string, so option "unescape" is not supported
    boolean liberal = false;
    String handleDuplicates = OPTION_DUPLICATES_USE_LAST;
    if (getArgumentCount() == 2) {
        final MapType options = (MapType) args[1].itemAt(0);
        final Sequence liberalOpt = options.get(new StringValue(OPTION_LIBERAL));
        if (liberalOpt.hasOne()) {
            liberal = liberalOpt.itemAt(0).convertTo(Type.BOOLEAN).effectiveBooleanValue();
        }
        final Sequence duplicateOpt = options.get(new StringValue(OPTION_DUPLICATES));
        if (duplicateOpt.hasOne()) {
            handleDuplicates = duplicateOpt.itemAt(0).getStringValue();
        }
    }
    JsonFactory factory = createJsonFactory(liberal);
    if (isCalledAs("parse-json")) {
        return parse(args[0], handleDuplicates, factory);
    } else if (isCalledAs("json-to-xml")) {
        return toxml(args[0], handleDuplicates, factory);
    } else {
        return parseResource(args[0], handleDuplicates, factory);
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) MapType(org.exist.xquery.functions.map.MapType)

Example 8 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class LoadXQueryModule method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String targetNamespace = args[0].getStringValue();
    if (targetNamespace.isEmpty()) {
        throw new XPathException(this, ErrorCodes.FOQM0001, "Target namespace must be a string with length > 0");
    }
    AnyURIValue[] locationHints = null;
    String xqVersion = getXQueryVersion(context.getXQueryVersion());
    AbstractMapType externalVars = new MapType(context);
    Sequence contextItem = Sequence.EMPTY_SEQUENCE;
    // evaluate options
    if (getArgumentCount() == 2) {
        final AbstractMapType map = (AbstractMapType) args[1].itemAt(0);
        final Sequence locationHintsOption = map.get(OPTIONS_LOCATION_HINTS);
        locationHints = new AnyURIValue[locationHintsOption.getItemCount()];
        for (int i = 0; i < locationHints.length; i++) {
            locationHints[i] = (AnyURIValue) locationHintsOption.itemAt(i).convertTo(Type.ANY_URI);
        }
        final Sequence versions = map.get(OPTIONS_XQUERY_VERSION);
        if (!versions.isEmpty()) {
            xqVersion = versions.itemAt(0).getStringValue();
        }
        final Sequence vars = map.get(OPTIONS_VARIABLES);
        if (!vars.isEmpty()) {
            if (vars.hasOne() && vars.itemAt(0).getType() == Type.MAP) {
                externalVars = (AbstractMapType) vars.itemAt(0);
            } else {
                throw new XPathException(this, ErrorCodes.XPTY0004, "Option 'variables' must be a map");
            }
        }
        contextItem = map.get(OPTIONS_CONTEXT_ITEM);
        if (contextItem.getItemCount() > 1) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Option 'context-item' must contain zero or one " + "items");
        }
    }
    // create temporary context so main context is not polluted
    final XQueryContext tempContext = new XQueryContext(context.getBroker().getBrokerPool(), context.getProfiler());
    tempContext.setModuleLoadPath(context.getModuleLoadPath());
    setExternalVars(externalVars, tempContext::declareGlobalVariable);
    tempContext.prepareForExecution();
    Module[] loadedModules = null;
    try {
        loadedModules = tempContext.importModule(targetNamespace, null, locationHints);
    } catch (final XPathException e) {
        if (e.getErrorCode() == ErrorCodes.XQST0059) {
            // importModule may throw exception if no location is given and module cannot be resolved
            throw new XPathException(this, ErrorCodes.FOQM0002, "Module with URI " + targetNamespace + " not found");
        }
        throw new XPathException(this, ErrorCodes.FOQM0003, "Error found when importing module: " + e.getMessage());
    }
    // not found, raise error
    if (loadedModules == null || loadedModules.length == 0) {
        throw new XPathException(this, ErrorCodes.FOQM0002, "Module with URI " + targetNamespace + " not found");
    }
    if (!xqVersion.equals(getXQueryVersion(tempContext.getXQueryVersion()))) {
        throw new XPathException(ErrorCodes.FOQM0003, "Imported module has wrong XQuery version: " + getXQueryVersion(tempContext.getXQueryVersion()));
    }
    final IMap<AtomicValue, Sequence> variables = newLinearMap(null);
    final IMap<AtomicValue, IMap<AtomicValue, Sequence>> functions = newLinearMap(null);
    for (final Module loadedModule : loadedModules) {
        loadedModule.setContextItem(contextItem);
        setExternalVars(externalVars, loadedModule::declareVariable);
        if (!loadedModule.isInternalModule()) {
            // ensure variable declarations in the imported module are analyzed.
            // unlike when using a normal import statement, this is not done automatically
            ((ExternalModule) loadedModule).analyzeGlobalVars();
        }
        getModuleVariables(loadedModule, variables);
        getModuleFunctions(loadedModule, tempContext, functions);
    }
    final IMap<AtomicValue, Sequence> result = Map.from(io.lacuna.bifurcan.List.of(new Maps.Entry<>(RESULT_FUNCTIONS, new MapType(context, functions.mapValues((k, v) -> (Sequence) new MapType(context, v.forked(), Type.INTEGER)).forked(), Type.QNAME)), new Maps.Entry<>(RESULT_VARIABLES, new MapType(context, variables.forked(), Type.QNAME))));
    return new MapType(context, result, Type.STRING);
}
Also used : IEntry(io.lacuna.bifurcan.IEntry) AbstractMapType(org.exist.xquery.functions.map.AbstractMapType) java.util(java.util) Module(org.exist.xquery.Module) MapType.newLinearMap(org.exist.xquery.functions.map.MapType.newLinearMap) QName(org.exist.dom.QName) MapType(org.exist.xquery.functions.map.MapType) org.exist.xquery.value(org.exist.xquery.value) XQueryAST(org.exist.xquery.parser.XQueryAST) org.exist.xquery(org.exist.xquery) Maps(io.lacuna.bifurcan.Maps) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) IMap(io.lacuna.bifurcan.IMap) Map(io.lacuna.bifurcan.Map) AbstractMapType(org.exist.xquery.functions.map.AbstractMapType) AbstractMapType(org.exist.xquery.functions.map.AbstractMapType) MapType(org.exist.xquery.functions.map.MapType) IMap(io.lacuna.bifurcan.IMap) IEntry(io.lacuna.bifurcan.IEntry) Module(org.exist.xquery.Module)

Example 9 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class FunXmlToJson method eval.

public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final Sequence result;
    final Sequence seq = (getArgumentCount() > 0) ? args[0] : Sequence.EMPTY_SEQUENCE;
    // TODO: implement handling of options
    final MapType options = (getArgumentCount() > 1) ? (MapType) args[1].itemAt(0) : new MapType(context);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        result = new ValueSequence();
        final Item item = seq.itemAt(0);
        if (item.getType() != Type.DOCUMENT && item.getType() != Type.ELEMENT) {
            throw new XPathException(ErrorCodes.FOJS0006, "Invalid XML representation of JSON.");
        }
        final NodeValue nodeValue = (NodeValue) item;
        final StringWriter stringWriter = new StringWriter();
        nodeValueToJson(nodeValue, stringWriter);
        final String jsonString = stringWriter.toString();
        result.add(new StringValue(jsonString));
    }
    return result;
}
Also used : StringWriter(java.io.StringWriter) MapType(org.exist.xquery.functions.map.MapType)

Aggregations

MapType (org.exist.xquery.functions.map.MapType)9 XPathException (org.exist.xquery.XPathException)3 Description (org.junit.runner.Description)3 Failure (org.junit.runner.notification.Failure)3 IOException (java.io.IOException)2 AbstractMapType (org.exist.xquery.functions.map.AbstractMapType)2 Sequence (org.exist.xquery.value.Sequence)2 ConsumerE (com.evolvedbinary.j8fu.function.ConsumerE)1 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 IEntry (io.lacuna.bifurcan.IEntry)1 IMap (io.lacuna.bifurcan.IMap)1 Map (io.lacuna.bifurcan.Map)1 Maps (io.lacuna.bifurcan.Maps)1 StringWriter (java.io.StringWriter)1 java.util (java.util)1 IdentityHashMap (java.util.IdentityHashMap)1 Query (org.apache.lucene.search.Query)1 QName (org.exist.dom.QName)1 Match (org.exist.dom.persistent.Match)1