File indexing completed on 2024-05-12 03:50:31

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Andrew Manson <g.real.ate@gmail.com>
0004 // SPDX-FileCopyrightText: 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
0005 //
0006 
0007 #ifndef MARBLE_GEOTAGWRITER_H
0008 #define MARBLE_GEOTAGWRITER_H
0009 
0010 #include <QPair>
0011 #include <QHash>
0012 
0013 #include <marble_export.h>
0014 
0015 namespace Marble
0016 {
0017 
0018 class GeoNode;
0019 class GeoWriter;
0020 
0021 /**
0022  * @brief Base class intended to be subclassed by specific XML tag writers
0023  * This class provides a base class that allows the writing of many different
0024  * XML formats. The system used to implement this writing system is very strongly
0025  * based on the @see GeoTagHandler system.
0026  */
0027 class MARBLE_EXPORT GeoTagWriter
0028 {
0029 public:
0030     virtual bool write( const GeoNode *node, GeoWriter& writer ) const = 0;
0031 
0032     /**
0033      * @brief Object Name and Namespace Pair
0034      * This type is intended to be used in a similar way to @see GeoParser::QualifiedName
0035      * but in practice will act differently. The Namespace will not be an XML
0036      * namespace directly but instead it will refere to a Document Type so that
0037      * the GeoWriter will be able to identify what GeoTagWriter to use even in
0038      * absence of an XML namespace. This also allows for the case where data
0039      * using an internal representation of the KML classes can be outputted in
0040      * alternative XML formats. For XML formats that have namespaces this
0041      * document type will usually correspond with the XML namespace. Use in the
0042      * order QPair<QString tagName, QString documentType>.
0043      */
0044     typedef QPair<QString, QString> QualifiedName;
0045 
0046 protected:
0047     GeoTagWriter();
0048     virtual ~GeoTagWriter();
0049 
0050     static bool writeElement(const GeoNode *object, GeoWriter &writer);
0051 
0052 private:
0053     // Only our registrar is allowed to register tag writers.
0054     friend struct GeoTagWriterRegistrar;
0055     static void registerWriter(const QualifiedName&, const GeoTagWriter*);
0056     static void unregisterWriter(const QualifiedName&);
0057 
0058 private:
0059     //Collect the Tag Writers and provide a singleton like accessor
0060     typedef QHash<QualifiedName, const GeoTagWriter*> TagHash;
0061     static TagHash* tagWriterHash();
0062 
0063 private:
0064     // Only our writer is allowed to access tag handlers.
0065     friend class GeoWriter;
0066     friend class GeoDataDocumentWriter;
0067     static const GeoTagWriter* recognizes(const QualifiedName&);
0068 };
0069 
0070 // Helper structure
0071 struct GeoTagWriterRegistrar
0072 {
0073 public:
0074     GeoTagWriterRegistrar(const GeoTagWriter::QualifiedName& name, const GeoTagWriter* writer) :
0075         m_name(name)
0076     {
0077         GeoTagWriter::registerWriter(name, writer);
0078     }
0079 
0080     ~GeoTagWriterRegistrar()
0081     {
0082         GeoTagWriter::unregisterWriter(m_name);
0083     }
0084 
0085 private:
0086     GeoTagWriter::QualifiedName m_name;
0087 };
0088 
0089 }
0090 
0091 #endif