Search in sources :

Example 16 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class CollectionConfigurationTest method rangeIndex1.

@Test
@Ignore
public void rangeIndex1() throws XMLDBException {
    Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
    // Configure collection automatically
    IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(CONFIG2);
    // ... then index document
    XMLResource doc = (XMLResource) testCollection.createResource(TestConstants.TEST_XML_URI.toString(), "XMLResource");
    doc.setContent(DOCUMENT_CONTENT2);
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    ResourceSet result = service.query("util:index-key-occurrences(/test/c, xs:dateTime(\"2002-12-07T12:20:46.275+01:00\") )");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/c)");
    assertEquals("xs:dateTime", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/d, xs:double(1) )");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/d)");
    assertEquals("xs:double", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/e, xs:float(1) )");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/e)");
    assertEquals("xs:float", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/f, true())");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/f)");
    assertEquals("xs:boolean", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/g, xs:integer(1))");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/g)");
    assertEquals("xs:integer", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/h, '1')");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/h)");
    assertEquals("xs:string", result.getResource(0).getContent());
    result = service.query("/test/c[(# exist:force-index-use #) { . = xs:dateTime(\"2002-12-07T12:20:46.275+01:00\") }]");
    assertEquals(1, result.getSize());
    result = service.query("/test[(# exist:force-index-use #) { c = xs:dateTime(\"2002-12-07T12:20:46.275+01:00\") }]");
    assertEquals(1, result.getSize());
    result = service.query("/test/d[(# exist:force-index-use #) { . = xs:double(1) }]");
    assertEquals(1, result.getSize());
    result = service.query("/test[(# exist:force-index-use #) { d = xs:double(1) }]");
    assertEquals(1, result.getSize());
    result = service.query("/test/e[(# exist:force-index-use #) { . = xs:float(1) }]");
    assertEquals(1, result.getSize());
    result = service.query("/test[(# exist:force-index-use #) { e = xs:float(1) }]");
    assertEquals(1, result.getSize());
    result = service.query("/test/f[(# exist:force-index-use #) { . = true() }]");
    assertEquals(1, result.getSize());
    result = service.query("/test[(# exist:force-index-use #) { f = true() }]");
    assertEquals(1, result.getSize());
    result = service.query("/test/g[(# exist:force-index-use #) { . = 1 }]");
    assertEquals(1, result.getSize());
    result = service.query("/test[(# exist:force-index-use #) { g = 1 }]");
    assertEquals(1, result.getSize());
    result = service.query("/test/h[(# exist:force-index-use #) { . = '1' }]");
    assertEquals(1, result.getSize());
    result = service.query("/test[(# exist:force-index-use #) { h = '1' }]");
    assertEquals(1, result.getSize());
    boolean exceptionCaught = false;
    try {
        result = service.query("/test[(# exist:force-index-use #) { contains(d, '1') }]");
        assertEquals(0, result.getSize());
    } catch (XMLDBException e) {
        exceptionCaught = true;
    }
    assertTrue("contains() should not use index of type xs:double", exceptionCaught);
    exceptionCaught = false;
    try {
        result = service.query("/test[(# exist:force-index-use #) { matches(d, '1') }]");
        assertEquals(0, result.getSize());
    } catch (XMLDBException e) {
        exceptionCaught = true;
    }
    assertTrue("matches() should not use index of type xs:double", exceptionCaught);
    result = service.query("/test[matches(h, '1')]");
    assertEquals(1, result.getSize());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 17 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class ContentAsDOMTest method setUp.

@Before
public void setUp() throws Exception {
    CollectionManagementService service = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    Collection testCollection = service.createCollection(TEST_COLLECTION);
    UserManagementService ums = (UserManagementService) testCollection.getService("UserManagementService", "1.0");
    // change ownership to guest
    Account guest = ums.getAccount(GUEST_DB_USER);
    ums.chown(guest, guest.getPrimaryGroup());
    ums.chmod(Permission.DEFAULT_COLLECTION_PERM);
    Resource resource = testCollection.createResource("test.xml", "XMLResource");
    resource.setContent(XML);
    testCollection.storeResource(resource);
    // change resource ownership to guest
    ums.chown(resource, guest, GUEST_DB_USER);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Account(org.exist.security.Account) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) Before(org.junit.Before)

Example 18 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class TestEXistXMLSerialize method serialize1.

