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);
}
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);
}
}
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);
}
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;
}
Aggregations