Search in sources :

Example 91 with XMLResource

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

the class XQueryTest method largeAttributeSimple.

@Test
public void largeAttributeSimple() throws XMLDBException {
    ResourceSet result;
    String query;
    @SuppressWarnings("unused") XMLResource resu;
    String large = createXMLContentWithLargeString();
    XPathQueryService service = storeXMLStringAndGetQueryService(file_name, xml);
    query = "doc('" + file_name + "') / details/metadata[@docid= '" + large + "' ]";
    result = service.queryResource(file_name, query);
    assertEquals("XQuery: " + query, nbElem, result.getSize());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 92 with XMLResource

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

the class XQueryTest method cdataSerialization.

@Test
public void cdataSerialization() throws XMLDBException {
    ResourceSet result;
    String query;
    XMLResource resu;
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    query = "let $doc := document{ <root><![CDATA[gaga]]></root> } " + "return $doc/root/string()";
    result = service.query(query);
    resu = (XMLResource) result.getResource(0);
    assertEquals("XQuery: " + query, "gaga", resu.getContent().toString());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 93 with XMLResource

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

the class XQueryTest method storeXMLStringAndGetQueryService.

// ======================================
/**
 * @return
 * @throws XMLDBException
 */
private XPathQueryService storeXMLStringAndGetQueryService(String documentName, String content) throws XMLDBException {
    Collection testCollection = getTestCollection();
    XMLResource doc = (XMLResource) testCollection.createResource(documentName, "XMLResource");
    doc.setContent(content);
    testCollection.storeResource(doc);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    return service;
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) Collection(org.xmldb.api.base.Collection) XMLResource(org.xmldb.api.modules.XMLResource)

Example 94 with XMLResource

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

the class XQueryTest method variable.

@Test
public void variable() throws XMLDBException {
    ResourceSet result;
    String query;
    XMLResource resu;
    @SuppressWarnings("unused") boolean exceptionThrown;
    String message;
    XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
    query = "xquery version \"1.0\";\n" + "declare namespace param=\"param\";\n" + "declare variable $param:a {\"a\"};\n" + "declare function param:a() {$param:a};\n" + "let $param:a := \"b\" \n" + "return ($param:a, $param:a)";
    result = service.query(query);
    assertEquals("XQuery: " + query, 2, result.getSize());
    assertEquals("XQuery: " + query, "b", ((XMLResource) result.getResource(0)).getContent());
    assertEquals("XQuery: " + query, "b", ((XMLResource) result.getResource(1)).getContent());
    query = "xquery version \"1.0\";\n" + "declare namespace param=\"param\";\n" + "declare variable $param:a {\"a\"};\n" + "declare function param:a() {$param:a};\n" + "let $param:a := \"b\" \n" + "return param:a(), param:a()";
    result = service.query(query);
    assertEquals("XQuery: " + query, 2, result.getSize());
    assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContent());
    assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(1)).getContent());
    query = "declare variable $foo {\"foo1\"};\n" + "let $foo := \"foo2\" \n" + "for $bar in (1 to 1) \n" + "  let $foo := \"foo3\" \n" + "  return $foo";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "foo3", ((XMLResource) result.getResource(0)).getContent());
    try {
        message = "";
        query = "xquery version \"1.0\";\n" + "declare variable $a {\"1st instance\"};\n" + "declare variable $a {\"2nd instance\"};\n" + "$a";
        result = service.query(query);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XQST0049") > -1);
    query = "xquery version \"1.0\";\n" + "declare namespace param=\"param\";\n" + "declare function param:f() { $param:a };\n" + "declare variable $param:a {\"a\"};\n" + "param:f()";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContent());
    query = "let $a := <root> " + "<b name='1'>" + "  <c name='x'> " + "    <bar name='2'/> " + "    <bar name='3'> " + "      <bar name='4'/> " + "    </bar> " + "  </c> " + "</b> " + "</root> " + "let $b := for $bar in $a/b/c/bar " + "where ($bar/../@name = 'x') " + "return $bar " + "return $b";
    result = service.queryResource(NUMBERS_XML, query);
    assertEquals("XQuery: " + query, 2, result.getSize());
    resu = (XMLResource) result.getResource(0);
    assertEquals("XQuery: " + query, "2", ((Element) resu.getContentAsDOM()).getAttribute("name"));
    resu = (XMLResource) result.getResource(1);
    assertEquals("XQuery: " + query, "3", ((Element) resu.getContentAsDOM()).getAttribute("name"));
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 95 with XMLResource

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

the class XQueryTest method retrieveLargeAttribute.

@Test
public void retrieveLargeAttribute() throws XMLDBException {
    createXMLContentWithLargeString();
    storeXMLStringAndGetQueryService(file_name, xml);
    final XMLResource res = (XMLResource) getTestCollection().getResource(file_name);
    assertTrue(res != null);
}
Also used : XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

XMLResource (org.xmldb.api.modules.XMLResource)142 Collection (org.xmldb.api.base.Collection)56 ResourceSet (org.xmldb.api.base.ResourceSet)56 XPathQueryService (org.xmldb.api.modules.XPathQueryService)49 Test (org.junit.Test)33 Resource (org.xmldb.api.base.Resource)24 XQueryService (org.xmldb.api.modules.XQueryService)23 Node (org.w3c.dom.Node)22 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)20 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)18 Document (org.w3c.dom.Document)16 XMLDBException (org.xmldb.api.base.XMLDBException)15 EXistResource (org.exist.xmldb.EXistResource)9 EXistXQueryService (org.exist.xmldb.EXistXQueryService)9 BinaryResource (org.xmldb.api.modules.BinaryResource)9 Path (java.nio.file.Path)8 Before (org.junit.Before)8 Element (org.w3c.dom.Element)8 Source (javax.xml.transform.Source)7 SAXSerializer (org.exist.util.serializer.SAXSerializer)7