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