• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • geodata
  • parser
GeoParser.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 
4  This file is part of the KDE project
5 
6  This library is free software you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  aint with this library see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 
23 // Own
24 #include "GeoParser.h"
25 
26 // Marble
27 #include "MarbleDebug.h"
28 
29 // Geodata
30 #include "GeoDocument.h"
31 #include "GeoTagHandler.h"
32 
33 namespace Marble
34 {
35 
36 // Set to a value greater than 0, to dump parent node chain while parsing
37 #define DUMP_PARENT_STACK 0
38 
39 GeoParser::GeoParser( GeoDataGenericSourceType source )
40  : QXmlStreamReader(),
41  m_document( 0 ),
42  m_source( source )
43 {
44 }
45 
46 GeoParser::~GeoParser()
47 {
48  delete m_document;
49 }
50 
51 #if DUMP_PARENT_STACK > 0
52 static void dumpParentStack( const QString& name, int size, bool close )
53 {
54  static int depth = 0;
55 
56  if ( !close )
57  depth++;
58 
59  QString result;
60  for ( int i = 0; i < depth; ++i )
61  result += ' ';
62 
63  if ( close ) {
64  depth--;
65  result += "</";
66  } else
67  result += '<';
68 
69  result += name + "> stack size " + QString::number( size );
70  fprintf( stderr, "%s\n", qPrintable( result ));
71 }
72 #endif
73 
74 bool GeoParser::read( QIODevice* device )
75 {
76  // Assert previous document got released.
77  Q_ASSERT( !m_document );
78  m_document = createDocument();
79  Q_ASSERT( m_document );
80 
81  // Set data source
82  setDevice( device );
83 
84  // Start parsing
85  while ( !atEnd() ) {
86  readNext();
87 
88  if ( isStartElement() ) {
89  if ( isValidRootElement() ) {
90 #if DUMP_PARENT_STACK > 0
91  dumpParentStack( name().toString(), m_nodeStack.size(), false );
92 #endif
93 
94  parseDocument();
95 
96  if ( !m_nodeStack.isEmpty() )
97  raiseError(
98  QObject::tr("Parsing failed line %1. Still %n unclosed tag(s) after document end. ", "",
99  m_nodeStack.size() ).arg( lineNumber() ) + errorString());
100  } else
101  return false;
102  }
103  }
104 
105  if ( error() ) {
106  if ( lineNumber() == 1) {
107  raiseError("");
108  }
109  // Defer the deletion to the dtor
110  // This allows the BookmarkManager to recover the broken .kml files it produced in Marble 1.0 and 1.1
112  // delete releaseDocument();
113  }
114  return !error();
115 }
116 
117 bool GeoParser::isValidElement( const QString& tagName ) const
118 {
119  return name() == tagName;
120 }
121 
122 GeoStackItem GeoParser::parentElement( unsigned int depth ) const
123 {
124  QStack<GeoStackItem>::const_iterator it = m_nodeStack.constEnd() - 1;
125 
126  if ( it - depth < m_nodeStack.constBegin() )
127  return GeoStackItem();
128 
129  return *(it - depth);
130 }
131 
132 void GeoParser::parseDocument()
133 {
134  if( !isStartElement() ) {
135  raiseError( QObject::tr("Error parsing file at line: %1 and column %2 . ")
136  .arg( lineNumber() ).arg( columnNumber() )
137  + QObject::tr("This is an Invalid File") );
138  return;
139  }
140 
141  bool processChildren = true;
142  QualifiedName qName( name().toString(), namespaceUri().toString() );
143 
144  if( tokenType() == QXmlStreamReader::Invalid )
145  raiseWarning( QString( "%1: %2" ).arg( error() ).arg( errorString() ) );
146 
147  GeoStackItem stackItem( qName, 0 );
148 
149  if ( const GeoTagHandler* handler = GeoTagHandler::recognizes( qName )) {
150  stackItem.assignNode( handler->parse( *this ));
151  processChildren = !isEndElement();
152  }
153  // Only add GeoStackItem to the parent chain, if the tag handler
154  // for the current element possibly contains non-textual children.
155  // Consider following DGML snippet "<name>Test</name>" - the
156  // DGMLNameTagHandler assumes that <name> only contains textual
157  // children, and reads the joined value of all children using
158  // readElementText(). This implicates that tags like <name>
159  // don't contain any children that would need to be processed using
160  // this parseDocument() function.
161  if ( processChildren ) {
162  m_nodeStack.push( stackItem );
163 #if DUMP_PARENT_STACK > 0
164  dumpParentStack( name().toString(), m_nodeStack.size(), false );
165 #endif
166  while ( !atEnd() ) {
167  readNext();
168  if ( isEndElement() ) {
169  m_nodeStack.pop();
170 #if DUMP_PARENT_STACK > 0
171  dumpParentStack( name().toString(), m_nodeStack.size(), true );
172 #endif
173  break;
174  }
175 
176  if ( isStartElement() ) {
177  parseDocument();
178  }
179  }
180  }
181 #if DUMP_PARENT_STACK > 0
182  else {
183  // This is only used for debugging purposes.
184  m_nodeStack.push( stackItem );
185  dumpParentStack( name().toString() + "-discarded", m_nodeStack.size(), false );
186 
187  m_nodeStack.pop();
188  dumpParentStack( name().toString() + "-discarded", m_nodeStack.size(), true );
189  }
190 #endif
191 }
192 
193 void GeoParser::raiseWarning( const QString& warning )
194 {
195  // TODO: Maybe introduce a strict parsing mode where we feed the warning to
196  // raiseError() (which stops parsing).
197  mDebug() << "[GeoParser::raiseWarning] -> " << warning;
198 }
199 
200 QString GeoParser::attribute( const char* attributeName ) const
201 {
202  return attributes().value( QString::fromLatin1( attributeName )).toString();
203 }
204 
205 GeoDocument* GeoParser::releaseDocument()
206 {
207  GeoDocument* document = m_document;
208  m_document = 0;
209  return document;
210 }
211 
212 }
QIODevice
QXmlStreamReader::atEnd
bool atEnd() const
QXmlStreamReader::errorString
QString errorString() const
QXmlStreamReader::columnNumber
qint64 columnNumber() const
Marble::GeoParser::m_document
GeoDocument * m_document
Definition: GeoParser.h:89
GeoParser.h
QStringRef::toString
QString toString() const
QXmlStreamReader::namespaceUri
QStringRef namespaceUri() const
Marble::GeoParser::parentElement
GeoStackItem parentElement(unsigned int depth=0) const
Definition: GeoParser.cpp:122
Marble::GeoParser::GeoParser
GeoParser(GeoDataGenericSourceType sourceType)
Definition: GeoParser.cpp:39
GeoTagHandler.h
QXmlStreamReader::error
Error error() const
Marble::dgml::handler
static GeoTagHandlerRegistrar handler(GeoParser::QualifiedName(dgmlTag_DownloadPolicy, dgmlTag_nameSpace20), new DgmlDownloadPolicyTagHandler)
QXmlStreamAttributes::value
QStringRef value(const QString &namespaceUri, const QString &name) const
QXmlStreamReader::setDevice
void setDevice(QIODevice *device)
MarbleDebug.h
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QXmlStreamReader::raiseError
void raiseError(const QString &message)
QString::number
QString number(int n, int base)
Marble::GeoParser::read
bool read(QIODevice *)
Main API for reading the XML document.
Definition: GeoParser.cpp:74
Marble::GeoParser::attribute
QString attribute(const char *attributeName) const
Definition: GeoParser.cpp:200
GeoDocument.h
Marble::GeoStackItem
Definition: GeoParser.h:97
QString
QXmlStreamReader::readNext
TokenType readNext()
QXmlStreamReader::tokenType
TokenType tokenType() const
Marble::GeoParser::createDocument
virtual GeoDocument * createDocument() const =0
Marble::GeoParser::isValidRootElement
virtual bool isValidRootElement()=0
This method is intended to check if the current element being served by the GeoParser is a valid Docu...
Marble::GeoParser::~GeoParser
virtual ~GeoParser()
Definition: GeoParser.cpp:46
Marble::GeoParser::isValidElement
virtual bool isValidElement(const QString &tagName) const
Definition: GeoParser.cpp:117
QXmlStreamReader
QXmlStreamReader::isStartElement
bool isStartElement() const
QTest::toString
char * toString(const T &value)
Marble::GeoDocument
A shared base class between GeoDataDocument/GeoSourceDocument.
Definition: GeoDocument.h:42
Marble::GeoDataGenericSourceType
int GeoDataGenericSourceType
Definition: GeoParser.h:34
QXmlStreamReader::attributes
QXmlStreamAttributes attributes() const
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QXmlStreamReader::lineNumber
qint64 lineNumber() const
Marble::GeoParser::raiseWarning
void raiseWarning(const QString &)
Definition: GeoParser.cpp:193
QXmlStreamReader::name
QStringRef name() const
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::GeoParser::QualifiedName
QPair< QString, QString > QualifiedName
Definition: GeoParser.h:43
QStack
Marble::GeoParser::releaseDocument
GeoDocument * releaseDocument()
retrieve the parsed document and reset the parser If parsing was successful, retrieve the resulting d...
Definition: GeoParser.cpp:205
QXmlStreamReader::isEndElement
bool isEndElement() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal