highlighter application  1.1
HCE project utils : highlighter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
HighlightJsonWrapper.cpp
Go to the documentation of this file.
1 
12 #include <Poco/JSON/JSONException.h>
13 #include <Poco/JSON/Stringifier.h>
14 
16 #include "HighlightJsonWrapper.hpp"
17 #include "SearchStringParser.hpp"
18 
19 const std::string HighlightJsonWrapper::QUERY_NAME = "q";
20 const std::string HighlightJsonWrapper::CONTENTS_NAME = "contents";
21 const std::string HighlightJsonWrapper::PARAMETERS_NAME = "parameters";
22 const std::string HighlightJsonWrapper::RETURN_HASH_NAME = "return_hash";
23 
24 const std::string HighlightJsonWrapper::PARAM_BRGIN_MARKER_NAME = "begin_marker";
25 const std::string HighlightJsonWrapper::PARAM_END_MARKER_NAME = "end_marker";
26 const std::string HighlightJsonWrapper::PARAM_MAX_NUMBER_NAME = "max_number";
27 const std::string HighlightJsonWrapper::PARAM_DELIMITERS_NAME = "delimiters";
28 const std::string HighlightJsonWrapper::PARAM_ACRONYMS_NAME = "acronyms";
29 const std::string HighlightJsonWrapper::PARAM_NUMBERS_AS_WORDS_NAME = "numbers_as_words";
30 const std::string HighlightJsonWrapper::PARAM_USE_SINGLE_CHARS_NAME = "use_single_chars";
31 const std::string HighlightJsonWrapper::PARAM_TIMEOUT = "timeout";
32 //----------------------- OutPut Params names --------------------------------------------------------------------------------------
33 const std::string HighlightJsonWrapper::PARAM_ERROR_CODE_NAME = "error_code";
34 const std::string HighlightJsonWrapper::PARAM_ERROR_MSG_NAME = "error_msg";
35 const std::string HighlightJsonWrapper::PARAM_CONTENTS_NAME = "contents";
36 const std::string HighlightJsonWrapper::PARAM_HIGHLIGHTTED_NAME = "highlighted";
37 const std::string HighlightJsonWrapper::PARAM_FOUND_NAME = "found";
38 const std::string HighlightJsonWrapper::PARAM_TOTAL_TIME_NAME = "time";
39 
40 using namespace HCE::exception;
41 
42 Poco::Dynamic::Var HighlightJsonWrapper::extractParam(const std::string& paramName, Poco::JSON::Object::Ptr parametersPtr)
43 {
44  std::vector<Poco::Dynamic::Var> varPath;
45  Poco::Dynamic::Var ret;
46  std::string errStr;
47 
48  varPath.push_back(Poco::Dynamic::Var(paramName));
49  ret = jsonObjExtractor.getAsVar(parametersPtr, varPath);
50  if(ret.isEmpty())
51  {
52  errStr = paramName;
53  errStr += " PARAM NOT FOUND";
54  throw HighlightJsonException(errStr);
55  }
56  return ret;
57 }
58 
59 void HighlightJsonWrapper::parseParams(Poco::SharedPtr<InDataStruct> inDataStructPtr, Poco::JSON::Object::Ptr parametersPtr)
60 {
62  Poco::Dynamic::Var localVar;
63 
64  localVar = extractParam(PARAM_BRGIN_MARKER_NAME, parametersPtr);
65  inDataParams.beginMarker = localVar.extract<std::string>();
66 
67  localVar = extractParam(PARAM_END_MARKER_NAME, parametersPtr);
68  inDataParams.endMarker = localVar.extract<std::string>();
69 
70  localVar = extractParam(PARAM_MAX_NUMBER_NAME, parametersPtr);
71  inDataParams.maxNumber = localVar.extract<int>();
72 
73  localVar = extractParam(PARAM_DELIMITERS_NAME, parametersPtr);
74  inDataParams.delimiters = localVar.extract<std::string>();
75 
76  localVar = extractParam(PARAM_ACRONYMS_NAME, parametersPtr);
77  inDataParams.acronyms = localVar.extract<std::string>();
78 
79  localVar = extractParam(PARAM_NUMBERS_AS_WORDS_NAME, parametersPtr);
80  inDataParams.numbersAsWords = localVar.extract<bool>();
81 
82  localVar = extractParam(PARAM_USE_SINGLE_CHARS_NAME, parametersPtr);
83  inDataParams.useSingleChars = localVar.extract<bool>();
84 
85  localVar = extractParam(PARAM_TIMEOUT, parametersPtr);
86  inDataParams.timeout = localVar.extract<int>();
87 
88  inDataStructPtr->addParams(inDataParams);
89 }
90 
91 Poco::SharedPtr<InDataStruct> HighlightJsonWrapper::inDataFromJsomString(const std::string &jsonString)
92 {
93  Poco::JSON::Parser parser;
94  Poco::SharedPtr<InDataStruct> ret = nullptr;
95  Poco::JSON::Object::Ptr obj = nullptr;
96  std::string localQueryStr;
97  Poco::Dynamic::Var result;
98  try
99  {
100  result = parser.parse(jsonString);
101  }
102  catch(Poco::JSON::JSONException &excp)
103  {
104  throw HighlightJsonException("PARSE JSON EXCEPTION --- " + excp.message());
105  }
106  catch(Poco::SyntaxException &excp)
107  {
108  throw HighlightJsonException("PARSE JSON EXCEPTION --- " + excp.message());
109  }
110  std::string errStr;
111  if(result.type() == typeid(Poco::JSON::Object::Ptr))
112  {
113  try
114  {
115  obj = result.extract<Poco::JSON::Object::Ptr>();
116  }
117  catch(Poco::BadCastException &excp)
118  {
119  throw HighlightJsonException("CANT'T CAST JSON RESULT TO THE [Poco::JSON::Object::Ptr]");
120  }
121  }
122  if(obj)
123  {
124  std::vector<Poco::Dynamic::Var> varPath;
125  varPath.push_back(Poco::Dynamic::Var(QUERY_NAME));
126  Poco::Dynamic::Var queryStr = jsonObjExtractor.getAsVar(obj, varPath);
127  if(queryStr.isEmpty())
128  {
129  errStr = "EMPTY OR NOT FOUND [";
130  errStr += QUERY_NAME;
131  errStr += "] PARAM";
132  throw HighlightJsonException(errStr);
133  }
134 
135  varPath.clear();
136  varPath.push_back(Poco::Dynamic::Var(CONTENTS_NAME));
137  Poco::JSON::Array::Ptr contentArrayPtr = jsonObjExtractor.getAsArray(obj, varPath);
138  if(contentArrayPtr.isNull())
139  {
140  errStr = "EMPTY OR NOT FOUND, OR TYPE NOT ARRAY [";
141  errStr += CONTENTS_NAME;
142  errStr += "] PARAM";
143  throw HighlightJsonException(errStr);
144  }
145 
146  varPath.clear();
147  varPath.push_back(Poco::Dynamic::Var(PARAMETERS_NAME));
148  Poco::JSON::Object::Ptr parametersPtr = jsonObjExtractor.getAsObject(obj, varPath);
149  if(parametersPtr.isNull())
150  {
151  errStr = "EMPTY OR NOT FOUND, OR TYPE NOT OBJECT [";
152  errStr += PARAMETERS_NAME;
153  errStr += "] PARAM";
154  throw HighlightJsonException(errStr);
155  }
156 
157  varPath.clear();
158  varPath.push_back(Poco::Dynamic::Var(RETURN_HASH_NAME));
159  Poco::Dynamic::Var returnHashVar = jsonObjExtractor.getAsVar(obj, varPath);
160  if(returnHashVar.isEmpty())
161  {
162  errStr = "EMPTY OR NOT FOUND[";
163  errStr += RETURN_HASH_NAME;
164  errStr += "] PARAM";
165  throw HighlightJsonException(errStr);
166  }
167 
168  ret = Poco::SharedPtr<InDataStruct>(new InDataStruct());
169  ret->setSendCacheBack(returnHashVar.extract<bool>());
170  try
171  {
172  parseParams(ret, parametersPtr);
173  }
174  catch(Poco::BadCastException &excp)
175  {
176  errStr = "CAN'T CAST SOME PARAM'S TYPE, CHECK THEIR TYPES";
177  throw HighlightJsonException(errStr);
178  }
179 
180  Poco::JSON::Array::Ptr arrayVal;
181  Poco::Dynamic::Var arraySubVal;
182  for(unsigned int i = 0; i < contentArrayPtr->size(); i++)
183  {
184  arrayVal = contentArrayPtr->getArray(i);
185  if(!arrayVal.isNull())
186  {
187  try
188  {
189  arraySubVal = arrayVal->get(0);
190  const std::string &constContentStr = arraySubVal.extract<std::string>();
191  ret->addContent(const_cast<std::string &>(constContentStr));
192  arraySubVal = arrayVal->get(1);
193  const std::string &constWcacheStr = arraySubVal.extract<std::string>();
194  ret->addWcache(const_cast<std::string &>(constWcacheStr));
195  }
196  catch(Poco::BadCastException &excp)
197  {
198  errStr = "CAN'T CAST CONTENT SUB_ARRAY ELEM [";
199  errStr += std::to_string(i);
200  errStr += "] TO STRING";
201  throw HighlightJsonException(errStr);
202  }
203  }
204  else
205  {
206  errStr = "CAN'T CAST CONTENT ARRAY ELEM [";
207  errStr += std::to_string(i);
208  errStr += "] TO STRING";
209  throw HighlightJsonException(errStr);
210  }
211  }
212  try
213  {
214  localQueryStr = queryStr.extract<std::string>();
215  }
216  catch(Poco::BadCastException &excp)
217  {
218  throw HighlightJsonException("CAN'T CAST QUERY TO STRING");
219  }
220  std::vector<std::vector<std::pair<SearchType, std::string> > > searchStringsVector = SearchStringParser::parse(localQueryStr, SEARCH_STRING_SEPARATOR);
221  if(searchStringsVector.size() == 0)
222  {
223  throw HighlightJsonException("CAN'T EXTRACT SEARCH STRINGS FROM QUERY STRING");
224  }
225  ret->setSearchStrings(searchStringsVector);
226  }
227  return ret;
228 }
229 
230 void HighlightJsonWrapper::jsonStringFromOutData(Poco::SharedPtr<OutDataStruct> outDataStructPtr, std::string &outJSONString, Poco::Timestamp::TimeDiff tDiff)
231 {
232  Poco::Dynamic::Var var;
233  Poco::Dynamic::Var val;
234  Poco::JSON::Array::Ptr contentArray = Poco::JSON::Array::Ptr(new Poco::JSON::Array());
235  Poco::JSON::Array::Ptr contentSubArray = nullptr;
236  Poco::JSON::Object::Ptr obj = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
237  val = outDataStructPtr->getErrCode();
238  obj->set(PARAM_ERROR_CODE_NAME, val);
239  val = outDataStructPtr->getErrorMsg();
240  obj->set(PARAM_ERROR_MSG_NAME, val);
241  val = outDataStructPtr->getHighlightedCounter();
242  obj->set(PARAM_HIGHLIGHTTED_NAME, val);
243  val = outDataStructPtr->getFoundCounter();
244  obj->set(PARAM_FOUND_NAME, val);
245  obj->set(PARAM_TOTAL_TIME_NAME, static_cast<unsigned int>(tDiff));
246 
247  for(unsigned int i = 0; i < outDataStructPtr->getContentsCount(); i++)
248  {
249  contentSubArray = Poco::JSON::Array::Ptr(new Poco::JSON::Array());
250  val = outDataStructPtr->getContent(i);
251  contentSubArray->add(val);
252  if(i < outDataStructPtr->getWcachesCount())
253  {
254  val = outDataStructPtr->getWcache(i);
255  }
256  else
257  {
258  val = std::string("");
259  }
260  contentSubArray->add(val);
261  contentArray->add(contentSubArray);
262  contentSubArray = nullptr;
263  }
264 
265  obj->set(PARAM_CONTENTS_NAME, contentArray);
266  var = obj;
267  std::ostringstream outJSON;
268  Poco::JSON::Stringifier::stringify(var, outJSON, 1);
269  outJSONString = outJSON.str();
270 }