Search in sources :

Example 76 with XMLDBException

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

the class XQueryTest method functionDoc.

@Test
public void functionDoc() throws XMLDBException, IOException, SAXException {
    ResourceSet result;
    String query;
    @SuppressWarnings("unused") boolean exceptionThrown;
    @SuppressWarnings("unused") String message;
    XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
    query = "doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "')";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
    DetailedDiff d = new DetailedDiff(compareXML(numbers, n.toString()));
    assertEquals(0, d.getAllDifferences().size());
    // ignore eXist namespace's attributes
    // assertEquals(1, d.getAllDifferences().size());
    query = "let $v := ()\n" + "return doc($v)";
    result = service.query(query);
    assertEquals("XQuery: " + query, 0, result.getSize());
    query = "doc('" + XmldbURI.ROOT_COLLECTION + "/test/dummy" + NUMBERS_XML + "')";
    try {
        exceptionThrown = false;
        result = service.query(query);
    } catch (XMLDBException e) {
        exceptionThrown = true;
        message = e.getMessage();
    }
    // TODO : to be decided !
    // assertTrue(exceptionThrown);
    assertEquals(0, result.getSize());
    query = "doc-available('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "')";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "true", result.getResource(0).getContent());
    query = "let $v := ()\n" + "return doc-available($v)";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "false", result.getResource(0).getContent());
    query = "doc-available('" + XmldbURI.ROOT_COLLECTION + "/test/dummy" + NUMBERS_XML + "')";
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "false", result.getResource(0).getContent());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) Node(org.w3c.dom.Node) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 77 with XMLDBException

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

the class XQueryFunctionsTest method min.

@Test
public void min() throws XPathException, XMLDBException {
    ResourceSet result = existEmbeddedServer.executeQuery("min((1, 2))");
    String r = (String) result.getResource(0).getContent();
    assertEquals("1", r);
    result = existEmbeddedServer.executeQuery("min((<a>1</a>, <b>2</b>))");
    r = (String) result.getResource(0).getContent();
    assertEquals("1", r);
    result = existEmbeddedServer.executeQuery("min(())");
    assertEquals(0, result.getSize());
    result = existEmbeddedServer.executeQuery("min((xs:dateTime('2005-12-19T16:22:40.006+01:00'), xs:dateTime('2005-12-19T16:29:40.321+01:00')))");
    r = (String) result.getResource(0).getContent();
    assertEquals("2005-12-19T16:22:40.006+01:00", r);
    result = existEmbeddedServer.executeQuery("min(('a', 'b'))");
    r = (String) result.getResource(0).getContent();
    assertEquals("a", r);
    String message = "";
    try {
        result = existEmbeddedServer.executeQuery("min((xs:dateTime('2005-12-19T16:22:40.006+01:00'), 'a'))");
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("FORG0006") > -1);
    try {
        message = "";
        result = existEmbeddedServer.executeQuery("min(1, 2)");
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    // depends whether we have strict type checking or not
    assertTrue(message.indexOf("XPTY0004") > -1 | message.indexOf("FORG0001") > -1 | message.indexOf("FOCH0002") > -1);
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 78 with XMLDBException

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

the class XMLDBExtractTask method execute.

@Override
public void execute() throws BuildException {
    if (uri == null) {
        if (failonerror) {
            throw (new BuildException("You need to specify an XMLDB collection URI"));
        }
    } else {
        registerDatabase();
        try {
            final Collection base = DatabaseManager.getCollection(uri, user, password);
            if (base == null) {
                throw (new BuildException("Collection " + uri + " could not be found."));
            }
            if ((resource != null) && (destDir == null)) {
                // extraction of a single resource
                log("Extracting resource: " + resource + " to " + destFile.toAbsolutePath().toString(), 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 {
                    writeResource(res, destFile);
                }
            } else {
                // extraction of a collection
                extractResources(base, null);
                if (subcollections) {
                    extractSubCollections(base, null);
                }
            }
        } catch (final XMLDBException e) {
            final String msg = "XMLDB exception caught while executing query: " + e.getMessage();
            if (failonerror) {
                throw (new BuildException(msg, e));
            } else {
                log(msg, e, Project.MSG_ERR);
            }
        } catch (final IOException e) {
            final String msg = "XMLDB exception caught while writing destination file: " + e.getMessage();
            if (failonerror) {
                throw (new BuildException(msg, e));
            } else {
                log(msg, e, Project.MSG_ERR);
            }
        }
    }
}
Also used : ExtendedResource(org.exist.xmldb.ExtendedResource) XMLResource(org.xmldb.api.modules.XMLResource) 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 79 with XMLDBException

use of org.xmldb.api.base.XMLDBException 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 80 with XMLDBException

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

the class XMLDBShutdownTask method execute.

/* (non-Javadoc)
     * @see org.apache.tools.ant.Task#execute()
     */
public void execute() throws BuildException {
    if (uri == null) {
        throw (new BuildException("you have to specify an XMLDB collection URI"));
    }
    registerDatabase();
    try {
        log("Get base collection: " + uri, Project.MSG_DEBUG);
        final Collection root = DatabaseManager.getCollection(uri, user, password);
        if (root == null) {
            final String msg = "Collection " + uri + " could not be found.";
            if (failonerror) {
                throw (new BuildException(msg));
            } else {
                log(msg, Project.MSG_ERR);
            }
        } else {
            final DatabaseInstanceManager mgr = (DatabaseInstanceManager) root.getService("DatabaseInstanceManager", "1.0");
            log("Shutdown database instance", Project.MSG_INFO);
            mgr.shutdown();
        }
    } catch (final XMLDBException e) {
        final String msg = "Error during database shutdown: " + e.getMessage();
        if (failonerror) {
            throw (new BuildException(msg, e));
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    }
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException) DatabaseInstanceManager(org.exist.xmldb.DatabaseInstanceManager)

Aggregations

XMLDBException (org.xmldb.api.base.XMLDBException)174 Collection (org.xmldb.api.base.Collection)66 Resource (org.xmldb.api.base.Resource)34 URISyntaxException (java.net.URISyntaxException)30 XMLResource (org.xmldb.api.modules.XMLResource)30 ResourceSet (org.xmldb.api.base.ResourceSet)23 BuildException (org.apache.tools.ant.BuildException)21 IOException (java.io.IOException)19 XPathException (org.exist.xquery.XPathException)18 PermissionDeniedException (org.exist.security.PermissionDeniedException)16 SAXException (org.xml.sax.SAXException)16 EXistException (org.exist.EXistException)15 UserManagementService (org.exist.xmldb.UserManagementService)15 XPathQueryService (org.xmldb.api.modules.XPathQueryService)13 BinaryResource (org.xmldb.api.modules.BinaryResource)12 ArrayList (java.util.ArrayList)11 Account (org.exist.security.Account)11 EXistResource (org.exist.xmldb.EXistResource)10 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)10 Properties (java.util.Properties)9