#include <QDebug>
#include <QList>
#include <QFile>
+#include <QStringList>
RoutePrivate::RoutePrivate( QObject *parent ) :
m_fromValid(false),
QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
{
qDebug() << "parsing route";
- QFile file( "/home/user/route.txt" );
- if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
- QTextStream out(&file);
- out << reply;
- file.close();
- } else {
- qDebug() << "Could not open /home/user/route.txt";
- }
-
QList<RouteData> retVal;
RouteData routeData;
+ LegData legData;
QXmlStreamReader xml( reply );
QHash<QString, bool> in;
QHash<QString, bool> have;
- have[ "LINE" ] = false;
- have[ "TIME" ] = false;
+ QStringList haveKeys;
+ QStringList inKeys;
+
+ haveKeys
+ << "LINE"
+ << "TIME"
+ << "TRIP"
+ << "DEPARTURE"
+ << "ARRIVAL"
+ ;
+
+ inKeys
+ << "ROUTE"
+ << "LINE"
+ << "STOP"
+ << "WALK"
+ << "POINT"
+ ;
+
+ foreach( QString key, haveKeys ) {
+ have[ key ] = false;
+ }
- in[ "ROUTE" ] = false;
- in[ "LINE" ] = false;
- in[ "STOP" ] = false;
+ foreach( QString key, inKeys ) {
+ in[ key ] = false;
+ }
while ( !xml.atEnd() ) {
xml.readNext();
+
+ QString xmlName = xml.name().toString();
+
if ( xml.isStartElement() ) {
- in[ xml.name().toString() ] = true;
+ if ( inKeys.contains( xmlName ) ) {
+ in[ xmlName ] = true;
+ //qDebug() << "in[" << xmlName << "] = true";
+ }
- if ( xml.name() == "ROUTE" ) {
- have[ "TIME" ] = false;
- have[ "LINE" ] = false;
+ if ( xmlName == "ROUTE" ) {
+ foreach( QString key, haveKeys ) {
+ have[ key ] = false;
}
+ }
+
+ if ( xmlName == "WALK" ) {
+ legData.m_how = "WALK";
+ have[ "DEPARTURE" ] = false;
+ have[ "ARRIVAL" ] = false;
+ have[ "LENGTH" ] = false;
+ }
+
+ if ( xmlName == "LINE" ) {
+ legData.m_how = "LINE";
+ QString lineCode( xml.attributes().value("code").toString() );
+ legData.m_lineCode = parseJORECode( lineCode );
+ have[ "DEPARTURE" ] = false;
+ have[ "ARRIVAL" ] = false;
+ have[ "LENGTH" ] = false;
+ }
}
if ( xml.isEndElement() ) {
- in[ xml.name().toString() ] = false;
+ if ( inKeys.contains( xmlName ) ) {
+ in[ xmlName ] = false;
+ //qDebug() << "in[" << xmlName << "] = false";
+ }
+
+ if ( xmlName == "ROUTE" ) {
+ retVal.append( routeData );
+ routeData.clear();
+ }
+
+ if ( xmlName == "WALK" || xmlName == "LINE" ) {
+ routeData.m_legData.append( legData );
+ legData.clear();
+ have[ "LENGTH" ] = false;
+ }
}
- if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xml.name() == "LINE" ) {
+ if ( !have[ "ARRIVAL" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && ( in[ "STOP" ] || in[ "POINT" ] ) && xml.isStartElement() && xmlName == "ARRIVAL" ) {
+ QString arrivalTime( xml.attributes().value("time").toString() );
+ legData.m_arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
+
+ // don't set have[ "ARRIVAL" ] since we want the last one of many STOPs
+ }
+
+ if ( !have[ "DEPARTURE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.isStartElement() && xmlName == "DEPARTURE" ) {
+ QString departureTime( xml.attributes().value("time").toString() );
+ legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
+
+ have[ "DEPARTURE" ] = true;
+ }
+
+ if ( !have[ "DEPARTURE" ] && in[ "WALK" ] && ( in[ "POINT" ] || in[ "STOP" ] ) && xml.isStartElement() && xmlName == "DEPARTURE" ) {
+ QString departureTime( xml.attributes().value("time").toString() );
+ legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
+
+ have[ "DEPARTURE" ] = true;
+ }
+
+ if ( !have[ "LENGTH" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && xml.isStartElement() && xmlName == "LENGTH" ) {
+ legData.m_tripTime = xml.attributes().value("time").toString();
+ legData.m_tripDistance = xml.attributes().value("dist").toString();
+
+ have[ "LENGTH" ] = true;
+ }
+
+ if ( !have[ "TRIP" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LENGTH" ) {
+ routeData.m_tripTime = xml.attributes().value("time").toString();
+ routeData.m_tripDistance = xml.attributes().value("dist").toString();
+
+ have[ "TRIP" ] = true;
+ }
+
+ if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LINE" ) {
QString lineCode( xml.attributes().value("code").toString() );
- routeData.lineCode = parseJORECode( lineCode );
+ routeData.m_lineCode = parseJORECode( lineCode );
have[ "LINE" ] = true;
-
- if ( have[ "LINE" ] && have[ "TIME" ] ) {
- retVal.append( routeData );
- }
}
- if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.name() == "ARRIVAL" ) {
- QString arrivalTime( xml.attributes().value("time").toString() );
+ if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xmlName == "DEPARTURE" ) {
+ QString departureTime( xml.attributes().value("time").toString() );
- routeData.arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
+ routeData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
have[ "TIME" ] = true;
-
- if ( have[ "LINE" ] && have[ "TIME" ] ) {
- retVal.append( routeData );
- }
}
}
QCOMPARE( routeData.count(), expectedResults.count() );
- for( int index=0; index<routeData.count(); ++index ) {
- QCOMPARE( routeData.at( index ).lineCode, expectedResults.at( index ).lineCode );
- QCOMPARE( routeData.at( index ).arrivalTime, expectedResults.at( index ).arrivalTime );
+ for( int routeIndex=0; routeIndex<routeData.count(); ++routeIndex ) {
+ const RouteData &thisRouteData = routeData.at( routeIndex );
+ const RouteData &thisExpectedRoute = expectedResults.at( routeIndex );
+
+ QCOMPARE( thisRouteData.m_tripTime, thisExpectedRoute.m_tripTime );
+ QCOMPARE( thisRouteData.m_tripDistance, thisExpectedRoute.m_tripDistance );
+ QCOMPARE( thisRouteData.m_departureTime, thisExpectedRoute.m_departureTime );
+ QCOMPARE( thisRouteData.m_lineCode, thisExpectedRoute.m_lineCode );
+ QCOMPARE( thisRouteData.m_legData.count(), thisExpectedRoute.m_legData.count() );
+
+ for( int legIndex=0; legIndex<thisRouteData.m_legData.count(); ++legIndex ) {
+ const LegData &thisLegData = thisRouteData.m_legData.at( legIndex );
+ const LegData &thisExpectedLeg = thisExpectedRoute.m_legData.at( legIndex );
+
+ QCOMPARE( thisLegData.m_how, thisExpectedLeg.m_how );
+ QCOMPARE( thisLegData.m_tripTime, thisExpectedLeg.m_tripTime );
+ QCOMPARE( thisLegData.m_tripDistance, thisExpectedLeg.m_tripDistance );
+ QCOMPARE( thisLegData.m_departureTime, thisExpectedLeg.m_departureTime );
+ QCOMPARE( thisLegData.m_arrivalTime, thisExpectedLeg.m_arrivalTime );
+ QCOMPARE( thisLegData.m_lineCode, thisExpectedLeg.m_lineCode );
+ }
}
}
QTest::newRow("single route")
<< sampleInput[0]
<< ( QList<RouteData>()
- << RouteData( "65A", "18:20" )
- << RouteData( "102T", "18:26" )
- << RouteData( "110T", "18:34" )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "14.411", "2510.063", "18:20", "65A" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "4.475", "357.069", "18:15", "18:20" )
+ << LegData( "LINE", "5.000", "1760.931", "18:20", "18:25", "65A" )
+ << LegData( "WALK", "4.936", "392.062", "18:25", "18:29" )
+ )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "13.411", "2501.497", "18:26", "102T" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "4.475", "357.069", "18:21", "18:26" )
+ << LegData( "LINE", "4.000", "1751.582", "18:26", "18:30", "102T" )
+ << LegData( "WALK", "4.936", "392.846", "18:30", "18:34" )
+ )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "13.411", "2501.497", "18:34", "110T" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "4.475", "357.069", "18:29", "18:34" )
+ << LegData( "LINE", "4.000", "1751.582", "18:34", "18:38", "110T" )
+ << LegData( "WALK", "4.936", "392.846", "18:38", "18:42" )
+ )
);
QTest::newRow("route with bus change")
<< sampleInput[1]
<< ( QList<RouteData>()
- << RouteData( "111", "08:18" )
- << RouteData( "111", "08:33" )
- << RouteData( "111T", "08:34" )
- << RouteData( "111", "08:50" )
- << RouteData( "111", "09:07" )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "28.633", "8902.040", "08:18", "111" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "3.479", "254.753", "08:14", "08:18" )
+ << LegData( "LINE", "8.000", "5225.092", "08:18", "08:26", "111" )
+ << LegData( "LINE", "5.000", "2926.431", "08:32", "08:37", "121T" )
+ << LegData( "WALK", "6.154", "495.764", "08:37", "08:43" )
+ )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "25.633", "8902.040", "08:33", "111" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "3.479", "254.753", "08:29", "08:33" )
+ << LegData( "LINE", "8.000", "5225.092", "08:33", "08:41", "111" )
+ << LegData( "LINE", "5.000", "2926.431", "08:44", "08:49", "102T" )
+ << LegData( "WALK", "6.154", "495.764", "08:49", "08:55" )
+ )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "33.510", "11193.458", "08:34", "111T" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "2.356", "172.693", "08:31", "08:34" )
+ << LegData( "LINE", "6.000", "3392.054", "08:34", "08:40", "111T" )
+ << LegData( "LINE", "5.000", "4206.516", "08:43", "08:48", "112" )
+ << LegData( "LINE", "5.000", "2926.431", "08:54", "08:59", "102T" )
+ << LegData( "WALK", "6.154", "495.764", "08:59", "09:05" )
+ )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "29.633", "8902.040", "08:50", "111" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "3.479", "254.753", "08:46", "08:50" )
+ << LegData( "LINE", "8.000", "5225.092", "08:50", "08:58", "111" )
+ << LegData( "LINE", "5.000", "2926.431", "09:05", "09:10", "102T" )
+ << LegData( "WALK", "6.154", "495.764", "09:10", "09:16" )
+ )
+ // Summary : TTime TDist First Vehicle
+ << ( RouteData( "29.633", "8902.040", "09:07", "111" )
+ // How TTime TDist Depart Arrive Line
+ << LegData( "WALK", "3.479", "254.753", "09:03", "09:07" )
+ << LegData( "LINE", "8.000", "5225.092", "09:07", "09:15", "111" )
+ << LegData( "LINE", "5.000", "2926.431", "09:22", "09:27", "160T" )
+ << LegData( "WALK", "6.154", "495.764", "09:27", "09:33" )
+ )
);
}