void ATemplateNodeHandler_MODEL::Node::process(ATemplateContext& context, AOutputBuffer& output) { AAutoPtr<AEventVisitor::ScopedEvent> scoped; if (context.useEventVisitor().isLogging(AEventVisitor::EL_DEBUG)) { scoped.reset(new AEventVisitor::ScopedEvent(context.useEventVisitor(), ASW("ATemplateNodeHandler_MODEL",26), m_BlockData, AEventVisitor::EL_DEBUG)); } if (m_BlockData.isEmpty()) return; AXmlElement *pElement = context.useModel().useRoot().findElement(m_BlockData); if (pElement) { //a_Found object pElement->emitContent(output); } else { ARope rope("<!--Unable to find element for '",32); rope.append(m_BlockData); rope.append("'-->",4); output.append(rope); } }
AXmlElement& AXmlElement::addContent( AXmlElement *pnode, const AString& path, // = AConstant::ASTRING_EMPTY bool insertIntoFront // = false ) { if (path.isEmpty() || path.equals(AConstant::ASTRING_SLASH)) { AASSERT(this, m_Content.size() < DEBUG_MAXSIZE_AXmlElement); //Debug only limit AASSERT(this, pnode); pnode->setParent(this); if (insertIntoFront) m_Content.push_front(pnode); else m_Content.push_back(pnode); } else { LIST_AString parts; path.split(parts, '/'); if (!parts.size()) ATHROW(this, AException::InvalidParameter); AXmlElement *pNewParent = _createAndAppend(parts, this, insertIntoFront); AASSERT_EX(this, pNewParent, path); pNewParent->addContent(pnode, AConstant::ASTRING_EMPTY, insertIntoFront); } return *this; }
AXmlElement *AXmlElement::_createAndAppend(LIST_AString& xparts, AXmlElement *pParent, bool insertIntoFront) { AString& strName = xparts.front(); AXmlElement *p = new AXmlElement(strName, pParent); addContent(p, AConstant::ASTRING_EMPTY, insertIntoFront); xparts.pop_front(); if (xparts.size() > 0) return p->_createAndAppend(xparts, this, insertIntoFront); else return p; }
AXmlElement& AXmlElement::addElement(const AString& path, const AEmittable& object, AXmlElement::Encoding encoding, bool overwrite, bool insertIntoFront) { AXmlElement *p = _addElement(path, overwrite, insertIntoFront); AASSERT(this, p); //a_Emit to rope ARope value; object.emit(value); p->addData(value, encoding); return *p; }
bool AXmlElement::remove(const AString& path) { AXmlElement::CONTAINER result; if (_find(path, result) > 0) { for (AXmlElement::CONTAINER::iterator it = result.begin(); result.end() != it; ++it) { AXmlElement *p = (*it)->getParent(); p->_removeChildElement(*it); } return true; } else return false; }
void AXmlElement::emitXmlContent(AXmlElement& target) const { CONTAINER::const_iterator cit = m_Content.begin(); while (cit != m_Content.end()) { target.addContent((*cit)->clone()); ++cit; } }
int main(int argc, char* argv[]) { try { //a_XML dump AFile_AString stringfile("<root><c_0><gc_00/><gc_01>5</gc_01></c_0><c_1><gc_10/><gc_11/></c_1></root>"); AXmlElement element; element.fromAFile(stringfile); ARope rope; element.emitXml(rope, 0); // element.debugDump(); std::cout << rope.toString() << std::endl; } catch(AException& ex) { std::cerr << ex.what() << std::endl; } return 0; }
AXmlElement *AXmlElement::_getOrCreate(LIST_AString& xparts, AXmlElement* pParent, bool insertIntoFront) { AString& strName = xparts.front(); CONTAINER::iterator it = m_Content.begin(); while (it != m_Content.end()) { if ((*it)->isNameEquals(strName)) { AXmlElement *p = dynamic_cast<AXmlElement *>(*it); if (!xparts.size()) { return p; } else { if (p) { xparts.pop_front(); if (!xparts.size()) return p; else return p->_getOrCreate(xparts, this, insertIntoFront); } else ATHROW_EX(this, AException::DataConflict, strName); //a_ Not AXmlElement type } } ++it; } //a_strName not found, create it AXmlElement *p = new AXmlElement(strName, pParent); addContent(p, AConstant::ASTRING_EMPTY, insertIntoFront); xparts.pop_front(); if (xparts.size() == 0) { return p; } else { return p->_getOrCreate(xparts, this, insertIntoFront); } }
Bool HsRobot::Init(const AString& sCfg) { HawkXmlFile xmlCfg; HawkXmlDocument<AString> xmlDoc; if (!xmlCfg.Open(sCfg, xmlDoc)) { HawkPrint("Robot Open Config Error."); return false; } AXmlElement* pRoot = xmlDoc.GetRoot(); if (pRoot->GetChildAttribute("Robot", "Addr")) m_sSvrAddr = pRoot->GetChildAttribute("Robot", "Addr")->StringValue(); if (pRoot->GetChildAttribute("Robot", "Count")) m_iCount = pRoot->GetChildAttribute("Robot", "Count")->IntValue(); return true; }
/*! Gets text value for a given path thisfunction("element path") lua param: Element path lua return: Text content for a given element or nil if it doesn't exist */ static int alibrary_Objects_Model_getText(lua_State *L) { ATemplateContext *pLuaContext = static_cast<ATemplateContext *>(L->acontext); AASSERT(NULL, pLuaContext); size_t len = AConstant::npos; const char *s = luaL_checklstring(L, 1, &len); const AString& xmlpath = AString::wrap(s, len); AXmlElement *pElement = pLuaContext->useModel().useRoot().findElement(xmlpath); if (!pElement) { return 0; } else { AString str; pElement->emitContent(str); lua_pushlstring(L, str.c_str(), str.getSize()); return 1; } }
void AMySQLServer::emit(AXmlElement& target) const { if (target.useName().isEmpty()) target.useName().assign("AMySQLServer", 12); ADatabase::emitXml(target); target.addElement(ASW("client",6)).addData(mysql_get_client_info()); if (mp_mydata) { target.addElement(ASW("stat",4)).addData(mysql_stat(mp_mydata)); target.addElement(ASW("server",6)).addData(mysql_get_server_info(mp_mydata)); target.addElement(ASW("host",4)).addData(mysql_get_host_info(mp_mydata)); } }
void test_iterator() { AFile_AString f("<inserted><var a=1 b=2 c=3/></inserted>"); AXmlElement element; element.fromAFile(f); std::cout << "element_name=" << element.getName() << std::endl; AXmlElement::NodeContainer::const_iterator cit = element.getContent().begin(); while (cit != element.getContent().end()) { AXmlElement *p = dynamic_cast<AXmlElement *>(*cit); if (p) { ARope rope; p->emitXml(rope); std::cout << " " << rope << std::endl; } ++cit; } }
void __insert(AObjectContainer& ns) { AFile_AString f("<inserted><var a=1 b=2 c=3/><mixed>foo<bar/>baz</mixed></inserted>"); AXmlElement element; element.fromAFile(f); ARope rope; element.emitXml(rope); // element.debugDump(std::cout, 0); // std::cout << rope << std::endl; AObjectContainer nsNew; nsNew.fromElement(element); rope.clear(); element.clear(); nsNew.publish(element); // nsNew.debugDump(std::cout, 0); element.debugDump(std::cout, 0); element.emitXml(rope); // std::cout << rope << std::endl; ns.insert("/incoming/", nsNew.clone()); // ns.debugDump(std::cout, 0); }
Bool HsLogger::Init(const AString& sCfg) { if (!m_bRunning) { HawkXmlFile xmlCfg; HawkXmlDocument<AString> xmlDoc; if (!xmlCfg.Open(sCfg, xmlDoc)) { HawkPrint("LogServer Open Config Error."); return false; } AXmlElement* pRoot = xmlDoc.GetRoot(); AXmlElement* pCfg = pRoot->GetChildren("LgCfg"); AXmlElement* pDbCfg = pRoot->GetChildren("DbCfg"); if(!pCfg || !pDbCfg || !pCfg->GetAttribute("Addr")) { HawkPrint("LogServer Config Tag Error."); return false; } AString sAddr = pCfg->GetAttribute("Addr")->StringValue(); if (pRoot->GetAttribute("Console")) EnableConsole(pRoot->GetAttribute("Console")->BoolValue()); HawkDatabase::DBConn sConn(HawkDatabase::HDB_MYSQL); if (pDbCfg->GetAttribute("Host")) sConn.SetHost(pDbCfg->GetAttribute("Host")->StringValue()); if (pDbCfg->GetAttribute("Port")) sConn.SetPort(pDbCfg->GetAttribute("Port")->IntValue()); if (pDbCfg->GetAttribute("User")) sConn.SetUser(pDbCfg->GetAttribute("User")->StringValue()); if (pDbCfg->GetAttribute("Pwd")) sConn.SetPwd(pDbCfg->GetAttribute("Pwd")->StringValue()); sConn.SetDBName(pDbCfg->GetAttribute("DB")->StringValue()); return HawkLogServer::Init(sAddr, sConn); } return false; }
Bool HsGateway::Init(const AString& sCfg) { if (!m_bRunning) { HawkXmlFile xmlCfg; HawkXmlDocument<AString> xmlDoc; if (!xmlCfg.Open(sCfg, xmlDoc)) { HawkPrint("Gateway Open Config Error."); return false; } AXmlElement* pRoot = xmlDoc.GetRoot(); AXmlElement* pCfg = pRoot->GetChildren("GwCfg"); if(!pCfg || !pCfg->GetAttribute("Frontend") || !pCfg->GetAttribute("Backend")) { HawkPrint("Gateway Config Tag Error."); return false; } AString sFrontend = pCfg->GetAttribute("Frontend")->StringValue(); AString sBackend = pCfg->GetAttribute("Backend")->StringValue(); HawkStringUtil::Replace<AString>(sBackend, "*", "127.0.0.1"); Int32 iThread = 4; if (pCfg->GetAttribute("Threads")) iThread = pCfg->GetAttribute("Threads")->IntValue(); Bool bCrossDomain = false; if (pCfg->GetAttribute("CrossDomain")) bCrossDomain = pCfg->GetAttribute("CrossDomain")->BoolValue(); if (pCfg->GetAttribute("Platform")) m_iPlatform = pCfg->GetAttribute("Platform")->IntValue(); AString sProfiler = ""; if (pCfg->GetAttribute("Profiler")) sProfiler = pCfg->GetAttribute("Profiler")->StringValue(); Int32 iKeepAlive = 0; if (pCfg->GetAttribute("KeepAlive")) iKeepAlive = pCfg->GetAttribute("KeepAlive")->IntValue(); if (HawkGateway::Init(sFrontend, sBackend, iThread)) { P_ProtocolManager->SetSizeLimit(PAGE_SIZE); if (iKeepAlive > 0) SetSessionTimeout(iKeepAlive); if (bCrossDomain) TurnOnCrossDomain(); if (sProfiler.size()) TurnOnProfiler(sProfiler); return true; } } return false; }
AOSContext::ReturnCode AOSModule_Template::execute(AOSContext& context, const AXmlElement& params) { const AXmlElement *pNode = params.findElement(ASW("template",8)); AAutoPtr<ATemplate> pTemplate(NULL, false); AAutoPtr<AFile> pFile(NULL, false); if (pNode) { //a_Element contains script pTemplate.reset(m_Services.createTemplate(), true); //a_Parse template AFile_AString strfile; pNode->emitContent(strfile); pTemplate->fromAFile(strfile); } else { //a_Filename provided, use the cache pNode = params.findElement(AOS_BaseModules_Constants::FILENAME); if (pNode) { AString relativePath; pNode->emitContent(relativePath); //a_File to be used (may need caching for it, but for now keep it dynamic) AFilename filename(m_Services.useConfiguration().getAosBaseDataDirectory(), true); filename.join(relativePath, false); if (ACacheInterface::NOT_FOUND == m_Services.useCacheManager().getTemplate(context, filename, pTemplate)) { //a_Not found, return error ARope rope; rope.append(getClass()); rope.append(": Unable to find a template file: ",34); rope.append(filename); context.useEventVisitor().startEvent(rope, AEventVisitor::EL_ERROR); return AOSContext::RETURN_ERROR; } } else { context.addError(getClass(), ASWNL("Unable to find module/template nor module/filename, Template module did not execute, params")); return AOSContext::RETURN_ERROR; //a_Did not find either module/template or module/filename } } //a_Process and save output ARope ropeOutput; pTemplate->process(context.useLuaTemplateContext(), ropeOutput); //a_Add template to debug if (context.getDumpContextLevel() > 0) { AString str("debug/",6); str.append(getClass()); str.append("/template",9); AXmlElement& base = context.useModel().addElement(str); pTemplate->emitXml(base); } //a_Insert output into outpath (if any) pNode = params.findElement(AOS_BaseModules_Constants::PATH); if (pNode) { AString xmlpath; pNode->emitContent(xmlpath); if (!xmlpath.isEmpty()) { //a_Add output as CDATA context.useModel().addElement(xmlpath).addData(ropeOutput, AXmlElement::ENC_CDATADIRECT); } } else { context.useEventVisitor().addEvent(ASWNL("Unable to find module/path, output from template discarded"), AEventVisitor::EL_WARN); } return AOSContext::RETURN_OK; }
AOSContext::ReturnCode AOSModule_Wiki_ViewFromFileSystem::execute(AOSContext& context, const AXmlElement& moduleParams) { //a_Get base path AString basePath; if (!moduleParams.emitString(AOS_Wiki_Constants::PARAM_BASE_PATH, basePath)) { context.addError(getClass(), ASWNL("Unable to find module/base-path parameter")); return AOSContext::RETURN_ERROR; } AFilename wikifile(m_Services.useConfiguration().getAosBaseDataDirectory()); wikifile.join(basePath, true); //a_Get relative wiki path AString str; context.useRequestParameterPairs().get(ASW("wikipath",8), str); wikifile.join(str, false); u4 type = AFileSystem::getType(wikifile); //a_Directory only will get default index.html if (type & AFileSystem::Directory) { //a_Directory name specified wikifile.useFilename().assign(ASW("index.html",10)); } else wikifile.setExtension(ASW(".html",5)); AString strData; if (context.useRequestParameterPairs().get(ASW("wiki.newdata",12), strData)) { //a_Check if authentication passed if ( moduleParams.exists(AOS_Wiki_Constants::PARAM_SECURE) && !context.useModel().exists(ASW("wiki/Authenticated",18)) ) { context.useModel().overwriteElement(ASW("wiki/AuthFailed",15)); return AOSContext::RETURN_OK; } //a_New data submitted u4 type = AFileSystem::getType(wikifile); if (AFileSystem::DoesNotExist != type) { //a_New data submitted, old file exists AFilename newwikifile(wikifile); //a_Temporary filename to use during the swap AFilename tempwikifile(newwikifile); tempwikifile.setExtension(ASW("_temporary_rename_",19)); //a_Generate temporary filename AFileSystem::generateTemporaryFilename(newwikifile); //a_Save new data to temp filename AFile_Physical file(newwikifile, "wb"); file.open(); file.write(strData); file.close(); //a_Add to context so it can ve viewed after save context.useModel().overwriteElement(AOS_Wiki_Constants::ELEMENT_DATA).addData(strData, AXmlElement::ENC_CDATADIRECT); //a_Rename temp to current AFileSystem::rename(wikifile, tempwikifile); AFileSystem::rename(newwikifile, wikifile); AFileSystem::remove(tempwikifile); } else { //a_Does not exist yet //a_Make sure directories exist AFileSystem::createDirectories(wikifile); //a_Save new data AFile_Physical file(wikifile, "wb"); file.open(); file.write(strData); file.close(); //a_Add to context so it can ve viewed after save context.useModel().overwriteElement(AOS_Wiki_Constants::ELEMENT_DATA).addData(strData, AXmlElement::ENC_CDATADIRECT); } } else { u4 type = AFileSystem::getType(wikifile); if (AFileSystem::DoesNotExist != type) { AFile_Physical file(wikifile); file.open(); context.useModel().overwriteElement(AOS_Wiki_Constants::ELEMENT_DATA).addData(file, AXmlElement::ENC_CDATADIRECT); } else { //a_Signal that the wiki file does not exist context.useModel().overwriteElement(AOS_Wiki_Constants::ELEMENT_DOES_NOT_EXIST); } if (moduleParams.exists(AOS_Wiki_Constants::PARAM_SECURE)) { context.useModel().overwriteElement(AOS_Wiki_Constants::ELEMENT_SECURE_EDIT); } } return AOSContext::RETURN_OK; }
void AOSContextManager::adminEmitXml(AXmlElement& eBase, const AHTTPRequestHeader& request) { AOSAdminInterface::adminEmitXml(eBase, request); //a_Check if specific context is required AString contextId; if (request.getUrl().getParameterPairs().get(ASW("contextId",9), contextId)) { //a_Find the specific context { ALock lock(m_InUseSync); for (CONTEXT_INUSE::const_iterator cit = m_InUse.begin(); cit != m_InUse.end(); ++cit) { if (contextId == AString::fromPointer(*cit)) { adminAddProperty(eBase, ASW("contextDetail",7), *(*cit), AXmlElement::ENC_CDATADIRECT); return; } } } { ALock lock(m_ErrorHistory.useSync()); for (ABase *p = m_ErrorHistory.useTail(); p; p = p->usePrev()) { AOSContext *pContext = dynamic_cast<AOSContext *>(p); if (contextId == AString::fromPointer(pContext)) { adminAddProperty(eBase, ASW("contextDetail",7), *pContext, AXmlElement::ENC_CDATADIRECT); return; } } } { ALock lock(m_History.useSync()); for (ABase *p = m_History.useTail(); p; p = p->usePrev()) { AOSContext *pContext = dynamic_cast<AOSContext *>(p); if (contextId == AString::fromPointer(pContext)) { adminAddProperty(eBase, ASW("contextDetail",7), *pContext, AXmlElement::ENC_CDATADIRECT); return; } } } //a_Context no longer available adminAddError(eBase, ASWNL("AOSContext no longer available")); } else { //a_Display context summary for (size_t i=0; i < m_Queues.size(); ++i) { AOSContextQueueInterface *pQueue = m_Queues.at(i); if (pQueue) adminAddProperty(eBase, AString::fromSize_t(i), pQueue->getClass()); else adminAddProperty(eBase, AString::fromSize_t(i), AConstant::ASTRING_NULL); } adminAddPropertyWithAction( eBase, ASW("log_level",9), AString::fromInt(m_DefaultEventLogLevel), ASW("Update",6), ASWNL("Maximum event to log 0:Disabled 1:Error, 2:Event, 3:Warning, 4:Info, 5:Debug"), ASW("Set",3) ); { AString str; str.assign('['); str.append(AString::fromSize_t(m_History.size())); str.append('/'); str.append(AString::fromSize_t(m_HistoryMaxSize)); str.append(']'); AXmlElement& eHistory = eBase.addElement("object").addAttribute("name", "history"); adminAddPropertyWithAction(eHistory, ASWNL("max_size"), str, ASW("Set",3), ASWNL("Set maximum AOSContext history size (objects not immediately deleted)"), ASW("Set",3)); } adminAddPropertyWithAction( eBase, ASW("clear_history",13), AConstant::ASTRING_EMPTY, ASW("Clear",5), ASWNL("Clear context history: 0:all 1:history 2:error history"), ASW("Clear",5) ); { AString str; str.assign('['); str.append(AString::fromSize_t(m_ErrorHistory.size())); str.append('/'); str.append(AString::fromSize_t(m_ErrorHistoryMaxSize)); str.append(']'); AXmlElement& eErrorHistory = eBase.addElement("object").addAttribute("name", "error_history"); adminAddPropertyWithAction(eErrorHistory, ASWNL("max_size"), str, ASW("Set",3), ASWNL("Set maximum AOSContext error history size (objects not immediately deleted)"), ASW("Set",3)); } { AString str; str.assign('['); str.append(AString::fromSize_t(m_FreeStore.size())); str.append('/'); str.append(AString::fromSize_t(m_FreeStoreMaxSize)); str.append(']'); AXmlElement& eFreestore = eBase.addElement("object").addAttribute("name", "freestore"); adminAddPropertyWithAction(eFreestore, ASWNL("max_size"), str, ASW("Set",3), ASWNL("Set maximum AOSContext freestore size (objects not immediately deleted)"), ASW("Set",3)); } AXmlElement& eInUse = eBase.addElement("object"); eInUse.addAttribute("name", "InUse"); eInUse.addAttribute("size", AString::fromSize_t(m_InUse.size())); CONTEXT_INUSE::const_iterator citU = m_InUse.begin(); if (citU != m_InUse.end()) { ALock lock(m_InUseSync); while(citU != m_InUse.end()) { AXmlElement& eProp = adminAddProperty(eInUse, ASW("context",7), (*citU)->useEventVisitor(), AXmlElement::ENC_CDATADIRECT); eProp.addAttribute(ASW("errors",6), AString::fromSize_t((*citU)->useEventVisitor().getErrorCount())); eProp.addElement(ASW("url",3)).addData((*citU)->useEventVisitor().useName(), AXmlElement::ENC_CDATADIRECT); eProp.addElement(ASW("contextId",9)).addData(AString::fromPointer(*citU), AXmlElement::ENC_CDATADIRECT); ++citU; } } { AXmlElement& eHistory = eBase.addElement("object").addAttribute("name", "history"); ALock lock(m_History.useSync()); for (ABase *p = m_History.useTail(); p; p = p->usePrev()) { AOSContext *pContext = dynamic_cast<AOSContext *>(p); if (pContext) { AXmlElement& eProp = adminAddProperty(eHistory, ASW("context",7), pContext->useEventVisitor(), AXmlElement::ENC_CDATADIRECT); eProp.addAttribute(ASW("errors",6), AString::fromSize_t(pContext->useEventVisitor().getErrorCount())); eProp.addElement(ASW("url",3), pContext->useEventVisitor().useName(), AXmlElement::ENC_CDATADIRECT); eProp.addElement(ASW("contextId",3)).addData(AString::fromPointer(pContext), AXmlElement::ENC_CDATADIRECT); } else ATHROW(this, AException::InvalidObject); } } { AXmlElement& eErrorHistory = eBase.addElement("object").addAttribute("name", "error_history"); ALock lock(m_ErrorHistory.useSync()); for (ABase *p = m_ErrorHistory.useTail(); p; p = p->usePrev()) { AOSContext *pContext = dynamic_cast<AOSContext *>(p); if (pContext) { AXmlElement& eProp = adminAddProperty(eErrorHistory, ASW("context",7), pContext->useEventVisitor(), AXmlElement::ENC_CDATADIRECT); eProp.addAttribute(ASW("errors",6), AString::fromSize_t(pContext->useEventVisitor().getErrorCount())); eProp.addElement(ASW("url",3), pContext->useEventVisitor().useName(), AXmlElement::ENC_CDATADIRECT); eProp.addElement(ASW("contextId",3)).addData(AString::fromPointer(pContext), AXmlElement::ENC_CDATADIRECT); } else ATHROW(this, AException::InvalidObject); } } } }