File indexing completed on 2024-04-28 03:50:31

0001 /*
0002     SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004     SPDX-FileCopyrightText: 2013 Ander Pijoan <ander.pijoan@deusto.es>
0005     SPDX-FileCopyrightText: 2019 John Zaitseff <J.Zaitseff@zap.org.au>
0006 */
0007 
0008 #include "JsonRunner.h"
0009 #include "JsonParser.h"
0010 
0011 #include "GeoDataDocument.h"
0012 #include "MarbleDebug.h"
0013 
0014 #include <QFile>
0015 
0016 namespace Marble
0017 {
0018 
0019 JsonRunner::JsonRunner(QObject *parent) :
0020     ParsingRunner(parent)
0021 {
0022 }
0023 
0024 JsonRunner::~JsonRunner()
0025 {
0026 }
0027 
0028 GeoDataDocument *JsonRunner::parseFile(const QString &fileName, DocumentRole role, QString &error)
0029 {
0030     // Check that the file exists
0031     QFile file(fileName);
0032     if (! file.exists()) {
0033         error = QStringLiteral("File %1 does not exist").arg(fileName);
0034         mDebug() << error;
0035         return nullptr;
0036     }
0037 
0038     // Open file in the correct mode
0039     file.open(QIODevice::ReadOnly);
0040 
0041     // Create parser
0042     JsonParser parser;
0043 
0044     // Start parsing
0045     if (! parser.read(&file)) {
0046         error = QStringLiteral("Could not parse GeoJSON from %1").arg(fileName);
0047         mDebug() << error;
0048         return nullptr;
0049     }
0050 
0051     GeoDataDocument* document = parser.releaseDocument();
0052     file.close();
0053 
0054     document->setDocumentRole( role );
0055     document->setFileName( fileName );
0056 
0057     return document;
0058 }
0059 
0060 }
0061 
0062 #include "moc_JsonRunner.cpp"