use of org.exist.xmldb.EXistXPathQueryService in project exist by eXist-db.
the class XPathQueryTest method staticVariables.
@Test
public void staticVariables() throws XMLDBException {
final XMLResource doc = (XMLResource) testCollection.createResource("numbers.xml", "XMLResource");
doc.setContent(numbers);
testCollection.storeResource(doc);
XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
final EXistXPathQueryService service2 = (EXistXPathQueryService) service;
service2.declareVariable("name", "MONTAGUE");
service2.declareVariable("name", "43");
// ResourceSet result = service.query("//SPEECH[SPEAKER=$name]");
ResourceSet result = service2.query(doc, "//item[stock=$name]");
result = service2.query("$name");
assertEquals(1, result.getSize());
result = service2.query(doc, "//item[stock=43]");
assertEquals(1, result.getSize());
result = service2.query(doc, "//item");
assertEquals(4, result.getSize());
// assertEquals(10, result.getSize());
}
use of org.exist.xmldb.EXistXPathQueryService in project exist by eXist-db.
the class XQueryTest method importExternalClasspathLibraryModule.
@Test
public void importExternalClasspathLibraryModule() throws XMLDBException {
final long timestamp = System.currentTimeMillis();
final Collection testCollection = getTestCollection();
final Resource doc = testCollection.createResource("import-external-classpath.xq", "BinaryResource");
doc.setContent("import module namespace ext1 = \"http://import-external-classpath-library-module-test.com\" at \"resource:org/exist/xquery/external-classpath-library-module.xqm\";\n" + "ext1:echo(" + timestamp + ")");
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
final EXistXPathQueryService service = (EXistXPathQueryService) testCollection.getService("XPathQueryService", "1.0");
final ResourceSet resourceSet = service.executeStoredQuery("/db/test/import-external-classpath.xq");
assertEquals(1, resourceSet.getSize());
final Resource resource = resourceSet.getResource(0);
final Source expected = Input.fromString("<echo>" + timestamp + "</echo>").build();
final Source actual = Input.fromString(resource.getContent().toString()).build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForIdentical().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
use of org.exist.xmldb.EXistXPathQueryService in project exist by eXist-db.
the class ProtectedModeTest method queryDocs.
@Test
public void queryDocs() throws XMLDBException {
final Collection root = DatabaseManager.getCollection("xmldb:exist:///db/protected", "admin", "");
final EXistXPathQueryService service = (EXistXPathQueryService) root.getService("XQueryService", "1.0");
final Random random = new Random();
for (int i = 0; i < COLLECTION_COUNT; i++) {
String docURI = "doc('/db/protected/test" + i + "/xdb" + random.nextInt(DOCUMENT_COUNT) + ".xml')";
try {
service.beginProtected();
final ResourceSet result = service.query(docURI + "//book");
assertEquals(1, result.getSize());
} finally {
service.endProtected();
}
}
}
use of org.exist.xmldb.EXistXPathQueryService in project exist by eXist-db.
the class InteractiveClient method find.
private ResourceSet find(String xpath) throws XMLDBException {
if (xpath.substring(xpath.length() - EOL.length()).equals(EOL)) {
xpath = xpath.substring(0, xpath.length() - EOL.length());
}
final String xpathCopy = xpath;
getTraceWriter().ifPresent(writer -> {
try {
writer.write("<query>");
writer.write(xpathCopy);
writer.write("</query>");
writer.write(EOL);
} catch (final IOException e) {
// TODO report error?
}
});
String sortBy = null;
final int p = xpath.indexOf(" sort by ");
if (p != Constants.STRING_NOT_FOUND) {
final String xp = xpath.substring(0, p);
sortBy = xpath.substring(p + " sort by ".length());
xpath = xp;
System.out.println("XPath = " + xpath);
System.out.println("Sort-by = " + sortBy);
}
final EXistXPathQueryService service = (EXistXPathQueryService) current.getService("XPathQueryService", "1.0");
service.setProperty(OutputKeys.INDENT, properties.getProperty(INDENT));
service.setProperty(OutputKeys.ENCODING, properties.getProperty(ENCODING));
for (final Map.Entry<String, String> mapping : namespaceMappings.entrySet()) {
service.setNamespace(mapping.getKey(), mapping.getValue());
}
return (sortBy == null) ? service.query(xpath) : service.query(xpath, sortBy);
}
use of org.exist.xmldb.EXistXPathQueryService in project exist by eXist-db.
the class XMLDBSecurityTest method cannotExecuteXQueryWithoutExecutePermission.
@Test(expected = XMLDBException.class)
public void cannotExecuteXQueryWithoutExecutePermission() throws XMLDBException {
Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
final UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
final String xquery = "<xquery>{ 1 + 2 }</xquery>";
Resource xqueryResource = test.createResource("test.xquery", BinaryResource.RESOURCE_TYPE);
xqueryResource.setContent(xquery);
test.storeResource(xqueryResource);
// execute only on xquery
ums.chmod(xqueryResource, "rw-------");
test.close();
test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
xqueryResource = test.getResource("test.xquery");
assertEquals(xquery, new String((byte[]) xqueryResource.getContent()));
// execute the stored XQuery
final EXistXPathQueryService queryService = (EXistXPathQueryService) test.getService("XPathQueryService", "1.0");
final ResourceSet result = queryService.executeStoredQuery("/db/securityTest1/test.xquery");
assertEquals("<xquery>3</xquery>", result.getResource(0).getContent());
}
Aggregations