Ejemplo n.º 1
0
void XMLConfigurator::save(XMLDocumentWrapper& xmlDoc, 
                           XMLNodeWrapperPtr& pXML,
                           const Configurator::ConfMap& confMap)
{
    Configurator conf(confMap);
    Configurator::Iterator it = conf.iterator();
    while (it.hasNext())
    {
        Configurator::KeyValuePair kv = it.next();
        
        if (kv.second.getType() == typeid(Configurator::ConfMap))
        {
            XMLNodeWrapperPtr pNode = xmlDoc.allocateNode(
                    XMLDocumentWrapper::NODE_ELEMENT, kv.first.c_str());
            save(xmlDoc, pNode, AnyCast<Configurator::ConfMap>(kv.second));
            pXML->appendNode(pNode);
        }
        else 
        {
            string str = AnyCast<string>(kv.second);
            XMLNodeWrapperPtr pNode = xmlDoc.allocateNode(
                    XMLDocumentWrapper::NODE_ELEMENT, kv.first.c_str(), str);
            pXML->appendNode(pNode);
        }
    }
}
Ejemplo n.º 2
0
void XMLDocumentWrapperTestCase::testParse()
{
    std::string sXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                       "<root>\n"
                       "\t<global>\n"
                       "\t\t<general>\n"
                       "\t\t\t<dict id=\"id_attr_value\">dict_value</dict>\n"
                       "\t\t</general>\n"
                       "\t</global>\n"
                       "</root>\n\n";
    XMLDocumentWrapper xmlDoc;
    try
    {
        xmlDoc.parse(sXml);
    }
    catch(const BadXmlFormatException& )
    {
        CPPUNIT_ASSERT(false);
    }

    XMLNodeWrapperPtr firstNode = xmlDoc.firstNode();
    CPPUNIT_ASSERT(firstNode;
    CPPUNIT_ASSERT_EQUAL(std::string("root"), std::string(firstNode->getName()));

    string sPrint;
    xmlDoc.print(sPrint);
    CPPUNIT_ASSERT_EQUAL(sXml, sPrint);
}
Ejemplo n.º 3
0
void XMLConfigurator::load(XMLDocumentWrapper& xmlDoc)
{
    setMode(Configurator::FROM_CONF);
    XMLNodeWrapperPtr pNode = xmlDoc.firstNode(CONFIGURE_TAG_NAME);
    if (pNode.isNull())
    {
        FIRTEX_THROW(InvalidConfigException, "Load xml configure FAILED: no configure element.");
    }
    load(pNode, getMap());
}
Ejemplo n.º 4
0
void XMLConfigurator::loadFromBuffer(const string& sXML)
{
    setMode(Configurator::FROM_CONF);
    XMLDocumentWrapper xmlDoc;
    xmlDoc.parse(sXML);
    XMLNodeWrapperPtr pRoot = xmlDoc.firstNode(CONFIGURE_TAG_NAME);
    if (pRoot.isNotNull())
    {
        load(pRoot, getMap());
    }
}
Ejemplo n.º 5
0
void XMLResultFormatter::formatTracer(XMLNodeWrapperPtr& pTraceRootNode,
                                      const QueryTracerPtr& pTracer)
{
    XMLNodeWrapperPtr pTraceNode = pTraceRootNode->appendNode(
            XMLDocumentWrapper::NODE_ELEMENT, "tracer");
    pTraceNode->appendAttribute("path", pTracer->getPath());
    pTraceNode->appendAttribute("level",
                                LoggingLevel::levelToStr(pTracer->getLevel()));

    const QueryTracer::TraceInfo& traceInfo = pTracer->getTraceInfo();
    for (size_t i = 0; i < traceInfo.size(); ++i)
    {
        XMLNodeWrapperPtr pCDataNode = pTraceNode->appendNode(
                XMLDocumentWrapper::NODE_ELEMENT, "message");
        pCDataNode->appendNode(XMLDocumentWrapper::NODE_CDATA,
                               "", traceInfo[i]);
    }

    const QueryTracer::TracerVector& tracers = pTracer->getChildTracers();
    for (size_t i = 0; i < tracers.size(); ++i)
    {
        const QueryTracerPtr& pChildTracer = tracers[i];
        if (pChildTracer)
        {
            formatTracer(pTraceNode, pChildTracer);
        }
    }
}
Ejemplo n.º 6
0
void XMLDocumentWrapperTestCase::testPrint()
{
    XMLDocumentWrapper xmlDoc;
    xmlDoc.addDeclarationNode("UTF-8");

    xmlDoc.appendNode(XMLDocumentWrapper::NODE_ELEMENT, "root");
    XMLNodeWrapperPtr pRoot = xmlDoc.firstNode();
    pRoot->appendNode(XMLDocumentWrapper::NODE_ELEMENT, "global", "testvalue");
    pRoot->appendAttribute("id", "testid");
        
    ostringstream ss;
    xmlDoc.print(ss);

    string sExpect = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                     "<root id=\"testid\">\n"
                     "\t<global>testvalue</global>\n"
                     "</root>\n\n";
    CPPUNIT_ASSERT_EQUAL(sExpect, ss.str());
}
Ejemplo n.º 7
0
void XMLConfigurator::save(const string& sCfgFile, FileSystemPtr& pFileSys)
{
    XMLDocumentWrapper xmlDoc;
    XMLNodeWrapperPtr pRoot = xmlDoc.appendNode(
            XMLDocumentWrapper::NODE_ELEMENT, CONFIGURE_TAG_NAME);
    Iterator it = iterator();
    while (it.hasNext())
    {
        Configurator::KeyValuePair kv = it.next();

        if (kv.second.getType() == typeid(Configurator::ConfMap))
        {
            XMLNodeWrapperPtr pNode = xmlDoc.allocateNode(
                    XMLDocumentWrapper::NODE_ELEMENT, kv.first.c_str());
            save(xmlDoc, pNode, AnyCast<Configurator::ConfMap>(kv.second));
            pRoot->appendNode(pNode);
        }
        else 
        {
            string str = AnyCast<string>(kv.second);
            XMLNodeWrapperPtr pNode = xmlDoc.allocateNode(
                    XMLDocumentWrapper::NODE_ELEMENT, kv.first.c_str(), str);
            pRoot->appendNode(pNode);
        }
    }

    OutputStreamPtr pOutStream = pFileSys->createFile(sCfgFile);
    if (pOutStream.isNull())
    {
        FIRTEX_THROW(FileIOException, "Save configure [%s] FAILED ",
                     sCfgFile.c_str());
    }
    string str;
    xmlDoc.addDeclarationNode("UTF-8");
    xmlDoc.print(str);
    pOutStream->write((const void*)str.c_str(), str.length());
}
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()));
        }
    }
}
Ejemplo n.º 9
0
void XMLDocumentWrapperTestCase::testClearAndReuse()
{
    XMLDocumentWrapper xmlDoc;

    string sExpect = "<result>\n"
                     "\t<hits number_hits=\"1\">\n"
                     "\t\t<hit>hit_value1</hit>\n"
                     "\t</hits>\n"
                     "\t<hits number_hits=\"2\">\n"
                     "\t\t<hit>hit_value2</hit>\n"
                     "\t</hits>\n"
                     "</result>\n\n";
                 
    for (size_t i = 0; i < 2; ++i)
    {
        XMLNodeWrapperPtr pRootNode = xmlDoc.appendNode(
                XMLDocumentWrapper::NODE_ELEMENT, "result");

        XMLNodeWrapperPtr pHitsNode = pRootNode->appendNode(
            XMLDocumentWrapper::NODE_ELEMENT, "hits");
        pHitsNode->appendAttribute("number_hits", "1");
        pHitsNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                              "hit", "hit_value1");
        
        pHitsNode = pRootNode->appendNode(
                XMLDocumentWrapper::NODE_ELEMENT, "hits");
        pHitsNode->appendAttribute("number_hits", "2");
        pHitsNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                              "hit", "hit_value2");

        ostringstream ss;
        xmlDoc.print(ss);
        CPPUNIT_ASSERT_EQUAL(sExpect, ss.str());

        xmlDoc.clear();
    }
}
Ejemplo n.º 10
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;
        }
    }
}
Ejemplo n.º 11
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
}
Ejemplo n.º 12
0
void XMLResultFormatter::format(const QueryResult& result,
                                std::stringstream& ss)
{
    m_xmlDoc.clear();

    m_xmlDoc.addDeclarationNode(result.getEncoding());
    XMLNodeWrapperPtr pRootNode = m_xmlDoc.appendNode(
            XMLDocumentWrapper::NODE_ELEMENT, "result");
    if (result.hasError())
    {
        formatError(pRootNode, result.getErrorMsg());
    }
    else 
    {
        XMLNodeWrapperPtr resultNode = pRootNode->appendNode(
                XMLDocumentWrapper::NODE_ELEMENT, "hits");
    
        string str;
        NumberFormatter::append(str, (int32_t)result.size());
        resultNode->appendAttribute("number_hits", str);

        str.clear();
        NumberFormatter::append(str, result.getTotalHits());
        resultNode->appendAttribute("total_hits", str);

        str.clear();
        NumberFormatter::append(str, result.getTimeCost());
        resultNode->appendAttribute("cost", str);

        QueryResult::Iterator it = result.iterator();
        while (it.hasNext())
        {
            XMLNodeWrapperPtr pHitNode = resultNode->appendNode(
                    XMLDocumentWrapper::NODE_ELEMENT, "hit");

            const ResultDocPtr& pResDoc = it.next();

            if (result.hasShardId())
            {
                str.clear();
                NumberFormatter::append(str, pResDoc->getShardId());
                pHitNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                        "shardid", str);
            }

            if (result.hasDocId())
            {
                str.clear();
                NumberFormatter::append(str, pResDoc->getDocId());
                pHitNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                        "docid", str);
            }

            if (result.hasScore())
            {
                str.clear();
                NumberFormatter::append(str, pResDoc->getScore(), 2);
                pHitNode->appendNode(XMLDocumentWrapper::NODE_ELEMENT,
                        "score", str);
            }

            if (result.hasFields())
            {
                XMLNodeWrapperPtr pFieldsNode = pHitNode->appendNode(
                        XMLDocumentWrapper::NODE_ELEMENT, "fields");

                ResultDoc::Iterator fieldIt = pResDoc->iterator();
                while (fieldIt.hasNext())
                {
                    const ResultDoc::Field& field = fieldIt.next();

                    XMLNodeWrapperPtr pCDataNode = pFieldsNode->appendNode(
                            XMLDocumentWrapper::NODE_ELEMENT, field.first);
                    pCDataNode->appendNode(XMLDocumentWrapper::NODE_CDATA,
                            "", field.second);
                }
            }
        } // end while 

        const QueryTracerPtr& pTracer = result.getTracer();
        if (pTracer)
        {
            formatTracer(pRootNode, pTracer);
        }
    }
    m_xmlDoc.print(ss);
}
Ejemplo n.º 13
0
void XMLResultFormatter::formatError(XMLNodeWrapperPtr& pRootNode, const string& sErrorMsg)
{
    XMLNodeWrapperPtr pErrorNode = pRootNode->appendNode(
            XMLDocumentWrapper::NODE_ELEMENT, "error");
    pErrorNode->appendNode(XMLDocumentWrapper::NODE_CDATA, "", sErrorMsg);
}