Search in sources :

Example 71 with Resource

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

the class UpdateReplaceTest method replaceFirstChildWhereParentHasAttribute.

@Test
public void replaceFirstChildWhereParentHasAttribute() throws XMLDBException {
    final String testDocName = "replaceFirstChildWhereParentHasAttribute.xml";
    final String testDoc = "<Test><Content Foo=\"bar\"><A/><A/></Content></Test>";
    final String updateQuery = "let $content := doc('/db/test/" + testDocName + "')/Test/Content\n" + "    let $legacy := $content/A[1]\n" + "    return\n" + "      update replace $legacy with <AA/>,\n" + "    doc('/db/test/" + testDocName + "')/Test";
    final XQueryService xqueryService = storeXMLStringAndGetQueryService(testDocName, testDoc);
    final ResourceSet result = xqueryService.query(updateQuery);
    assertNotNull(result);
    assertEquals(1, result.getSize());
    final Resource res1 = result.getResource(0);
    assertNotNull(res1);
    assertEquals(XMLResource.RESOURCE_TYPE, res1.getResourceType());
    final Document doc = ((XMLResource) res1).getContentAsDOM().getOwnerDocument();
    final Source actual = Input.fromDocument(doc).build();
    final Source expected = Input.fromString("<Test><Content Foo='bar'><AA/><A/></Content></Test>").build();
    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Diff(org.xmlunit.diff.Diff) XQueryService(org.xmldb.api.modules.XQueryService) EXistResource(org.exist.xmldb.EXistResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) Document(org.w3c.dom.Document) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 72 with Resource

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

the class CDataIntergationTest method cdataXmlDbApi.

@Test
public void cdataXmlDbApi() throws XMLDBException {
    final String docName = "xmldb-cdata-test.xml";
    final Database database = new DatabaseImpl();
    DatabaseManager.registerDatabase(database);
    // store document
    Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
    try {
        final Resource resource = root.createResource(docName, XMLResource.RESOURCE_TYPE);
        resource.setContent(cdata_xml);
        root.storeResource(resource);
    } finally {
        root.close();
    }
    // retrieve document
    root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
    try {
        final Resource resource = root.getResource(docName);
        assertNotNull(resource);
        assertEquals(cdata_xml, resource.getContent().toString());
    } finally {
        root.close();
    }
}
Also used : DatabaseImpl(org.exist.xmldb.DatabaseImpl) Database(org.xmldb.api.base.Database) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) Test(org.junit.Test)

Example 73 with Resource

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

the class XQueryTest method currentDateTimeInModules_1894009.

