use of org.xmldb.api.modules.XUpdateQueryService in project exist by eXist-db.
the class InteractiveClient method xupdate.
private void xupdate(final Optional<String> resource, final Path file) throws XMLDBException, IOException {
if (!(Files.exists(file) && Files.isReadable(file))) {
messageln("cannot read file " + file.normalize().toAbsolutePath().toString());
return;
}
final String commands = XMLUtil.readFile(file, UTF_8);
final XUpdateQueryService service = (XUpdateQueryService) current.getService("XUpdateQueryService", "1.0");
final long modifications;
if (resource.isPresent()) {
modifications = service.updateResource(resource.get(), commands);
} else {
modifications = service.update(commands);
}
messageln(modifications + " modifications processed " + "successfully.");
}
use of org.xmldb.api.modules.XUpdateQueryService in project exist by eXist-db.
the class XMLDBXUpdateTask method execute.
@Override
public void execute() throws BuildException {
if (uri == null) {
throw (new BuildException("You have to specify an XMLDB collection URI"));
}
log("XUpdate command is: " + commands, Project.MSG_DEBUG);
registerDatabase();
try {
log("Get base collection: " + uri, Project.MSG_DEBUG);
final Collection base = DatabaseManager.getCollection(uri, user, password);
if (base == null) {
final String msg = "Collection " + uri + " could not be found.";
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
} else {
final XUpdateQueryService service = (XUpdateQueryService) base.getService("XUpdateQueryService", "1.0");
if (resource != null) {
log("Updating resource: " + resource, Project.MSG_INFO);
final Resource res = base.getResource(resource);
if (res == null) {
final String msg = "Resource " + resource + " not found.";
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
} else {
service.updateResource(resource, commands);
}
} else {
log("Updating collection: " + base.getName(), Project.MSG_INFO);
service.update(commands);
}
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception during XUpdate: " + e.getMessage();
if (failonerror) {
throw (new BuildException(msg, e));
} else {
log(msg, e, Project.MSG_ERR);
}
}
}
use of org.xmldb.api.modules.XUpdateQueryService in project exist by eXist-db.
the class XMLDBXUpdate method evalWithCollection.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence evalWithCollection(Collection c, Sequence[] args, Sequence contextSequence) throws XPathException {
final NodeValue data = (NodeValue) args[1].itemAt(0);
final StringWriter writer = new StringWriter();
final Properties properties = new Properties();
properties.setProperty(OutputKeys.INDENT, "yes");
final DOMSerializer serializer = new ExtendedDOMSerializer(context.getBroker(), writer, properties);
try {
serializer.serialize(data.getNode());
} catch (final TransformerException e) {
logger.debug("Exception while serializing XUpdate document", e);
throw new XPathException(this, "Exception while serializing XUpdate document: " + e.getMessage(), e);
}
final String xupdate = writer.toString();
long modifications = 0;
try {
final XUpdateQueryService service = (XUpdateQueryService) c.getService("XUpdateQueryService", "1.0");
logger.debug("Processing XUpdate request: {}", xupdate);
modifications = service.update(xupdate);
} catch (final XMLDBException e) {
throw new XPathException(this, "Exception while processing xupdate: " + e.getMessage(), e);
}
context.getRootExpression().resetState(false);
return new IntegerValue(modifications);
}
use of org.xmldb.api.modules.XUpdateQueryService in project exist by eXist-db.
the class ValueIndexTest method updates.
@Test
public void updates() throws Exception {
configureCollection(CONFIG_PATH);
storeXMLFileAndGetQueryService(ITEMS_FILENAME, ITEMS_FILE);
for (int i = 100; i <= 150; i++) {
String append = "<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" + " <xu:append select=\"/items\">" + " <item id=\"i" + i + "\">" + " <itemno>" + i + "</itemno>" + " <name>New Item</name>" + " <price>55.50</price>" + " </item>" + " </xu:append>" + "</xu:modifications>";
String remove = "<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" + " <xu:remove select=\"/items/item[itemno=" + i + "]\"/>" + "</xu:modifications>";
XPathQueryService query = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
XUpdateQueryService update = (XUpdateQueryService) testCollection.getService("XUpdateQueryService", "1.0");
long mods = update.updateResource(ITEMS_FILENAME, append);
assertEquals(mods, 1);
queryResource(query, ITEMS_FILENAME, "//item[price = 55.50]", 1);
queryResource(query, ITEMS_FILENAME, "//item[@id = 'i" + i + "']", 1);
mods = update.updateResource(ITEMS_FILENAME, remove);
assertEquals(mods, 1);
queryResource(query, ITEMS_FILENAME, "//item[itemno = " + i + "]", 0);
}
}
use of org.xmldb.api.modules.XUpdateQueryService in project exist by eXist-db.
the class ValueIndexTest method updatesQName.
@Test
public void updatesQName() throws Exception {
configureCollection(CONFIG_QNAME);
storeXMLFileAndGetQueryService(ITEMS_FILENAME, ITEMS_FILE);
for (int i = 100; i <= 150; i++) {
String append = "<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" + " <xu:append select=\"/items\">" + " <item id=\"i" + i + "\">" + " <itemno>" + i + "</itemno>" + " <name>New Item</name>" + " <price>55.50</price>" + " </item>" + " </xu:append>" + "</xu:modifications>";
String remove = "<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" + " <xu:remove select=\"/items/item[itemno=" + i + "]\"/>" + "</xu:modifications>";
XPathQueryService query = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
XUpdateQueryService update = (XUpdateQueryService) testCollection.getService("XUpdateQueryService", "1.0");
long mods = update.updateResource(ITEMS_FILENAME, append);
assertEquals(mods, 1);
queryResource(query, ITEMS_FILENAME, "//((#exist:optimize#) { item[price = 55.50] })", 1);
queryResource(query, ITEMS_FILENAME, "//((#exist:optimize#) { item[@id = 'i" + i + "']})", 1);
queryResource(query, ITEMS_FILENAME, "//((#exist:optimize#) { item[itemno = " + i + "] })", 1);
mods = update.updateResource(ITEMS_FILENAME, remove);
assertEquals(mods, 1);
queryResource(query, ITEMS_FILENAME, "//((#exist:optimize#) { item[itemno = " + i + "] })", 0);
}
}
Aggregations