TSecTm::TSecTm(const PXmlTok& XmlTok) { const int Year = XmlTok->GetIntArgVal("Year"); const int Month = XmlTok->GetIntArgVal("Month"); const int Day = XmlTok->GetIntArgVal("Day"); const int Hour = XmlTok->GetIntArgVal("Hour"); const int Min = XmlTok->GetIntArgVal("Min"); const int Sec = XmlTok->GetIntArgVal("Sec"); AbsSecs = TSecTm(Year, Month, Day, Hour, Min, Sec).GetAbsSecs(); }
bool GetBoolArg(const PXmlTok& QueryXml, const TStr& ArgNm, const bool DfVal) { if (QueryXml.Empty()) return DfVal; TStr val = QueryXml->GetArgVal(ArgNm, ""); if (val == "") return DfVal; val = val.GetLc(); if (val == "true" || val == "1") return true; return false; }
void TDzsHKwBs::AddHits(const PXmlTok& TopicTok, const PBook& Book){ IAssert(TopicTok->IsTag("topic")); // execute query & add hits if (TopicTok->IsSubTag("query")){ // get query string TStr QueryStr=TopicTok->GetTagTok("query")->GetTokStr(false); // search PBixRes BixRes=Book->Search(QueryStr); if (BixRes->IsOk()){ printf("%s - %d\n", QueryStr.CStr(), BixRes->GetHits()); int Hits=BixRes->GetHits(); // retrieve or create <refs> subtag PXmlTok RefsTok; if (Hits>0){ if (TopicTok->IsSubTag("refs")){ RefsTok=TopicTok->GetTagTok("refs"); } else { RefsTok=TXmlTok::New(xsyTag, "refs"); TopicTok->AddSubTok(RefsTok); } } // add hits for (int HitN=0; HitN<Hits; HitN++){ // get hit info TStr SecIdStr; TStr TitleStr; TStr SecStr; TStr CtxStr; BixRes->GetHitInfo( Book, HitN, -1, 100, SecIdStr, TitleStr, SecStr, CtxStr); TStr TitleXmlStr=TXmlLx::GetXmlFromPlainStr(TitleStr); TStr UrlXmlStr=TXmlLx::GetXmlFromPlainStr(SecIdStr); // create <ref> PXmlTok RefTok=TXmlTok::New(xsyTag, "ref"); //RefTok->AddArg("title", TitleXmlStr); RefsTok->AddSubTok(RefTok); // create <title> PXmlTok TitleTok=TXmlTok::New(xsyTag, "title"); RefTok->AddSubTok(TitleTok); PXmlTok TitleStrTok=TXmlTok::New(xsyStr, TitleXmlStr); TitleTok->AddSubTok(TitleStrTok); // create <url> PXmlTok UrlTok=TXmlTok::New(xsyTag, "url"); RefTok->AddSubTok(UrlTok); PXmlTok UrlStrTok=TXmlTok::New(xsyStr, UrlXmlStr); UrlTok->AddSubTok(UrlStrTok); } } } // recurse for (int SubTokN=0; SubTokN<TopicTok->GetSubToks(); SubTokN++){ PXmlTok CurTok=TopicTok->GetSubTok(SubTokN); if (CurTok->IsTag("topic")){ AddHits(CurTok, Book); } } }
void TCpDoc::LoadReuters2000DocFromXml(const TStr& FNm, TStr& DocId, TStr& DateStr, TStr& TitleStr, TStr& HeadlineStr, TStr& BylineStr, TStr& DatelineStr, TStrV& ParStrV, TStrV& TopCdNmV, TStrV& GeoCdNmV, TStrV& IndCdNmV){ PXmlDoc Doc=TXmlDoc::LoadTxt(FNm); // get text strings // general document data DocId=Doc->GetTagTok("newsitem")->GetArgVal("itemid"); DateStr=Doc->GetTagTok("newsitem")->GetArgVal("date"); TitleStr=Doc->GetTagTok("newsitem|title")->GetTokStr(false); HeadlineStr=Doc->GetTagTok("newsitem|headline")->GetTokStr(false); BylineStr=""; PXmlTok BylineTok; if (Doc->IsTagTok("newsitem|byline", BylineTok)){ BylineStr=BylineTok->GetTokStr(false);} DatelineStr=""; PXmlTok DatelineTok; if (Doc->IsTagTok("newsitem|dateline", DatelineTok)){ DatelineStr=DatelineTok->GetTokStr(false);} // text paragraphs ParStrV.Clr(); TXmlTokV ParTokV; Doc->GetTagTokV("newsitem|text|p", ParTokV); for (int ParTokN=0; ParTokN<ParTokV.Len(); ParTokN++){ TStr ParStr=ParTokV[ParTokN]->GetTokStr(false); ParStrV.Add(ParStr); } // codes TopCdNmV.Clr(); GeoCdNmV.Clr(); IndCdNmV.Clr(); TXmlTokV CdsTokV; Doc->GetTagTokV("newsitem|metadata|codes", CdsTokV); for (int CdsTokN=0; CdsTokN<CdsTokV.Len(); CdsTokN++){ PXmlTok CdsTok=CdsTokV[CdsTokN]; TXmlTokV CdTokV; CdsTok->GetTagTokV("code", CdTokV); if (CdsTok->GetArgVal("class")=="bip:topics:1.0"){ for (int CdTokN=0; CdTokN<CdTokV.Len(); CdTokN++){ TStr CdNm=CdTokV[CdTokN]->GetArgVal("code"); TopCdNmV.Add(CdNm); } } else if (CdsTok->GetArgVal("class")=="bip:countries:1.0"){ for (int CdTokN=0; CdTokN<CdTokV.Len(); CdTokN++){ TStr CdNm=CdTokV[CdTokN]->GetArgVal("code"); GeoCdNmV.Add(CdNm); } } else if (CdsTok->GetArgVal("class")=="bip:industries:1.0"){ for (int CdTokN=0; CdTokN<CdTokV.Len(); CdTokN++){ TStr CdNm=CdTokV[CdTokN]->GetArgVal("code"); IndCdNmV.Add(CdNm); } } else { Fail; } } }
///////////////////////////////////////////////// // Dzs-Hierarchical-Keyword-Base bool TDzsHKwBs::IsTopicOk(const PXmlTok& TopicTok){ // <topic> is expected if (!TopicTok->IsTag("topic")){return false;} // <title> is mandatory subtag if (!TopicTok->IsSubTag("title")){return false;} // recurse for (int SubTokN=0; SubTokN<TopicTok->GetSubToks(); SubTokN++){ PXmlTok CurTok=TopicTok->GetSubTok(SubTokN); if (CurTok->IsTag("topic")){ if (!IsTopicOk(CurTok)){return false;} } } return true; }
PTransCorpus TTransCorpus::LoadAC(const TStr& InXmlFNm, const int& MxSents) { // prepare prset structures PTransCorpus TransCorpus = TTransCorpus::New(); // we load xml by skiping first tags PSIn XmlSIn=TFIn::New(InXmlFNm); TXmlDoc::SkipTopTag(XmlSIn); // ignore TEI printf("Ignoring: %s\n", TXmlDoc::LoadTxt(XmlSIn)->GetTok()->GetTagNm().CStr()); // ignore teiHeader TXmlDoc::SkipTopTag(XmlSIn); // ignore text TXmlDoc::SkipTopTag(XmlSIn); // ignore body PXmlDoc XmlDoc; int XmlDocs = 0, SentId = 0;; forever{ // load xml tree XmlDocs++; printf("%7d Sentences \r", SentId); XmlDoc=TXmlDoc::LoadTxt(XmlSIn); // stop if at the last tag if (!XmlDoc->IsOk()) { /*printf("Error: %s\n", XmlDoc->GetMsgStr().CStr());*/ break; } // extract documents from xml-trees PXmlTok TopTok=XmlDoc->GetTok(); if (TopTok->IsTag("div")){ // extract document Id TStr DocNm = TopTok->GetArgVal("n"); // and paragraphs TXmlTokV LinkTokV; TopTok->GetTagTokV("linkGrp|link", LinkTokV); for (int LinkTokN = 0; LinkTokN < LinkTokV.Len(); LinkTokN++) { PXmlTok LinkTok = LinkTokV[LinkTokN]; TStr LinkType = LinkTok->GetArgVal("type"); // skip if paragraph for one language is empty if (LinkType == "1:1") { TXmlTokV S1TokV; LinkTok->GetTagTokV("s1", S1TokV); TXmlTokV S2TokV; LinkTok->GetTagTokV("s2", S2TokV); IAssert(S1TokV.Len() == 1); IAssert(S2TokV.Len() == 1); TStr ParaStr1 = S1TokV[0]->GetTagTokStr(""); TStr ParaStr2 = S2TokV[0]->GetTagTokStr(""); TransCorpus->AddSentenceNoTrans(SentId, ParaStr1, ParaStr2); SentId++; } } } else { printf("Unknow tag: %s\n", TopTok->GetTagNm().CStr()); } if ((MxSents != -1) && (TransCorpus->GetSentences() > MxSents)) { break; } } printf("\n"); // finish return TransCorpus; }
///////////////////////////////////////////////// // Dzs-Base-Document void TDzsBsDoc::AddDataTokToChA(const PXmlTok& Tok, TChA& ChA){ switch (Tok->GetSym()){ case xsyWs: case xsyStr: case xsyQStr: ChA+=Tok->GetStr(); break; case xsyTag:{ TStr TagNm=Tok->GetStr(); if ((TagNm=="GUID")||(TagNm=="REFF")||(TagNm=="TYPE")||(TagNm=="FILE")){break;} if ((TagNm=="TITLE")||(TagNm=="TOPICTITLE")){break;} ChA+=' '; if ((TagNm=="TITLE")||(TagNm=="TOPICTITLE")){ChA+="<TITLE>";} for (int SubTokN=0; SubTokN<Tok->GetSubToks(); SubTokN++){ AddDataTokToChA(Tok->GetSubTok(SubTokN), ChA);} if ((TagNm=="TITLE")||(TagNm=="TOPICTITLE")){ChA+="</TITLE>";} ChA+=' '; break;} default: Fail; } }
TIntV GetEmailIds(const PXmlTok& QueryXml, const TStr& TagPath) { TIntV EmailIdV; TXmlTokV Ids; QueryXml->GetTagTokV(TagPath, Ids); for (int EmlInd = 0; EmlInd < Ids.Len(); EmlInd++) { TInt EmailIdInt = Ids[EmlInd]->GetIntArgVal("id", -1); if (EmailIdInt != -1) EmailIdV.AddUnique(EmailIdInt); } return EmailIdV; }
void GetKeywords(const PXmlTok& QueryXml, const TStr& TagPath, TStrV& KeywordsV, TStrV& IgnoreKeywordsV) { TXmlTokV KwsXmlV; QueryXml->GetTagTokV(TagPath, KwsXmlV); for (int KwInd = 0; KwInd < KwsXmlV.Len(); KwInd++) { TStr Kw = KwsXmlV[KwInd]->GetTokStr(false); int hide = KwsXmlV[KwInd]->GetIntArgVal("hide", 0); if (hide) IgnoreKeywordsV.Add(Kw); else KeywordsV.Add(Kw); } }
void TDzsBsDoc::GetDocParts( const TStr& FNm, const PXmlDoc& XmlDoc, const TStr& FPath, const TStr& WebAlias, bool& Ok, TStr& IdStr, TStr& TitleStr, TStr& DataStr, int& YearN){ Ok=false; if (!XmlDoc->IsOk()){return;} // id IdStr=FNm; IdStr.ChangeStr(FPath, WebAlias); // PXmlTok IdTok; // if (XmlDoc->IsTagTok("term|metadata|identifier", IdTok)){ // IdStr=IdTok->GetTokStr(false);} // else {return;} // title PXmlTok TitleTok; if (XmlDoc->IsTagTok("term|metadata|title", TitleTok)){ TitleStr=TitleTok->GetTokStr(false);} else {return;} // timedata TXmlTokV TimeDataTokV; XmlDoc->GetTagTokV("term|data|frame|timedata|fromyear", TimeDataTokV); TStr TimeDataStr=TXmlTok::GetTokVStr(TimeDataTokV, false); if (TimeDataStr.IsInt(YearN)){} else {YearN=0;} // locdata TXmlTokV LocDataTokV; XmlDoc->GetTagTokV("term|data|frame|locdata", LocDataTokV); TStr LocDataStr=TXmlTok::GetTokVStr(LocDataTokV, true); // pages TXmlTokV PageTokV; XmlDoc->GetTagTokV("term|data|frame|page", PageTokV); DataStr=GetDataTokVStr(PageTokV, "\n")+" "+LocDataStr; // character-set transformation TitleStr=THtmlLxChDef::GetCSZFromWin1250(TitleStr); DataStr=THtmlLxChDef::GetCSZFromWin1250(DataStr); // success Ok=true; }
PXmlTok TSecTm::GetXmlTok() const { PXmlTok NodeTok = TXmlTok::New("NodeTime"); NodeTok->AddArg("Year", GetYearN()); NodeTok->AddArg("Month", GetMonthN()); NodeTok->AddArg("Day", GetDayN()); NodeTok->AddArg("Hour", GetHourN()); NodeTok->AddArg("Min", GetMinN()); NodeTok->AddArg("Sec", GetSecN()); return NodeTok; }
void TSAppSrv::OnHttpRq(const int& SockId, const PHttpRq& HttpRq) { PHttpResp HttpResp; try { // check http-request correctness - return if error EAssertR(HttpRq->IsOk(), "Bad HTTP request!"); // check url correctness - return if error PUrl RqUrl = HttpRq->GetUrl(); EAssertR(RqUrl->IsOk(), "Bad request URL!"); // extract function name PUrl HttpRqUrl = HttpRq->GetUrl(); TStr FunNm = HttpRqUrl->GetPathSeg(0); EAssertR(FunNmToFunH.IsKey(FunNm) || FunNm.Empty(), "Unknown function '" + FunNm + "' !"); // extract parameters TStrKdV FldNmValPrV; PUrlEnv HttpRqUrlEnv = HttpRq->GetUrlEnv(); const int Keys = HttpRqUrlEnv->GetKeys(); for (int KeyN = 0; KeyN < Keys; KeyN++) { TStr KeyNm = HttpRqUrlEnv->GetKeyNm(KeyN); const int Vals = HttpRqUrlEnv->GetVals(KeyN); for (int ValN = 0; ValN < Vals; ValN++) { TStr Val = HttpRqUrlEnv->GetVal(KeyN, ValN); FldNmValPrV.Add(TStrKd(KeyNm, Val)); } } // log the call TStr TimeNow = TTm::GetCurLocTm().GetWebLogDateTimeStr(true); GetNotify()->OnStatus(TStr::Fmt("[%s] Request %s", TimeNow.CStr(), FunNm.CStr())); // prepare request environment PSAppSrvRqEnv RqEnv = TSAppSrvRqEnv::New(this, SockId, HttpRq); PSIn BodySIn; TStr ContTypeVal; if (!FunNm.Empty()) { // call function PSAppSrvFun SrvFun = FunNmToFunH.GetDat(FunNm); if (SrvFun->GetFunOutType() == saotXml) { PXmlDoc ResXmlDoc = SrvFun->Exec(FldNmValPrV, RqEnv); TStr ResXmlStr; ResXmlDoc->SaveStr(ResXmlStr); //ResXmlDoc->SaveTxt(TFile::GetUniqueFNm("test.xml")); BodySIn = TMIn::New(XmlHdStr + ResXmlStr); ContTypeVal = THttp::TextXmlFldVal; } else if (SrvFun->GetFunOutType() == saotJSon) { TStr ResStr = SrvFun->ExecJSon(FldNmValPrV, RqEnv); BodySIn = TMIn::New(ResStr); //ContTypeVal = THttp::TextHtmlFldVal; ContTypeVal = THttp::AppJSonFldVal; } else { BodySIn = SrvFun->ExecCustom(FldNmValPrV, RqEnv, ContTypeVal); } } else { PXmlTok TopTok = TXmlTok::New("registered-functions"); int KeyId = FunNmToFunH.FFirstKeyId(); while (FunNmToFunH.FNextKeyId(KeyId)) { PXmlTok FunTok = TXmlTok::New("function"); FunTok->AddArg("name", FunNmToFunH.GetKey(KeyId)); TopTok->AddSubTok(FunTok); } PXmlDoc ResXmlDoc = TXmlDoc::New(TopTok); TStr ResXmlStr; ResXmlDoc->SaveStr(ResXmlStr); BodySIn = TMIn::New(XmlHdStr + ResXmlStr); ContTypeVal = THttp::TextXmlFldVal; } // prepare response HttpResp = THttpResp::New(THttp::OkStatusCd, ContTypeVal, false, BodySIn); // send response } catch (PExcept Except) { PXmlTok TopTok = TXmlTok::New("error"); TopTok->AddSubTok(TXmlTok::New("message", Except->GetMsgStr())); TopTok->AddSubTok(TXmlTok::New("location", Except->GetLocStr())); PXmlDoc ErrorXmlDoc = TXmlDoc::New(TopTok); TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr); HttpResp = THttpResp::New(THttp::OkStatusCd, THttp::TextHtmlFldVal, false, TMIn::New(XmlHdStr + ResXmlStr)); } catch (...) { PXmlDoc ErrorXmlDoc = TXmlDoc::New(TXmlTok::New("error")); TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr); HttpResp = THttpResp::New(THttp::OkStatusCd, THttp::TextHtmlFldVal, false, TMIn::New(XmlHdStr + ResXmlStr)); } SendHttpResp(SockId, HttpResp); }
void TSAppSrvFun::Exec(const TStrKdV& FldNmValPrV, const PSAppSrvRqEnv& RqEnv) { const PNotify& Notify = RqEnv->GetWebSrv()->GetNotify(); PHttpResp HttpResp; try { // log the call if (NotifyOnRequest) Notify->OnStatus(TStr::Fmt("RequestStart %s", FunNm.CStr())); TTmStopWatch StopWatch(true); // execute the actual function, according to the type PSIn BodySIn; TStr ContTypeVal; if (GetFunOutType() == saotXml) { PXmlDoc ResXmlDoc = ExecXml(FldNmValPrV, RqEnv); TStr ResXmlStr; ResXmlDoc->SaveStr(ResXmlStr); BodySIn = TMIn::New(XmlHdStr + ResXmlStr); ContTypeVal = THttp::TextXmlFldVal; } else if (GetFunOutType() == saotJSon) { TStr ResStr = ExecJSon(FldNmValPrV, RqEnv); BodySIn = TMIn::New(ResStr); ContTypeVal = THttp::AppJSonFldVal; } else { BodySIn = ExecSIn(FldNmValPrV, RqEnv, ContTypeVal); } if (ReportResponseSize) Notify->OnStatusFmt("Response size: %.1f KB", BodySIn->Len() / (double) TInt::Kilo); // log finish of the call if (NotifyOnRequest) Notify->OnStatus(TStr::Fmt("RequestFinish %s [request took %d ms]", FunNm.CStr(), StopWatch.GetMSecInt())); // prepare response HttpResp = THttpResp::New(THttp::OkStatusCd, ContTypeVal, false, BodySIn); } catch (PExcept Except) { // known internal error Notify->OnStatusFmt("Exception: %s", Except->GetMsgStr().CStr()); Notify->OnStatusFmt("Location: %s", Except->GetLocStr().CStr()); TStr ResStr, ContTypeVal = THttp::TextPlainFldVal; if (GetFunOutType() == saotXml) { PXmlTok TopTok = TXmlTok::New("error"); TopTok->AddSubTok(TXmlTok::New("message", Except->GetMsgStr())); TopTok->AddSubTok(TXmlTok::New("location", Except->GetLocStr())); PXmlDoc ErrorXmlDoc = TXmlDoc::New(TopTok); ResStr = XmlHdStr + ErrorXmlDoc->SaveStr(); ContTypeVal = THttp::TextXmlFldVal; } else if (GetFunOutType() == saotJSon) { PJsonVal ResVal = TJsonVal::NewObj(); ResVal->AddToObj("message", Except->GetMsgStr()); ResVal->AddToObj("location", Except->GetLocStr()); ResStr = TJsonVal::NewObj("error", ResVal)->SaveStr(); ContTypeVal = THttp::AppJSonFldVal; } // prepare response HttpResp = THttpResp::New(THttp::InternalErrStatusCd, ContTypeVal, false, TMIn::New(ResStr)); } catch (...) { // unknown internal error TStr ResStr, ContTypeVal = THttp::TextPlainFldVal; if (GetFunOutType() == saotXml) { PXmlDoc ErrorXmlDoc = TXmlDoc::New(TXmlTok::New("error")); ResStr = XmlHdStr + ErrorXmlDoc->SaveStr(); ContTypeVal = THttp::TextXmlFldVal; } else if (GetFunOutType() == saotJSon) { ResStr = TJsonVal::NewObj("error", "Unknown")->SaveStr(); ContTypeVal = THttp::AppJSonFldVal; } // prepare response HttpResp = THttpResp::New(THttp::InternalErrStatusCd, ContTypeVal, false, TMIn::New(ResStr)); } if (LogRqToFile) LogReqRes(FldNmValPrV, HttpResp); // send response RqEnv->GetWebSrv()->SendHttpResp(RqEnv->GetSockId(), HttpResp); }
void TSAppSrv::OnHttpRq(const uint64& SockId, const PHttpRq& HttpRq) { // last appropriate error code, start with bad request int ErrStatusCd = THttp::BadRqStatusCd; try { // check http-request correctness - return if error EAssertR(HttpRq->IsOk(), "Bad HTTP request!"); // check url correctness - return if error PUrl RqUrl = HttpRq->GetUrl(); EAssertR(RqUrl->IsOk(), "Bad request URL!"); // extract function name PUrl HttpRqUrl = HttpRq->GetUrl(); TStr FunNm = HttpRqUrl->GetPathSeg(0); // check if we have the function registered if (FunNm == "favicon.ico") { PHttpResp HttpResp = THttpResp::New(THttp::OkStatusCd, THttp::ImageIcoFldVal, false, Favicon.GetSIn()); SendHttpResp(SockId, HttpResp); return; } else if (!FunNm.Empty() && !FunNmToFunH.IsKey(FunNm)) { ErrStatusCd = THttp::ErrNotFoundStatusCd; GetNotify()->OnStatusFmt("[AppSrv] Unknown function '%s'!", FunNm.CStr()); TExcept::Throw("Unknown function '" + FunNm + "'!"); } // extract parameters TStrKdV FldNmValPrV; PUrlEnv HttpRqUrlEnv = HttpRq->GetUrlEnv(); const int Keys = HttpRqUrlEnv->GetKeys(); for (int KeyN = 0; KeyN < Keys; KeyN++) { TStr KeyNm = HttpRqUrlEnv->GetKeyNm(KeyN); const int Vals = HttpRqUrlEnv->GetVals(KeyN); for (int ValN = 0; ValN < Vals; ValN++) { TStr Val = HttpRqUrlEnv->GetVal(KeyN, ValN); FldNmValPrV.Add(TStrKd(KeyNm, Val)); } } // report call if (ShowParamP) { GetNotify()->OnStatus(" " + HttpRq->GetUrl()->GetUrlStr()); } // request parsed well, from now on it's internal error ErrStatusCd = THttp::InternalErrStatusCd; // processed requested function if (!FunNm.Empty()) { // prepare request environment PSAppSrvRqEnv RqEnv = TSAppSrvRqEnv::New(this, SockId, HttpRq, FunNmToFunH); // retrieve function PSAppSrvFun SrvFun = FunNmToFunH.GetDat(FunNm); // call function SrvFun->Exec(FldNmValPrV, RqEnv); } else { // internal SAppSrv call if (!ListFunP) { // we are not allowed to list functions ErrStatusCd = THttp::ErrNotFoundStatusCd; TExcept::Throw("Unknown page"); } // prepare a list of registered functions PXmlTok TopTok = TXmlTok::New("registered-functions"); int KeyId = FunNmToFunH.FFirstKeyId(); while (FunNmToFunH.FNextKeyId(KeyId)) { PXmlTok FunTok = TXmlTok::New("function"); FunTok->AddArg("name", FunNmToFunH.GetKey(KeyId)); TopTok->AddSubTok(FunTok); } TStr ResXmlStr; TXmlDoc::New(TopTok)->SaveStr(ResXmlStr); PSIn BodySIn = TMIn::New(TSAppSrvFun::XmlHdStr + ResXmlStr); // prepare response PHttpResp HttpResp = THttpResp::New(THttp::OkStatusCd, THttp::TextXmlFldVal, false, BodySIn); // send response SendHttpResp(SockId, HttpResp); } } catch (PExcept Except) { // known internal error PXmlTok TopTok = TXmlTok::New("error"); TopTok->AddSubTok(TXmlTok::New("message", Except->GetMsgStr())); TopTok->AddSubTok(TXmlTok::New("location", Except->GetLocStr())); PXmlDoc ErrorXmlDoc = TXmlDoc::New(TopTok); TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr); // prepare response PHttpResp HttpResp = THttpResp::New(ErrStatusCd, THttp::TextHtmlFldVal, false, TMIn::New(TSAppSrvFun::XmlHdStr + ResXmlStr)); // send response SendHttpResp(SockId, HttpResp); } catch (...) { // unknown internal error PXmlDoc ErrorXmlDoc = TXmlDoc::New(TXmlTok::New("error")); TStr ResXmlStr; ErrorXmlDoc->SaveStr(ResXmlStr); // prepare response PHttpResp HttpResp = THttpResp::New(ErrStatusCd, THttp::TextHtmlFldVal, false, TMIn::New(TSAppSrvFun::XmlHdStr + ResXmlStr)); // send response SendHttpResp(SockId, HttpResp); } }
void TDzsHKwBs::SaveHtml(const PXmlTok& TopicTok, const TStr& FPath){ TStr NrFPath=TStr::GetNrFPath(FPath); TStr TitleStr=TopicTok->GetTagTok("title")->GetTokStr(false); TStr FPathSeg=GetFPathSegStr(TitleStr); printf("%s\n", TitleStr.CStr()); TDir::GenDir(NrFPath+FPathSeg); PSOut SOut=TFOut::New(NrFPath+FPathSeg+"/"+"default.htm"); SOut->PutStr("<html>"); SOut->PutDosLn(); SOut->PutDosLn(); // head SOut->PutStr("<head>"); SOut->PutDosLn(); SOut->PutStr("<title>"); SOut->PutStr(TitleStr); SOut->PutStr("</title>"); SOut->PutDosLn(); SOut->PutStr("</head>"); SOut->PutDosLn(); SOut->PutDosLn(); // body-start SOut->PutStr("<body>"); SOut->PutDosLn(); SOut->PutStr("<center><h3>"); SOut->PutStr(TitleStr); SOut->PutStr("</h3></center"); SOut->PutDosLn(); // subtrees SOut->PutStr("<ul>"); SOut->PutDosLn(); for (int SubTokN=0; SubTokN<TopicTok->GetSubToks(); SubTokN++){ PXmlTok CurTok=TopicTok->GetSubTok(SubTokN); if (CurTok->IsTag("topic")){ TStr SubTitleStr=CurTok->GetTagTok("title")->GetTokStr(false); TStr SubFPathSeg=GetFPathSegStr(SubTitleStr); SOut->PutStr("<li>"); SOut->PutStr(TStr("<a href=\"")+SubFPathSeg+"/\">"); SOut->PutStr("<b>"); SOut->PutStr(SubTitleStr); SOut->PutStr("</b>"); SOut->PutStr("</a>"); SOut->PutStr("</li>"); SOut->PutDosLn(); SaveHtml(CurTok, NrFPath+FPathSeg); } } SOut->PutStr("</ul>"); SOut->PutDosLn(); // references if (TopicTok->IsSubTag("refs")){ SOut->PutDosLn(); SOut->PutStr("<hr>"); SOut->PutDosLn(); SOut->PutDosLn(); PXmlTok RefsTok=TopicTok->GetTagTok("refs"); SOut->PutStr("<ul>"); SOut->PutDosLn(); for (int RefTokN=0; RefTokN<RefsTok->GetSubToks(); RefTokN++){ // get <ref> data PXmlTok RefTok=RefsTok->GetSubTok(RefTokN); TStr RefTitleStr=RefTok->GetTagTok("title")->GetTokStr(false); TStr RefUrlStr=RefTok->GetTagTok("url")->GetTokStr(false); // generate <li> SOut->PutStr("<li>"); SOut->PutStr(TStr("<a href=\"")+RefUrlStr+"\">"); SOut->PutStr(RefTitleStr); SOut->PutStr("</a>"); SOut->PutStr("</li>"); SOut->PutDosLn(); } SOut->PutStr("</ul>"); SOut->PutDosLn(); } // footer SOut->PutStr("<center>"); SOut->PutDosLn(); SOut->PutStr("<small>"); SOut->PutStr("Copyright DZS"); SOut->PutStr("</small>"); SOut->PutDosLn(); SOut->PutStr("</center>"); SOut->PutDosLn(); // body-end SOut->PutStr("</body>"); SOut->PutDosLn(); SOut->PutStr("</html>"); SOut->PutDosLn(); }
PDzsHKwBs TDzsHKwBs::LoadTxt(const TStr& FNm){ TSStack<PXmlTok> TokS; PXmlTok RootTok; PSIn SIn=TFIn::New(FNm); TILx Lx(SIn, TFSet()|iloTabSep); Lx.GetSym(syTab, syStr, syEoln, syEof); int PrevLev=-1; while (Lx.Sym!=syEof){ printf("."); int Lev=0; while (Lx.Sym==syTab){Lev++; Lx.GetSym(syTab, syStr);} IAssert(Lx.Sym==syStr); for (int ChN=0; ChN<Lx.Str.Len(); ChN++){ uchar Ch=Lx.Str[ChN]; if (Ch>127){printf("%c", Ch); Fail;}} // create topic PXmlTok TopicTok=TXmlTok::New(xsyTag, "topic"); //TopicTok->AddArg("title", Lx.Str); // topic title TStr TitleStr=TXmlLx::GetChRefFromYuEntRef(Lx.Str); PXmlTok TitleTok=TXmlTok::New(xsyTag, "title"); TopicTok->AddSubTok(TitleTok); PXmlTok TitleStrTok=TXmlTok::New(xsyStr, TitleStr); TitleTok->AddSubTok(TitleStrTok); // topic query TStr QueryStr=GetQueryStr(TitleStr); PXmlTok QueryTok=TXmlTok::New(xsyTag, "query"); TopicTok->AddSubTok(QueryTok); PXmlTok QueryStrTok=TXmlTok::New(xsyStr, QueryStr); QueryTok->AddSubTok(QueryStrTok); // insert topic into tree if (Lev==0){ IAssert(RootTok.Empty()); RootTok=TopicTok; TokS.Push(RootTok); } else if (PrevLev+1==Lev){ TokS.Top()->AddSubTok(TopicTok); TokS.Push(TopicTok); } else if (PrevLev==Lev){ TokS.Pop(); TokS.Top()->AddSubTok(TopicTok); TokS.Push(TopicTok); } else if (PrevLev>Lev){ int UpLev=PrevLev; while (UpLev>Lev){UpLev--; TokS.Pop();} TokS.Pop(); TokS.Top()->AddSubTok(TopicTok); TokS.Push(TopicTok); } else { Fail; } while (Lx.GetSym()!=syEoln){} Lx.GetSym(syTab, syStr, syEoln, syEof); PrevLev=Lev; //if (Lx.SymLnN>100){break;} } PXmlDoc XmlDoc=TXmlDoc::New(RootTok); PDzsHKwBs HKwBs=TDzsHKwBs::New(XmlDoc); return HKwBs; }
TStr GetStrArg(const PXmlTok& QueryXml, const TStr& ArgNm, const TStr& DfVal) { if (QueryXml.Empty()) return DfVal; return QueryXml->GetArgVal(ArgNm, DfVal); }
TInt GetIntArg(const PXmlTok& QueryXml, const TStr& ArgNm, int DfVal) { if (QueryXml.Empty()) return DfVal; return QueryXml->GetIntArgVal(ArgNm, DfVal); }
TStr GetStrArg(const PXmlTok& QueryXml, const TStr& TagPath, const TStr& ArgNm, const TStr& DfVal) { PXmlTok XmlTok = QueryXml->GetTagTok(TagPath); if (XmlTok.Empty()) return DfVal; return XmlTok->GetArgVal(ArgNm, DfVal); }