1 |
|
|
2 |
#include "TrainInfo.h" |
#include "TrainInfo.h" |
3 |
|
|
4 |
#include "string_nocase.h" |
//#include "string_nocase.h" |
5 |
#include "Util.h" |
//#include "Util.h" |
6 |
#include "Exceptions.h" |
//#include "Exceptions.h" |
7 |
#include "Logger.h" |
#include "Logger.h" |
8 |
|
|
9 |
#include "HttpClient.h" |
#include "HttpClient.h" |
12 |
#include <sstream> |
#include <sstream> |
13 |
#include <stdexcept> |
#include <stdexcept> |
14 |
|
|
15 |
|
#include <expat.h> |
16 |
|
|
17 |
|
|
18 |
|
|
19 |
using namespace std; |
using namespace std; |
20 |
|
|
21 |
|
vector<TrainInfo> traininfoList; |
22 |
|
TrainInfo tmpTrainInfo; |
23 |
|
ostringstream buffer; |
24 |
|
|
25 |
string ConvertUpdateInfo(char ch) |
string ConvertUpdateInfo(char ch) |
26 |
{ |
{ |
27 |
string result; |
string result; |
45 |
return result; |
return result; |
46 |
} |
} |
47 |
|
|
48 |
inline string clean(string& str) |
|
49 |
|
|
50 |
|
|
51 |
|
void startElement(void *userData, const char *el, const char **attr) |
52 |
{ |
{ |
53 |
using namespace Util; |
buffer.str(""); //clear buffer |
54 |
|
} |
55 |
|
|
56 |
|
void endElement(void *userData, const char *el) |
57 |
|
{ |
58 |
|
string name(el); |
59 |
|
if (name == "time") { |
60 |
|
tmpTrainInfo.time = buffer.str(); |
61 |
|
} else if (name == "updated") { |
62 |
|
tmpTrainInfo.update = ConvertUpdateInfo( buffer.str().at(0) ); |
63 |
|
} else if (name == "trainnumber") { |
64 |
|
tmpTrainInfo.type = buffer.str(); |
65 |
|
} else if (name == "destination") { |
66 |
|
tmpTrainInfo.destination = buffer.str(); |
67 |
|
} else if (name == "origin") { |
68 |
|
tmpTrainInfo.origin = buffer.str(); |
69 |
|
} else if (name == "location") { |
70 |
|
tmpTrainInfo.current = buffer.str(); |
71 |
|
} else if (name == "status") { |
72 |
|
tmpTrainInfo.status = buffer.str(); |
73 |
|
} else if (name == "note") { |
74 |
|
tmpTrainInfo.note = buffer.str(); |
75 |
|
} else if (name == "train") { |
76 |
|
traininfoList.push_back(tmpTrainInfo); |
77 |
|
} |
78 |
|
} |
79 |
|
|
|
str = str_replace(str, " ", " "); |
|
80 |
|
|
81 |
return str_trim(str_characters(str_striptags(str))); |
void charData(void *userData, const XML_Char *s,int len) |
82 |
|
{ |
83 |
|
for (int i=0; i<len; i++) { |
84 |
|
buffer << s[i] ; |
85 |
|
} |
86 |
} |
} |
87 |
|
|
88 |
void CleanTrainInfo(TrainInfo& info) |
|
89 |
|
|
90 |
|
vector<TrainInfo> GetTrainInfo(int stationID) |
91 |
{ |
{ |
92 |
info.time = clean( info.time ); |
traininfoList.clear(); |
93 |
info.type = clean( info.type ); |
|
94 |
info.destination = clean( info.destination ); |
ostringstream url; |
95 |
info.origin = clean( info.origin ); |
url << "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" << stationID; |
96 |
info.current = clean( info.current ); |
|
97 |
info.status = clean( info.status ); |
|
98 |
info.note = clean( info.note ); |
XML_Parser parser = NULL; |
99 |
|
|
100 |
|
try |
101 |
|
{ |
102 |
|
string trainXml = HttpClient::GetString( url.str() ); |
103 |
|
|
104 |
|
parser = XML_ParserCreate( NULL ); |
105 |
|
XML_SetElementHandler(parser, startElement, endElement); |
106 |
|
XML_SetCharacterDataHandler(parser, charData); |
107 |
|
|
108 |
|
|
109 |
|
int done; |
110 |
|
XML_Parse(parser, trainXml.c_str(), trainXml.size(), done); |
111 |
|
} |
112 |
|
catch (exception &e) |
113 |
|
{ |
114 |
|
Logger::logMessage( e.what() ); |
115 |
|
} |
116 |
|
catch (... ) |
117 |
|
{ |
118 |
|
Logger::logMessage( "unknown error in GetTrainInfo(int stationID)" ); |
119 |
|
} |
120 |
|
|
121 |
|
if (parser != NULL) |
122 |
|
XML_ParserFree(parser); |
123 |
|
|
124 |
|
|
125 |
|
return traininfoList; |
126 |
} |
} |
127 |
|
|
128 |
|
/* |
129 |
|
//old html screen scaper bit |
130 |
vector<TrainInfo> GetTrainInfo(string stationcode, string stationname) |
vector<TrainInfo> GetTrainInfo(string stationcode, string stationname) |
131 |
{ |
{ |
132 |
vector<TrainInfo> result; |
vector<TrainInfo> result; |
230 |
|
|
231 |
return result; |
return result; |
232 |
} |
} |
233 |
|
*/ |
234 |
|
|