QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsgmlschema.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgmlschema.h
3  --------------------------------------
4  Date : Sun Sep 16 12:19:55 AKDT 2007
5  Copyright : (C) 2007 by Gary E. Sherman
6  Email : sherman at mrcc dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSGMLSCHEMA_H
16 #define QGSGMLSCHEMA_H
17 
18 #include <expat.h>
19 #include "qgis.h"
20 #include "qgsapplication.h"
21 #include "qgsdataprovider.h"
22 #include "qgsfeature.h"
23 #include "qgsfield.h"
24 #include "qgslogger.h"
25 #include "qgspoint.h"
26 #include <list>
27 #include <set>
28 #include <stack>
29 #include <QPair>
30 #include <QByteArray>
31 #include <QDomElement>
32 #include <QStringList>
33 #include <QStack>
34 class QgsRectangle;
36 
37 /* Description of feature class in GML */
38 class CORE_EXPORT QgsGmlFeatureClass
39 {
40  public:
42  QgsGmlFeatureClass( QString name, QString path );
43 
45 
46  QList<QgsField> & fields() { return mFields; }
47 
48  int fieldIndex( const QString & name );
49 
50  QString path() const { return mPath; }
51 
52  QStringList & geometryAttributes() { return mGeometryAttributes; }
53 
54  private:
55  /* Feature class name:
56  * - element name without NS or known prefix/suffix (_feature)
57  * - typeName attribute name */
58  QString mName;
59 
60  //QString mElementName;
61 
62  /* Dot separated path to element including the name */
63  QString mPath;
64 
65  /* Fields */
66  // Do not use QMap to keep original fields order. If it gets to performance,
67  // add a field index map
68  QList<QgsField> mFields;
69 
70  /* Geometry attribute */
71  QStringList mGeometryAttributes;
72 };
73 
74 class CORE_EXPORT QgsGmlSchema: public QObject
75 {
76  Q_OBJECT
77  public:
78  QgsGmlSchema();
79 
80  ~QgsGmlSchema();
81 
83  bool parseXSD( const QByteArray &xml );
84 
89  bool guessSchema( const QByteArray &data );
90 
92  QStringList typeNames() const;
93 
95  QList<QgsField> fields( const QString & typeName );
96 
98  QStringList geometryAttributes( const QString & typeName );
99 
100  private:
101 
103  {
106  featureMember, // gml:featureMember
107  feature, // feature element containint attrs and geo (inside gml:featureMember)
109  geometry
110  };
111 
113  void startElement( const XML_Char* el, const XML_Char** attr );
114  void endElement( const XML_Char* el );
115  void characters( const XML_Char* chars, int len );
116  static void start( void* data, const XML_Char* el, const XML_Char** attr )
117  {
118  static_cast<QgsGmlSchema*>( data )->startElement( el, attr );
119  }
120  static void end( void* data, const XML_Char* el )
121  {
122  static_cast<QgsGmlSchema*>( data )->endElement( el );
123  }
124  static void chars( void* data, const XML_Char* chars, int len )
125  {
126  static_cast<QgsGmlSchema*>( data )->characters( chars, len );
127  }
128 
129  //helper routines
130 
133  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
134 
136  QWidget* findMainWindow() const;
137 
139  QList<QDomElement> domElements( const QDomElement &element, const QString & path );
140 
142  QDomElement domElement( const QDomElement &element, const QString & path );
143 
145  QList<QDomElement> domElements( QList<QDomElement> &elements, const QString & attr, const QString & attrVal );
146 
148  QDomElement domElement( const QDomElement &element, const QString & path, const QString & attr, const QString & attrVal );
149 
151  QString stripNS( const QString & name );
152 
158  QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString & name );
159 
161  bool xsdFeatureClass( const QDomElement &element, const QString & typeName, QgsGmlFeatureClass & featureClass );
162 
163 
165  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
166 
168  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
169 
171  //std::stack<ParseMode> mParseModeStack;
172  QStack<ParseMode> mParseModeStack;
174  QString mStringCash;
178  QString mAttributeName;
183 
184  /* Schema information guessed/parsed from GML in getSchema() */
185 
187  int mLevel;
188 
191 
193  QStringList mParsePathStack;
194 
196 
197  // List of know geometries (Point, Multipoint,...)
198  QStringList mGeometryTypes;
199 
200  /* Feature classes map with element paths as keys */
201  QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
202 };
203 
204 #endif