void TrecDocumentProcessorTestCase::makeAnswer(const std::string& str, Answer& ans)
{
    XMLDocumentWrapper xmlDoc;
    xmlDoc.parse(str);
    
    for (XMLNodeWrapperPtr pDocNode = xmlDoc.firstNode();
         pDocNode; pDocNode = pDocNode->nextSibling())
    {
        for (XMLNodeWrapperPtr pChildNode = pDocNode->firstNode();
             pChildNode; pChildNode = pChildNode->nextSibling())
        {
            ans.push_back(make_pair(pChildNode->getName(), pChildNode->getValue()));
        }
    }
}
Esempio n. 2
0
void XMLConfigurator::load(XMLNodeWrapperPtr& pNode, Configurator::ConfMap& conf)
{
    XMLNodeWrapperPtr pChild = pNode->firstNode();
    if (pChild.isNull())
    {
        Configurator c(conf, Configurator::TO_CONF);
        string sValue = pNode->getValue();
        c.configure((const string&)pNode->getName(), 
                    sValue, (const string&)_T(""));
        conf = c.getMap();
    }
    else if (pChild->nextSibling().isNull() 
             && pChild->type() == XMLDocumentWrapper::NODE_DATA)
    {
        Configurator c(conf, Configurator::TO_CONF);
        string sValue = pChild->getValue();
        c.configure((const string&)pNode->getName(), 
                    sValue, (const string&)_T(""));
        conf = c.getMap();
    }
    else 
    {
        Configurator::ConfMap confMap;
        for (XMLNodeWrapperPtr pSubNode = pNode->firstNode();
             pSubNode.isNotNull(); pSubNode = pSubNode->nextSibling())
        {
            load(pSubNode, confMap);
        }

        if (strCompareNoCase(pNode->getName(), CONFIGURE_TAG_NAME))
        {
            Configurator c(conf, Configurator::TO_CONF);
            c.configure(pNode->getName(), confMap);
            conf = c.getMap();
        }
        else 
        {
            conf = confMap;
        }
    }
}
Esempio n. 3
0
bool HTTPWatchdogService::commandToTask(EvHttpRequestContext* pCtx,
                                        ExecutionTask& task, 
                                        int32_t& stopSignal)
{
    string sContent;
    if (pCtx->getMethodType() == EVHTTP_REQ_GET)
    {
        sContent = pCtx->getQuery();
        KeyValueParser kvParser;
        if (!kvParser.parse(sContent, "&", "="))
        {
            stringstream ss;
            ss << "Invalid request: [" << sContent << "]";
            sendErrorMsg(pCtx, ss.str(), HTTPTypes::RESPONSE_CODE_BAD_REQUEST);
            return false;
        }
        string sValue;
        if (kvParser.getValue("process_name", sValue))
        {
            task.command.processName = sValue;
        }
        if (kvParser.getValue("parameters", sValue))
        {
            StringTokenizer st(sValue, ",", StringTokenizer::TOKEN_TRIM
                    | StringTokenizer::TOKEN_IGNORE_EMPTY);
            task.command.parameters.assign(st.begin(), st.end());
        }
        return true;
    }
    else // POST/PUT/DELETE
    {
        const char* p = (const char*)(pCtx->getInBuffer()->getData());
        if (!p || !*p)
        {
            sendErrorMsg(pCtx, "Request missing content", 
                        HTTPTypes::RESPONSE_CODE_BAD_REQUEST);
            return false;
        }

        XMLDocumentWrapper xmlDoc;
        try
        {
            string str(p, pCtx->getInBuffer()->getLength());
            xmlDoc.parse(str);
        }
        catch(const FirteXException& ex)
        {
            sendErrorMsg(pCtx, ex.what(), HTTPTypes::RESPONSE_CODE_BAD_REQUEST);
            return false;
        }

        XMLNodeWrapperPtr pRootNode = xmlDoc.firstNode("process");
        if (pRootNode.isNull())
        {
            sendErrorMsg(pCtx, "Missing process info", 
                        HTTPTypes::RESPONSE_CODE_BAD_REQUEST);
            return false;
        }
            
        EXTRACT_NODE_STRING2(pRootNode, "process_name", 
                             task.command.processName);
        if (task.command.processName.empty())
        {
            sendErrorMsg(pCtx, "Missing process name", 
                        HTTPTypes::RESPONSE_CODE_BAD_REQUEST);
            return false;
        }
            
        XMLNodeWrapperPtr pParamRootNode = pRootNode->firstNode("parameters");
        if (pParamRootNode.isNotNull())
        {
            for (XMLNodeWrapperPtr pParamNode = pParamRootNode->firstNode("parameter");
                 pParamNode.isNotNull(); pParamNode = pParamNode->nextSibling())
            {
                string str;
                EXTRACT_NODE_STRING(pParamNode, str);
                task.command.parameters.push_back(str);
            }
        }

        XMLNodeWrapperPtr pEnvNode = pRootNode->firstNode("environment");
        if (pEnvNode.isNotNull())
        {
            for (XMLNodeWrapperPtr pValueNode = pEnvNode->firstNode("value");
                 pValueNode.isNotNull(); pValueNode = pValueNode->nextSibling())
            {
                XMLAttributeWrapperPtr pNameAttr = pValueNode->firstAttribute("name");
                XMLNodeWrapperPtr pDataNode = pValueNode->firstNode();
                task.envVariables.insert(make_pair(pNameAttr->getValue(), 
                                pDataNode->getValue()));
            }
        }

        XMLNodeWrapperPtr pProcInfoKeepTimeNode = 
            pRootNode->firstNode("process_info_keep_time");
        if (pProcInfoKeepTimeNode.isNotNull())
        {
            task.processInfoKeepTime = NumberParser::parseInt32(
                    pProcInfoKeepTimeNode->getValue());
        }

        XMLNodeWrapperPtr pStopSignalNode = pRootNode->firstNode("stop_signal");
        if (pStopSignalNode.isNotNull())
        {
            stopSignal = NumberParser::parseInt32(pStopSignalNode->getValue());
        }
            
        EXTRACT_NODE_STRING2(pRootNode, "work_directory", task.workDirectory);

        XMLNodeWrapperPtr pRestartAfterCrash = pRootNode->firstNode("restart_after_crash");
        if (pRestartAfterCrash.isNotNull())
        {
            if (!strCompareNoCase(pRestartAfterCrash->getValue(), "true"))
            {
                task.restartAfterCrash = true;
            }
            else
            {
                task.restartAfterCrash = false;
            }
        }
        return true;
    } //end else // POST/PUT/DELETE
}