use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DebuggeeImpl method start.
@Override
public String start(String uri) throws Exception {
Database db = null;
ScriptRunner runner = null;
try {
db = BrokerPool.getInstance();
try (final DBBroker broker = db.getBroker()) {
// Try to find the XQuery
Source source = SourceFactory.getSource(broker, "", uri, true);
if (source == null)
return null;
XQuery xquery = broker.getBrokerPool().getXQueryService();
XQueryContext queryContext = new XQueryContext(broker.getBrokerPool());
// Find correct script load path
queryContext.setModuleLoadPath(XmldbURI.create(uri).removeLastSegment().toString());
CompiledXQuery compiled;
try {
compiled = xquery.compile(broker, queryContext, source);
} catch (IOException e) {
e.printStackTrace();
return null;
}
String sessionId = String.valueOf(queryContext.hashCode());
// link debugger session & script
DebuggeeJointImpl joint = new DebuggeeJointImpl();
SessionImpl session = new SessionImpl();
joint.setCompiledScript(compiled);
queryContext.setDebuggeeJoint(joint);
joint.continuation(new Init(session, sessionId, "eXist"));
runner = new ScriptRunner(session, compiled);
runner.start();
int count = 0;
while (joint.firstExpression == null && runner.exception == null && count < 10) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
count++;
}
if (runner.exception != null) {
throw runner.exception;
}
if (joint.firstExpression == null) {
throw new XPathException("Can't run debug session.");
}
// queryContext.declareVariable(Debuggee.SESSION, sessionId);
// XXX: make sure that it started up
sessions.put(sessionId, session);
return sessionId;
}
} catch (Exception e) {
if (runner != null)
runner.stop();
throw e;
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class ContextGet method getPropertiesString.
private String getPropertiesString() {
if (variables == null)
// XXX: error?
return "";
StringBuilder properties = new StringBuilder();
XQueryContext ctx = getJoint().getContext();
for (Variable variable : variables.values()) {
properties.append(PropertyGet.getPropertyString(variable, ctx));
}
return properties.toString();
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class XIncludeFilter method processXInclude.
/**
* @param href The resource to be xincluded
* @param xpointer The xpointer
* @return Optionally a ResourceError if it was not possible to retrieve the resource
* to be xincluded
* @throws SAXException If a SAX processing error occurs
*/
protected Optional<ResourceError> processXInclude(final String href, String xpointer) throws SAXException {
if (href == null) {
throw new SAXException("No href attribute found in XInclude include element");
}
// save some settings
DocumentImpl prevDoc = document;
boolean createContainerElements = serializer.createContainerElements;
serializer.createContainerElements = false;
// The following comments are the basis for possible external documents
XmldbURI docUri = null;
try {
docUri = XmldbURI.xmldbUriFor(href);
/*
if(!stylesheetUri.toCollectionPathURI().equals(stylesheetUri)) {
externalUri = stylesheetUri.getXmldbURI();
}
*/
} catch (final URISyntaxException e) {
// could be an external URI!
}
// parse the href attribute
LOG.debug("found href=\"{}\"", href);
// String xpointer = null;
// String docName = href;
Map<String, String> params = null;
DocumentImpl doc = null;
org.exist.dom.memtree.DocumentImpl memtreeDoc = null;
boolean xqueryDoc = false;
if (docUri != null) {
final String fragment = docUri.getFragment();
if (!(fragment == null || fragment.length() == 0)) {
throw new SAXException("Fragment identifiers must not be used in an xinclude href attribute. To specify an xpointer, use the xpointer attribute.");
}
// extract possible parameters in the URI
params = null;
final String paramStr = docUri.getQuery();
if (paramStr != null) {
params = processParameters(paramStr);
// strip query part
docUri = XmldbURI.create(docUri.getRawCollectionPath());
}
// Patch 1520454 start
if (!docUri.isAbsolute() && document != null) {
final String base = document.getCollection().getURI() + "/";
final String child = "./" + docUri.toString();
final URI baseUri = URI.create(base);
final URI childUri = URI.create(child);
final URI uri = baseUri.resolve(childUri);
docUri = XmldbURI.create(uri);
}
// retrieve the document
try {
doc = serializer.broker.getResource(docUri, Permission.READ);
} catch (final PermissionDeniedException e) {
return Optional.of(new ResourceError("Permission denied to read XInclude'd resource", e));
}
/* Check if the document is a stored XQuery */
if (doc != null && doc.getResourceType() == DocumentImpl.BINARY_FILE) {
xqueryDoc = "application/xquery".equals(doc.getMimeType());
}
}
// The document could not be found: check if it points to an external resource
if (docUri == null || (doc == null && !docUri.isAbsolute())) {
try {
URI externalUri = new URI(href);
final String scheme = externalUri.getScheme();
// XQuery context.
if (scheme == null && moduleLoadPath != null) {
final String path = externalUri.getSchemeSpecificPart();
Path f = Paths.get(path);
if (!f.isAbsolute()) {
if (moduleLoadPath.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
final XmldbURI parentUri = XmldbURI.create(moduleLoadPath);
docUri = parentUri.append(path);
doc = (DocumentImpl) serializer.broker.getXMLResource(docUri);
if (doc != null && !doc.getPermissions().validate(serializer.broker.getCurrentSubject(), Permission.READ)) {
throw new PermissionDeniedException("Permission denied to read XInclude'd resource");
}
} else {
f = Paths.get(moduleLoadPath, path);
externalUri = f.toUri();
}
}
}
if (doc == null) {
final Either<ResourceError, org.exist.dom.memtree.DocumentImpl> external = parseExternal(externalUri);
if (external.isLeft()) {
return Optional.of(external.left().get());
} else {
memtreeDoc = external.right().get();
}
}
} catch (final PermissionDeniedException e) {
return Optional.of(new ResourceError("Permission denied on XInclude'd resource", e));
} catch (final ParserConfigurationException | URISyntaxException e) {
throw new SAXException("XInclude: failed to parse document at URI: " + href + ": " + e.getMessage(), e);
}
}
/* if document has not been found and xpointer is
* null, throw an exception. If xpointer != null
* we retry below and interpret docName as
* a collection.
*/
if (doc == null && memtreeDoc == null && xpointer == null) {
return Optional.of(new ResourceError("document " + docUri + " not found"));
}
if (xpointer == null && !xqueryDoc) {
// no xpointer found - just serialize the doc
if (memtreeDoc == null) {
serializer.serializeToReceiver(doc, false);
} else {
serializer.serializeToReceiver(memtreeDoc, false);
}
} else {
// process the xpointer or the stored XQuery
Source source = null;
final XQueryPool pool = serializer.broker.getBrokerPool().getXQueryPool();
CompiledXQuery compiled = null;
try {
if (xpointer == null) {
source = new DBSource(serializer.broker, (BinaryDocument) doc, true);
} else {
xpointer = checkNamespaces(xpointer);
source = new StringSource(xpointer);
}
final XQuery xquery = serializer.broker.getBrokerPool().getXQueryService();
XQueryContext context;
compiled = pool.borrowCompiledXQuery(serializer.broker, source);
if (compiled == null) {
context = new XQueryContext(serializer.broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.declareNamespaces(namespaces);
context.declareNamespace("xinclude", Namespaces.XINCLUDE_NS);
// setup the http context if known
if (serializer.httpContext != null) {
context.setHttpContext(serializer.httpContext);
}
// TODO: change these to putting the XmldbURI in, but we need to warn users!
if (document != null) {
context.declareVariable("xinclude:current-doc", document.getFileURI().toString());
context.declareVariable("xinclude:current-collection", document.getCollection().getURI().toString());
}
if (xpointer != null) {
if (doc != null) {
context.setStaticallyKnownDocuments(new XmldbURI[] { doc.getURI() });
} else if (docUri != null) {
context.setStaticallyKnownDocuments(new XmldbURI[] { docUri });
}
}
// pass parameters as variables
if (params != null) {
for (final Map.Entry<String, String> entry : params.entrySet()) {
context.declareVariable(entry.getKey(), entry.getValue());
}
}
if (compiled == null) {
try {
compiled = xquery.compile(context, source, xpointer != null);
} catch (final IOException e) {
throw new SAXException("I/O error while reading query for xinclude: " + e.getMessage(), e);
}
} else {
compiled.getContext().updateContext(context);
context.getWatchDog().reset();
}
LOG.info("xpointer query: {}", ExpressionDumper.dump((Expression) compiled));
Sequence contextSeq = null;
if (memtreeDoc != null) {
contextSeq = memtreeDoc;
}
try {
final Sequence seq = xquery.execute(serializer.broker, compiled, contextSeq);
if (Type.subTypeOf(seq.getItemType(), Type.NODE)) {
if (LOG.isDebugEnabled()) {
LOG.debug("xpointer found: {}", seq.getItemCount());
}
NodeValue node;
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
node = (NodeValue) i.nextItem();
serializer.serializeToReceiver(node, false);
}
} else {
String val;
for (int i = 0; i < seq.getItemCount(); i++) {
val = seq.itemAt(i).getStringValue();
characters(val);
}
}
} finally {
context.runCleanupTasks();
}
} catch (final XPathException | PermissionDeniedException e) {
LOG.warn("xpointer error", e);
throw new SAXException("Error while processing XInclude expression: " + e.getMessage(), e);
} finally {
if (compiled != null) {
pool.returnCompiledXQuery(source, compiled);
}
}
}
// restore settings
document = prevDoc;
serializer.createContainerElements = createContainerElements;
return Optional.empty();
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class XQueryCompilationTest method compileQuery.
protected static Either<XPathException, CompiledXQuery> compileQuery(final String string) throws EXistException, PermissionDeniedException {
final BrokerPool pool = server.getBrokerPool();
final XQuery xqueryService = pool.getXQueryService();
try (final DBBroker broker = pool.getBroker()) {
try {
return Right(xqueryService.compile(new XQueryContext(broker.getDatabase()), string));
} catch (final XPathException e) {
return Left(e);
}
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DocTest method doc_dynamicallyAvailableDocument_relativeUri.
@Test
public void doc_dynamicallyAvailableDocument_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 docRelativeUri = "doc1";
final String query = "fn:doc('" + docRelativeUri + "')";
try (final DBBroker broker = pool.getBroker()) {
final XQueryContext context = new XQueryContext(pool);
context.setBaseURI(new AnyURIValue(new URI(baseUri)));
context.addDynamicallyAvailableDocument(baseUri + docRelativeUri, (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());
}
}
Aggregations