This file demonstrates how the ItemFactory can be used to create new Items and bind the Items to external variables in the dynamic context of a query.
#include <iostream>
#include <fstream>
#include <sstream>
using namespace zorba;
bool
context_example_1(
Zorba* aZorba)
{
try {
std::cout << lQuery << std::endl;
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
context_example_2(
Zorba* aZorba)
{
try {
std::cout << lQuery << std::endl;
std::cerr << e << std::endl;
return true;
}
return false;
}
bool
context_example_3(
Zorba* aZorba)
{
try {
std::cout << lQuery << std::endl;
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
context_example_4(
Zorba* aZorba)
{
std::ostringstream outStream1;
std::ostringstream outStream2;
std::auto_ptr<std::istream> lDocStream1(
new std::stringstream("<books><book>Book 1</book><book>Book 2</book></books>"));
std::auto_ptr<std::istream> lDocStream2(
new std::stringstream("<books><book>Book 1.1</book><book>Book 2.2</book></books>"));
try
{
outStream1 << lQuery << std::endl;
std::cout << outStream1.str() << std::endl;
outStream2 << lQuery << std::endl;
std::cout << outStream2.str() << std::endl;
#ifndef ZORBA_NO_ICU
if (outStream2.str() != "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\nBook 1.1\n")
#else
if (outStream2.str() != "<?xml version=\"1.0\"?>\nBook 1.1\n")
#endif
{
std::cerr << "Test 4 failed with a wrong result : " << std::endl
<< outStream2.str() << std::endl;
return false;
}
}
{
std::cerr << "Test 4 failed with exception : " << e << std::endl;
return false;
}
catch (...)
{
std::cerr << "Test 4 failed with unknown exception" << std::endl;
return false;
}
return true;
}
bool
context_example_5(
Zorba* aZorba)
{
std::auto_ptr<std::istream> lDocStream(
new std::stringstream("<books><book>Book 1</book><book>Book 2</book></books>"));
try {
std::cout << lQuery << std::endl;
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
context_example_6(
Zorba* aZorba)
{
lStaticContext->addCollation("http://www.zorba-xquery.com/collations/PRIMARY/de/DE");
lStaticContext->setBaseURI("http://www.zorba-xquery.com/");
if ( lStaticContext->getBaseURI() != "http://www.zorba-xquery.com/")
return false;
XQuery_t lQuery = aZorba->
compileQuery(
"fn:compare('Strasse', 'Stra??e', 'http://www.zorba-xquery.com/collations/PRIMARY/de/DE')",
lStaticContext);
try {
std::cout << lQuery << std::endl;
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
context_example_7(
Zorba* aZorba)
{
try {
lStaticContext->addCollation("http://www.zorba-xquery.com/zorba/collations/PRIMARY");
XQuery_t lQuery = aZorba->
compileQuery(
"fn:compare('Strasse', 'Stra??e', 'http://www.zorba-xquery.com/zorba/collations/PRIMARY')",
lStaticContext);
std::cout << lQuery << std::endl;
std::cerr << e << std::endl;
return true;
}
return false;
}
bool
context_example_8(
Zorba* aZorba)
{
const StaticContext* lStaticContext1 = lQuery1->getStaticContext();
return false;
const StaticContext* lStaticContext2 = lQuery2->getStaticContext();
}
bool
context_example_9(
Zorba* aZorba)
{
try {
std::cout << lQuery << std::endl;
}
{
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
context_example_10(
Zorba* aZorba)
{
try
{
std::vector<Function_t> lFunctions;
lContext->findFunctions(lQName, lFunctions);
std::cout << "Number of Functions " << lFunctions.size() << std::endl;
for (std::vector<Function_t>::const_iterator lIter = lFunctions.begin();
lIter != lFunctions.end(); ++lIter)
{
std::cout << "Function Name " << (*lIter)->getQName().getStringValue() << std::endl;
std::cout << " is updating " << (*lIter)->isUpdating() << std::endl;
std::cout << " arity " << (*lIter)->getArity() << std::endl;
lContext->disableFunction(*lIter);
}
try
{
}
{
std::cerr << se << std::endl;
return true;
}
}
{
std::cerr << e << std::endl;
return false;
}
return false;
}
static void
releaseStream(std::istream* aStream)
{
delete aStream;
}
{
public:
virtual ~PrologModuleURLResolver() {}
{
aUrl == "http://www.zorba-xquery.com/mymodule")
{
std::auto_ptr<std::stringstream> lQuery(new std::stringstream());
(*lQuery)
<< "module namespace mymodule = 'http://www.zorba-xquery.com/mymodule';" << std::endl
<< "import module namespace dml = 'http://www.zorba-xquery.com/modules/store/static/collections/dml';" << std::endl
<< "declare namespace an = 'http://www.zorba-xquery.com/annotations';" << std::endl
<< "declare variable $mymodule:var := 'myvar';" << std::endl
<< "declare collection mymodule:collection;" << std::endl
<< "declare %an:automatic %an:value-equality index mymodule:index" << std::endl
<< " on nodes dml:collection(xs:QName('mymodule:collection'))" << std::endl
<< " by ./foo as xs:string;" << std::endl;
}
else {
return NULL;
}
}
};
bool
context_example_11(
Zorba* aZorba)
{
PrologModuleURLResolver lResolver;
lContext->registerURLResolver(&lResolver);
try {
std::stringstream lProlog;
lProlog << "import module namespace mymodule = 'http://www.zorba-xquery.com/mymodule';" << std::endl
<< "import module namespace ddl = 'http://www.zorba-xquery.com/modules/store/static/collections/ddl';"
<< std::endl
<< "declare function local:collections() { " << std::endl
<< " ddl:declared-collections()" << std::endl
<< "};" << std::endl;
lContext->loadProlog(lProlog.str(), hints);
Zorba_SerializerOptions lSerOptions;
std::stringstream lResult;
lQuery->execute(lResult, &lSerOptions);
std::cout << "result " << lResult.str() << std::endl;
if (lResult.str().compare("myvar mymodule:collection") == 0) {
return true;
} else {
std::cerr << "result doesn't match expected result (myvar mymodule:collection)"
<< std::endl;
return false;
}
std::cerr << e << std::endl;
return false;
}
return false;
}
bool
context_example_12(
Zorba* aZorba)
{
std::ostringstream lTraceStream;
lContext->declareOption(lTraceQName, "enable");
lContext->setTraceStream(lTraceStream);
try {
std::cout << aQuery << std::endl;
std::string lTraceString = lTraceStream.str();
std::cout << lTraceString << std::endl;
if (lTraceString.compare("foo [1]: 1\n"
"foo [2]: 2\n"
"foo [3]: 3\n") != 0) {
return false;
}
std::cout << lTraceString << std::endl;
return true;
std::cerr << e << std::endl;
return false;
}
return false;
}
struct callback_data
{
bool b;
};
int
context(int argc, char* argv[])
{
bool res = false;
std::cout << "executing example 1" << std::endl;
res = context_example_1(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 2" << std::endl;
res = context_example_2(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 3" << std::endl;
res = context_example_3(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 4" << std::endl;
res = context_example_4(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 5" << std::endl;
res = context_example_5(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 6" << std::endl;
res = context_example_6(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 7" << std::endl;
res = context_example_7(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example_8" << std::endl;
res = context_example_8(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example_9" << std::endl;
res = context_example_9(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example_10" << std::endl;
res = context_example_10(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example_11" << std::endl;
res = context_example_11(lZorba);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example_12" << std::endl;
res = context_example_12(lZorba);
if (!res) return 1;
std::cout << std::endl;
return 0;
}