Search in sources :

Example 56 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class XQueryTest method memproc_1938498.

/**
 * @see http://sourceforge.net/support/tracker.php?aid=1938498
 */
@Test
public void memproc_1938498() throws XMLDBException {
    String xmldocument = "<Root><Child/></Root>";
    String location = "1938498.xml";
    String query = "let $test := doc(\"1938498.xml\")" + "let $inmems := <InMem>{$test}</InMem>" + "return <Test>{$inmems/X}</Test>";
    String output = "<Test/>";
    XPathQueryService service = storeXMLStringAndGetQueryService(location, xmldocument);
    ResourceSet result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, output, result.getResource(0).getContent().toString());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 57 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class XQueryTest method stringOfEmptySequenceWithExplicitContext_2871975.

/**
 * @see http://sourceforge.net/support/tracker.php?aid=2871975
 */
@Ignore
@Test
public void stringOfEmptySequenceWithExplicitContext_2871975() throws XMLDBException {
    // OK
    String query = "empty( ()/string() )";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(1, result.getSize());
    assertEquals(query, "true", result.getResource(0).getContent().toString());
    // NOK
    query = "empty( ()/string(.) )";
    service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    result = service.query(query);
    assertEquals(1, result.getSize());
    assertEquals(query, "true", result.getResource(0).getContent().toString());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 58 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService 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)

Example 59 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class XQueryTest method doubleDocNode_2078755.

@Test
public void doubleDocNode_2078755() throws XMLDBException {
    Collection testCollection = getTestCollection();
    Resource doc = testCollection.createResource(MODULE8_NAME, "BinaryResource");
    doc.setContent(module8);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    service.setProperty(OutputKeys.INDENT, "no");
    String query = "import module namespace dr = \"double-root2\" at \"" + URI + "/test/" + MODULE8_NAME + "\";\n" + "let $doc1 := dr:documentIn() \n" + "let $count1 := count($doc1/element()) \n" + "let $doc2 := dr:documentIn() \n" + "let $count2 := count($doc2/element()) \n" + "return ($count1, $count2) \n";
    ResourceSet result = service.query(query);
    assertEquals(2, result.getSize());
    assertEquals("1", result.getResource(0).getContent().toString());
    assertEquals("1", 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 60 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class XQueryTest method messageDigester.

@Test
public void messageDigester() throws XMLDBException {
    String query = "let $value:=\"ABCDEF\"\n" + "let $alg:=\"MD5\"\n" + "return\n" + "(util:hash($value, $alg), util:hash($value, $alg, xs:boolean('true')))";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(2, result.getSize());
    assertEquals(query, "8827a41122a5028b9808c7bf84b9fcf6", result.getResource(0).getContent().toString());
    assertEquals(query, "iCekESKlAouYCMe/hLn89g==", result.getResource(1).getContent().toString());
    query = "let $value:=\"ABCDEF\"\n" + "let $alg:=\"SHA-1\"\n" + "return\n" + "(util:hash($value, $alg), util:hash($value, $alg, xs:boolean('true')))";
    service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    result = service.query(query);
    assertEquals(2, result.getSize());
    assertEquals(query, "970093678b182127f60bb51b8af2c94d539eca3a", result.getResource(0).getContent().toString());
    assertEquals(query, "lwCTZ4sYISf2C7UbivLJTVOeyjo=", result.getResource(1).getContent().toString());
    query = "let $value:=\"ABCDEF\"\n" + "let $alg:=\"SHA-256\"\n" + "return\n" + "(util:hash($value, $alg), util:hash($value, $alg, xs:boolean('true')))";
    service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    result = service.query(query);
    assertEquals(2, result.getSize());
    assertEquals(query, "e9c0f8b575cbfcb42ab3b78ecc87efa3b011d9a5d10b09fa4e96f240bf6a82f5", result.getResource(0).getContent().toString());
    assertEquals(query, "6cD4tXXL/LQqs7eOzIfvo7AR2aXRCwn6TpbyQL9qgvU=", result.getResource(1).getContent().toString());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Aggregations

XPathQueryService (org.xmldb.api.modules.XPathQueryService)148 ResourceSet (org.xmldb.api.base.ResourceSet)123 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)92 XMLResource (org.xmldb.api.modules.XMLResource)60 Collection (org.xmldb.api.base.Collection)31 Test (org.junit.Test)22 Resource (org.xmldb.api.base.Resource)18 XMLDBException (org.xmldb.api.base.XMLDBException)14 EXistResource (org.exist.xmldb.EXistResource)10 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)9 XUpdateQueryService (org.xmldb.api.modules.XUpdateQueryService)8 IndexQueryService (org.exist.xmldb.IndexQueryService)6 IOException (java.io.IOException)3 ResourceIterator (org.xmldb.api.base.ResourceIterator)3 BinaryResource (org.xmldb.api.modules.BinaryResource)3 MalformedURLException (java.net.MalformedURLException)2 Path (java.nio.file.Path)2 Node (org.w3c.dom.Node)2 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1