use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class XQueryTest method typedVariables.
@Test
public void typedVariables() throws XMLDBException {
ResourceSet result;
String query;
boolean exceptionThrown;
@SuppressWarnings("unused") String message;
XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
query = "let $v as element()* := ( <assign/> , <assign/> )\n" + "let $w := <r>{ $v }</r>\n" + "let $x as element()* := $w/assign\n" + "return $x";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "assign", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
query = "let $v as node()* := ()\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "let $v as item()* := ()\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "let $v as empty-sequence() := ()\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "let $v as item() := ()\n" + "return $v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue("XQuery: " + query, exceptionThrown);
query = "let $v as item()* := ( <a/> , 1 )\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
assertEquals("XQuery: " + query, "1", ((XMLResource) result.getResource(1)).getContent());
query = "let $v as node()* := ( <a/> , 1 )\n" + "return $v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "let $v as item()* := ( <a/> , 1 )\n" + "let $w as element()* := $v\n" + "return $w";
try {
exceptionThrown = false;
result = service.query(query);
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "declare variable $v as element()* {( <assign/> , <assign/> ) };\n" + "declare variable $w { <r>{ $v }</r> };\n" + "declare variable $x as element()* { $w/assign };\n" + "$x";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "assign", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
query = "declare variable $v as node()* { () };\n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "declare variable $v as item()* { () };\n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "declare variable $v as empty-sequence() { () };\n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "declare variable $v as item() { () };\n" + "$v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue("XQuery: " + query, exceptionThrown);
query = "declare variable $v as item()* { ( <a/> , 1 ) }; \n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
assertEquals("XQuery: " + query, "1", ((XMLResource) result.getResource(1)).getContent());
query = "declare variable $v as node()* { ( <a/> , 1 ) };\n" + "$v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "declare variable $v as item()* { ( <a/> , 1 ) };\n" + "declare variable $w as element()* { $v };\n" + "$w";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "let $v as document-node() := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "') \n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 1, result.getSize());
// TODO : no way to test the node type ?
// assertEquals( "XQuery: " + query, Node.DOCUMENT_NODE, ((XMLResource)result.getResource(0)));
assertEquals("XQuery: " + query, "test", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
}
use of org.xmldb.api.base.XMLDBException 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());
}
use of org.xmldb.api.base.XMLDBException 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);
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class XQueryTest method globalVars.
@Test
public void globalVars() throws XMLDBException {
Collection testCollection = getTestCollection();
Resource doc = testCollection.createResource(MODULE5_NAME, "BinaryResource");
doc.setContent(module5);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
doc = testCollection.createResource(MODULE6_NAME, "BinaryResource");
doc.setContent(module6);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
XQueryService service = (XQueryService) testCollection.getService("XPathQueryService", "1.0");
String query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE5_NAME + "\";\n" + "$foo:bar";
ResourceSet result = service.query(query);
assertEquals(result.getSize(), 1);
assertEquals(result.getResource(0).getContent(), "bar");
query = "xquery version \"1.0\";\n" + "declare variable $local:a := 'abc';" + "$local:a";
result = service.query(query);
assertEquals(result.getSize(), 1);
assertEquals(result.getResource(0).getContent(), "abc");
boolean gotException = false;
try {
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE6_NAME + "\";\n" + "$foo:bar";
result = service.query(query);
} catch (XMLDBException e) {
assertTrue("Test should generate err:XQST0049, got: " + e.getMessage(), e.getMessage().indexOf("err:XQST0049") > -1);
gotException = true;
}
assertTrue("Duplicate global variable should generate error", gotException);
gotException = false;
try {
query = "xquery version \"1.0\";\n" + "declare variable $local:a := 'abc';" + "declare variable $local:a := 'abc';" + "$local:a";
result = service.query(query);
} catch (XMLDBException e) {
assertTrue("Test should generate err:XQST0049, got: " + e.getMessage(), e.getMessage().indexOf("err:XQST0049") > -1);
gotException = true;
}
assertTrue("Duplicate global variable should generate error", gotException);
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class XQueryTest method serialization.
@Test
public void serialization() throws XMLDBException {
@SuppressWarnings("unused") ResourceSet result;
String query;
@SuppressWarnings("unused") boolean exceptionThrown;
String message;
XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
query = "let $a := <test><foo name='bar'/><foo name='bar'/></test>" + "return <attribute>{$a/foo/@name}</attribute>";
try {
message = "";
result = service.query(query);
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("XQDY0025") > -1);
query = "let $a := <foo name='bar'/> return $a/@name";
try {
message = "";
result = service.query(query);
} catch (XMLDBException e) {
message = e.getMessage();
}
// TODO : how toserialize this resultand get the error ? -pb
// assertTrue(message.indexOf("XQDY0025") > -1);
}
Aggregations