@Test
public void serialize1() throws TransformerException, XMLDBException, ParserConfigurationException, SAXException, IOException, URISyntaxException {
    Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
    XMLResource resource = (XMLResource) testCollection.createResource(null, "XMLResource");
    Document doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(Paths.get(testFile.toURI()).toFile());
    resource.setContentAsDOM(doc);
    testCollection.storeResource(resource);
    resource = (XMLResource) testCollection.getResource(resource.getId());
    assertNotNull(resource);
    Node node = resource.getContentAsDOM();
    node = node.getOwnerDocument();
    // Attempting serialization
    DOMSource source = new DOMSource(node);
    try (final UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream()) {
        StreamResult result = new StreamResult(out);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Collection(org.xmldb.api.base.Collection) Document(org.w3c.dom.Document) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 19 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class SerializationTest method setUp.

@Before
public void setUp() throws XMLDBException {
    final Collection root = DatabaseManager.getCollection(getBaseUri() + "/db", TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
    final CollectionManagementService service = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
    testCollection = service.createCollection(TEST_COLLECTION_NAME);
    assertNotNull(testCollection);
    final XMLResource res = (XMLResource) testCollection.createResource(XML_DOC_NAME, "XMLResource");
    res.setContent(XML);
    testCollection.storeResource(res);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Collection(org.xmldb.api.base.Collection) XMLResource(org.xmldb.api.modules.XMLResource) Before(org.junit.Before)

Example 20 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class CollectionConfigurationTest method rangeIndex2.

@Test
@Ignore
public void rangeIndex2() throws XMLDBException {
    Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
    // Configure collection automatically
    IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(QNAME_CONFIG2);
    // ... then index document
    XMLResource doc = (XMLResource) testCollection.createResource(TestConstants.TEST_XML_URI.toString(), "XMLResource");
    doc.setContent(DOCUMENT_CONTENT2);
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    ResourceSet result = service.query("util:index-key-occurrences(/test/c, xs:dateTime(\"2002-12-07T12:20:46.275+01:00\") )");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/c)");
    assertEquals("xs:dateTime", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/d, xs:double(1) )");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/d)");
    assertEquals("xs:double", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/e, xs:float(1) )");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/e)");
    assertEquals("xs:float", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/f, true())");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/f)");
    assertEquals("xs:boolean", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/g, xs:integer(1))");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/g)");
    assertEquals("xs:integer", result.getResource(0).getContent());
    result = service.query("util:index-key-occurrences(/test/h, '1')");
    assertEquals(1, result.getSize());
    assertEquals("1", result.getResource(0).getContent());
    result = service.query("util:index-type(/test/h)");
    assertEquals("xs:string", result.getResource(0).getContent());
    result = service.query("(# exist:force-index-use #) { /test/c[. = xs:dateTime(\"2002-12-07T12:20:46.275+01:00\")] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test[c = xs:dateTime(\"2002-12-07T12:20:46.275+01:00\")] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test/d[. = xs:double(1)] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test[d = xs:double(1)] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test/e[. = xs:float(1)] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test[e = xs:float(1)] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test/f[. = true()] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test[f = true()] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test/g[. = 1] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test[g = 1] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test/h[. = '1'] }");
    assertEquals(1, result.getSize());
    result = service.query("(# exist:force-index-use #) { /test[h = '1'] }");
    assertEquals(1, result.getSize());
    boolean exceptionCaught = false;
    try {
        result = service.query("(# exist:force-index-use #) { /test[contains(d, '1')] }");
        assertEquals(0, result.getSize());
    } catch (XMLDBException e) {
        exceptionCaught = true;
    }
    assertTrue("contains() should not use index of type xs:double", exceptionCaught);
    exceptionCaught = false;
    try {
        result = service.query("(# exist:force-index-use #) { /test[matches(d, '1')] }");
        assertEquals(0, result.getSize());
    } catch (XMLDBException e) {
        exceptionCaught = true;
    }
    assertTrue("matches() should not use index of type xs:double", exceptionCaught);
    result = service.query("/test[matches(h, '1')]");
    assertEquals(1, result.getSize());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

XMLResource (org.xmldb.api.modules.XMLResource)142 Collection (org.xmldb.api.base.Collection)56 ResourceSet (org.xmldb.api.base.ResourceSet)56 XPathQueryService (org.xmldb.api.modules.XPathQueryService)49 Test (org.junit.Test)33 Resource (org.xmldb.api.base.Resource)24 XQueryService (org.xmldb.api.modules.XQueryService)23 Node (org.w3c.dom.Node)22 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)20 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)18 Document (org.w3c.dom.Document)16 XMLDBException (org.xmldb.api.base.XMLDBException)15 EXistResource (org.exist.xmldb.EXistResource)9 EXistXQueryService (org.exist.xmldb.EXistXQueryService)9 BinaryResource (org.xmldb.api.modules.BinaryResource)9 Path (java.nio.file.Path)8 Before (org.junit.Before)8 Element (org.w3c.dom.Element)8 Source (javax.xml.transform.Source)7 SAXSerializer (org.exist.util.serializer.SAXSerializer)7