StdAir Logo  1.00.13
C++ Standard Airline IT Object Library
BomJSONImport.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <string>
7 #include <sstream>
8 #if BOOST_VERSION_MACRO >= 104100
9 // Boost Property Tree
10 #include <boost/property_tree/ptree.hpp>
11 #include <boost/property_tree/json_parser.hpp>
12 #endif // BOOST_VERSION_MACRO >= 104100
13 #include <boost/regex.hpp>
14 // StdAir
17 #include <stdair/stdair_json.hpp>
20 
21 #if BOOST_VERSION_MACRO >= 104100
22 namespace bpt = boost::property_tree;
23 #else // BOOST_VERSION_MACRO >= 104100
24 namespace boost {
25  namespace property_tree {
26  typedef std::string ptree;
27  }
28 }
29 #endif // BOOST_VERSION_MACRO >= 104100
30 
31 namespace stdair {
32 
33  // ////////////////////////////////////////////////////////////////////
35  jsonImportCommand (const JSONString& iBomJSONStr,
36  JSonCommand::EN_JSonCommand& ioEnumJSonCommand) {
37 
38  bool hasCommandBeenSuccessfullyRetrieved = true;
39 
40  try {
49  const std::string lRegEx("^[{][[:space:]]*\""
50  "([[:alpha:]|_]*)\"[[:space:]]*:"
51  "[[]?"
52  "[[:space:]]*[{]?"
53  "([[:alnum:]|[:punct:]|[:space:]]*)"
54  "[}]?[]]?[}]");
55 
56  // See the caller for the regular expression
57  boost::regex lExpression (lRegEx);
58 
59  const std::string& lBomJSONStr = iBomJSONStr.getString();
60  std::string::const_iterator itStart = lBomJSONStr.begin();
61  std::string::const_iterator itEnd = lBomJSONStr.end();
62 
63  boost::match_results<std::string::const_iterator> lWhat;
64  boost::match_flag_type lFlags = boost::match_default;
65 
66  regex_search (itStart, itEnd, lWhat, lExpression, lFlags);
67 
68  // Put the matched strings in the list of tokens to be returned back
69  // to the caller
70  std::vector<std::string> oTokenList;
71  for (boost::match_results<std::string::const_iterator>::const_iterator itMatch
72  = lWhat.begin(); itMatch != lWhat.end(); ++itMatch) {
73 
74  const std::string lMatchedString (std::string (itMatch->first,
75  itMatch->second));
76  oTokenList.push_back (lMatchedString);
77  }
78 
79  // If the retrieved token list is empty, the command has not been
80  // retrieved
81  if (oTokenList.size() <= 1) {
82  hasCommandBeenSuccessfullyRetrieved = false;
83  return hasCommandBeenSuccessfullyRetrieved;
84  }
85 
86  assert (oTokenList.size() >= 2);
87  // Retrieved the command string into the token list
88  const std::string lCommandStr = oTokenList.at(1);
89  const JSonCommand lJSonCommand (lCommandStr);
90  ioEnumJSonCommand = lJSonCommand.getCommand();
91 
92  } catch (stdair::CodeConversionException& ccException) {
93  hasCommandBeenSuccessfullyRetrieved = false;
94  }
95 
96  return hasCommandBeenSuccessfullyRetrieved;
97 
98  }
99 
100  // ////////////////////////////////////////////////////////////////////
101  bool BomJSONImport::jsonImportInventoryKey (const JSONString& iBomJSONStr,
102  AirlineCode_T& ioAirlineCode) {
103  bool hasKeyBeenSuccessfullyRetrieved = true;
104 
105 #if BOOST_VERSION_MACRO >= 104100
106  // Create an empty property tree object
107  bpt::ptree pt;
108 
109  try {
110 
111  // Load the JSON formatted string into the property tree.
112  // If reading fails (cannot open stream, parse error), an
113  // exception is thrown.
114  std::istringstream iStr (iBomJSONStr.getString());
115  read_json (iStr, pt);
116 
117  // Build the right path to obtain the airline code value.
118  bpt::ptree::const_iterator itBegin = pt.begin();
119  const std::string lCommandName = itBegin->first;
120  std::ostringstream lPath;
121  lPath << lCommandName << ".airline_code";
122 
123  // Get the airline_code.
124  // If the path key is not found, an exception is thrown.
125  ioAirlineCode = pt.get<AirlineCode_T> (lPath.str());
126 
127  } catch (bpt::ptree_error& bptException) {
128  hasKeyBeenSuccessfullyRetrieved = false;
129  }
130 
131 #endif // BOOST_VERSION_MACRO >= 104100
132  return hasKeyBeenSuccessfullyRetrieved;
133  }
134 
135  // ////////////////////////////////////////////////////////////////////
136  bool BomJSONImport::jsonImportFlightDate (const JSONString& iBomJSONStr,
137  Date_T& ioDepartureDate) {
138  bool hasKeyBeenSuccessfullyRetrieved = true;
139 
140 #if BOOST_VERSION_MACRO >= 104100
141  // Create an empty property tree object
142  bpt::ptree pt;
143 
144  try {
145 
146  // Load the JSON formatted string into the property tree.
147  // If reading fails (cannot open stream, parse error), an
148  // exception is thrown.
149  std::istringstream iStr (iBomJSONStr.getString());
150  read_json (iStr, pt);
151 
152  // Build the right path to obtain the departure date value.
153  const std::string& lDepartureDateStr =
154  pt.get<std::string> ("flight_date.departure_date");
155 
156  // Get the departure_date.
157  // If the path key is not found, an exception is thrown.
158  ioDepartureDate =
159  boost::gregorian::from_simple_string (lDepartureDateStr);
160 
161  } catch (bpt::ptree_error& bptException) {
162  hasKeyBeenSuccessfullyRetrieved = false;
163  }
164 #endif // BOOST_VERSION_MACRO >= 104100
165 
166  return hasKeyBeenSuccessfullyRetrieved;
167  }
168 
169  // ////////////////////////////////////////////////////////////////////
170  bool BomJSONImport::jsonImportFlightNumber (const JSONString& iBomJSONStr,
171  FlightNumber_T& ioFlightNumber) {
172 
173  bool hasKeyBeenSuccessfullyRetrieved = true;
174 
175 #if BOOST_VERSION_MACRO >= 104100
176  // Create an empty property tree object
177  bpt::ptree pt;
178 
179  try {
180 
181  // Load the JSON formatted string into the property tree.
182  // If reading fails (cannot open stream, parse error), an
183  // exception is thrown.
184  std::istringstream iStr (iBomJSONStr.getString());
185  read_json (iStr, pt);
186 
187  // Build the right path to obtain the flight number value.
188  bpt::ptree::const_iterator itBegin = pt.begin();
189  const std::string lCommandName = itBegin->first;
190  std::ostringstream lPath;
191  lPath << lCommandName << ".flight_number";
192 
193  // Get the flight_number.
194  // If the path key is not found, an exception is thrown.
195  ioFlightNumber = pt.get<FlightNumber_T> (lPath.str());
196 
197  } catch (bpt::ptree_error& bptException) {
198  hasKeyBeenSuccessfullyRetrieved = false;
199  }
200 #endif // BOOST_VERSION_MACRO >= 104100
201 
202  return hasKeyBeenSuccessfullyRetrieved;
203  }
204 
205  // ////////////////////////////////////////////////////////////////////
206  bool BomJSONImport::jsonImportBreakPoints (const JSONString& iBomJSONStr,
207  BreakPointList_T& oBreakPointList) {
208 
209  bool hasKeyBeenSuccessfullyRetrieved = true;
210 
211 #if BOOST_VERSION_MACRO >= 104100
212  // Create an empty property tree object
213  bpt::ptree pt;
214 
215  try {
216 
217  // Load the JSON formatted string into the property tree.
218  // If reading fails (cannot open stream, parse error), an
219  // exception is thrown.
220  std::istringstream iStr (iBomJSONStr.getString());
221  read_json (iStr, pt);
222 
223  // Access the break point list tree
224  bpt::ptree::const_iterator itBegin = pt.begin();
225  bpt::ptree ptListOfBP = itBegin->second;
226  // Browse the break point list
227  for (bpt::ptree::const_iterator itBP = ptListOfBP.begin();
228  itBP != ptListOfBP.end(); ++itBP) {
229  // Access the current break point tree
230  bpt::ptree ptBP = itBP->second;
231  // Access the date of the break point
232  bpt::ptree::const_iterator itDate = ptBP.begin();
233  bpt::ptree ptDate = itDate->second;
234  // Recover the string containing the date
235  std::string lDateString = ptDate.data();
236  if (lDateString.empty() == false) {
237  // Construct the break point using the recovered string
238  const Date_T lDate =
239  boost::gregorian::from_simple_string (lDateString);
240  BreakPointStruct lBreakPoint (lDate);
241  // Add the break point to the list
242  oBreakPointList.push_back (lBreakPoint);
243  }
244  }
245  } catch (bpt::ptree_error& bptException) {
246  hasKeyBeenSuccessfullyRetrieved = false;
247  } catch (boost::bad_lexical_cast& eCast) {
248  hasKeyBeenSuccessfullyRetrieved = false;
249  }
250 #endif // BOOST_VERSION_MACRO >= 104100
251 
252  return hasKeyBeenSuccessfullyRetrieved;
253  }
254 
255  // ////////////////////////////////////////////////////////////////////
256  bool BomJSONImport::jsonImportEventType (const JSONString& iBomJSONStr,
257  EventType::EN_EventType& ioEventType) {
258 
259  bool hasKeyBeenSuccessfullyRetrieved = true;
260 
261 #if BOOST_VERSION_MACRO >= 104100
262  // Create an empty property tree object
263  bpt::ptree pt;
264 
265  try {
266 
267  // Load the JSON formatted string into the property tree.
268  // If reading fails (cannot open stream, parse error), an
269  // exception is thrown.
270  std::istringstream iStr (iBomJSONStr.getString());
271  read_json (iStr, pt);
272 
273  // Build the right path to obtain the event type value.
274  bpt::ptree::const_iterator itBegin = pt.begin();
275  const std::string lEventTypeName = itBegin->first;
276  std::ostringstream lPath;
277  lPath << lEventTypeName << ".event_type";
278 
279  // Get the event type string
280  // If the path key is not found, an exception bpt::ptree_error is thrown.
281  const std::string lEventTypeStr = pt.get<std::string> (lPath.str());
282  // Build the event type using the string.
283  // If the input string is incorrect, an exception
284  // stdair::CodeConversionException is thrown.
285  const EventType lEventType (lEventTypeStr);
286  ioEventType = lEventType.getType();
287 
288  } catch (bpt::ptree_error& bptException) {
289  hasKeyBeenSuccessfullyRetrieved = false;
290  } catch (stdair::CodeConversionException& cceException) {
291  hasKeyBeenSuccessfullyRetrieved = false;
292  }
293 #endif // BOOST_VERSION_MACRO >= 104100
294 
295  return hasKeyBeenSuccessfullyRetrieved;
296  }
297 
298  // ////////////////////////////////////////////////////////////////////
299  bool BomJSONImport::jsonImportConfig (const JSONString& iBomJSONStr,
300  ConfigHolderStruct& iConfigHolderStruct) {
301 
302  bool hasConfigBeenSuccessfullyRetrieved = true;
303 
304 #if BOOST_VERSION_MACRO >= 104100
305  // Create an empty property tree object
306  bpt::ptree pt;
307 
308  try {
309 
310  // Load the JSON formatted string into the property tree.
311  // If reading fails (cannot open stream, parse error), an
312  // exception is thrown.
313  std::istringstream iStr (iBomJSONStr.getString());
314  read_json (iStr, pt);
315 
316  // Load the pt in the configuration holder
317  iConfigHolderStruct.add (pt);
318  } catch (bpt::ptree_error& bptException) {
319  hasConfigBeenSuccessfullyRetrieved = false;
320  }
321 #endif // BOOST_VERSION_MACRO >= 104100
322 
323  return hasConfigBeenSuccessfullyRetrieved;
324  }
325 
326 }
Handle on the StdAir library context.
boost::gregorian::date Date_T
std::list< BreakPointStruct > BreakPointList_T
unsigned short FlightNumber_T
std::string AirlineCode_T
Forward declarations.
char ptree
Enumeration of json commands.
Definition: JSonCommand.hpp:17
static EN_JSonCommand getCommand(const std::string &iCommandStr)
Definition: JSonCommand.cpp:31
static bool jsonImportConfig(const JSONString &, ConfigHolderStruct &)
static bool jsonImportBreakPoints(const JSONString &, BreakPointList_T &)
static bool jsonImportCommand(const JSONString &, JSonCommand::EN_JSonCommand &)
static bool jsonImportInventoryKey(const JSONString &, AirlineCode_T &)
static bool jsonImportFlightNumber(const JSONString &, FlightNumber_T &)
static bool jsonImportFlightDate(const JSONString &, Date_T &)
static bool jsonImportEventType(const JSONString &, EventType::EN_EventType &)
JSON-formatted string.
Definition: stdair_json.hpp:16
const std::string & getString() const
Definition: stdair_json.hpp:36