1 /******************************************************************************
2  * $Id: gmlreaderp.h 29217 2015-05-21 09:08:48Z rouault $
3  *
4  * Project:  GML Reader
5  * Purpose:  Private Declarations for OGR free GML Reader code.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2002, Frank Warmerdam
10  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  ****************************************************************************/
30 
31 #ifndef _CPL_GMLREADERP_H_INCLUDED
32 #define _CPL_GMLREADERP_H_INCLUDED
33 
34 #include "gmlreader.h"
35 #include "ogr_api.h"
36 #include "cpl_vsi.h"
37 #include "cpl_multiproc.h"
38 
39 #include <string>
40 #include <vector>
41 
42 #define PARSER_BUF_SIZE (10*8192)
43 
44 class GMLReader;
45 
46 typedef struct _GeometryNamesStruct GeometryNamesStruct;
47 
48 /************************************************************************/
49 /*                        GFSTemplateList                               */
50 /************************************************************************/
51 
52 class GFSTemplateItem;
53 
54 class GFSTemplateList
55 {
56 private:
57     int             m_bSequentialLayers;
58     GFSTemplateItem *pFirst;
59     GFSTemplateItem *pLast;
60     GFSTemplateItem *Insert( const char *pszName );
61 public:
62                     GFSTemplateList( void );
63                     ~GFSTemplateList();
64     void            Update( const char *pszName, int bHasGeom );
GetFirst()65     GFSTemplateItem *GetFirst() { return pFirst; }
HaveSequentialLayers()66     int             HaveSequentialLayers() { return m_bSequentialLayers; }
67     int             GetClassCount();
68 };
69 
70 void gmlUpdateFeatureClasses ( GFSTemplateList *pCC,
71                                GMLReader *pReader,
72                                int *pbSequentialLayers );
73 
74 /************************************************************************/
75 /*                              GMLHandler                              */
76 /************************************************************************/
77 
78 #define STACK_SIZE 5
79 
80 typedef enum
81 {
82     STATE_TOP,
83     STATE_DEFAULT,
84     STATE_FEATURE,
85     STATE_PROPERTY,
86     STATE_FEATUREPROPERTY,
87     STATE_GEOMETRY,
88     STATE_IGNORED_FEATURE,
89     STATE_BOUNDED_BY,
90     STATE_CITYGML_ATTRIBUTE
91 } HandlerState;
92 
93 typedef struct
94 {
95     CPLXMLNode* psNode;
96     CPLXMLNode* psLastChild;
97 } NodeLastChild;
98 
99 
100 typedef enum
101 {
102     APPSCHEMA_GENERIC,
103     APPSCHEMA_CITYGML,
104     APPSCHEMA_AIXM,
105     APPSCHEMA_MTKGML /* format of National Land Survey Finnish */
106 } GMLAppSchemaType;
107 
108 class GMLHandler
109 {
110     char      *m_pszCurField;
111     size_t     m_nCurFieldAlloc;
112     size_t     m_nCurFieldLen;
113     int        m_bInCurField;
114     int        m_nAttributeIndex;
115     int        m_nAttributeDepth;
116 
117 
118     char      *m_pszGeometry;
119     int        m_nGeomAlloc;
120     int        m_nGeomLen;
121     int        m_nGeometryDepth;
122     int        m_bAlreadyFoundGeometry;
123     int        m_nGeometryPropertyIndex;
124 
125     int        m_nDepth;
126     int        m_nDepthFeature;
127 
128     int        m_inBoundedByDepth;
129 
130     char      *m_pszCityGMLGenericAttrName;
131     int        m_inCityGMLGenericAttrDepth;
132 
133     int        m_bReportHref;
134     char      *m_pszHref;
135     char      *m_pszUom;
136     char      *m_pszValue;
137     char      *m_pszKieli;
138 
139     GeometryNamesStruct* pasGeometryNames;
140 
141     std::vector<NodeLastChild> apsXMLNode;
142 
143     OGRErr     startElementTop(const char *pszName, int nLenName, void* attr);
144 
145     OGRErr     endElementIgnoredFeature();
146 
147     OGRErr     startElementBoundedBy(const char *pszName, int nLenName, void* attr);
148     OGRErr     endElementBoundedBy();
149 
150     OGRErr     startElementFeatureAttribute(const char *pszName, int nLenName, void* attr);
151     OGRErr     endElementFeature();
152 
153     OGRErr     startElementCityGMLGenericAttr(const char *pszName, int nLenName, void* attr);
154     OGRErr     endElementCityGMLGenericAttr();
155 
156     OGRErr     startElementGeometry(const char *pszName, int nLenName, void* attr);
157     CPLXMLNode* ParseAIXMElevationPoint(CPLXMLNode*);
158     OGRErr     endElementGeometry();
159     OGRErr     dataHandlerGeometry(const char *data, int nLen);
160 
161     OGRErr     endElementAttribute();
162     OGRErr     dataHandlerAttribute(const char *data, int nLen);
163 
164     OGRErr     startElementDefault(const char *pszName, int nLenName, void* attr);
165     OGRErr     endElementDefault();
166 
167     OGRErr     startElementFeatureProperty(const char *pszName, int nLenName, void* attr);
168     OGRErr     endElementFeatureProperty();
169 
170     void       DealWithAttributes(const char *pszName, int nLenName, void* attr );
171     int        IsConditionMatched(const char* pszCondition, void* attr);
172     int        FindRealPropertyByCheckingConditions(int nIdx, void* attr);
173 
174 protected:
175     GMLReader  *m_poReader;
176     GMLAppSchemaType eAppSchemaType;
177 
178     int              nStackDepth;
179     HandlerState     stateStack[STACK_SIZE];
180 
181     std::string      osFID;
182     virtual const char* GetFID(void* attr) = 0;
183 
184     virtual CPLXMLNode* AddAttributes(CPLXMLNode* psNode, void* attr) = 0;
185 
186     OGRErr      startElement(const char *pszName, int nLenName, void* attr);
187     OGRErr      endElement();
188     OGRErr      dataHandler(const char *data, int nLen);
189 
190     int         IsGeometryElement( const char *pszElement );
191 
192 public:
193     GMLHandler( GMLReader *poReader );
194     virtual ~GMLHandler();
195 
196     virtual char*       GetAttributeValue(void* attr, const char* pszAttributeName) = 0;
197     virtual char*       GetAttributeByIdx(void* attr, unsigned int idx, char** ppszKey) = 0;
198 };
199 
200 
201 #if defined(HAVE_XERCES)
202 
203 // This works around problems with math.h on some platforms #defining INFINITY
204 #ifdef INFINITY
205 #undef  INFINITY
206 #define INFINITY INFINITY_XERCES
207 #endif
208 
209 #include <util/PlatformUtils.hpp>
210 #include <sax2/DefaultHandler.hpp>
211 #include <sax2/ContentHandler.hpp>
212 #include <sax2/SAX2XMLReader.hpp>
213 #include <sax2/XMLReaderFactory.hpp>
214 #include <sax2/Attributes.hpp>
215 #include <sax/InputSource.hpp>
216 #include <util/BinInputStream.hpp>
217 
218 #ifdef XERCES_CPP_NAMESPACE_USE
219 XERCES_CPP_NAMESPACE_USE
220 #endif
221 
222 /************************************************************************/
223 /*                        GMLBinInputStream                             */
224 /************************************************************************/
225 class GMLBinInputStream : public BinInputStream
226 {
227     VSILFILE* fp;
228     XMLCh emptyString;
229 
230 public :
231 
232              GMLBinInputStream(VSILFILE* fp);
233     virtual ~GMLBinInputStream();
234 
235 #if XERCES_VERSION_MAJOR >= 3
236     virtual XMLFilePos curPos() const;
237     virtual XMLSize_t readBytes(XMLByte* const toFill, const XMLSize_t maxToRead);
238     virtual const XMLCh* getContentType() const ;
239 #else
240     virtual unsigned int curPos() const;
241     virtual unsigned int readBytes(XMLByte* const toFill, const unsigned int maxToRead);
242 #endif
243 };
244 
245 /************************************************************************/
246 /*                           GMLInputSource                             */
247 /************************************************************************/
248 
249 class GMLInputSource : public InputSource
250 {
251     GMLBinInputStream* binInputStream;
252 
253 public:
254              GMLInputSource(VSILFILE* fp,
255                             MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
256     virtual ~GMLInputSource();
257 
258     virtual BinInputStream* makeStream() const;
259 };
260 
261 
262 /************************************************************************/
263 /*          XMLCh / char translation functions - trstring.cpp           */
264 /************************************************************************/
265 int tr_strcmp( const char *, const XMLCh * );
266 void tr_strcpy( XMLCh *, const char * );
267 void tr_strcpy( char *, const XMLCh * );
268 char *tr_strdup( const XMLCh * );
269 int tr_strlen( const XMLCh * );
270 
271 /************************************************************************/
272 /*                         GMLXercesHandler                             */
273 /************************************************************************/
274 class GMLXercesHandler : public DefaultHandler, public GMLHandler
275 {
276     int        m_nEntityCounter;
277 
278 public:
279     GMLXercesHandler( GMLReader *poReader );
280 
281     void startElement(
282         const   XMLCh* const    uri,
283         const   XMLCh* const    localname,
284         const   XMLCh* const    qname,
285         const   Attributes& attrs
286     );
287     void endElement(
288         const   XMLCh* const    uri,
289         const   XMLCh* const    localname,
290         const   XMLCh* const    qname
291     );
292 #if XERCES_VERSION_MAJOR >= 3
293     void characters( const XMLCh *const chars,
294                      const XMLSize_t length );
295 #else
296     void characters( const XMLCh *const chars,
297                      const unsigned int length );
298 #endif
299 
300     void fatalError(const SAXParseException&);
301 
302     void startEntity (const XMLCh *const name);
303 
304     virtual const char* GetFID(void* attr);
305     virtual CPLXMLNode* AddAttributes(CPLXMLNode* psNode, void* attr);
306     virtual char*       GetAttributeValue(void* attr, const char* pszAttributeName);
307     virtual char*       GetAttributeByIdx(void* attr, unsigned int idx, char** ppszKey);
308 };
309 
310 #endif
311 
312 
313 #if defined(HAVE_EXPAT)
314 
315 #include "ogr_expat.h"
316 
317 /************************************************************************/
318 /*                           GMLExpatHandler                            */
319 /************************************************************************/
320 class GMLExpatHandler : public GMLHandler
321 {
322     XML_Parser m_oParser;
323     int        m_bStopParsing;
324     int        m_nDataHandlerCounter;
325 
326 public:
327     GMLExpatHandler( GMLReader *poReader, XML_Parser oParser );
328 
HasStoppedParsing()329     int         HasStoppedParsing() { return m_bStopParsing; }
330 
ResetDataHandlerCounter()331     void        ResetDataHandlerCounter() { m_nDataHandlerCounter = 0; }
332 
333     virtual const char* GetFID(void* attr);
334     virtual CPLXMLNode* AddAttributes(CPLXMLNode* psNode, void* attr);
335     virtual char*       GetAttributeValue(void* attr, const char* pszAttributeName);
336     virtual char*       GetAttributeByIdx(void* attr, unsigned int idx, char** ppszKey);
337 
338     static void XMLCALL startElementCbk(void *pUserData, const char *pszName,
339                                         const char **ppszAttr);
340 
341     static void XMLCALL endElementCbk(void *pUserData, const char *pszName);
342 
343     static void XMLCALL dataHandlerCbk(void *pUserData, const char *data, int nLen);
344 };
345 
346 #endif
347 
348 /************************************************************************/
349 /*                             GMLReadState                             */
350 /************************************************************************/
351 
352 class GMLReadState
353 {
354     std::vector<std::string> aosPathComponents;
355 
356 public:
357     GMLReadState();
358     ~GMLReadState();
359 
360     void        PushPath( const char *pszElement, int nLen = -1 );
361     void        PopPath();
362 
GetLastComponent()363     const char  *GetLastComponent() const {
364         return ( m_nPathLength == 0 ) ? "" : aosPathComponents[m_nPathLength-1].c_str();
365     }
366 
367 
GetLastComponentLen()368     size_t GetLastComponentLen() const {
369         return ( m_nPathLength == 0 ) ? 0: aosPathComponents[m_nPathLength-1].size();
370     }
371 
372     void        Reset();
373 
374     GMLFeature  *m_poFeature;
375     GMLReadState *m_poParentState;
376 
377     std::string  osPath; // element path ... | as separator.
378     int          m_nPathLength;
379 };
380 
381 /************************************************************************/
382 /*                              GMLReader                               */
383 /************************************************************************/
384 
385 typedef enum
386 {
387     OGRGML_XERCES_UNINITIALIZED,
388     OGRGML_XERCES_INIT_FAILED,
389     OGRGML_XERCES_INIT_SUCCESSFUL
390 } OGRGMLXercesState;
391 
392 class GMLReader : public IGMLReader
393 {
394 private:
395     static OGRGMLXercesState    m_eXercesInitState;
396     static int    m_nInstanceCount;
397     int           m_bClassListLocked;
398 
399     int         m_nClassCount;
400     GMLFeatureClass **m_papoClass;
401     int           m_bLookForClassAtAnyLevel;
402 
403     char          *m_pszFilename;
404 
405     int            bUseExpatReader;
406 
407     GMLHandler    *m_poGMLHandler;
408 
409 #if defined(HAVE_XERCES)
410     SAX2XMLReader *m_poSAXReader;
411     XMLPScanToken m_oToFill;
412     GMLFeature   *m_poCompleteFeature;
413     GMLInputSource *m_GMLInputSource;
414     int           m_bEOF;
415     int           SetupParserXerces();
416     GMLFeature   *NextFeatureXerces();
417 #endif
418 
419 #if defined(HAVE_EXPAT)
420     XML_Parser    oParser;
421     GMLFeature ** ppoFeatureTab;
422     int           nFeatureTabLength;
423     int           nFeatureTabIndex;
424     int           nFeatureTabAlloc;
425     int           SetupParserExpat();
426     GMLFeature   *NextFeatureExpat();
427     char         *pabyBuf;
428 #endif
429 
430     VSILFILE*     fpGML;
431     int           m_bReadStarted;
432 
433     GMLReadState *m_poState;
434     GMLReadState *m_poRecycledState;
435 
436     int           m_bStopParsing;
437 
438     int           SetupParser();
439     void          CleanupParser();
440 
441     int           m_bFetchAllGeometries;
442 
443     int           m_bInvertAxisOrderIfLatLong;
444     int           m_bConsiderEPSGAsURN;
445     int           m_bGetSecondaryGeometryOption;
446 
447     int           ParseFeatureType(CPLXMLNode *psSchemaNode,
448                                 const char* pszName,
449                                 const char *pszType);
450 
451     char         *m_pszGlobalSRSName;
452     int           m_bCanUseGlobalSRSName;
453 
454     char         *m_pszFilteredClassName;
455     int           m_nFilteredClassIndex;
456 
457     int           m_bSequentialLayers;
458 
459     std::string   osElemPath;
460 
461     int           m_bFaceHoleNegative;
462 
463     int           m_bSetWidthFlag;
464 
465     int           m_bReportAllAttributes;
466 
467     int           m_bIsWFSJointLayer;
468 
469     int           m_bEmptyAsNull;
470 
471     int           ParseXMLHugeFile( const char *pszOutputFilename,
472                                     const int bSqliteIsTempFile,
473                                     const int iSqliteCacheMB );
474 
475 
476 public:
477                 GMLReader(int bExpatReader, int bInvertAxisOrderIfLatLong,
478                           int bConsiderEPSGAsURN, int bGetSecondaryGeometryOption);
479     virtual     ~GMLReader();
480 
IsClassListLocked()481     int              IsClassListLocked() const { return m_bClassListLocked; }
SetClassListLocked(int bFlag)482     void             SetClassListLocked( int bFlag )
483         { m_bClassListLocked = bFlag; }
484 
485     void             SetSourceFile( const char *pszFilename );
486     void             SetFP( VSILFILE* fp );
487     const char*      GetSourceFileName();
488 
GetClassCount()489     int              GetClassCount() const { return m_nClassCount; }
490     GMLFeatureClass *GetClass( int i ) const;
491     GMLFeatureClass *GetClass( const char *pszName ) const;
492 
493     int              AddClass( GMLFeatureClass *poClass );
494     void             ClearClasses();
495 
496     GMLFeature       *NextFeature();
497 
498     int              LoadClasses( const char *pszFile = NULL );
499     int              SaveClasses( const char *pszFile = NULL );
500 
501     int              ResolveXlinks( const char *pszFile,
502                                     int* pbOutIsTempFile,
503                                     char **papszSkip = NULL,
504                                     const int bStrict = FALSE );
505 
506     int              HugeFileResolver( const char *pszFile,
507                                        int pbSqliteIsTempFile,
508                                        int iSqliteCacheMB );
509 
510     int              PrescanForSchema(int bGetExtents = TRUE,
511                                       int bAnalyzeSRSPerFeature = TRUE,
512                                       int bOnlyDetectSRS = FALSE );
513     int              PrescanForTemplate( void );
514     int              ReArrangeTemplateClasses( GFSTemplateList *pCC );
515     void             ResetReading();
516 
517 // ---
518 
GetState()519     GMLReadState     *GetState() const { return m_poState; }
520     void             PopState();
521     void             PushState( GMLReadState * );
522 
ShouldLookForClassAtAnyLevel()523     int              ShouldLookForClassAtAnyLevel() { return m_bLookForClassAtAnyLevel; }
524 
525     int         GetFeatureElementIndex( const char *pszElement, int nLen, GMLAppSchemaType eAppSchemaType );
526     int         GetAttributeElementIndex( const char *pszElement, int nLen, const char* pszAttrKey = NULL );
527     int         IsCityGMLGenericAttributeElement( const char *pszElement, void* attr );
528 
529     void        PushFeature( const char *pszElement,
530                              const char *pszFID,
531                              int nClassIndex );
532 
533     void        SetFeaturePropertyDirectly( const char *pszElement,
534                                             char *pszValue,
535                                             int iPropertyIn,
536                                             GMLPropertyType eType = GMLPT_Untyped );
537 
SetWidthFlag(int bFlag)538     void        SetWidthFlag(int bFlag) { m_bSetWidthFlag = bFlag; }
539 
HasStoppedParsing()540     int         HasStoppedParsing() { return m_bStopParsing; }
541 
FetchAllGeometries()542     int         FetchAllGeometries() { return m_bFetchAllGeometries; }
543 
544     void        SetGlobalSRSName( const char* pszGlobalSRSName ) ;
GetGlobalSRSName()545     const char* GetGlobalSRSName() { return m_pszGlobalSRSName; }
546 
CanUseGlobalSRSName()547     int         CanUseGlobalSRSName() { return m_bCanUseGlobalSRSName; }
548 
549     int         SetFilteredClassName(const char* pszClassName);
GetFilteredClassName()550     const char* GetFilteredClassName() { return m_pszFilteredClassName; }
GetFilteredClassIndex()551     int         GetFilteredClassIndex() { return m_nFilteredClassIndex; }
552 
IsSequentialLayers()553     int         IsSequentialLayers() const { return m_bSequentialLayers == TRUE; }
554 
SetReportAllAttributes(int bFlag)555     void        SetReportAllAttributes(int bFlag) { m_bReportAllAttributes = bFlag; }
ReportAllAttributes()556     int         ReportAllAttributes() const { return m_bReportAllAttributes; }
557 
SetIsWFSJointLayer(int bFlag)558     void             SetIsWFSJointLayer( int bFlag ) { m_bIsWFSJointLayer = bFlag; }
IsWFSJointLayer()559     int              IsWFSJointLayer() const { return m_bIsWFSJointLayer; }
560 
SetEmptyAsNull(int bFlag)561     void             SetEmptyAsNull( int bFlag ) { m_bEmptyAsNull = bFlag; }
IsEmptyAsNull()562     int              IsEmptyAsNull() const { return m_bEmptyAsNull; }
563 
564     static CPLMutex* hMutex;
565 };
566 
567 #endif /* _CPL_GMLREADERP_H_INCLUDED */
568