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