$treeview $search $mathjax
00001 // ////////////////////////////////////////////////////////////////////// 00002 // Import section 00003 // ////////////////////////////////////////////////////////////////////// 00004 // STL 00005 #include <cassert> 00006 #include <sstream> 00007 #if BOOST_VERSION >= 104100 00008 // Boost Property Tree 00009 #include <boost/property_tree/ptree.hpp> 00010 #include <boost/property_tree/json_parser.hpp> 00011 #include <boost/regex.hpp> 00012 #endif // BOOST_VERSION >= 104100 00013 // StdAir 00014 #include <stdair/bom/BomJSONImport.hpp> 00015 #include <stdair/stdair_exceptions.hpp> 00016 #include <stdair/stdair_json.hpp> 00017 #include <stdair/basic/BasConst_General.hpp> 00018 #include <stdair/bom/ConfigHolderStruct.hpp> 00019 00020 #if BOOST_VERSION >= 104100 00021 namespace bpt = boost::property_tree; 00022 #else // BOOST_VERSION >= 104100 00023 namespace bpt { 00024 typedef char ptree; 00025 } 00026 #endif // BOOST_VERSION >= 104100 00027 00028 namespace stdair { 00029 00030 // //////////////////////////////////////////////////////////////////// 00031 bool BomJSONImport:: 00032 jsonImportCommand (const JSONString& iBomJSONStr, 00033 JSonCommand::EN_JSonCommand& ioEnumJSonCommand) { 00034 00035 bool hasCommandBeenSuccessfullyRetrieved = true; 00036 00037 try { 00046 const std::string lRegEx("^[{][[:space:]]*\"" 00047 "([[:alpha:]|_]*)\"[[:space:]]*:" 00048 "[[]?" 00049 "[[:space:]]*[{]?" 00050 "([[:alnum:]|[:punct:]|[:space:]]*)" 00051 "[}]?[]]?[}]"); 00052 00053 // See the caller for the regular expression 00054 boost::regex lExpression (lRegEx); 00055 00056 const std::string& lBomJSONStr = iBomJSONStr.getString(); 00057 std::string::const_iterator itStart = lBomJSONStr.begin(); 00058 std::string::const_iterator itEnd = lBomJSONStr.end(); 00059 00060 boost::match_results<std::string::const_iterator> lWhat; 00061 boost::match_flag_type lFlags = boost::match_default; 00062 00063 regex_search (itStart, itEnd, lWhat, lExpression, lFlags); 00064 00065 // Put the matched strings in the list of tokens to be returned back 00066 // to the caller 00067 std::vector<std::string> oTokenList; 00068 for (boost::match_results<std::string::const_iterator>::const_iterator itMatch 00069 = lWhat.begin(); itMatch != lWhat.end(); ++itMatch) { 00070 00071 const std::string lMatchedString (std::string (itMatch->first, 00072 itMatch->second)); 00073 oTokenList.push_back (lMatchedString); 00074 } 00075 00076 // If the retrieved token list is empty, the command has not been 00077 // retrieved 00078 if (oTokenList.size() <= 1) { 00079 hasCommandBeenSuccessfullyRetrieved = false; 00080 return hasCommandBeenSuccessfullyRetrieved; 00081 } 00082 00083 assert (oTokenList.size() >= 2); 00084 // Retrieved the command string into the token list 00085 const std::string lCommandStr = oTokenList.at(1); 00086 const JSonCommand lJSonCommand (lCommandStr); 00087 ioEnumJSonCommand = lJSonCommand.getCommand(); 00088 00089 } catch (stdair::CodeConversionException& ccException) { 00090 hasCommandBeenSuccessfullyRetrieved = false; 00091 } 00092 00093 return hasCommandBeenSuccessfullyRetrieved; 00094 00095 } 00096 00097 // //////////////////////////////////////////////////////////////////// 00098 bool BomJSONImport::jsonImportInventoryKey (const JSONString& iBomJSONStr, 00099 AirlineCode_T& ioAirlineCode) { 00100 bool hasKeyBeenSuccessfullyRetrieved = true; 00101 00102 #if BOOST_VERSION >= 104100 00103 // Create an empty property tree object 00104 bpt::ptree pt; 00105 00106 try { 00107 00108 // Load the JSON formatted string into the property tree. 00109 // If reading fails (cannot open stream, parse error), an 00110 // exception is thrown. 00111 std::istringstream iStr (iBomJSONStr.getString()); 00112 read_json (iStr, pt); 00113 00114 // Build the right path to obtain the airline code value. 00115 bpt::ptree::const_iterator itBegin = pt.begin(); 00116 const std::string lCommandName = itBegin->first; 00117 std::ostringstream lPath; 00118 lPath << lCommandName << ".airline_code"; 00119 00120 // Get the airline_code. 00121 // If the path key is not found, an exception is thrown. 00122 ioAirlineCode = pt.get<AirlineCode_T> (lPath.str()); 00123 00124 } catch (bpt::ptree_error& bptException) { 00125 hasKeyBeenSuccessfullyRetrieved = false; 00126 } 00127 00128 #endif // BOOST_VERSION >= 104100 00129 return hasKeyBeenSuccessfullyRetrieved; 00130 } 00131 00132 // //////////////////////////////////////////////////////////////////// 00133 bool BomJSONImport::jsonImportFlightDate (const JSONString& iBomJSONStr, 00134 Date_T& ioDepartureDate) { 00135 bool hasKeyBeenSuccessfullyRetrieved = true; 00136 00137 #if BOOST_VERSION >= 104100 00138 // Create an empty property tree object 00139 bpt::ptree pt; 00140 00141 try { 00142 00143 // Load the JSON formatted string into the property tree. 00144 // If reading fails (cannot open stream, parse error), an 00145 // exception is thrown. 00146 std::istringstream iStr (iBomJSONStr.getString()); 00147 read_json (iStr, pt); 00148 00149 // Build the right path to obtain the departure date value. 00150 const std::string& lDepartureDateStr = 00151 pt.get<std::string> ("flight_date.departure_date"); 00152 00153 // Get the departure_date. 00154 // If the path key is not found, an exception is thrown. 00155 ioDepartureDate = 00156 boost::gregorian::from_simple_string (lDepartureDateStr); 00157 00158 } catch (bpt::ptree_error& bptException) { 00159 hasKeyBeenSuccessfullyRetrieved = false; 00160 } 00161 #endif // BOOST_VERSION >= 104100 00162 00163 return hasKeyBeenSuccessfullyRetrieved; 00164 } 00165 00166 // //////////////////////////////////////////////////////////////////// 00167 bool BomJSONImport::jsonImportFlightNumber (const JSONString& iBomJSONStr, 00168 FlightNumber_T& ioFlightNumber) { 00169 00170 bool hasKeyBeenSuccessfullyRetrieved = true; 00171 00172 #if BOOST_VERSION >= 104100 00173 // Create an empty property tree object 00174 bpt::ptree pt; 00175 00176 try { 00177 00178 // Load the JSON formatted string into the property tree. 00179 // If reading fails (cannot open stream, parse error), an 00180 // exception is thrown. 00181 std::istringstream iStr (iBomJSONStr.getString()); 00182 read_json (iStr, pt); 00183 00184 // Build the right path to obtain the flight number value. 00185 bpt::ptree::const_iterator itBegin = pt.begin(); 00186 const std::string lCommandName = itBegin->first; 00187 std::ostringstream lPath; 00188 lPath << lCommandName << ".flight_number"; 00189 00190 // Get the flight_number. 00191 // If the path key is not found, an exception is thrown. 00192 ioFlightNumber = pt.get<FlightNumber_T> (lPath.str()); 00193 00194 } catch (bpt::ptree_error& bptException) { 00195 hasKeyBeenSuccessfullyRetrieved = false; 00196 } 00197 #endif // BOOST_VERSION >= 104100 00198 00199 return hasKeyBeenSuccessfullyRetrieved; 00200 } 00201 00202 // //////////////////////////////////////////////////////////////////// 00203 bool BomJSONImport::jsonImportBreakPoints (const JSONString& iBomJSONStr, 00204 BreakPointList_T& oBreakPointList) { 00205 00206 bool hasKeyBeenSuccessfullyRetrieved = true; 00207 00208 #if BOOST_VERSION >= 104100 00209 // Create an empty property tree object 00210 bpt::ptree pt; 00211 00212 try { 00213 00214 // Load the JSON formatted string into the property tree. 00215 // If reading fails (cannot open stream, parse error), an 00216 // exception is thrown. 00217 std::istringstream iStr (iBomJSONStr.getString()); 00218 read_json (iStr, pt); 00219 00220 // Access the break point list tree 00221 bpt::ptree::const_iterator itBegin = pt.begin(); 00222 bpt::ptree ptListOfBP = itBegin->second; 00223 // Browse the break point list 00224 for (bpt::ptree::const_iterator itBP = ptListOfBP.begin(); 00225 itBP != ptListOfBP.end(); ++itBP) { 00226 // Access the current break point tree 00227 bpt::ptree ptBP = itBP->second; 00228 // Access the date of the break point 00229 bpt::ptree::const_iterator itDate = ptBP.begin(); 00230 bpt::ptree ptDate = itDate->second; 00231 // Recover the string containing the date 00232 std::string lDateString = ptDate.data(); 00233 if (lDateString.empty() == false) { 00234 // Construct the break point using the recovered string 00235 const Date_T lDate = 00236 boost::gregorian::from_simple_string (lDateString); 00237 BreakPointStruct lBreakPoint (lDate); 00238 // Add the break point to the list 00239 oBreakPointList.push_back (lBreakPoint); 00240 } 00241 } 00242 } catch (bpt::ptree_error& bptException) { 00243 hasKeyBeenSuccessfullyRetrieved = false; 00244 } catch (boost::bad_lexical_cast& eCast) { 00245 hasKeyBeenSuccessfullyRetrieved = false; 00246 } 00247 #endif // BOOST_VERSION >= 104100 00248 00249 return hasKeyBeenSuccessfullyRetrieved; 00250 } 00251 00252 // //////////////////////////////////////////////////////////////////// 00253 bool BomJSONImport::jsonImportEventType (const JSONString& iBomJSONStr, 00254 EventType::EN_EventType& ioEventType) { 00255 00256 bool hasKeyBeenSuccessfullyRetrieved = true; 00257 00258 #if BOOST_VERSION >= 104100 00259 // Create an empty property tree object 00260 bpt::ptree pt; 00261 00262 try { 00263 00264 // Load the JSON formatted string into the property tree. 00265 // If reading fails (cannot open stream, parse error), an 00266 // exception is thrown. 00267 std::istringstream iStr (iBomJSONStr.getString()); 00268 read_json (iStr, pt); 00269 00270 // Build the right path to obtain the event type value. 00271 bpt::ptree::const_iterator itBegin = pt.begin(); 00272 const std::string lEventTypeName = itBegin->first; 00273 std::ostringstream lPath; 00274 lPath << lEventTypeName << ".event_type"; 00275 00276 // Get the event type string 00277 // If the path key is not found, an exception bpt::ptree_error is thrown. 00278 const std::string lEventTypeStr = pt.get<std::string> (lPath.str()); 00279 // Build the event type using the string. 00280 // If the input string is incorrect, an exception 00281 // stdair::CodeConversionException is thrown. 00282 const EventType lEventType (lEventTypeStr); 00283 ioEventType = lEventType.getType(); 00284 00285 } catch (bpt::ptree_error& bptException) { 00286 hasKeyBeenSuccessfullyRetrieved = false; 00287 } catch (stdair::CodeConversionException& cceException) { 00288 hasKeyBeenSuccessfullyRetrieved = false; 00289 } 00290 #endif // BOOST_VERSION >= 104100 00291 00292 return hasKeyBeenSuccessfullyRetrieved; 00293 } 00294 00295 // //////////////////////////////////////////////////////////////////// 00296 bool BomJSONImport::jsonImportConfig (const JSONString& iBomJSONStr, 00297 ConfigHolderStruct& iConfigHolderStruct) { 00298 00299 bool hasConfigBeenSuccessfullyRetrieved = true; 00300 00301 #if BOOST_VERSION >= 104100 00302 // Create an empty property tree object 00303 bpt::ptree pt; 00304 00305 try { 00306 00307 // Load the JSON formatted string into the property tree. 00308 // If reading fails (cannot open stream, parse error), an 00309 // exception is thrown. 00310 std::istringstream iStr (iBomJSONStr.getString()); 00311 read_json (iStr, pt); 00312 00313 // Load the pt in the configuration holder 00314 iConfigHolderStruct.add (pt); 00315 } catch (bpt::ptree_error& bptException) { 00316 hasConfigBeenSuccessfullyRetrieved = false; 00317 } 00318 #endif // BOOST_VERSION >= 104100 00319 00320 return hasConfigBeenSuccessfullyRetrieved; 00321 } 00322 00323 }