/* * Copyright (c) 2011, 2014 Damien Grauser (Damien.Grauser@pev-geneve.ch) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "RouteParser.h" #include #include #include #include "Route.h" #define DATETIME_FORMAT "yyyy/MM/dd hh:mm:ss' UTC'" bool RouteParser::startDocument() { buffer.clear(); return true; } bool RouteParser::endElement( const QString&, const QString&, const QString &qName ) { if(qName == "name") route.setName(buffer.trimmed()); else if (qName == "id") route.setId(QUuid(buffer.trimmed())); else if(qName == "route") { routes.append(route); } else if(qName == "point") { route.addPoint(point); } else if(qName == "ride") { route.addRide(ride); } else if(qName == "lat") { point.lat = buffer.trimmed().toDouble(); } else if(qName == "lon") { point.lon = buffer.trimmed().toDouble(); } else if(qName == "date") { ride.startTime = QDateTime::fromString(buffer.trimmed(), DATETIME_FORMAT); } else if(qName == "start") { ride.start = buffer.trimmed().toDouble(); } else if(qName == "stop") { ride.stop = buffer.trimmed().toDouble(); } else if(qName == "precision") { ride.precision = buffer.trimmed().toDouble(); } return true; } bool RouteParser::startElement( const QString&, const QString&, const QString &name, const QXmlAttributes & ) { buffer.clear(); if(name == "route") { route = RouteSegment(); } else if(name == "point") { point = RoutePoint(); } else if(name == "ride") { ride = RouteRide(); } return true; } bool RouteParser::characters( const QString& str ) { buffer += str; return true; } QList RouteParser::getRoutes() { return routes; } bool RouteParser::endDocument() { return true; } bool RouteParser::serialize(QString filename, QListroutes) { // open file - truncate contents QFile file(filename); file.open(QFile::WriteOnly); file.resize(0); QTextStream out(&file); // begin document out << "\n"; // write out to file foreach (RouteSegment route, routes) { // main attributes qDebug() << "write route " << route.id().toString(); out<\n" "\t\t%1\n" "\t\t%2\n") .arg(route.getName()) .arg(route.id().toString()); out << "\t\t\n"; qDebug() << "route->getPoints() " << route.getPoints().count(); foreach (RoutePoint point, route.getPoints() ) { out << QString("\t\t\t%1%2\n").arg(point.lat).arg(point.lon); } out << "\t\t\n"; out << "\t\t\n"; qDebug() << "route->getRides() " << route.getRides().count(); foreach (RouteRide ride, route.getRides() ) { out << QString("\t\t\t%1%2%3%4\n").arg(ride.startTime.toString(DATETIME_FORMAT)).arg(ride.start).arg(ride.stop).arg(ride.precision); } out << "\t\t\n"; out <\n"); } // end document out << "\n"; // close file file.close(); return true; // success }