datamanager.cpp
This file contains some examples that demonstrate how the XmlDataManager can be used to load files, create collection, etc.
#include <iostream>
#include <sstream>
#include <zorba/zorba.h>
#include <zorba/store_manager.h>
#include <zorba/zorba_exception.h>
#include <zorba/iterator.h>
#include <zorba/singleton_item_sequence.h>
#include <zorba/serializer.h>
using namespace zorba;
bool
datamanager_example_1(Zorba* aZorba, XmlDataManager* aDataManager)
{
try {
std::stringstream lInStream;
lInStream
<< "<books>" << std::endl
<< " <book>Book 1</book>" << std::endl
<< " <book>Book 2</book>" << std::endl
<< "</books>";
Item lDoc = aDataManager->parseXML(lInStream);
DocumentManager* lDocMgr = aDataManager->getDocumentManager();
lDocMgr->put("books.xml", lDoc);
XQuery_t lQuery = aZorba->compileQuery("doc('books.xml')//book");
std::cout << lQuery << std::endl;
lDocMgr->remove("books.xml");
bool lAvailable = lDocMgr->isAvailableDocument("books.xml");
if (lAvailable)
return false;
} catch (ZorbaException& e) {
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
datamanager_example_2(Zorba* aZorba, XmlDataManager* aDataManager)
{
try {
CollectionManager* lColMgr = aDataManager->getCollectionManager();
Item lColName = aZorba->getItemFactory()->createQName(
"http://zorba.io/collections", "mybooks");
lColMgr->createCollection(lColName);
ItemSequence_t lSeq = lColMgr->availableCollections();
Iterator_t lIter = lSeq->getIterator();
lIter->open();
while (lIter->next(lColName)) {
std::cout << "collection " << lColName.getStringValue() << std::endl;
}
lColMgr->deleteCollection(lColName);
bool lAvailable = lColMgr->isAvailableCollection(lColName);
if (lAvailable)
return false;
} catch (ZorbaException& e) {
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
datamanager_example_3(Zorba* aZorba, XmlDataManager* aDataManager)
{
try {
CollectionManager* lColMgr = aDataManager->getCollectionManager();
Item lColName = aZorba->getItemFactory()->createQName(
"http://zorba.io/collections", "mybooks");
lColMgr->createCollection(lColName);
std::stringstream lInStream;
lInStream
<< "<books>" << std::endl
<< " <book>Book 1</book>" << std::endl
<< " <book>Book 2</book>" << std::endl
<< "</books>";
Item lDoc = aDataManager->parseXML(lInStream);
Collection_t lColl = lColMgr->getCollection(lColName);
if (!lColl) {
return false;
}
std::vector<Annotation_t> lAnnotations;
lColl->getAnnotations(lAnnotations);
size_t num_annotations = 0;
for (std::vector<Annotation_t>::const_iterator lIter = lAnnotations.begin();
lIter != lAnnotations.end(); ++lIter)
{
std::cout << "Annotation QName "
<< (*lIter)->getQName().getStringValue() << std::endl;
++num_annotations;
}
if (num_annotations != 3)
{
return false;
}
lColl->insertNodesLast(new SingletonItemSequence(lDoc));
ItemSequence_t lContents = lColl->contents();
Iterator_t lIter = lContents->getIterator();
lIter->open();
while (lIter->next(lDoc)) {
std::cout << "node found at position "
<< lColl->indexOf(lDoc) << std::endl;
}
lColl->deleteNodeLast();
lIter->close();
lIter->open();
while (lIter->next(lDoc)) {
return false;
}
lColMgr->deleteCollection(lColName);
} catch (ZorbaException& e) {
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
datamanager_example_4(Zorba* aZorba, XmlDataManager* aDataManager)
{
try {
CollectionManager* lColMgr = aDataManager->getW3CCollectionManager();
Item lColName = aZorba->getItemFactory()->createString(
"http://www.blub.com/");
lColMgr->createCollection(lColName);
ItemSequence_t lSeq = lColMgr->availableCollections();
Iterator_t lIter = lSeq->getIterator();
lIter->open();
while (lIter->next(lColName)) {
std::cout << "collection " << lColName.getStringValue() << std::endl;
}
lColMgr->deleteCollection(lColName);
bool lAvailable = lColMgr->isAvailableCollection(lColName);
if (lAvailable)
return false;
} catch (ZorbaException& e) {
std::cerr << e << std::endl;
return false;
}
return true;
}
bool
datamanager_example_5(Zorba* aZorba, XmlDataManager* aDataManager)
{
try {
std::stringstream lStream;
lStream
<< "<book><title>XQuery From The Experts</title></book>"
<< "<book><title>XQuery Kick Start</title></book>"
<< "<book><title>Querying XML</title></book>";
XmlDataManager::ParseOptions lOptions;
lOptions.setExternalEntityProcessing(true);
ItemSequence_t lSeq = aDataManager->parseXML(lStream, lOptions);
Iterator_t lIter = lSeq->getIterator();
lIter->open();
Item lItem;
while (lIter->next(lItem)) {
std::cout << "element " << lItem.getStringValue() << std::endl;
}
} catch (ZorbaException& e) {
std::cerr << e << std::endl;
return false;
}
return true;
}
int
datamanager(int argc, char* argv[])
{
void* lStore = zorba::StoreManager::getStore();
Zorba* lZorba = Zorba::getInstance(lStore);
XmlDataManager_t lDataManager = lZorba->getXmlDataManager();
bool res = false;
std::cout << "executing example 1" << std::endl;
res = datamanager_example_1(lZorba, lDataManager);
if (!res) return 1;
std::cout << std::endl;
std::cout << "executing example 2" << std::endl;
res = datamanager_example_2(lZorba, lDataManager);
if (!res) return 2;
std::cout << std::endl;
std::cout << "executing example 3" << std::endl;
res = datamanager_example_3(lZorba, lDataManager);
if (!res) return 3;
std::cout << std::endl;
std::cout << "executing example 4" << std::endl;
res = datamanager_example_4(lZorba, lDataManager);
if (!res) return 4;
std::cout << std::endl;
std::cout << "executing example 5" << std::endl;
res = datamanager_example_5(lZorba, lDataManager);
if (!res) return 5;
std::cout << std::endl;
lZorba->shutdown();
zorba::StoreManager::shutdownStore(lStore);
return 0;
}