Marble

GeoDataDocumentWriter.cpp
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2016 Dennis Nienhüser <[email protected]>
9 //
10 
11 #include "GeoDataDocumentWriter.h"
12 
13 #include "GeoWriterBackend.h"
14 #include "GeoWriter.h"
15 #include "GeoTagWriter.h"
16 #include "GeoDataDocument.h"
17 #include "KmlElementDictionary.h"
18 
19 #include <QFileInfo>
20 #include <MarbleDebug.h>
21 
22 namespace Marble {
23 
24 QSet<QPair<QString, GeoWriterBackend*> > GeoDataDocumentWriter::s_backends;
25 
26 bool GeoDataDocumentWriter::write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
27 {
28  const GeoTagWriter* tagWriter = GeoTagWriter::recognizes(GeoTagWriter::QualifiedName(QString(), documentIdentifier));
29  if (tagWriter) {
30  GeoWriter writer;
31  writer.setDocumentType(documentIdentifier);
32  return writer.write(device, &document);
33  } else {
34  for(const auto &backend: s_backends) {
35  if (backend.first == documentIdentifier) {
36  backend.second->write(device, document);
37  return true;
38  }
39  }
40 
41  mDebug() << "No writer registered to handle " << documentIdentifier;
42  return false;
43  }
44 }
45 
46 bool GeoDataDocumentWriter::write(const QString &filename, const GeoDataDocument &document, const QString &documentIdentifier)
47 {
48  QFile file(filename);
49  if (!file.open(QIODevice::WriteOnly)) {
50  mDebug() << "Cannot open" << filename << "for writing:" << file.errorString();
51  return false;
52  }
53 
54  QString const docType = documentIdentifier.isEmpty() ? determineDocumentIdentifier(filename) : documentIdentifier;
55  return write(&file, document, docType);
56 }
57 
58 void GeoDataDocumentWriter::registerWriter(GeoWriterBackend *writer, const QString &fileExtension)
59 {
60  s_backends << QPair<QString, GeoWriterBackend*>(fileExtension, writer);
61 }
62 
63 void GeoDataDocumentWriter::unregisterWriter(GeoWriterBackend *writer, const QString &fileExtension)
64 {
65  auto pair = QPair<QString, GeoWriterBackend*>(fileExtension, writer);
66  s_backends.remove(pair);
67  delete writer;
68 }
69 
70 QString GeoDataDocumentWriter::determineDocumentIdentifier(const QString &filename)
71 {
72  QString const fileExtension = QFileInfo(filename).suffix().toLower();
73  if (fileExtension == QLatin1String("kml")) {
74  return kml::kmlTag_nameSpaceOgc22;
75  }
76  if (fileExtension == QLatin1String("osm")) {
77  return "0.6";
78  }
79 
80  for(const auto &backend: s_backends) {
81  if (backend.first == fileExtension) {
82  return backend.first;
83  }
84  }
85 
86  qDebug() << "Unable to determine document from file extension " << fileExtension << ", falling back to KML document type";
87  return kml::kmlTag_nameSpaceOgc22;
88 }
89 
90 }
A container for Features, Styles and in the future Schemas.
QString errorString() const const
Binds a QML item to a specific geodetic location in screen coordinates.
static bool write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
Write the content of the given GeoDataDocument to the given I/O device.
Base class intended to be subclassed by specific XML tag writers This class provides a base class tha...
Definition: GeoTagWriter.h:32
bool isEmpty() const const
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition: GeoWriter.h:30
virtual bool open(QIODevice::OpenMode mode) override
QString toLower() const const
void setDocumentType(const QString &documentType)
Set the current document type.
Definition: GeoWriter.cpp:78
bool remove(const T &value)
QString suffix() const const
bool write(QIODevice *device, const GeoNode *feature)
The main API call to use the XML writer.
Definition: GeoWriter.cpp:29
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
static void registerWriter(GeoWriterBackend *writer, const QString &fileExtension)
Registers a plugin as a backend for writing a certain file extension.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 23:18:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.