ItemSequence_t evaluate(const ExternalFunction::Arguments_t& args) const { Item item; int64_t sum1 = 0; int64_t sum2 = 0; Iterator_t iter = args[0]->getIterator(); iter->open(); while (iter->next(item)) sum1 += item.getLongValue(); iter->close(); //iter = args[0]->getIterator(); iter->open(); while (iter->next(item)) sum2 += item.getLongValue(); iter->close(); if (sum1 != sum2) { std::cerr << "sum1 = " << sum1 << " sum2 = " << sum2 << std::endl; throw std::exception(); } return ItemSequence_t(new EmptySequence()); }
void GraphvizFunction::printTypeAndAttr( ItemFactory* aFactory, const Item& in, std::fstream& os) { Item lItem; Item lTypeQName = aFactory->createQName("", "", "type"); Item lAttrQName = aFactory->createQName("", "", "attr"); Iterator_t lChildren = in.getChildren(); lChildren->open(); while (lChildren->next(lItem)) { // needed? if (!lItem.isNode()) continue; Item lNodeName; lItem.getNodeName(lNodeName); if (lNodeName.getLocalName() != lAttrQName.getLocalName()) { Item lNameAttr; if (!getAttribute(aFactory, "name", lItem, lNameAttr)) { GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: attr node does not have a name attribute"); } os << " \"" << lNameAttr.getStringValue() << "\"=\""; // get the values of all bool, string, float, and int elements Iterator_t lAttrChildren = lItem.getChildren(); Item lChild; lAttrChildren->open(); while (lAttrChildren->next(lChild)) { if (!lChild.isNode()) continue; Item lNodeName; lChild.getNodeName(lNodeName); String lChildName = lNodeName.getLocalName(); if ( (lChildName == "bool") || (lChildName == "string") || (lChildName == "float") || (lChildName == "int")) { os << lChild.getStringValue(); } } os << "\"" << std::endl; } else if (lNodeName.getStringValue() == lTypeQName.getStringValue()) { Item lHRefAttr; if (!getAttribute(aFactory, "href", lItem, lHRefAttr)) { GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: type node does not have a href attribute"); } os << " _gxl_type=\"" << lHRefAttr.getStringValue() << "\"" << std::endl; } } }
void CClientMimeHandler::body() { theBody->type = TYPEOTHER; Iterator_t lChildIter; Item lChild; String lNodeName; lChildIter = theBodyItem.getChildren(); lChildIter->open(); while (lChildIter->next(lChild)) { if (lChild.getNodeKind() != store::StoreConsts::elementNode) { continue; } getNodeName(lChild, lNodeName); if (lNodeName == "content") { parse_content(theBody, lChild); } else if (lNodeName == "multipart") { parse_multipart(theBody, lChild); } } lChildIter->close(); }
// test for bug #3085093 "ZorbaError not caught in SerializerImpl::serialize" bool test_serialization_error(Zorba* aZorba) { XQuery_t lQuery = aZorba->compileQuery("<a a='1'/>/@a"); Zorba_SerializerOptions_t lOptions; Serializer_t lSerializer = Serializer::createSerializer(lOptions); Iterator_t lIterator = lQuery->iterator(); try { lIterator->open(); Item lItem; while ( lIterator->next(lItem) ) { // we have to wrap the item in a Serializable object SingletonItemSequence lSequence(lItem); lSerializer->serialize(&lSequence, std::cout); } lIterator->close(); } catch (ZorbaException& e) { std::cerr << e << std::endl; lIterator->close(); return true; } return false; }
void CClientMimeHandler::begin(const Item& aMimeItem) { Iterator_t lChildIter; //initialize ENVELOPE theEnvelope = mail_newenvelope (); //initialize BODY theBody = mail_newbody (); mail_initbody(theBody); //set theMessageItem lChildIter = aMimeItem.getChildren(); lChildIter->open(); // read envelope and body elements but skip non-element nodes while (lChildIter->next(theEnvelopeItem)) { if (theEnvelopeItem.getNodeKind() == store::StoreConsts::elementNode) { break; } } while (lChildIter->next(theBodyItem)) { if (theBodyItem.getNodeKind() == store::StoreConsts::elementNode) { break; } } lChildIter->close(); }
// helper function for retrieving the name and email address from an item static void getNameAndEmailAddress( Item& aEmailItem, String& aName, String& aMailbox, String& aHost) { Iterator_t lChildren = aEmailItem.getChildren(); lChildren->open(); Item lChild; // name might not exist -> empty string by default aName = ""; while (lChildren->next(lChild)) { if (lChild.getNodeKind() != store::StoreConsts::elementNode) { continue; } String lNodeName; getNodeName(lChild, lNodeName); if (lNodeName == "name") { aName = lChild.getStringValue(); } else { String lEmail = lChild.getStringValue(); int lIndexOfAt = lEmail.find('@'); aMailbox = lEmail.substr(0, lIndexOfAt).c_str(); aHost = lEmail.substr(lIndexOfAt + 1, lEmail.length() - lIndexOfAt - 1).c_str(); } } }
/** * Test accessing a JSONArray's members */ bool example_1(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("[ 1, 2, 3 ]"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); // Ensure we got a JSON array if (!lItem.isJSONItem() || lItem.getJSONItemKind() != store::StoreConsts::jsonArray) { std::cerr << "Item is not JSON Array!" << std::endl; return false; } // Ensure array has 3 integer members uint64_t lSize = lItem.getArraySize(); if (lSize != 3) { std::cerr << lSize << " array members returned, expecting 3" << std::endl; return false; } for(uint64_t i = 1; i <= lSize; ++i) { Item lMember = lItem.getArrayValue(i); // This will throw an exception if the item isn't an integer std::cout << lMember.getLongValue() << std::endl; } lIterator->close(); return true; }
bool xmldatamanager_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; }
void GraphvizFunction::printGraph( ItemFactory* aFactory, const Item& in, std::fstream& os) { // create helper qnames for comparison Item lNodeQName = aFactory->createQName("", "", "node"); Item lEdgeQName = aFactory->createQName("", "", "edge"); Item lRelQName = aFactory->createQName("", "", "rel"); // print the graph with all its children Item lGraphId; if (!getAttribute(aFactory, "id", in, lGraphId)) { GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: edge does not have an 'id' attribute"); } os << "digraph \"" << lGraphId.getStringValue() << "\" {" << std::endl; // visit nodes and edges (TODO add rel elements) Iterator_t lChildren = in.getChildren(); lChildren->open(); Item item; while (lChildren->next(item)) { Item lNodeName; item.getNodeName(lNodeName); if (lNodeName.getLocalName() == lNodeQName.getLocalName()) { visitNode(aFactory, item, os); } else if (lNodeName.getLocalName() == lEdgeQName.getLocalName()) { visitEdge(aFactory, item, os); } } os << "}" << std::endl; } /* GraphvizFunction::printGraph */
bool cxx_api_changes_test4(Zorba* aZorba) { try { std::ifstream lIn("cxx_api_ch4.xq"); assert(lIn.good()); StaticContext_t lStaticContext = aZorba->createStaticContext(); std::vector<String> lModulePaths; lModulePaths.push_back(zorba::CMAKE_BINARY_DIR+"/TEST_URI_PATH/"); lStaticContext->setModulePaths(lModulePaths); XQuery_t lQuery = aZorba->compileQuery(lIn, lStaticContext); std::vector<Item> lVars; Iterator_t varsIte; lQuery->getExternalVariables(varsIte); varsIte->open(); Item temp; while(varsIte->next(temp)) lVars.push_back(temp); varsIte->close(); if (lVars.size() != 3) return false; std::ostringstream lOut; std::vector<Item>::const_iterator lIte = lVars.begin(); std::vector<Item>::const_iterator lEnd = lVars.end(); for (; lIte != lEnd; ++lIte) { lOut << lIte->getStringValue() << " "; } std::string out = lOut.str(); if (out != "testGetExtVarA:ext a testGetExtVarB:ext " && out != "testGetExtVarB:ext a testGetExtVarA:ext " && out != "a testGetExtVarA:ext testGetExtVarB:ext " && out != "a testGetExtVarB:ext testGetExtVarA:ext " && out != "testGetExtVarA:ext testGetExtVarB:ext a " && out != "testGetExtVarB:ext testGetExtVarA:ext a ") return false; } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
/** * test that declaredIndexes doesn't return temporary indexes and crashes * if one tries to create one */ bool staticcollectionmanager6(zorba::Zorba* z) { try { std::ifstream lIn("module1.xq"); zorba::XQuery_t lQuery = z->createQuery(); Zorba_CompilerHints lHints; lQuery->compile(lIn, lHints); StaticCollectionManager* lColMgr = lQuery->getStaticCollectionManager(); ItemFactory* lFac = z->getItemFactory(); Item lCollName2 = lFac->createQName("http://www.mod2.com/", "coll"); Item lIdxName = lFac->createQName("http://www.mod2.com/", "index"); Item lCollName3 = lFac->createQName("http://www.mod3.com/", "coll"); ItemSequence_t lSeq = lColMgr->declaredCollections(); Iterator_t lIter = lSeq->getIterator(); lIter->open(); Item lTmp; while (lIter->next(lTmp)) { std::cout << "name " << lTmp.getStringValue() << std::endl; lColMgr->createCollection(lTmp); } lSeq = lColMgr->declaredIndexes(); lIter = lSeq->getIterator(); lIter->open(); while (lIter->next(lTmp)) { std::cout << "name " << lTmp.getStringValue() << std::endl; lColMgr->createIndex(lTmp); } return true; } catch ( std::exception const &e ) { std::cerr << e.what() << std::endl; } catch ( ... ) { std::cerr << "caught unexpected exception" << std::endl; } return false; }
bool cxx_api_changes_test5(Zorba* aZorba) { try { std::string lIn = "declare variable $a external; 1+1"; XQuery_t lQuery = aZorba->compileQuery(lIn); std::vector<Item> lVars; Iterator_t varsIte; lQuery->getExternalVariables(varsIte); varsIte->open(); Item temp; while(varsIte->next(temp)) lVars.push_back(temp); varsIte->close(); std::vector<Item>::const_iterator lIte = lVars.begin(); std::vector<Item>::const_iterator lEnd = lVars.end(); Item item = aZorba->getItemFactory()->createInt(4); bool isBound1; bool isBound2; for(; lIte != lEnd; ++lIte) { Item qname = *lIte; isBound1 = lQuery->getDynamicContext()->isBoundExternalVariable(qname.getNamespace(), qname.getLocalName()); Item value = aZorba->getItemFactory()->createString("foo"); lQuery->getDynamicContext()->setVariable(qname.getStringValue(), value); isBound2 = lQuery->getDynamicContext()->isBoundExternalVariable(qname.getNamespace(), qname.getLocalName()); } if (!isBound1 && isBound2) return true; } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
Item JdbcModule::getItemArg(const ExternalFunction::Arguments_t& args, int index) { Item item; if (index < (int)args.size()) { Iterator_t lIter = args[index]->getIterator(); lIter->open(); lIter->next(item); lIter->close(); } return item; }
void SerializerImpl::serialize(Iterator_t aIterator, std::ostream& aOs) const { try { aIterator->open(); theInternalSerializer.serialize(Unmarshaller::getInternalIterator(aIterator), aOs); aIterator->close(); } catch (ZorbaException const &e) { ZorbaImpl::notifyError(theDiagnosticHandler, e); } }
bool cxx_api_changes_test3(Zorba* aZorba) { try { //tests the use of getExternalVariables std::ifstream lIn("cxx_api_ch3.xq"); assert(lIn.good()); XQuery_t lQuery = aZorba->compileQuery(lIn); std::vector<Item> lVars; Iterator_t varsIte; lQuery->getExternalVariables(varsIte); varsIte->open(); Item temp; while(varsIte->next(temp)) lVars.push_back(temp); varsIte->close(); if (lVars.size() != 2) { std::cout << "Expected 2 variables but got " << lVars.size() << std::endl; return false; } std::vector<Item>::iterator lIte = lVars.begin(); std::vector<Item>::iterator lEnd = lVars.end(); for (; lIte != lEnd; ++lIte) { String name = lIte->getStringValue(); if (name != "a" && name != "b") return false; } } catch (XQueryException& qe) { std::cerr << qe << std::endl; return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
String JdbcModule::getStringArg(const ExternalFunction::Arguments_t& args, int index) { String result; Iterator_t lIter = args[index]->getIterator(); lIter->open(); Item item; if( lIter->next(item) ) { result = item.getStringValue(); } lIter->close(); return result; }
void GraphvizFunction::gxl2dot( ItemFactory* aFactory, const Item& in, std::fstream& os) { Item lGXLQName = aFactory->createQName("", "", "gxl"); Item lGraphQName = aFactory->createQName("", "", "graph"); if (!in.isNode()) { GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: item is not a node"); } Item lNodeName; in.getNodeName(lNodeName); if (lNodeName.getLocalName() != lGXLQName.getLocalName()) { Item lNodeName; in.getNodeName(lNodeName); std::ostringstream lErrorMsg; lErrorMsg << "GXL parse error: only element with name " << lGXLQName.getStringValue() << " allowed (got " << lNodeName.getStringValue() << ")."; GraphvizFunction::throwErrorWithQName(aFactory, "IM003", lErrorMsg.str()); } Iterator_t lGraphs = in.getChildren(); lGraphs->open(); Item lGraph; while(lGraphs->next(lGraph)) { if (!lGraph.isNode()) { GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: item is not a node"); } lGraph.getNodeName(lNodeName); if (lNodeName.getLocalName() != lGraphQName.getLocalName()) { std::ostringstream lErrorMsg; Item lNodeName; lGraph.getNodeName(lNodeName); lErrorMsg << "GXL parse error: only elements with name " << lGraphQName.getStringValue() << " allowed (got " << lNodeName.getLocalName() << ")."; GraphvizFunction::throwErrorWithQName(aFactory, "IM003", lErrorMsg.str()); } printGraph(aFactory, lGraph, os); } } /* GraphvizFunction::gxl2dot */
bool JdbcModule::getOptionalStringArg(const ExternalFunction::Arguments_t& args, int index, String& aRes) { Iterator_t lIter = args[index]->getIterator(); lIter->open(); Item item; if( lIter->next(item) ) { aRes = item.getStringValue(); lIter->close(); return true; } lIter->close(); return false; }
virtual ItemSequence_t evaluate(const ExternalFunction::Arguments_t& args) const { iv_t vec; for(int i = 0; i < 2; ++i) { Item lItem; Iterator_t iter = args[i]->getIterator(); iter->open(); while(iter->next(lItem)) { vec.push_back(lItem); } iter->close(); } // transfer ownership of the IteratorBackedItemSequence to Zorba (using a unique_ptr) return ItemSequence_t(new IteratorBackedItemSequence(vec)); }
/** * Test accessing a non-existent JSONObject pair value */ bool example_5(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("{ \"one\" : 1, \"two\" : 2 }"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); Item lNonValue = lItem.getObjectValue("three"); lIterator->close(); if (!lNonValue.isNull()) { return false; } return true; }
bool GraphvizFunction::getAttribute(zorba::ItemFactory* aFactory, const char* attrname, const Item& elem, Item& attr) { Item lIdQName = aFactory->createQName("", "", attrname); Iterator_t lAttributes = elem.getAttributes(); lAttributes->open(); while (lAttributes->next(attr)) { Item lNodeName; attr.getNodeName(lNodeName); if (lNodeName.getLocalName() == lIdQName.getLocalName()) { return true; } } attr = NULL; return false; }
/** * Test accessing a JSONObject's values directly by name */ bool example_3(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("{ \"one\" : 1, \"two\" : 2 }"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); Item lValue; lValue = lItem.getObjectValue("one"); std::cout << lValue.getLongValue() << std::endl; lValue = lItem.getObjectValue("two"); std::cout << lValue.getLongValue() << std::endl; lIterator->close(); return true; }
/** * Test accessing a JSONArray's members directly by index */ bool example_2(Zorba* aZorba) { Iterator_t lIterator; XQuery_t lQuery = aZorba->compileQuery("[ 1, 2, 3 ]"); lIterator = lQuery->iterator(); lIterator->open(); Item lItem; lIterator->next(lItem); Item lMember; for (int i = 1; i <= 3; i++) { lMember = lItem.getArrayValue(i); std::cout << lMember.getLongValue() << std::endl; } lIterator->close(); return true; }
ItemSequence_t RegisterFunction::evaluate( const ExternalFunction::Arguments_t &args, const zorba::StaticContext *aStaticContext, const zorba::DynamicContext *aDynamincContext) const { JdbcModule::initGlobals(aStaticContext); Item result; JDBC_MODULE_TRY; jstring driverName(NULL); Item item = JdbcModule::getItemArg(args, 0); bool hasUsername = false; if (item.isJSONItem()) { Iterator_t lKeys = item.getObjectKeys(); lKeys->open(); Item lKey; while (lKeys->next(lKey)) { zorba::String keystring = lKey.getStringValue(); zorba::String value = item.getObjectValue(keystring).getStringValue(); if (keystring == "driver") { driverName = env->NewStringUTF(value.c_str()); CHECK_EXCEPTION; } } lKeys->close(); } jobject oRegister; if (driverName) { oRegister = env->CallStaticObjectMethod( jClass.classID, jClass.forName, driverName); CHECK_EXCEPTION; } JDBC_MODULE_CATCH; return ItemSequence_t(new EmptySequence()); }
/* * Example to show the usage of the collection manager to * (1) create a collection * (2) list available collections, * (3) delete a collection, and * (4) make sure the collection was removed. */ bool xmldatamanager_example_2(Zorba* aZorba, XmlDataManager* aDataManager) { try { CollectionManager* lColMgr = aDataManager->getCollectionManager(); // name of the collection Item lColName = aZorba->getItemFactory()->createQName( "http://zorba.io/collections", "mybooks"); // (1) create the collection lColMgr->createCollection(lColName); // (2) list available collections ItemSequence_t lSeq = lColMgr->availableCollections(); Iterator_t lIter = lSeq->getIterator(); lIter->open(); while (lIter->next(lColName)) { std::cout << "collection " << lColName.getStringValue() << std::endl; } // (3) delete collection lColMgr->deleteCollection(lColName); // (4) make sure the collection was removed bool lAvailable = lColMgr->isAvailableCollection(lColName); if (lAvailable) return false; } catch (ZorbaException& e) { std::cerr << e << std::endl; return false; } return true; }
bool staticcollectionamanger2(zorba::Zorba* z) { try { std::ifstream lIn("module1.xq"); zorba::XQuery_t lQuery = z->createQuery(); Zorba_CompilerHints lHints; lQuery->compile(lIn, lHints); StaticCollectionManager* lColMgr = lQuery->getStaticCollectionManager(); ItemFactory* lFac = z->getItemFactory(); Item lCollName2 = lFac->createQName("http://www.mod2.com/", "coll"); lColMgr->createCollection(lCollName2); Collection_t lCollection = lColMgr->getCollection(lCollName2); std::vector<Annotation_t> lAnnotations; lCollection->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; } if (lCollection->getType().getKind() != SequenceType::NODE_TYPE) { return false; } std::stringstream lInStream; lInStream << "<books>" << std::endl << " <book>Book 1</book>" << std::endl << " <book>Book 2</book>" << std::endl << "</books>"; Item lDoc = z->getXmlDataManager()->parseXML(lInStream); for (size_t i = 0; i < 10; ++i) { lCollection->insertNodesLast(new SingletonItemSequence(lDoc)); } lCollection->deleteNodesLast(2); ItemSequence_t lContents = lCollection->contents(); Iterator_t lIter = lContents->getIterator(); size_t num_nodes = 0; lIter->open(); while (lIter->next(lDoc)) { ++num_nodes; } lIter->close(); if (num_nodes != 8) { return false; } lDoc = NULL; lColMgr->deleteCollection(lCollName2); return true; } catch ( std::exception const &e ) { std::cerr << e.what() << std::endl; } catch ( ... ) { std::cerr << "caught unexpected exception" << std::endl; } return false; }
/* * Example to show the usage of the collection manager to * (1) create a collection * (2) insert a node at the end of the collection, * (3) retrieve the annotations of a collection * (4) iterate over the contents of the collection, * (5) delete the node, and * (6) make sure the node was deleted. */ bool xmldatamanager_example_3(Zorba* aZorba, XmlDataManager* aDataManager) { try { CollectionManager* lColMgr = aDataManager->getCollectionManager(); // name of the collection Item lColName = aZorba->getItemFactory()->createQName( "http://zorba.io/collections", "mybooks"); // (1) create the collection lColMgr->createCollection(lColName); // create a document in a temporary stream 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; } // (2) get the annotations of a collections 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; } // (3) insert a node at the end of the collection lColl->insertNodesLast(new SingletonItemSequence(lDoc)); // (4) iterate over the contents of the collection 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; } // (5) delete the node lColl->deleteNodeLast(); // (6) make sure the node was deleted 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; }
void CClientMimeHandler::envelope() { if (theEnvelopeItem.isNull()) { throw EmailException("PARSE_ERROR", "The message could not be parsed."); } //set the date from the client. //If this is not set it defaults to the date of the SMTP server. char line[MAILTMPLEN]; rfc822_date (line); theEnvelope->date = (unsigned char *) fs_get (1+strlen (line)); strcpy((char *)theEnvelope->date,line); Iterator_t lChildIter; zorba::Item lChild; String lNodeName, lNodeValue; String lName, lMailbox, lHost; lChildIter = theEnvelopeItem.getChildren(); lChildIter->open(); while (lChildIter->next(lChild)) { if (lChild.getNodeKind() != store::StoreConsts::elementNode) { continue; } getNodeName(lChild, lNodeName); getTextValue(lChild, lNodeValue); if (lNodeName == "date") { char lDate[MAILTMPLEN]; parseXmlDateTime(lNodeValue, lDate); theEnvelope->date = (unsigned char *) fs_get (1+strlen (lDate)); strcpy ((char *)theEnvelope->date, lDate); } else if (lNodeName == "from") { getNameAndEmailAddress(lChild, lName, lMailbox, lHost); theEnvelope->from = create_mail_address(lName, lMailbox, lHost); } else if (lNodeName == "sender") { getNameAndEmailAddress(lChild, lName, lMailbox, lHost); theEnvelope->sender = create_mail_address(lName, lMailbox, lHost); } else if (lNodeName == "replyto") { getNameAndEmailAddress(lChild, lName, lMailbox, lHost); theEnvelope->reply_to = create_mail_address(lName, lMailbox, lHost); } else if (lNodeName == "subject") { encodeStringForEMailHeader(lNodeValue, theEnvelope->subject); } else if (lNodeName == "recipient") { Iterator_t lRecipentChildren = lChild.getChildren(); lRecipentChildren->open(); Item lRecipentChild; // read the recipient element but skip comments while (lRecipentChildren->next(lRecipentChild)) { if (lRecipentChild.getNodeKind() == store::StoreConsts::elementNode) { break; } } getNodeName(lRecipentChild, lNodeName); lRecipentChildren->close(); if (lNodeName == "to") { getNameAndEmailAddress(lRecipentChild, lName, lMailbox, lHost); // there can be multiple to nodes, iterate to the next free one! ADDRESS** lNext = &theEnvelope->to; while (*lNext) { lNext = &((*lNext)->next); } *lNext = create_mail_address(lName, lMailbox, lHost); } else if(lNodeName == "cc") { getNameAndEmailAddress(lRecipentChild, lName, lMailbox, lHost); ADDRESS** lNext = &theEnvelope->cc; while (*lNext) { lNext = &((*lNext)->next); } *lNext = create_mail_address(lName, lMailbox, lHost); } else if (lNodeName == "bcc") { getNameAndEmailAddress(lRecipentChild, lName, lMailbox, lHost); ADDRESS** lNext = &theEnvelope->bcc; while (*lNext) { lNext = &((*lNext)->next); } *lNext = create_mail_address(lName, lMailbox, lHost); } } } lChildIter->close(); }
ItemSequence_t GeneratePDFFunction::evaluate(const ExternalFunction::Arguments_t& args, const zorba::StaticContext* aStaticContext, const zorba::DynamicContext* aDynamincContext) const { Iterator_t lIter = args[0]->getIterator(); lIter->open(); Item outputFormat; lIter->next(outputFormat); lIter->close(); jthrowable lException = 0; static JNIEnv* env; try { env = zorba::jvm::JavaVMSingleton::getInstance(aStaticContext)->getEnv(); jstring outFotmatString = env->NewStringUTF(outputFormat.getStringValue().c_str()); // Local variables std::ostringstream os; Zorba_SerializerOptions_t lOptions; Serializer_t lSerializer = Serializer::createSerializer(lOptions); jclass fopFactoryClass; jobject fopFactory; jmethodID fopFactoryNewInstance; jclass byteArrayOutputStreamClass; jobject byteArrayOutputStream; jobject fop; jmethodID newFop; jclass transformerFactoryClass; jobject transformerFactory; jobject transormer; jclass stringReaderClass; jobject stringReader; jstring xmlUTF; const char* xml; std::string xmlString; jclass streamSourceClass; jobject streamSource; jobject defaultHandler; jclass saxResultClass; jobject saxResult; jboolean isCopy; jbyteArray res; Item base64; String resStore; jsize dataSize; jbyte* dataElements; Item item; lIter = args[1]->getIterator(); lIter->open(); lIter->next(item); lIter->close(); // Searialize Item SingletonItemSequence lSequence(item); lSerializer->serialize(&lSequence, os); xmlString = os.str(); xml = xmlString.c_str(); // Create an OutputStream byteArrayOutputStreamClass = env->FindClass("java/io/ByteArrayOutputStream"); CHECK_EXCEPTION(env); byteArrayOutputStream = env->NewObject(byteArrayOutputStreamClass, env->GetMethodID(byteArrayOutputStreamClass, "<init>", "()V")); CHECK_EXCEPTION(env); // Create a FopFactory instance fopFactoryClass = env->FindClass("org/apache/fop/apps/FopFactory"); CHECK_EXCEPTION(env); fopFactoryNewInstance = env->GetStaticMethodID(fopFactoryClass, "newInstance", "()Lorg/apache/fop/apps/FopFactory;"); CHECK_EXCEPTION(env); fopFactory = env->CallStaticObjectMethod(fopFactoryClass, fopFactoryNewInstance); CHECK_EXCEPTION(env); // Create the Fop newFop = env->GetMethodID(fopFactoryClass, "newFop", "(Ljava/lang/String;Ljava/io/OutputStream;)Lorg/apache/fop/apps/Fop;"); CHECK_EXCEPTION(env); fop = env->CallObjectMethod(fopFactory, newFop, outFotmatString, byteArrayOutputStream); CHECK_EXCEPTION(env); // Create the Transformer transformerFactoryClass = env->FindClass("javax/xml/transform/TransformerFactory"); CHECK_EXCEPTION(env); transformerFactory = env->CallStaticObjectMethod(transformerFactoryClass, env->GetStaticMethodID(transformerFactoryClass, "newInstance", "()Ljavax/xml/transform/TransformerFactory;")); CHECK_EXCEPTION(env); transormer = env->CallObjectMethod(transformerFactory, env->GetMethodID(transformerFactoryClass, "newTransformer", "()Ljavax/xml/transform/Transformer;")); CHECK_EXCEPTION(env); // Create Source xmlUTF = env->NewStringUTF(xml); stringReaderClass = env->FindClass("java/io/StringReader"); CHECK_EXCEPTION(env); stringReader = env->NewObject(stringReaderClass, env->GetMethodID(stringReaderClass, "<init>", "(Ljava/lang/String;)V"), xmlUTF); CHECK_EXCEPTION(env); streamSourceClass = env->FindClass("javax/xml/transform/stream/StreamSource"); CHECK_EXCEPTION(env); streamSource = env->NewObject(streamSourceClass, env->GetMethodID(streamSourceClass, "<init>", "(Ljava/io/Reader;)V"), stringReader); CHECK_EXCEPTION(env); // Create the SAXResult defaultHandler = env->CallObjectMethod(fop, env->GetMethodID(env->FindClass("org/apache/fop/apps/Fop"), "getDefaultHandler", "()Lorg/xml/sax/helpers/DefaultHandler;")); CHECK_EXCEPTION(env); saxResultClass = env->FindClass("javax/xml/transform/sax/SAXResult"); CHECK_EXCEPTION(env); saxResult = env->NewObject(saxResultClass, env->GetMethodID(saxResultClass, "<init>", "(Lorg/xml/sax/ContentHandler;)V"), defaultHandler); CHECK_EXCEPTION(env); // Transform env->CallObjectMethod(transormer, env->GetMethodID(env->FindClass("javax/xml/transform/Transformer"), "transform", "(Ljavax/xml/transform/Source;Ljavax/xml/transform/Result;)V"), streamSource, saxResult); CHECK_EXCEPTION(env); // Close outputstream env->CallObjectMethod(byteArrayOutputStream, env->GetMethodID(env->FindClass("java/io/OutputStream"), "close", "()V")); CHECK_EXCEPTION(env); saxResultClass = env->FindClass("javax/xml/transform/sax/SAXResult"); CHECK_EXCEPTION(env); // Get the byte array res = (jbyteArray) env->CallObjectMethod(byteArrayOutputStream, env->GetMethodID(byteArrayOutputStreamClass, "toByteArray", "()[B")); CHECK_EXCEPTION(env); // Create the result dataSize = env->GetArrayLength(res); dataElements = env->GetByteArrayElements(res, &isCopy); std::string lBinaryString((const char*) dataElements, dataSize); std::stringstream lStream(lBinaryString); String base64S; base64::encode(lStream, &base64S); Item lRes( theFactory->createBase64Binary(base64S.data(), base64S.size(), true) ); return ItemSequence_t(new SingletonItemSequence(lRes)); } catch (zorba::jvm::VMOpenException&) { Item lQName = theFactory->createQName("http://zorba.io/modules/xsl-fo", "JVM-NOT-STARTED"); throw USER_EXCEPTION(lQName, "Could not start the Java VM (is the classpath set?)"); } catch (JavaException&) { jclass stringWriterClass = env->FindClass("java/io/StringWriter"); jclass printWriterClass = env->FindClass("java/io/PrintWriter"); jclass throwableClass = env->FindClass("java/lang/Throwable"); jobject stringWriter = env->NewObject( stringWriterClass, env->GetMethodID(stringWriterClass, "<init>", "()V")); jobject printWriter = env->NewObject( printWriterClass, env->GetMethodID(printWriterClass, "<init>", "(Ljava/io/Writer;)V"), stringWriter); env->CallObjectMethod(lException, env->GetMethodID(throwableClass, "printStackTrace", "(Ljava/io/PrintWriter;)V"), printWriter); //env->CallObjectMethod(printWriter, env->GetMethodID(printWriterClass, "flush", "()V")); jmethodID toStringMethod = env->GetMethodID(stringWriterClass, "toString", "()Ljava/lang/String;"); jobject errorMessageObj = env->CallObjectMethod( stringWriter, toStringMethod); jstring errorMessage = (jstring) errorMessageObj; const char *errMsg = env->GetStringUTFChars(errorMessage, 0); std::stringstream s; s << "A Java Exception was thrown:" << std::endl << errMsg; env->ReleaseStringUTFChars(errorMessage, errMsg); std::string err(""); err += s.str(); env->ExceptionClear(); Item lQName = theFactory->createQName("http://zorba.io/modules/xsl-fo", "JAVA-EXCEPTION"); throw USER_EXCEPTION(lQName, err); } return ItemSequence_t(new EmptySequence()); }
bool staticcollectionamanger1(zorba::Zorba* z) { try { std::ifstream lIn("module1.xq"); zorba::XQuery_t lQuery = z->createQuery(); Zorba_CompilerHints lHints; lQuery->compile(lIn, lHints); StaticCollectionManager* lColMgr = lQuery->getStaticCollectionManager(); ItemFactory* lFac = z->getItemFactory(); Item lCollName2 = lFac->createQName("http://www.mod2.com/", "coll"); Item lIdxName = lFac->createQName("http://www.mod2.com/", "index"); Item lCollName3 = lFac->createQName("http://www.mod3.com/", "coll"); ItemSequence_t lSeq = lColMgr->declaredCollections(); Iterator_t lIter = lSeq->getIterator(); lIter->open(); Item lTmp; int num_colls = 0; while (lIter->next(lTmp)) { std::cout << "name " << lTmp.getStringValue() << std::endl; ++num_colls; } if (num_colls != 2) { return false; } int num_idxs = 0; lSeq = lColMgr->declaredIndexes(); lIter = lSeq->getIterator(); lIter->open(); while (lIter->next(lTmp)) { std::cout << "name " << lTmp.getStringValue() << std::endl; ++num_idxs; } if (num_idxs != 1) { return false; } if (!lColMgr->isDeclaredCollection(lCollName2) || !lColMgr->isDeclaredCollection(lCollName3)) { return false; } if (!lColMgr->isDeclaredIndex(lIdxName)) { return false; } lColMgr->createCollection(lCollName2); lColMgr->createCollection(lCollName3); lColMgr->createIndex(lIdxName); if (!lColMgr->isAvailableCollection(lCollName2) || !lColMgr->isAvailableCollection(lCollName3) || !lColMgr->isAvailableIndex(lIdxName)) { return false; } ItemSequence_t lAvailableColls = lColMgr->availableCollections(); lIter = lAvailableColls->getIterator(); lIter->open(); num_colls = 0; while (lIter->next(lTmp)) { std::cout << "name " << lTmp.getStringValue() << std::endl; ++num_colls; } if (num_colls != 2) { return false; } lColMgr->deleteIndex(lIdxName); lColMgr->deleteCollection(lCollName2); lColMgr->deleteCollection(lCollName3); if (lColMgr->isAvailableCollection(lCollName2) || lColMgr->isAvailableCollection(lCollName3) || lColMgr->isAvailableIndex(lIdxName)) { return false; } return true; } catch ( ZorbaException const &e ) { std::cerr << e << std::endl; } catch ( std::exception const &e ) { std::cerr << e.what() << std::endl; } catch ( ... ) { std::cerr << "caught unexpected exception" << std::endl; } return false; }