Search in sources :

Example 6 with XUpdateQueryService

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

the class UpdateRecoverTest method xmldbStore.

private void xmldbStore(final BrokerPool pool) throws IllegalAccessException, DatabaseConfigurationException, InstantiationException, ClassNotFoundException, XMLDBException, EXistException {
    final org.xmldb.api.base.Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
    assertNotNull(root);
    final EXistCollectionManagementService mgr = (EXistCollectionManagementService) root.getService("CollectionManagementService", "1.0");
    assertNotNull(mgr);
    org.xmldb.api.base.Collection test = root.getChildCollection("test");
    if (test == null) {
        test = mgr.createCollection(TestConstants.TEST_COLLECTION_URI.toString());
    }
    assertNotNull(test);
    org.xmldb.api.base.Collection test2 = test.getChildCollection("test2");
    if (test2 == null) {
        test2 = mgr.createCollection(TestConstants.TEST_COLLECTION_URI2.toString());
    }
    assertNotNull(test2);
    final Resource res = test2.createResource("test_xmldb.xml", "XMLResource");
    assertNotNull(res);
    res.setContent(TEST_XML);
    test2.storeResource(res);
    final XUpdateQueryService service = (XUpdateQueryService) test2.getService("XUpdateQueryService", "1.0");
    assertNotNull(service);
    // insert some nodes
    for (int i = 1; i <= 200; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:insert-before select=\"/products/product[1]\">" + "       <product>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:insert-before>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // add attribute
    for (int i = 1; i <= 200; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products/product[" + i + "]\">" + "         <xu:attribute name=\"id\">" + i + "</xu:attribute>" + " </xu:append>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // replace some
    for (int i = 1; i <= 100; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:replace select=\"/products/product[" + i + "]\">" + "     <product id=\"" + i + "\">" + "         <description>Replaced product</description>" + "         <price>" + (i * 0.75) + "</price>" + "     </product>" + " </xu:replace>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // remove some
    for (int i = 1; i <= 100; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:remove select=\"/products/product[last()]\"/>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    for (int i = 1; i <= 100; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product>" + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:append>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // rename element "description" to "descript"
    String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:rename select=\"/products/product/description\">descript</xu:rename>" + "</xu:modifications>";
    service.updateResource("test_xmldb.xml", xupdate);
    // update attribute values
    for (int i = 1; i <= 200; i++) {
        xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/@id\">" + i + "u</xu:update>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // append new element to records
    for (int i = 1; i <= 200; i++) {
        xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products/product[" + i + "]\">" + "       <date><xu:value-of select=\"current-dateTime()\"/></date>" + "   </xu:append>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // update element content
    for (int i = 1; i <= 200; i++) {
        xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/price\">19.99</xu:update>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) Resource(org.xmldb.api.base.Resource) Database(org.xmldb.api.base.Database)

Example 7 with XUpdateQueryService

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

the class ComplexUpdateAction method update.

private void update(final Collection col, final String xupdate) throws XMLDBException {
    final XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
    long mods = service.updateResource(resourceName, xupdate);
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService)

Example 8 with XUpdateQueryService

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

the class TextUpdateAction method execute.

@Override
public boolean execute() throws XMLDBException {
    final Collection col = DatabaseManager.getCollection(collectionPath, "admin", "");
    final XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
    // append a new section
    long mods = service.update(APPEND);
    assertEquals(1, mods);
    // update paragraph content
    String updateText = Integer.toString(rand.nextInt()) + " &amp; " + Integer.toString(rand.nextInt());
    final String update = UPDATE_START + updateText + UPDATE_END;
    mods = service.update(update);
    assertEquals(1, mods);
    // query for section
    final XPathQueryService query = (XPathQueryService) col.getService("XPathQueryService", "1.0");
    ResourceSet result = query.query("/article/section/para/text()");
    assertEquals(1, result.getSize());
    updateText = result.getResource(0).getContent().toString();
    result = query.query("/article/section/para[. = '" + updateText + "']");
    assertEquals(1, result.getSize());
    result.getResource(0).getContent();
    mods = service.update(REMOVE);
    assertEquals(1, mods);
    return true;
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 9 with XUpdateQueryService

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

the class RemoveAppendTest method appendRemove.

@Test
public void appendRemove() throws XMLDBException, IOException {
    XUpdateQueryService service = (XUpdateQueryService) testCol.getService("XUpdateQueryService", "1.0");
    XPathQueryService query = (XPathQueryService) testCol.getService("XPathQueryService", "1.0");
    for (int i = 1; i <= 100; i++) {
        append(service, i);
        ResourceSet result = query.query("/test/item[@id='" + i + "']");
        assertEquals(result.getSize(), 1);
    }
    for (int i = 100; i > 10; i--) {
        String xu = "<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" + "   <xu:remove select=\"/test/item[@id='" + i + "']\"/>" + "</xu:modifications>";
        long mods = service.update(xu);
        assertEquals(mods, 1);
        ResourceSet result = query.query("/test/item/e0");
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 10 with XUpdateQueryService

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

the class RemoveAppendTest method testRemoveAppend.

@Ignore
@Test
public void testRemoveAppend() throws Exception {
    XUpdateQueryService service = (XUpdateQueryService) testCol.getService("XUpdateQueryService", "1.0");
    XPathQueryService query = (XPathQueryService) testCol.getService("XPathQueryService", "1.0");
    for (int i = 1; i < 1000; i++) {
        int which = rand.nextInt(ITEM_COUNT) + 1;
        insert(service, which);
        remove(service, which);
        ResourceSet result = query.query("/test/item[@id='" + which + "']");
        assertEquals(result.getSize(), 1);
        result.getResource(0).getContent();
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Aggregations

XUpdateQueryService (org.xmldb.api.modules.XUpdateQueryService)19 XPathQueryService (org.xmldb.api.modules.XPathQueryService)8 Collection (org.xmldb.api.base.Collection)6 ResourceSet (org.xmldb.api.base.ResourceSet)6 XMLResource (org.xmldb.api.modules.XMLResource)3 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)2 IndexQueryService (org.exist.xmldb.IndexQueryService)2 Test (org.junit.Test)2 Resource (org.xmldb.api.base.Resource)2 XMLDBException (org.xmldb.api.base.XMLDBException)2 StringWriter (java.io.StringWriter)1 URISyntaxException (java.net.URISyntaxException)1 Properties (java.util.Properties)1 UnsupportedLookAndFeelException (javax.swing.UnsupportedLookAndFeelException)1 TransformerException (javax.xml.transform.TransformerException)1 BuildException (org.apache.tools.ant.BuildException)1 Account (org.exist.security.Account)1 UserAider (org.exist.security.internal.aider.UserAider)1 StartException (org.exist.start.StartException)1 DOMSerializer (org.exist.util.serializer.DOMSerializer)1