int main() { AFile_AString tdata("Hello <aos:session>some/path</aos:session>"); AAutoPtr<ATemplate> pTemplate(new ATemplate(), true); pTemplate->addHandler(new ATemplateNodeHandler_LUA()); pTemplate->addHandler(new ATemplateNodeHandler_SESSION()); pTemplate->fromAFile(tdata); AXmlDocument model("root"); ABasePtrContainer objects; AEventVisitor visitor; ATemplateContext ctx(objects, model, visitor); pTemplate->debugDump(); AString str; pTemplate->process(ctx, str); return 0; }
bool LLNotifications::loadTemplates() { const std::string xml_filename = "notifications.xml"; LLXMLNodePtr root; BOOL success = LLUICtrlFactory::getLayeredXMLNode(xml_filename, root); if (!success || root.isNull() || !root->hasName( "notifications" )) { llerrs << "Problem reading UI Notifications file: " << xml_filename << llendl; return false; } clearTemplates(); for (LLXMLNodePtr item = root->getFirstChild(); item.notNull(); item = item->getNextSibling()) { // we do this FIRST so that item can be changed if we // encounter a usetemplate -- we just replace the // current xml node and keep processing item = checkForXMLTemplate(item); if (item->hasName("global")) { std::string global_name; if (item->getAttributeString("name", global_name)) { mGlobalStrings[global_name] = item->getTextContents(); } continue; } if (item->hasName("template")) { // store an xml template; templates must have a single node (can contain // other nodes) std::string name; item->getAttributeString("name", name); LLXMLNodePtr ptr = item->getFirstChild(); mXmlTemplates[name] = ptr; continue; } if (!item->hasName("notification")) { llwarns << "Unexpected entity " << item->getName()->mString << " found in " << xml_filename << llendl; continue; } // now we know we have a notification entry, so let's build it LLNotificationTemplatePtr pTemplate(new LLNotificationTemplate()); if (!item->getAttributeString("name", pTemplate->mName)) { llwarns << "Unable to parse notification with no name" << llendl; continue; } //llinfos << "Parsing " << pTemplate->mName << llendl; pTemplate->mMessage = item->getTextContents(); pTemplate->mDefaultFunctor = pTemplate->mName; item->getAttributeString("type", pTemplate->mType); item->getAttributeString("icon", pTemplate->mIcon); item->getAttributeString("label", pTemplate->mLabel); item->getAttributeU32("duration", pTemplate->mExpireSeconds); item->getAttributeU32("expireOption", pTemplate->mExpireOption); std::string priority; item->getAttributeString("priority", priority); pTemplate->mPriority = NOTIFICATION_PRIORITY_NORMAL; if (!priority.empty()) { if (priority == "low") pTemplate->mPriority = NOTIFICATION_PRIORITY_LOW; if (priority == "normal") pTemplate->mPriority = NOTIFICATION_PRIORITY_NORMAL; if (priority == "high") pTemplate->mPriority = NOTIFICATION_PRIORITY_HIGH; if (priority == "critical") pTemplate->mPriority = NOTIFICATION_PRIORITY_CRITICAL; } item->getAttributeString("functor", pTemplate->mDefaultFunctor); BOOL persist = false; item->getAttributeBOOL("persist", persist); pTemplate->mPersist = persist; std::string sound; item->getAttributeString("sound", sound); if (!sound.empty()) { // TODO: test for bad sound effect name / missing effect pTemplate->mSoundEffect = LLUUID(LLUI::sConfigGroup->getString(sound.c_str())); } for (LLXMLNodePtr child = item->getFirstChild(); !child.isNull(); child = child->getNextSibling()) { child = checkForXMLTemplate(child); // <url> if (child->hasName("url")) { pTemplate->mURL = child->getTextContents(); child->getAttributeU32("option", pTemplate->mURLOption); } if (child->hasName("unique")) { pTemplate->mUnique = true; for (LLXMLNodePtr formitem = child->getFirstChild(); !formitem.isNull(); formitem = formitem->getNextSibling()) { if (formitem->hasName("context")) { std::string key; formitem->getAttributeString("key", key); pTemplate->mUniqueContext.push_back(key); //llwarns << "adding " << key << " to unique context" << llendl; } else { llwarns << "'unique' has unrecognized subelement " << formitem->getName()->mString << llendl; } } } // <form> if (child->hasName("form")) { pTemplate->mForm = LLNotificationFormPtr(new LLNotificationForm(pTemplate->mName, child)); } } addTemplate(pTemplate->mName, pTemplate); } //std::ostringstream ostream; //root->writeToOstream(ostream, "\n "); //llwarns << ostream.str() << llendl; return true; }
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; }
bool LLNotifications::loadNotifications() { LL_INFOS() << "Reading notifications template" << LL_ENDL; // Passing findSkinnedFilenames(constraint=LLDir::ALL_SKINS) makes it // output all relevant pathnames instead of just the ones from the most // specific skin. std::vector<std::string> search_paths = gDirUtilp->findSkinnedFilenames(LLDir::XUI, "notifications.xml", LLDir::ALL_SKINS); std::string base_filename = search_paths.front(); LLXMLNodePtr root; BOOL success = LLXMLNode::getLayeredXMLNode(root, search_paths); if (!success || root.isNull() || !root->hasName( "notifications" )) { LL_ERRS() << "Problem reading XML from UI Notifications file: " << base_filename << LL_ENDL; return false; } for (LLXMLNodePtr item = root->getFirstChild(); item.notNull(); item = item->getNextSibling()) { // we do this FIRST so that item can be changed if we // encounter a usetemplate -- we just replace the // current xml node and keep processing item = LLNotificationTemplates::instance().checkForXMLTemplate(item); if (!item->hasName("notification")) continue; // now we know we have a notification entry, so let's build it LLNotificationTemplatePtr pTemplate(new LLNotificationTemplate()); if (!item->getAttributeString("name", pTemplate->mName)) { LL_WARNS() << "Unable to parse notification with no name" << LL_ENDL; continue; } //LL_INFOS() << "Parsing " << pTemplate->mName << LL_ENDL; pTemplate->mMessage = item->getTextContents(); pTemplate->mDefaultFunctor = pTemplate->mName; item->getAttributeString("type", pTemplate->mType); item->getAttributeString("icon", pTemplate->mIcon); item->getAttributeString("label", pTemplate->mLabel); item->getAttributeU32("duration", pTemplate->mExpireSeconds); item->getAttributeU32("expireOption", pTemplate->mExpireOption); std::string priority; item->getAttributeString("priority", priority); pTemplate->mPriority = NOTIFICATION_PRIORITY_NORMAL; if (!priority.empty()) { if (priority == "low") pTemplate->mPriority = NOTIFICATION_PRIORITY_LOW; if (priority == "normal") pTemplate->mPriority = NOTIFICATION_PRIORITY_NORMAL; if (priority == "high") pTemplate->mPriority = NOTIFICATION_PRIORITY_HIGH; if (priority == "critical") pTemplate->mPriority = NOTIFICATION_PRIORITY_CRITICAL; } item->getAttributeString("functor", pTemplate->mDefaultFunctor); BOOL persist = false; item->getAttributeBOOL("persist", persist); pTemplate->mPersist = persist; std::string sound; item->getAttributeString("sound", sound); if (!sound.empty()) { // TODO: test for bad sound effect name / missing effect pTemplate->mSoundEffect = LLUUID(LLUI::sConfigGroup->findString(sound.c_str())); } for (LLXMLNodePtr child = item->getFirstChild(); !child.isNull(); child = child->getNextSibling()) { child = LLNotificationTemplates::instance().checkForXMLTemplate(child); // <url> if (child->hasName("url")) { pTemplate->mURL = child->getTextContents(); child->getAttributeU32("option", pTemplate->mURLOption); } if (child->hasName("unique")) { pTemplate->mUnique = true; for (LLXMLNodePtr formitem = child->getFirstChild(); !formitem.isNull(); formitem = formitem->getNextSibling()) { if (formitem->hasName("context")) { std::string key; formitem->getAttributeString("key", key); pTemplate->mUniqueContext.push_back(key); //LL_WARNS() << "adding " << key << " to unique context" << LL_ENDL; } else { LL_WARNS() << "'unique' has unrecognized subelement " << formitem->getName()->mString << LL_ENDL; } } } // <form> if (child->hasName("form")) { pTemplate->mForm = LLNotificationFormPtr(new LLNotificationForm(pTemplate->mName, child)); } } LLNotificationTemplates::instance().addTemplate(pTemplate->mName, pTemplate); } //std::ostringstream ostream; //root->writeToOstream(ostream, "\n "); //LL_WARNS() << ostream.str() << LL_ENDL; return true; }
AOSContext::ReturnCode AOSOutput_Template::execute(AOSContext& context) { //a_See if extension for mime type set for the template(s) AString ext; if (context.getOutputParams().emitContentFromPath(AOS_BaseModules_Constants::MIME_EXTENSION, ext)) { m_Services.useConfiguration().setMimeTypeFromExt(ext, context); } else { //a_Set content type based on request URL extension m_Services.useConfiguration().setMimeTypeFromExt(context.useRequestUrl().getExtension(), context); } // Iterate parameters and build the template const AXmlElement::CONTAINER& container = context.getOutputParams().getContentContainer(); for (AXmlElement::CONTAINER::const_iterator cit = container.begin(); cit != container.end(); ++cit) { //a_Check "if" condition AString ifElement; if ((*cit)->getAttributes().get(ASW("if",2), ifElement)) { if (ifElement.getSize() > 0) { //a_Check condition, if not met continue with next template if (!context.useModel().exists(ifElement)) { if (context.useEventVisitor().isLogging(AEventVisitor::EL_DEBUG)) context.useEventVisitor().startEvent(ARope("Skipping conditional file template IF ")+ifElement, AEventVisitor::EL_DEBUG); continue; } } } //a_Check "ifnot" condition ifElement.clear(); if ((*cit)->getAttributes().get(ASW("ifnot",5), ifElement)) { if (ifElement.getSize() > 0) { //a_Check condition, if not met continue with next template if (context.useModel().exists(ifElement)) { if (context.useEventVisitor().isLogging(AEventVisitor::EL_DEBUG)) context.useEventVisitor().startEvent(ARope("Skipping conditional file template IFNOT ")+ifElement, AEventVisitor::EL_DEBUG); continue; } } } // // Now we check if this is inlined or filename // AString str(1024, 512); if ((*cit)->getName().equals(AOS_BaseModules_Constants::TEMPLATE)) { if (context.useEventVisitor().isLoggingDebug()) { context.useEventVisitor().startEvent(getClass() + ": Creating new inline template", AEventVisitor::EL_DEBUG); } // Create a new template AAutoPtr<ATemplate> pTemplate(m_Services.createTemplate(), true); // Add inline template str.clear(); (*cit)->emitContent(str); AFile_AString strfile(str); pTemplate->clear(); pTemplate->fromAFile(strfile); pTemplate->process(context.useLuaTemplateContext(), context.useOutputBuffer()); } else if ((*cit)->getName().equals(AOS_BaseModules_Constants::FILENAME)) { // Add filename based template AFilename filename(m_Services.useConfiguration().getAosBaseDataDirectory()); str.clear(); (*cit)->emitContent(str); filename.join(str, false); if (context.useEventVisitor().isLoggingDebug()) { context.useEventVisitor().startEvent(getClass()+": Fetching/parsing template for: "+filename, AEventVisitor::EL_DEBUG); } AAutoPtr<ATemplate> pT(NULL, false); if (ACacheInterface::NOT_FOUND == m_Services.useCacheManager().getTemplate(context, filename, pT)) { //a_Not found add error and continue if (context.useEventVisitor().isLogging(AEventVisitor::EL_WARN)) context.useEventVisitor().startEvent(ARope(getClass())+ASWNL(": Unable to find a template file: ")+filename+ASWNL(", ignoring and continuing"), AEventVisitor::EL_WARN); continue; } //a_Parse template if (context.useEventVisitor().isLogging(AEventVisitor::EL_DEBUG)) { context.useEventVisitor().startEvent(getClass()+"Processing template file: "+filename, AEventVisitor::EL_DEBUG); } AASSERT(this, pT.isNotNull()); pT->process(context.useLuaTemplateContext(), context.useOutputBuffer()); } } return AOSContext::RETURN_OK; }
u4 AOSContextQueue_ErrorExecutor::_threadproc(AThread& thread) { AOSContextQueue_ErrorExecutor *pThis = dynamic_cast<AOSContextQueue_ErrorExecutor *>(thread.getThis()); AASSERT(&thread, pThis); AOSContext *pContext = NULL; thread.setRunning(true); while(thread.isRun()) { try { while (pContext = pThis->_nextContext()) { #ifndef NDEBUG if (!ADebugDumpable::isPointerValid(pContext)) { AString error("AOSContext pointer is invalid: "); error.append(AString::fromPointer(pContext)); AASSERT_EX(NULL, false, error); continue; } #endif pContext->useEventVisitor().startEvent(ASW("AOSContextQueue_ErrorExecutor: Processing error condition", 57)); //a_Should only be here if an error occured, if status not set >200, then assume 500 if (pContext->useResponseHeader().getStatusCode() == AHTTPResponseHeader::SC_200_Ok) pContext->useResponseHeader().setStatusCode(AHTTPResponseHeader::SC_500_Internal_Server_Error); //a_Check is socket was closed, if so do nothing else, we are done if (pContext->useConnectionFlags().isSet(AOSContext::CONFLAG_IS_SOCKET_ERROR)) { //a_Proceed m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext); continue; } if ( pContext->useContextFlags().isSet(AOSContext::CTXFLAG_IS_RESPONSE_HEADER_SENT) || pContext->useContextFlags().isSet(AOSContext::CTXFLAG_IS_OUTPUT_SENT) ) { //a_Log to file, output is done already m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE); } else { //a_Add XSLT stylesheet for the error XML //if (m_Services.useConfiguration().exists(ASW("/config/server/error-stylesheet",31))) //{ // AString errorStylesheet; // m_Services.useConfiguration().emitString(ASW("/config/server/error-stylesheet",31), errorStylesheet); // pContext->useModelXmlDocument().addInstruction(AXmlInstruction::XML_STYLESHEET) // .addAttribute(ASW("type",4), ASW("text/xsl",8)) // .addAttribute(ASW("href",4), errorStylesheet); //} //a_Add request header to result XML if (!pContext->useModel().exists(ASW("REQUEST",7))) pContext->useRequestHeader().emitXml(pContext->useModel().overwriteElement(ASW("REQUEST",7))); pContext->useResponseHeader().emitXml(pContext->useModel().overwriteElement(ASW("RESPONSE",8))); //a_Check if dumpContext is specified to override and emit XML int dumpContextLevel = pContext->getDumpContextLevel(); pContext->dumpContext(dumpContextLevel); if (dumpContextLevel > 0) { //a_Write contents of the output XML instead of output buffer m_Services.useConfiguration().setMimeTypeFromExt(ASW("xml",3), *pContext); pContext->useResponseHeader().setStatusCode(AHTTPResponseHeader::SC_200_Ok); pContext->writeOutputBuffer(true); } else { //a_Set the current content type as text/html pContext->useResponseHeader().set(AHTTPHeader::HT_ENT_Content_Type, ASW("text/html; charset=utf-8",24)); int statusCode = pContext->useResponseHeader().getStatusCode(); AAutoPtr<ATemplate> pTemplate(NULL, false); //a_Call to cache manager will set a template pContext->clearOutputBuffer(); if (m_Services.useCacheManager().getStatusTemplate(statusCode, pTemplate)) { //a_Template for this status code is found, so process and emit into output buffer pTemplate->process(pContext->useLuaTemplateContext(), pContext->useOutputBuffer()); if (pContext->useEventVisitor().isLogging(AEventVisitor::EL_DEBUG)) { ARope rope("Using error template for status "); rope.append(AString::fromInt(statusCode)); pContext->useEventVisitor().startEvent(rope, AEventVisitor::EL_DEBUG); } } else { if (pContext->useEventVisitor().isLogging(AEventVisitor::EL_WARN)) { ARope rope("Did not find error template for status "); rope.append(AString::fromInt(statusCode)); pContext->useEventVisitor().startEvent(rope, AEventVisitor::EL_WARN); } } if (pContext->isOutputBufferEmpty()) { AString strError(1024, 256); strError.assign("Error ",6); strError.append(AString::fromInt(pContext->useResponseHeader().getStatusCode())); strError.append(": ", 2); strError.append(pContext->useResponseHeader().getStatusCodeReasonPhrase(pContext->useResponseHeader().getStatusCode())); //a_Put some generic stuff since there is no error template pContext->useOutputBuffer().append("<html><head><title>",19); pContext->useOutputBuffer().append(strError); pContext->useOutputBuffer().append("</title></head>",15); pContext->useOutputBuffer().append("<body>",6); pContext->useOutputBuffer().append(strError); pContext->useOutputBuffer().append("</body></html>",14); } try { pContext->writeOutputBuffer(); } catch(ASocketException& ex) { pContext->useEventVisitor().addEvent(ex, AEventVisitor::EL_ERROR); pContext->useConnectionFlags().setBit(AOSContext::CONFLAG_IS_SOCKET_ERROR); m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext); continue; } } } //a_Close connection pContext->useSocket().close(); //a_Proceed m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext); continue; } AThread::sleep(pThis->m_SleepDelay); //a_Empty queue, avoid thrashing } catch(AException& e) { pContext->useEventVisitor().addEvent(e, AEventVisitor::EL_ERROR); m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE); m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext); } catch(std::exception& e) { pContext->useEventVisitor().addEvent(ASWNL(e.what()), AEventVisitor::EL_ERROR); m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE); m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext); } catch(...) { m_Services.useLog().add(pContext->useEventVisitor(), ALog::EVENT_FAILURE); pContext->useEventVisitor().addEvent(ASWNL("Unknown exception caught in AOSContextQueue_ErrorExecutor::_threadproc"), AEventVisitor::EL_ERROR); m_Services.useContextManager().changeQueueState(AOSContextManager::STATE_TERMINATE, &pContext); break; } } thread.setRunning(false); return 0; }