Marble

GeoParser.h
1/*
2 SPDX-FileCopyrightText: 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#ifndef MARBLE_GEOPARSER_H
7#define MARBLE_GEOPARSER_H
8
9#include <QPair>
10#include <QStack>
11#include <QXmlStreamReader>
12
13#include "geodata_export.h"
14
15namespace Marble
16{
17
18using GeoDataGenericSourceType = int;
19
20class GeoDocument;
21class GeoNode;
22class GeoStackItem;
23
24class GEODATA_EXPORT GeoParser : public QXmlStreamReader
25{
26public:
27 using QualifiedName = QPair<QString, QString>; // Tag Name & Namespace pair
28
29 explicit GeoParser(GeoDataGenericSourceType sourceType);
30 virtual ~GeoParser();
31
32 /**
33 * @brief Main API for reading the XML document.
34 * This is the only method that is necessary to call to start the GeoParser.
35 * To retrieve the resulting data see @see releaseDocument() and
36 * @see releaseModel()
37 */
38 bool read(QIODevice *);
39
40 /**
41 * @brief retrieve the parsed document and reset the parser
42 * If parsing was successful, retrieve the resulting document
43 * and set the contained m_document pointer to 0.
44 */
45 GeoDocument *releaseDocument();
46 GeoDocument *activeDocument()
47 {
48 return m_document;
49 }
50
51 // Used by tag handlers, to be overridden by GeoDataParser/GeoSceneParser
52 virtual bool isValidElement(const QString &tagName) const;
53
54 // Used by tag handlers, to access a parent element's associated GeoStackItem
55 GeoStackItem parentElement(unsigned int depth = 0) const;
56
57 // Used by tag handlers, to emit a warning while parsing
58 void raiseWarning(const QString &);
59
60 // Used by tag handlers, to retrieve the value for an attribute of the currently parsed element
61 QString attribute(const char *attributeName) const;
62
63protected:
64 /**
65 * This method is intended to check if the current element being served by
66 * the GeoParser is a valid Document Root element. This method is to be
67 * implemented by GeoDataParser/GeoSceneParser and must check based on the
68 * current XML Document type, e.g. KML, GPX etc.
69 * @return @c true if the element is a valid document root.
70 */
71 virtual bool isValidRootElement() = 0;
72
73 virtual GeoDocument *createDocument() const = 0;
74
75protected:
76 GeoDocument *m_document;
77 GeoDataGenericSourceType m_source;
78
79private:
80 void parseDocument();
81 QStack<GeoStackItem> m_nodeStack;
82};
83
84class GeoStackItem
85{
86public:
87 GeoStackItem()
88 : m_qualifiedName()
89 , m_node(nullptr)
90 {
91 }
92
93 GeoStackItem(const GeoParser::QualifiedName &qualifiedName, GeoNode *node)
94 : m_qualifiedName(qualifiedName)
95 , m_node(node)
96 {
97 }
98
99 // Fast path for tag handlers
100 bool represents(const char *tagName) const
101 {
102 return m_node && QLatin1StringView(tagName) == m_qualifiedName.first;
103 }
104
105 // Helper for tag handlers. Does NOT guard against miscasting. Use with care.
106 template<class T>
107 T *nodeAs()
108 {
109 Q_ASSERT(dynamic_cast<T *>(m_node) != nullptr);
110 return static_cast<T *>(m_node);
111 }
112
113 template<class T>
114 bool is() const
115 {
116 return nullptr != dynamic_cast<T *>(m_node);
117 }
118
119 GeoParser::QualifiedName qualifiedName() const
120 {
121 return m_qualifiedName;
122 }
123 GeoNode *associatedNode() const
124 {
125 return m_node;
126 }
127
128private:
129 friend class GeoParser;
130 void assignNode(GeoNode *node)
131 {
132 m_node = node;
133 }
134 GeoParser::QualifiedName m_qualifiedName;
135 GeoNode *m_node = nullptr;
136};
137
138}
139
140#endif
QVariant read(const QByteArray &data, int versionOverride=0)
Binds a QML item to a specific geodetic location in screen coordinates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:52:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.