AOSContext::ReturnCode AOSOutput_JSON::execute(AOSContext& context) { AXmlElement::CONST_CONTAINER paths; AXmlElement::CONST_CONTAINER nodes; if (context.getOutputParams().find(AOS_BaseModules_Constants::PATH, paths)) { // Get the path fragments and convert into nodes for (AXmlElement::CONST_CONTAINER::const_iterator cit = paths.begin(); cit != paths.end(); ++cit) { // Get path and find associated nodes AString path; (*cit)->emitContent(path); context.useModel().find(path, nodes); } } if (nodes.size() > 0) { if (nodes.size() > 1) { // Array context.useOutputBuffer().append("[\r\n",3); AXmlElement::CONST_CONTAINER::const_iterator cit = nodes.begin(); while(cit != nodes.end()) { (*cit)->emitJson(context.useOutputBuffer(),1); ++cit; if (cit != nodes.end()) context.useOutputBuffer().append("\r\n,\r\n",5); } context.useOutputBuffer().append("\r\n]",3); } else { // Single object nodes.front()->emitJson(context.useOutputBuffer(),0); } } else { // Entire context model to JSON if (context.useEventVisitor().isLoggingDebug()) { context.useEventVisitor().addEvent(ASWNL("Emitting the entire model as JSON object"), AEventVisitor::EL_DEBUG); } context.useModel().emitJson(context.useOutputBuffer(),0); } // Set the correct content type for XML extension m_Services.useConfiguration().setMimeTypeFromExt(ASW("json",4), context); return AOSContext::RETURN_OK; }
size_t AXmlElement::_const_find(LIST_AString listPath, AXmlElement::CONST_CONTAINER& result) const { if (isNameEquals(listPath.front())) { listPath.pop_front(); size_t ret = 0; switch(listPath.size()) { case 0: //a_This node is it result.push_back(this); ++ret; break; case 1: { //a_Include immediate children nodes CONTAINER::const_iterator cit = m_Content.begin(); while (cit != m_Content.end()) { // Name only, add if martches if ((*cit)->isNameEquals(listPath.front())) { result.push_back(*cit); ++ret; } ++cit; } } break; default: { //a_Recurse deeper for each element CONTAINER::const_iterator cit = m_Content.begin(); while (cit != m_Content.end()) { if ((*cit)->isNameEquals(listPath.front())) { ret += (*cit)->_const_find(listPath, result); } ++cit; } } break; } return ret; } else return 0; }
/*! Uses the model to emit content for a path thisfunction("<path to the AXmlElement to emit>", separate) lua namespace: model lua param: Path to emit lua param: if non-nil then each element found will be returned by itself lua return: Content at the given path or nil if element does not exist */ static int alibrary_Objects_Model_emitContentFromPath(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); int mode = 0; if (lua_gettop(L) > 1) mode = luaL_checkint(L, 2); AXmlElement::CONST_CONTAINER nodes; size_t ret = pLuaContext->useModel().useRoot().find(xmlpath, nodes); if (mode) { //a_Return each as separate values AString str; for (AXmlElement::CONST_CONTAINER::const_iterator cit = nodes.begin(); cit != nodes.end(); ++cit) { (*cit)->emitContent(str); lua_pushlstring(L, str.c_str(), str.getSize()); str.clear(); } return (int)ret; } else { if (ret > 0) { //a_Return content concatinated ARope rope; for (AXmlElement::CONST_CONTAINER::const_iterator cit = nodes.begin(); cit != nodes.end(); ++cit) (*cit)->emitContent(rope); const AString& str = rope.toAString(); lua_pushlstring(L, str.c_str(), str.getSize()); return 1; } else return 0; } }
size_t AXmlElement::find(const AString& path, AXmlElement::CONST_CONTAINER& result) const { AString strAttribute; AString strPath(path); bool leadingSlash = false; if ('/' == strPath.at(0)) { leadingSlash = true; //a_Signals that slash leads the path so much include current name } //a_Remove the leading name (which should be this element's name) LIST_AString listPath; strPath.split(listPath, '/'); if (leadingSlash) { if (0 == listPath.size()) { //a_ "/" selects current node result.push_back(this); return 1; } else if (0 != listPath.front().compare(m_Name)) { //a_First token MUST be the name of this element if / leads AString str("Absolute path must start with the name of the current root element: root=/"); str.append(m_Name); str.append(" while path="); str.append(path); ATHROW_EX(this, AException::ProgrammingError, str); } } else if (0 == listPath.size()) { //a_ "/" selects current node result.push_back(this); return 1; } else { //a_Relative path implies this node is first listPath.push_front(m_Name); } return _const_find(listPath, result); }
void ATemplateNodeHandler_DADA::_loadStaticData() { AXmlElement::CONST_CONTAINER nodes; m_Services.useConfiguration().getConfigRoot().find("AOS_DadaData/dataset", nodes); AXmlElement::CONST_CONTAINER::iterator it; for (it = nodes.begin(); it != nodes.end(); ++it) { AString strSet; (*it)->getAttributes().get(ASW("name",4), strSet); if (strSet.isEmpty()) ATHROW_EX(*it, AException::InvalidData, ASWNL("AOS_DadaData/dataset missing 'name' parameter")); ADadaDataHolder *pddh = new ADadaDataHolder(); pddh->readData(m_Services, *it); m_Objects.insert(strSet, pddh, true); } nodes.clear(); m_Services.useConfiguration().getConfigRoot().find(ASW("AOS_DadaData/template",21), nodes); it = nodes.begin(); while (it != nodes.end()) { AString str; (*it)->emitContent(str); AString strName; if ((*it)->getAttributes().get(ASW("name",4), strName)) { AFilename filename(m_Services.useConfiguration().getAosBaseDataDirectory(), str, false); if (AFileSystem::exists(filename)) { AFile_Physical file(filename, ASW("r", 1)); file.open(); str.clear(); while (AConstant::npos != file.readLine(str)) { if ('#' != str.at(0, '\x0')) { m_Templates[strName].push_back(str); } str.clear(); } } else m_Services.useLog().add(ARope("AOS_DadaData: Missing file: ")+filename, ALog::EVENT_WARNING); } else m_Services.useLog().add(ASWNL("AOS_DadaData: AOS_DadaData/template missing 'name' attribute"), ALog::EVENT_FAILURE); ++it; } }