use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class CollectionTest method doc_dynamicallyAvailableCollection_relativeUri.
@Test
public void doc_dynamicallyAvailableCollection_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
final BrokerPool pool = BrokerPool.getInstance();
final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
final String baseUri = "http://from-dynamic-context/";
final String collectionRelativeUri = "collection1";
final String query = "fn:collection('" + collectionRelativeUri + "')";
try (final DBBroker broker = pool.getBroker()) {
final XQueryContext context = new XQueryContext(pool);
context.setBaseURI(new AnyURIValue(new URI(baseUri)));
context.addDynamicallyAvailableCollection(baseUri + collectionRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof Node);
final Source expectedSource = Input.fromString(doc).build();
final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
}
use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class GetPrincipalMetadataFunction method getPrincipalMetadataKeys.
private Sequence getPrincipalMetadataKeys(final Principal principal) throws XPathException {
final Set<SchemaType> metadataKeys = principal.getMetadataKeys();
final Sequence seq = new ValueSequence(metadataKeys.size());
for (final SchemaType schemaType : metadataKeys) {
seq.add(new AnyURIValue(schemaType.getNamespace()));
}
return seq;
}
use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class XQueryTrigger method execute.
private void execute(boolean isBefore, DBBroker broker, Txn transaction, QName functionName, XmldbURI src, XmldbURI dst) throws TriggerException {
final CompiledXQuery compiledQuery = getScript(isBefore, broker, transaction, src);
if (compiledQuery == null) {
return;
}
ProcessMonitor pm = null;
final XQueryContext context = compiledQuery.getContext();
// execute the XQuery
try {
int nParams = 1;
if (dst != null)
nParams = 2;
final UserDefinedFunction function = context.resolveFunction(functionName, nParams);
if (function != null) {
final List<Expression> args = new ArrayList<>(nParams);
if (isBefore) {
args.add(new LiteralValue(context, new AnyURIValue(src)));
if (dst != null)
args.add(new LiteralValue(context, new AnyURIValue(dst)));
} else {
if (dst != null)
args.add(new LiteralValue(context, new AnyURIValue(dst)));
args.add(new LiteralValue(context, new AnyURIValue(src)));
}
pm = broker.getBrokerPool().getProcessMonitor();
context.getProfiler().traceQueryStart();
pm.queryStarted(context.getWatchDog());
final FunctionCall call = new FunctionCall(context, function);
call.setArguments(args);
call.analyze(new AnalyzeContextInfo());
final Sequence contextSequence;
final ContextItemDeclaration cid = call.getContext().getContextItemDeclartion();
if (cid != null) {
contextSequence = cid.eval(null);
} else {
contextSequence = NodeSet.EMPTY_SET;
}
call.eval(contextSequence);
}
} catch (final XPathException e) {
TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
TriggerStatePerThread.setTransaction(null);
throw new TriggerException(PREPARE_EXCEPTION_MESSAGE, e);
} finally {
if (pm != null) {
context.getProfiler().traceQueryEnd(context);
pm.queryCompleted(context.getWatchDog());
}
compiledQuery.reset();
context.reset();
}
if (!isBefore) {
TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
TriggerStatePerThread.setTransaction(null);
LOG.debug("Trigger fired 'after'");
} else {
LOG.debug("Trigger fired 'before'");
}
}
use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class FunResolveURI method eval.
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
AnyURIValue base;
if (getArgumentCount() == 1) {
if (!context.isBaseURIDeclared()) {
throw new XPathException(this, ErrorCodes.FONS0005, "base URI of the static context has not been assigned a value.");
}
base = context.getBaseURI();
} else {
try {
final Item item = getArgument(1).eval(contextSequence).itemAt(0).convertTo(Type.ANY_URI);
base = (AnyURIValue) item;
} catch (final XPathException e) {
throw new XPathException(this, ErrorCodes.FORG0002, "invalid argument to fn:resolve-uri(): " + e.getMessage(), null, e);
}
}
Sequence result;
final Sequence seq = getArgument(0).eval(contextSequence);
if (seq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
AnyURIValue relative;
try {
final Item item = seq.itemAt(0).convertTo(Type.ANY_URI);
relative = (AnyURIValue) item;
} catch (final XPathException e) {
throw new XPathException(this, ErrorCodes.FORG0002, "invalid argument to fn:resolve-uri(): " + e.getMessage(), seq, e);
}
URI relativeURI;
URI baseURI;
try {
relativeURI = new URI(relative.getStringValue());
baseURI = new URI(base.getStringValue());
} catch (final URISyntaxException e) {
throw new XPathException(this, ErrorCodes.FORG0009, "unable to resolve a relative URI against a base URI in fn:resolve-uri(): " + e.getMessage(), null, e);
}
if (relativeURI.isAbsolute()) {
result = relative;
} else {
result = new AnyURIValue(baseURI.resolve(relativeURI));
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class QNameFunctions method eval.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
}
Sequence result;
if (args[0].isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final QNameValue value = (QNameValue) args[0].itemAt(0);
final QName qname = value.getQName();
if (isCalledAs("prefix-from-QName")) {
final String prefix = qname.getPrefix();
if (prefix == null || prefix.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
result = new StringValue(prefix, Type.NCNAME);
}
} else if (isCalledAs("local-name-from-QName")) {
result = new StringValue(qname.getLocalPart(), Type.NCNAME);
} else {
// fn:namespace-uri-from-QName
String uri = qname.getNamespaceURI();
if (uri == null) {
uri = "";
}
result = new AnyURIValue(uri);
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
Aggregations