StdAir Logo  1.00.3
C++ Standard Airline IT Object Library
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
19 #include <stdair/bom/ParsedKey.hpp>
21 
22 namespace stdair {
23 
24  // ////////////// Tokenising support ///////////////
28  typedef boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
29 
33  const boost::char_separator<char> TokeniserDashSeparator ("-");
34 
38  const boost::char_separator<char> TokeniserTimeSeparator (":");
39 
40  // ////////////////////////////////////////////////////////////////////
41  ParsedKey::ParsedKey() : _fullKey (""), _airlineCode (""), _flightNumber (""),
42  _departureDate (""), _boardingPoint (""),
43  _offPoint (""), _boardingTime ("") {
44  }
45 
46  // ////////////////////////////////////////////////////////////////////
48  }
49 
50  // ////////////////////////////////////////////////////////////////////
52  if (_airlineCode.size() < 2 || _airlineCode.size() > 3) {
53  STDAIR_LOG_ERROR ("No airline code can be found in '" << _fullKey << "'");
54  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
55  throw KeyNotFoundException ("No airline code can be found in '"
56  + _fullKey + "'");
57  }
58  return _airlineCode;
59  }
60 
61  // ////////////////////////////////////////////////////////////////////
63  // Check whether the departure date has been parsed correctly.
64  Tokeniser_T lDateTokens (_departureDate, TokeniserDashSeparator);
65 
66  if (lDateTokens.begin() == lDateTokens.end()) {
67  STDAIR_LOG_ERROR ("No date can be found in '" << _fullKey << "'");
68  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
69  throw KeyNotFoundException ("No date can be found in '" + _fullKey + "'");
70  }
71 
72  const FlightNumber_T lFlightNumber =
73  boost::lexical_cast<FlightNumber_T> (_flightNumber);
74 
75  const Date_T lDepartureDate =
76  boost::gregorian::from_simple_string (_departureDate);
77 
78  const FlightDateKey oFlightDateKey (lFlightNumber, lDepartureDate);
79 
80  return oFlightDateKey;
81  }
82 
83  // ////////////////////////////////////////////////////////////////////
85  if (_boardingPoint.size() != 3) {
86  STDAIR_LOG_ERROR ("No airport code can be found in '" << _fullKey << "'");
87  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
88  throw KeyNotFoundException ("No airport code can be found in '"
89  + _fullKey + "'");
90  }
91 
92  const LegDateKey oLegDateKey (_boardingPoint);
93 
94  return oLegDateKey;
95  }
96 
97  // ////////////////////////////////////////////////////////////////////
99  if (_boardingPoint.size() != 3 || _offPoint.size() != 3) {
100  STDAIR_LOG_ERROR ("No airport code can be found in '" << _fullKey << "'");
101  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
102  throw KeyNotFoundException ("No airport code can be found in '"
103  + _fullKey + "'");
104  }
105 
106  const SegmentDateKey oSegmentDateKey (_boardingPoint, _offPoint);
107 
108  return oSegmentDateKey;
109  }
110 
111  // ////////////////////////////////////////////////////////////////////
113  // Check whether the boarding time has been parsed correctly.
114  Tokeniser_T lTimeTokens (_boardingTime, TokeniserTimeSeparator);
115 
116  if (lTimeTokens.begin() == lTimeTokens.end()) {
117  STDAIR_LOG_ERROR ("No boarding time can be found in '" << _fullKey << "'");
118  STDAIR_LOG_DEBUG ("Parsed key: " << toString());
119  throw KeyNotFoundException ("No boarding time can be found in '"
120  + _fullKey + "'");
121  }
122 
123  const Duration_T oBoardingTime (boost::posix_time::
124  duration_from_string (_boardingTime));
125 
126  return oBoardingTime;
127  }
128 
129  // ////////////////////////////////////////////////////////////////////
130  void ParsedKey::toStream (std::ostream& ioOut) const {
131  ioOut << "ParsedKey: " << toString();
132  }
133 
134  // ////////////////////////////////////////////////////////////////////
135  void ParsedKey::fromStream (std::istream& ioIn) {
136  }
137 
138  // ////////////////////////////////////////////////////////////////////
139  const std::string ParsedKey::toString() const {
140  std::ostringstream oStr;
141 
142  oStr << _airlineCode
143  << DEFAULT_KEY_FLD_DELIMITER << " "
144  << _flightNumber
146  << _departureDate
147  << DEFAULT_KEY_FLD_DELIMITER << " "
148  << _boardingPoint
150  << _offPoint
151  << DEFAULT_KEY_FLD_DELIMITER << " "
152  << _boardingTime;
153 
154  return oStr.str();
155  }
156 
157 }
const std::string DEFAULT_KEY_SUB_FLD_DELIMITER
std::string _offPoint
Definition: ParsedKey.hpp:81
const boost::char_separator< char > TokeniserDashSeparator("-")
std::string _flightNumber
Definition: ParsedKey.hpp:78
#define STDAIR_LOG_ERROR(iToBeLogged)
Definition: Logger.hpp:23
const boost::char_separator< char > TokeniserTimeSeparator(":")
unsigned short FlightNumber_T
Handle on the StdAir library context.
boost::posix_time::time_duration Duration_T
std::string _departureDate
Definition: ParsedKey.hpp:79
SegmentDateKey getSegmentKey() const
Definition: ParsedKey.cpp:98
const std::string DEFAULT_KEY_FLD_DELIMITER
const std::string toString() const
Definition: ParsedKey.cpp:139
std::string _fullKey
Definition: ParsedKey.hpp:76
LegDateKey getLegKey() const
Definition: ParsedKey.cpp:84
std::string _airlineCode
Definition: ParsedKey.hpp:77
Key of a given inventory, made of the airline code.
FlightDateKey getFlightDateKey() const
Definition: ParsedKey.cpp:62
std::string _boardingTime
Definition: ParsedKey.hpp:82
InventoryKey getInventoryKey() const
Definition: ParsedKey.cpp:51
Key of a given flight-date, made of a flight number and a departure date.
std::string _boardingPoint
Definition: ParsedKey.hpp:80
boost::gregorian::date Date_T
void toStream(std::ostream &ioOut) const
Definition: ParsedKey.cpp:130
Key of a given segment-date, made of an origin and a destination airports.
void fromStream(std::istream &ioIn)
Definition: ParsedKey.cpp:135
boost::tokenizer< boost::char_separator< char > > Tokeniser_T
const Duration_T getBoardingTime() const
Definition: ParsedKey.cpp:112
#define STDAIR_LOG_DEBUG(iToBeLogged)
Definition: Logger.hpp:32