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);
}
}
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);
}
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()) + " & " + 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;
}
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");
}
}
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();
}
}
Aggregations