Search in sources :

Example 6 with EXistXPathQueryService

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());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Example 7 with EXistXPathQueryService

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());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) Diff(org.xmlunit.diff.Diff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) Source(javax.xml.transform.Source)

Example 8 with EXistXPathQueryService

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();
        }
    }
}
Also used : Random(java.util.Random) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 9 with EXistXPathQueryService

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);
}
Also used : EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService)

Example 10 with EXistXPathQueryService

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());
}
Also used : EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) UserManagementService(org.exist.xmldb.UserManagementService) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Aggregations

EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)15 ResourceSet (org.xmldb.api.base.ResourceSet)13 Collection (org.xmldb.api.base.Collection)12 Test (org.junit.Test)10 XMLResource (org.xmldb.api.modules.XMLResource)10 Resource (org.xmldb.api.base.Resource)8 UserManagementService (org.exist.xmldb.UserManagementService)7 BinaryResource (org.xmldb.api.modules.BinaryResource)7 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)4 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)4 Random (java.util.Random)1 Source (javax.xml.transform.Source)1 DetailedDiff (org.custommonkey.xmlunit.DetailedDiff)1 EXistResource (org.exist.xmldb.EXistResource)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1 Database (org.xmldb.api.base.Database)1 XPathQueryService (org.xmldb.api.modules.XPathQueryService)1 Diff (org.xmlunit.diff.Diff)1