StdAir Logo  1.00.0
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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
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.
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.
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 }