Search in sources :

Example 1 with XUpdateQueryService

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.");
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService)

Example 2 with XUpdateQueryService

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);
        }
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException)

Example 3 with XUpdateQueryService

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);
}
Also used : ExtendedDOMSerializer(org.exist.util.serializer.ExtendedDOMSerializer) NodeValue(org.exist.xquery.value.NodeValue) ExtendedDOMSerializer(org.exist.util.serializer.ExtendedDOMSerializer) DOMSerializer(org.exist.util.serializer.DOMSerializer) StringWriter(java.io.StringWriter) XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) XMLDBException(org.xmldb.api.base.XMLDBException) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException)

Example 4 with XUpdateQueryService

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);
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

Example 5 with XUpdateQueryService

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);
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

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