use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DocUtils method getDocumentByPathFromDB.
private static Sequence getDocumentByPathFromDB(final XQueryContext context, final String path) throws XPathException, PermissionDeniedException {
// check if the loaded documents should remain locked
final LockMode lockType = context.lockDocumentsOnLoad() ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
try {
final XmldbURI baseURI = context.getBaseURI().toXmldbURI();
final XmldbURI pathUri;
if (baseURI != null && !(baseURI.equals("") || baseURI.equals("/db"))) {
// relative collection Path: add the current base URI
pathUri = baseURI.resolveCollectionPath(XmldbURI.xmldbUriFor(path, false));
} else {
pathUri = XmldbURI.xmldbUriFor(path, false);
}
// relative collection Path: add the current module call URI if applicable
final XmldbURI resourceUri = Optional.ofNullable(context.getModuleLoadPath()).filter(moduleLoadPath -> !moduleLoadPath.isEmpty()).flatMap(moduleLoadPath -> Try(() -> XmldbURI.xmldbUriFor(moduleLoadPath)).toOption()).map(moduleLoadPath -> moduleLoadPath.resolveCollectionPath(pathUri)).orElse(pathUri);
// try to open the document and acquire a lock
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(resourceUri, lockType)) {
if (lockedDoc == null) {
return Sequence.EMPTY_SEQUENCE;
} else {
final DocumentImpl doc = lockedDoc.getDocument();
if (!doc.getPermissions().validate(context.getSubject(), Permission.READ)) {
throw new PermissionDeniedException("Insufficient privileges to read resource " + path);
}
if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
throw new XPathException("Document " + path + " is a binary resource, not an XML document. Please consider using the function util:binary-doc() to retrieve a reference to it.");
}
return new NodeProxy(doc);
}
}
} catch (final URISyntaxException e) {
throw new XPathException(e);
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class LowLevelTextTest method setUp.
@Before
public void setUp() throws DatabaseConfigurationException, EXistException, XPathException, PermissionDeniedException, IOException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
xqueryPool = pool.getXQueryPool();
stringSource = new StringSource(TEST_XQUERY_SOURCE);
final XQuery xquery = pool.getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
preCompiledXQuery = xquery.compile(context, stringSource);
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DebuggeeJointImpl method evalution.
@Override
public String evalution(String script) throws Exception {
Database db = compiledXQuery.getContext().getDatabase();
// TODO: account required
try (final DBBroker broker = db.getBroker()) {
XQueryContext context = compiledXQuery.getContext().copyContext();
context.setDebuggeeJoint(null);
context.undeclareGlobalVariable(Debuggee.SESSION);
context.undeclareGlobalVariable(Debuggee.IDEKEY);
XQuery service = broker.getBrokerPool().getXQueryService();
CompiledXQuery compiled = service.compile(broker, context, script);
Sequence resultSequence = service.execute(broker, compiled, null);
SAXSerializer sax = null;
Serializer serializer = broker.getSerializer();
serializer.reset();
try {
sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
Properties outputProps = new Properties();
StringWriter writer = new StringWriter();
sax.setOutput(writer, outputProps);
serializer.setSAXHandlers(sax, sax);
for (SequenceIterator i = resultSequence.iterate(); i.hasNext(); ) {
Item next = i.nextItem();
if (Type.subTypeOf(next.getType(), Type.NODE))
serializer.toSAX((NodeValue) next);
else
writer.write(next.getStringValue());
}
return writer.toString();
} finally {
if (sax != null) {
SerializerPool.getInstance().returnObject(sax);
}
}
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class Util method compileQuery.
static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
if (compiled == null) {
compiled = xqueryService.compile(context, query);
} else {
compiled.getContext().updateContext(context);
context.getWatchDog().reset();
}
return compiled;
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DebuggeeImpl method joint.
public boolean joint(CompiledXQuery compiledXQuery) {
synchronized (this) {
IoSession session = connection.connect();
if (session == null)
return false;
// link debugger session & script
DebuggeeJointImpl joint = new DebuggeeJointImpl();
joint.setCompiledScript(compiledXQuery);
XQueryContext context = compiledXQuery.getContext();
context.setDebuggeeJoint(joint);
String idesession = "";
if (context.isVarDeclared(Debuggee.SESSION)) {
try {
Variable var = context.resolveVariable(Debuggee.SESSION);
idesession = var.getValue().toString();
} catch (XPathException e) {
}
}
String idekey = "";
if (context.isVarDeclared(Debuggee.IDEKEY)) {
try {
Variable var = context.resolveVariable(Debuggee.IDEKEY);
idekey = var.getValue().toString();
} catch (XPathException e) {
}
}
joint.continuation(new Init(session, idesession, idekey));
return true;
}
}
Aggregations