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{
26 public:
27 typedef QPair<QString, QString> QualifiedName; // 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() { return m_document; }
47
48 // Used by tag handlers, to be overridden by GeoDataParser/GeoSceneParser
49 virtual bool isValidElement( const QString& tagName ) const;
50
51 // Used by tag handlers, to access a parent element's associated GeoStackItem
52 GeoStackItem parentElement( unsigned int depth = 0 ) const;
53
54 // Used by tag handlers, to emit a warning while parsing
55 void raiseWarning( const QString& );
56
57 // Used by tag handlers, to retrieve the value for an attribute of the currently parsed element
58 QString attribute( const char* attributeName ) const;
59
60protected:
61 /**
62 * This method is intended to check if the current element being served by
63 * the GeoParser is a valid Document Root element. This method is to be
64 * implemented by GeoDataParser/GeoSceneParser and must check based on the
65 * current XML Document type, e.g. KML, GPX etc.
66 * @return @c true if the element is a valid document root.
67 */
68 virtual bool isValidRootElement() = 0;
69
70 virtual GeoDocument* createDocument() const = 0;
71
72protected:
73 GeoDocument* m_document;
74 GeoDataGenericSourceType m_source;
75
76private:
77 void parseDocument();
78 QStack<GeoStackItem> m_nodeStack;
79};
80
81class GeoStackItem
82{
83 public:
84 GeoStackItem()
85 : m_qualifiedName(),
86 m_node( nullptr )
87 {
88 }
89
90 GeoStackItem( const GeoParser::QualifiedName& qualifiedName, GeoNode* node )
91 : m_qualifiedName( qualifiedName ),
92 m_node( node )
93 {
94 }
95
96 // Fast path for tag handlers
97 bool represents( const char* tagName ) const
98 {
99 return m_node && tagName == m_qualifiedName.first;
100 }
101
102 // Helper for tag handlers. Does NOT guard against miscasting. Use with care.
103 template<class T>
104 T* nodeAs()
105 {
106 Q_ASSERT( dynamic_cast<T*>( m_node ) != nullptr );
107 return static_cast<T*>(m_node);
108 }
109
110 template<class T>
111 bool is() const
112 {
113 return nullptr != dynamic_cast<T*>(m_node);
114 }
115
116 GeoParser::QualifiedName qualifiedName() const { return m_qualifiedName; }
117 GeoNode* associatedNode() const { return m_node; }
118
119private:
120 friend class GeoParser;
121 void assignNode( GeoNode* node ) { m_node = node; }
122 GeoParser::QualifiedName m_qualifiedName;
123 GeoNode* m_node;
124};
125
126}
127
128#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 May 24 2024 11:50:53 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.