use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class FunNamespaceURI method eval.
@Override
public Sequence eval(Sequence contextSequence, final 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();
}
// If we have one argument, we take it into account
final Sequence seq;
if (getSignature().getArgumentCount() > 0) {
seq = getArgument(0).eval(contextSequence, contextItem);
} else {
// Otherwise, we take the context sequence and we iterate over it
seq = contextSequence;
}
if (seq == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
final Sequence result;
if (seq.isEmpty()) {
result = AnyURIValue.EMPTY_URI;
} else {
final Item item = seq.itemAt(0);
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node; got: " + Type.getTypeName(item.getType()));
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
String ns = n.getNamespaceURI();
if (ns == null) {
ns = XMLConstants.NULL_NS_URI;
}
result = new AnyURIValue(ns);
}
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 AbstractTestRunner method executeQuery.
protected static Sequence executeQuery(final BrokerPool brokerPool, final Source query, final List<Function<XQueryContext, Tuple2<String, Object>>> externalVariableBindings) throws EXistException, PermissionDeniedException, XPathException, IOException, DatabaseConfigurationException {
final SecurityManager securityManager = requireNonNull(brokerPool.getSecurityManager(), "securityManager is null");
try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
final XQueryPool queryPool = brokerPool.getXQueryPool();
CompiledXQuery compiledQuery = queryPool.borrowCompiledXQuery(broker, query);
try {
XQueryContext context;
if (compiledQuery == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiledQuery.getContext();
context.prepareForReuse();
}
// setup misc. context
context.setBaseURI(new AnyURIValue("/db"));
if (query instanceof FileSource) {
final Path queryPath = Paths.get(((FileSource) query).getPath().toAbsolutePath().toString());
if (Files.isDirectory(queryPath)) {
context.setModuleLoadPath(queryPath.toString());
} else {
context.setModuleLoadPath(queryPath.getParent().toString());
}
}
// declare variables for the query
for (final Function<XQueryContext, Tuple2<String, Object>> externalVariableBinding : externalVariableBindings) {
final Tuple2<String, Object> nameValue = externalVariableBinding.apply(context);
context.declareVariable(nameValue._1, nameValue._2);
}
final XQuery xqueryService = brokerPool.getXQueryService();
// compile or update the context
if (compiledQuery == null) {
compiledQuery = xqueryService.compile(context, query);
} else {
compiledQuery.getContext().updateContext(context);
context.getWatchDog().reset();
}
return xqueryService.execute(broker, compiledQuery, null);
} finally {
queryPool.returnCompiledXQuery(query, compiledQuery);
}
}
}
use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class XQueryTestRunner method run.
@Override
public void run(final RunNotifier notifier) {
try {
final String pkgName = getClass().getPackage().getName().replace('.', '/');
final Source query = new ClassLoaderSource(pkgName + "/xquery-test-runner.xq");
final URI testModuleUri = path.toAbsolutePath().toUri();
final String suiteName = getSuiteName();
final List<java.util.function.Function<XQueryContext, Tuple2<String, Object>>> externalVariableDeclarations = Arrays.asList(context -> new Tuple2<>("test-module-uri", new AnyURIValue(testModuleUri)), // set callback functions for notifying junit!
context -> new Tuple2<>("test-ignored-function", new FunctionReference(new FunctionCall(context, new ExtTestIgnoredFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-started-function", new FunctionReference(new FunctionCall(context, new ExtTestStartedFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-failure-function", new FunctionReference(new FunctionCall(context, new ExtTestFailureFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-assumption-failed-function", new FunctionReference(new FunctionCall(context, new ExtTestAssumptionFailedFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-error-function", new FunctionReference(new FunctionCall(context, new ExtTestErrorFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-finished-function", new FunctionReference(new FunctionCall(context, new ExtTestFinishedFunction(context, suiteName, notifier)))));
// NOTE: at this stage EXIST_EMBEDDED_SERVER_CLASS_INSTANCE in XSuite will be usable
final BrokerPool brokerPool = XSuite.EXIST_EMBEDDED_SERVER_CLASS_INSTANCE.getBrokerPool();
executeQuery(brokerPool, query, externalVariableDeclarations);
} catch (final DatabaseConfigurationException | IOException | EXistException | PermissionDeniedException | XPathException e) {
// TODO(AR) what to do here?
throw new RuntimeException(e);
}
}
use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class DocUtils method getFromDynamicallyAvailableDocuments.
@Nullable
private static Sequence getFromDynamicallyAvailableDocuments(final XQueryContext context, final String path) throws XPathException {
try {
URI uri = new URI(path);
if (!uri.isAbsolute()) {
final AnyURIValue baseXdmUri = context.getBaseURI();
if (baseXdmUri != null && !baseXdmUri.equals(AnyURIValue.EMPTY_URI)) {
URI baseUri = baseXdmUri.toURI();
if (!baseUri.toString().endsWith("/")) {
baseUri = new URI(baseUri.toString() + '/');
}
uri = baseUri.resolve(uri);
}
}
return context.getDynamicallyAvailableDocument(uri.toString());
} catch (final URISyntaxException e) {
throw new XPathException(context.getRootExpression(), ErrorCodes.FODC0005, e);
}
}
use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.
the class ZipEntryFunctions method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
final XmldbURI uri = ((AnyURIValue) args[0].itemAt(0)).toXmldbURI();
final String entryName = args[1].itemAt(0).getStringValue();
final ZipFileSource zipFileSource = new ZipFileFromDb(uri);
ZipInputStream zis = null;
boolean mustClose = true;
Sequence result = Sequence.EMPTY_SEQUENCE;
try {
zis = zipFileSource.getStream(context.getBroker());
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
try {
if (zipEntry.getName().equals(entryName)) {
// process
if (isCalledAs(BINARY_ENTRY_NAME)) {
result = extractBinaryEntry(zis);
mustClose = false;
} else if (isCalledAs(HTML_ENTRY_NAME)) {
result = extractHtmlEntry(zis);
} else if (isCalledAs(TEXT_ENTRY_NAME)) {
result = extractStringEntry(zis);
} else if (isCalledAs(XML_ENTRY_NAME)) {
result = extractXmlEntry(zis);
}
break;
}
} finally {
// DONT need to close as the extract functions
// close the stream on the zip entry
/*if(mustClose) {
zis.closeEntry();
}*/
}
}
} catch (final IOException | PermissionDeniedException ioe) {
LOG.error(ioe.getMessage(), ioe);
throw new XPathException(this, ioe.getMessage(), ioe);
} finally {
if (zis != null && mustClose) {
try {
zis.close();
} catch (final IOException ioe) {
LOG.warn(ioe.getMessage(), ioe);
}
}
zipFileSource.close();
}
return result;
}
Aggregations