use of org.exist.source.FileSource 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.source.FileSource in project exist by eXist-db.
the class XQueryTestRunner method extractTestInfo.
private static XQueryTestInfo extractTestInfo(final Path path) throws InitializationError {
try {
final Configuration config = getConfiguration();
final XQueryContext xqueryContext = new XQueryContext(config);
final Source xquerySource = new FileSource(path, UTF_8, false);
final XQuery xquery = new XQuery();
final CompiledXQuery compiledXQuery = xquery.compile(xqueryContext, xquerySource);
String moduleNsPrefix = null;
String moduleNsUri = null;
final List<XQueryTestInfo.TestFunctionDef> testFunctions = new ArrayList<>();
final Iterator<UserDefinedFunction> localFunctions = compiledXQuery.getContext().localFunctions();
while (localFunctions.hasNext()) {
final UserDefinedFunction localFunction = localFunctions.next();
final FunctionSignature localFunctionSignature = localFunction.getSignature();
String testName = null;
boolean isTest = false;
final Annotation[] annotations = localFunctionSignature.getAnnotations();
if (annotations != null) {
for (final Annotation annotation : annotations) {
final QName annotationName = annotation.getName();
if (annotationName.getNamespaceURI().equals(XQSUITE_NAMESPACE)) {
if (annotationName.getLocalPart().startsWith("assert")) {
isTest = true;
if (testName != null) {
break;
}
} else if (annotationName.getLocalPart().equals("name")) {
final LiteralValue[] annotationValues = annotation.getValue();
if (annotationValues != null && annotationValues.length > 0) {
testName = annotationValues[0].getValue().getStringValue();
if (isTest) {
break;
}
}
}
}
}
}
if (isTest) {
if (testName == null) {
testName = localFunctionSignature.getName().getLocalPart();
}
if (moduleNsPrefix == null) {
moduleNsPrefix = localFunctionSignature.getName().getPrefix();
}
if (moduleNsUri == null) {
moduleNsUri = localFunctionSignature.getName().getNamespaceURI();
}
testFunctions.add(new XQueryTestInfo.TestFunctionDef(testName));
}
}
return new XQueryTestInfo(moduleNsPrefix, moduleNsUri, testFunctions);
} catch (final DatabaseConfigurationException | IOException | PermissionDeniedException | XPathException e) {
throw new InitializationError(e);
}
}
use of org.exist.source.FileSource in project exist by eXist-db.
the class XQuery method execute.
public Sequence execute(final DBBroker broker, File file, Sequence contextSequence) throws XPathException, IOException, PermissionDeniedException {
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
final CompiledXQuery compiled = compile(context, new FileSource(file.toPath(), true));
return execute(broker, compiled, contextSequence);
}
Aggregations