Search in sources :

Example 86 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class XMLDBMoveTask method execute.

@Override
public void execute() throws BuildException {
    if (uri == null) {
        throw (new BuildException("You have to specify an XMLDB collection URI"));
    }
    if ((resource == null) && (collection == null)) {
        throw (new BuildException("Missing parameter: either resource or collection should be specified"));
    }
    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);
            }
        }
        log("Create collection management service for collection " + base.getName(), Project.MSG_DEBUG);
        final EXistCollectionManagementService service = (EXistCollectionManagementService) base.getService("CollectionManagementService", "1.0");
        if (resource != null) {
            log("Moving 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.moveResource(XmldbURI.xmldbUriFor(resource), XmldbURI.xmldbUriFor(destination), XmldbURI.xmldbUriFor(name));
            }
        } else {
            log("Moving collection: " + collection, Project.MSG_INFO);
            service.move(XmldbURI.xmldbUriFor(collection), XmldbURI.xmldbUriFor(destination), XmldbURI.xmldbUriFor(name));
        }
    } catch (final XMLDBException e) {
        final String msg = "XMLDB exception during move: " + e.getMessage();
        if (failonerror) {
            throw (new BuildException(msg, e));
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    } catch (final URISyntaxException e) {
        final String msg = "URI syntax exception: " + e.getMessage();
        if (failonerror) {
            throw (new BuildException(msg, e));
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    }
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException) URISyntaxException(java.net.URISyntaxException)

Example 87 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class ExpandTest method expandPersistentDomAttrNs.

@Test
public void expandPersistentDomAttrNs() throws XMLDBException {
    final String query = "declare namespace x = \"http://x\";\n" + "util:expand(doc('/db/expand-test/doc4.xml')/doc4/@x:foo)";
    final ResourceSet result = existEmbeddedServer.executeQuery(query);
    final Resource res = result.getResource(0);
    assertEquals(XMLResource.RESOURCE_TYPE, res.getResourceType());
    final XMLResource xmlRes = (XMLResource) res;
    final Node node = xmlRes.getContentAsDOM();
    assertEquals(Node.ATTRIBUTE_NODE, node.getNodeType());
    assertEquals("http://x", node.getNamespaceURI());
    assertEquals("foo", node.getNodeName());
    assertEquals("bar", node.getNodeValue());
}
Also used : Node(org.w3c.dom.Node) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 88 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class EvalTest method evalAndSerializeJson.

@Test
public void evalAndSerializeJson() throws XMLDBException {
    String query = "let $query := \"<outer><elem1>hello</elem1></outer>\"\n" + "return\n" + "util:eval-and-serialize($query, map { \"method\": \"json\" })";
    ResourceSet result = existEmbeddedServer.executeQuery(query);
    Resource r = result.getResource(0);
    assertEquals("{\"elem1\":\"hello\"}", r.getContent());
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 89 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class EvalTest method evalAndSerialize.

@Test
public void evalAndSerialize() throws XMLDBException {
    final String query = "let $query := \"<elem1>hello</elem1>\"\n" + "return\n" + "util:eval-and-serialize($query, ())";
    final ResourceSet result = existEmbeddedServer.executeQuery(query);
    final Resource r = result.getResource(0);
    assertEquals("<elem1>hello</elem1>", r.getContent());
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 90 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class EvalTest method evalAndSerializeAdaptive.

@Test
public void evalAndSerializeAdaptive() throws XMLDBException {
    String query = "let $query := 'map { \"key\": \"value\"}'\n" + "return\n" + "util:eval-and-serialize($query, map { \"method\": \"adaptive\" })";
    ResourceSet result = existEmbeddedServer.executeQuery(query);
    Resource r = result.getResource(0);
    assertEquals("map{\"key\":\"value\"}", r.getContent());
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet)

Aggregations

Resource (org.xmldb.api.base.Resource)173 XMLResource (org.xmldb.api.modules.XMLResource)126 Collection (org.xmldb.api.base.Collection)111 BinaryResource (org.xmldb.api.modules.BinaryResource)86 Test (org.junit.Test)77 UserManagementService (org.exist.xmldb.UserManagementService)52 ResourceSet (org.xmldb.api.base.ResourceSet)46 XMLDBException (org.xmldb.api.base.XMLDBException)38 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)32 EXistResource (org.exist.xmldb.EXistResource)27 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)25 XPathQueryService (org.xmldb.api.modules.XPathQueryService)18 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)16 Path (java.nio.file.Path)11 Database (org.xmldb.api.base.Database)11 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)9 Source (javax.xml.transform.Source)9 BuildException (org.apache.tools.ant.BuildException)9 Diff (org.xmlunit.diff.Diff)9