StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParsedKey.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // Boost
8 #include <boost/tokenizer.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <boost/date_time/gregorian/parsers.hpp>
11 // StdAir
18 #include <stdair/bom/ParsedKey.hpp>
20 
21 namespace stdair {
22 
23  // ////////////// Tokenising support ///////////////
27  typedef boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
28 
32  const boost::char_separator<char> TokeniserDashSeparator ("-");
33 
37  const boost::char_separator<char> TokeniserTimeSeparator (":");
38 
39  // ////////////////////////////////////////////////////////////////////
40  ParsedKey::ParsedKey() : _fullKey (""), _airlineCode (""), _flightNumber (""),
41  _departureDate (""), _boardingPoint (""),
42  _offPoint (""), _boardingTime ("") {
43  }
44 
45  // ////////////////////////////////////////////////////////////////////
47  }
48 
49  // ////////////////////////////////////////////////////////////////////
51  if (_airlineCode.size() < 2 || _airlineCode.size() > 3) {
52  STDAIR_LOG_ERROR ("No airline code can be found in '" << _fullKey << "'");
53  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
54  throw KeyNotFoundException ("No airline code can be found in '"
55  + _fullKey + "'");
56  }
57  return _airlineCode;
58  }
59 
60  // ////////////////////////////////////////////////////////////////////
62  // Check whether the departure date has been parsed correctly.
64 
65  if (lDateTokens.begin() == lDateTokens.end()) {
66  STDAIR_LOG_ERROR ("No date can be found in '" << _fullKey << "'");
67  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
68  throw KeyNotFoundException ("No date can be found in '" + _fullKey + "'");
69  }
70 
71  const FlightNumber_T lFlightNumber =
72  boost::lexical_cast<FlightNumber_T> (_flightNumber);
73 
74  const Date_T lDepartureDate =
75  boost::gregorian::from_simple_string (_departureDate);
76 
77  const FlightDateKey oFlightDateKey (lFlightNumber, lDepartureDate);
78 
79  return oFlightDateKey;
80  }
81 
82  // ////////////////////////////////////////////////////////////////////
84  if (_boardingPoint.size() != 3 || _offPoint.size() != 3) {
85  STDAIR_LOG_ERROR ("No airport code can be found in '" << _fullKey << "'");
86  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
87  throw KeyNotFoundException ("No airport code can be found in '"
88  + _fullKey + "'");
89  }
90 
91  const SegmentDateKey oSegmentDateKey (_boardingPoint, _offPoint);
92 
93  return oSegmentDateKey;
94  }
95 
96  // ////////////////////////////////////////////////////////////////////
98  // Check whether the boarding time has been parsed correctly.
100 
101  if (lTimeTokens.begin() == lTimeTokens.end()) {
102  STDAIR_LOG_ERROR ("No boarding time can be found in '" << _fullKey << "'");
103  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
104  throw KeyNotFoundException ("No boarding time can be found in '"
105  + _fullKey + "'");
106  }
107 
108  const Duration_T oBoardingTime (boost::posix_time::
109  duration_from_string (_boardingTime));
110 
111  return oBoardingTime;
112  }
113 
114  // ////////////////////////////////////////////////////////////////////
115  void ParsedKey::toStream (std::ostream& ioOut) const {
116  ioOut << "ParsedKey: " << toString();
117  }
118 
119  // ////////////////////////////////////////////////////////////////////
120  void ParsedKey::fromStream (std::istream& ioIn) {
121  }
122 
123  // ////////////////////////////////////////////////////////////////////
124  const std::string ParsedKey::toString() const {
125  std::ostringstream oStr;
126 
127  oStr << _airlineCode
128  << DEFAULT_KEY_FLD_DELIMITER << " "
129  << _flightNumber
131  << _departureDate
132  << DEFAULT_KEY_FLD_DELIMITER << " "
133  << _boardingPoint
135  << _offPoint
136  << DEFAULT_KEY_FLD_DELIMITER << " "
137  << _boardingTime;
138 
139  return oStr.str();
140  }
141 
142 }