@Test
public void currentDateTimeInModules_1894009() throws XMLDBException {
    String module = "module namespace dt = \"dt\";\n" + "\n" + "declare function dt:fib($n) {\n" + "  if ($n < 2) then $n else dt:fib($n - 1) + dt:fib($n - 2) \n" + "};\n" + "\n" + "declare function dt:dateTime() {\n" + "  (: Do something time consuming first. :)  \n" + "  let $a := dt:fib(25)" + "  return current-dateTime()\n" + "};";
    String module_name = "dt.xqm";
    Resource doc;
    // Store module
    Collection testCollection = getTestCollection();
    doc = testCollection.createResource(module_name, "BinaryResource");
    doc.setContent(module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    String query = "import module namespace dt = \"dt\" at" + "  \"xmldb:exist:///db/test/dt.xqm\"; " + "(<this>{current-dateTime()}</this>, <this>{dt:dateTime()}</this>)";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(2, result.getSize());
    assertEquals("First", result.getResource(0).getContent().toString(), result.getResource(1).getContent().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 74 with Resource

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

the class XQueryTest method namespace.

@Test
public void namespace() throws XMLDBException, IOException, SAXException {
    Resource doc;
    ResourceSet result;
    String query;
    @SuppressWarnings("unused") XMLResource resu;
    @SuppressWarnings("unused") boolean exceptionThrown;
    String message;
    Collection testCollection = getTestCollection();
    doc = testCollection.createResource(MODULE1_NAME, "BinaryResource");
    doc.setContent(module1);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(MODULE2_NAME, "BinaryResource");
    doc.setContent(module2);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(NAMESPACED_NAME, "XMLResource");
    doc.setContent(namespacedDocument);
    ((EXistResource) doc).setMimeType("application/xml");
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "(:: redefine existing prefix ::)\n" + "declare namespace blah=\"bla\";\n" + "$blah:param";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XQST0033") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "(:: redefine existing prefix with same getUri ::)\n" + "declare namespace blah=\"blah\";\n" + "declare variable $blah:param  {\"value-2\"};\n" + "$blah:param";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XQST0033") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"ho\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "$foo:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("does not match namespace URI") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"ho\" at \"" + URI + "/test/" + MODULE2_NAME + "\";\n" + "$bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("No namespace defined for prefix") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"blah\" at \"" + URI + "/test/" + MODULE2_NAME + "\";\n" + "$bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("No namespace defined for prefix") > -1);
    query = "declare namespace x = \"http://www.foo.com\"; \n" + "let $a := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NAMESPACED_NAME + "') \n" + "return $a//x:edition";
    result = service.query(query);
    assertEquals("XQuery: " + query, 0, result.getSize());
    query = "declare namespace x = \"http://www.foo.com\"; \n" + "declare namespace y = \"http://exist.sourceforge.net/dc-ext\"; \n" + "let $a := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NAMESPACED_NAME + "') \n" + "return $a//y:edition";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "<x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>", ((XMLResource) result.getResource(0)).getContent());
    query = "<result xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>{//rdf:Description}</result>";
    result = service.query(query);
    assertEquals(query, "<result xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" + "    <rdf:Description id=\"3\">\n" + "        <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title</dc:title>\n" + "        <dc:creator xmlns:dc=\"http://purl.org/dc/elements/1.1/\">creator</dc:creator>\n" + "        <x:place xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:place>\n" + "        <x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>\n" + "    </rdf:Description>\n" + "</result>", ((XMLResource) result.getResource(0)).getContent());
    query = "<result xmlns='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>{//Description}</result>";
    result = service.query(query);
    assertEquals("XQuery: " + query, "<result xmlns=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" + "    <rdf:Description xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" id=\"3\">\n" + "        <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title</dc:title>\n" + "        <dc:creator xmlns:dc=\"http://purl.org/dc/elements/1.1/\">creator</dc:creator>\n" + "        <x:place xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:place>\n" + "        <x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>\n" + "    </rdf:Description>\n" + "</result>", ((XMLResource) result.getResource(0)).getContent());
    // Interesting one : let's see with XQuery gurus :-)
    // declare namespace fn="";
    // fn:current-time()
    /*
        If the URILiteral part of a namespace declaration is a zero-length string,
        any existing namespace binding for the given prefix is removed from
        the statically known namespaces. This feature provides a way
        to remove predeclared namespace prefixes such as local.
         */
    query = "declare option exist:serialize 'indent=no';" + "for $x in <parent4 xmlns=\"http://www.example.com/parent4\"><child4/></parent4> " + "return <new>{$x//*:child4}</new>";
    result = service.query(query);
    assertXMLEqual("<new><child4 xmlns='http://www.example.com/parent4'/></new>", ((XMLResource) result.getResource(0)).getContent().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 75 with Resource

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

the class XQueryTest method module.

@Test
public void module() throws XMLDBException {
    Resource doc;
    ResourceSet result;
    String query;
    String message;
    Collection testCollection = getTestCollection();
    doc = testCollection.createResource(MODULE1_NAME, "BinaryResource");
    doc.setContent(module1);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(MODULE3_NAME, "BinaryResource");
    doc.setContent(module3);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(MODULE4_NAME, "BinaryResource");
    doc.setContent(module4);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(FATHER_MODULE_NAME, "BinaryResource");
    doc.setContent(fatherModule);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(CHILD1_MODULE_NAME, "BinaryResource");
    doc.setContent(child1Module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    doc = testCollection.createResource(CHILD2_MODULE_NAME, "BinaryResource");
    doc.setContent(child2Module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "$blah:param";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "value-1", result.getResource(0).getContent());
    // query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + getUri + "/test/" + MODULE1_NAME + "\";\n" + "(:: redefine variable ::)\n" + "declare variable $blah:param  {\"value-2\"};\n" + "$blah:param";
    // try {
    // message = "";
    // result = service.query(query);
    // } catch (XMLDBException e) {
    // message = e.getMessage();
    // }
    // assertTrue(message.indexOf("XQST0049") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "declare namespace blah2=\"blah\";\n" + "$blah2:param";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "value-1", result.getResource(0).getContent());
    query = "xquery version \"1.0\";\n" + "import module namespace blah=\"bla\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "$blah:param";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("does not match namespace URI") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "$foo:bar, $foo:bar1, $foo:bar2";
    result = service.query(query);
    assertEquals("XQuery: " + query, 3, result.getSize());
    assertEquals("XQuery: " + query, "bar", result.getResource(0).getContent());
    assertEquals("XQuery: " + query, "bar1", result.getResource(1).getContent());
    assertEquals("XQuery: " + query, "bar2", result.getResource(2).getContent());
    // Non-heritance check
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "declare namespace foo1=\"foo1\"; \n" + "$foo1:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XPDY0002") > -1);
    // Non-heritance check
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "declare namespace foo2=\"foo2\"; \n" + "$foo2:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XPDY0002") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo1=\"foo\" at \"" + URI + "/test/" + CHILD1_MODULE_NAME + "\";\n" + "import module namespace foo2=\"foo\" at \"" + URI + "/test/" + CHILD1_MODULE_NAME + "\";\n" + "$foo1:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    // Should be a XQST0047 error
    assertTrue(message.indexOf("does not match namespace URI") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE3_NAME + "\";\n" + "$bar:bar";
    try {
        message = "";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("No namespace defined for prefix") > -1);
    query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE4_NAME + "\";\n" + "foo:bar()";
    try {
        message = "";
        result = service.query(query);
        // WARNING !
        // This result is false ! The external vairable has not been resolved
        // Furthermore it is not in the module's namespace !
        assertEquals("XQuery: " + query, 0, result.getSize());
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
// This is the good result !
// assertTrue(message.indexOf("XQST0048") > -1);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) 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