use of org.exist.source.Source in project exist by eXist-db.
the class LocalXPathQueryService method execute.
private ResourceSet execute(final LocalXmldbFunction<Source> sourceOp) throws XMLDBException {
return withDb((broker, transaction) -> {
final long start = System.currentTimeMillis();
final Source source = sourceOp.apply(broker, transaction);
final XmldbURI[] docs = new XmldbURI[] { XmldbURI.create(collection.getName(broker, transaction)) };
final XQuery xquery = brokerPool.getXQueryService();
final XQueryPool pool = brokerPool.getXQueryPool();
XQueryContext context;
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.setStaticallyKnownDocuments(docs);
if (variableDecls.containsKey(Debuggee.PREFIX + ":session")) {
context.declareVariable(Debuggee.SESSION, variableDecls.get(Debuggee.PREFIX + ":session"));
variableDecls.remove(Debuggee.PREFIX + ":session");
}
setupContext(source, context);
if (compiled == null) {
compiled = xquery.compile(context, source);
}
try {
final Sequence result = xquery.execute(broker, compiled, null, properties);
if (LOG.isDebugEnabled()) {
LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
}
final Properties resourceSetProperties = new Properties(properties);
resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
return result != null ? new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, null) : null;
} finally {
compiled.getContext().runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
});
}
use of org.exist.source.Source in project exist by eXist-db.
the class FunUnparsedText method getSource.
private Source getSource(final String uriParam) throws XPathException {
try {
final URI uri = new URI(uriParam);
if (uri.getFragment() != null) {
throw new XPathException(this, ErrorCodes.FOUT1170, "href argument may not contain fragment identifier");
}
final Source source = SourceFactory.getSource(context.getBroker(), "", uri.toASCIIString(), false);
if (source == null) {
throw new XPathException(this, ErrorCodes.FOUT1170, "Could not find source for: " + uriParam);
}
if (source instanceof FileSource && !context.getBroker().getCurrentSubject().hasDbaRole()) {
throw new PermissionDeniedException("non-dba user not allowed to read from file system");
}
return source;
} catch (final IOException | PermissionDeniedException | URISyntaxException e) {
throw new XPathException(this, ErrorCodes.FOUT1170, e.getMessage());
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class JSON method parseResource.
private Sequence parseResource(Sequence href, String handleDuplicates, JsonFactory factory) throws XPathException {
if (href.isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
try {
String url = href.getStringValue();
if (url.indexOf(':') == Constants.STRING_NOT_FOUND) {
url = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + url;
}
final Source source = SourceFactory.getSource(context.getBroker(), "", url, false);
if (source == null) {
throw new XPathException(this, ErrorCodes.FOUT1170, "failed to load json doc from URI " + url);
}
try (final InputStream is = source.getInputStream();
final JsonParser parser = factory.createParser(is)) {
final Item result = readValue(context, parser, handleDuplicates);
return result == null ? Sequence.EMPTY_SEQUENCE : result.toSequence();
}
} catch (IOException | PermissionDeniedException e) {
throw new XPathException(this, ErrorCodes.FOUT1170, e.getMessage());
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class Conditional method process.
@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
LOG.debug("Processing xupdate:if ...");
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
final Source source = new StringSource(selectStmt);
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
// context.setBackwardsCompatibility(true);
context.setStaticallyKnownDocuments(docs);
declareNamespaces(context);
declareVariables(context);
if (compiled == null)
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
}
Sequence seq = null;
try {
seq = xquery.execute(broker, compiled, null);
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
if (seq.effectiveBooleanValue()) {
long mods = 0;
for (final Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} modifications processed.", mods);
}
return mods;
} else {
return 0;
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class InternalModuleTest method reusedModuleVariables.
/**
* Similar to {@link #moduleVariables()} but
* re-executes the query to ensure on subsequent
* invocations, reusing the cached query (and query
* context) do not cause problems.
*/
@Test
public void reusedModuleVariables() throws XMLDBException {
final Source querySource = new StringSource(getModuleVariableQuery("org.exist.xquery.InternalModuleTest$TestModuleWithVariables"));
final EXistXQueryService queryService = (EXistXQueryService) existServer.getRoot().getService("XQueryService", "1.0");
moduleVariablesQuery(queryService, querySource, COUNTER.get());
moduleVariablesQuery(queryService, querySource, COUNTER.get());
moduleVariablesQuery(queryService, querySource, COUNTER.get());
}
Aggregations