1 : /******************************************************************************
2 : * $Id: gmlreaderp.h 25183 2012-10-27 18:09:53Z 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 4 : GFSTemplateItem *GetFirst() { return pFirst; }
64 2 : 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 25625 : } 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 : int m_bAlreadyFoundGeometry;
111 :
112 : int m_nDepth;
113 : int m_nDepthFeature;
114 :
115 : int m_inBoundedByDepth;
116 :
117 : char *m_pszCityGMLGenericAttrName;
118 : int m_inCityGMLGenericAttrDepth;
119 : int m_bIsCityGML;
120 :
121 : int m_bReportHref;
122 : int m_bIsAIXM;
123 : char *m_pszHref;
124 : char *m_pszUom;
125 : char *m_pszValue;
126 :
127 : GeometryNamesStruct* pasGeometryNames;
128 :
129 : std::vector<NodeLastChild> apsXMLNode;
130 :
131 : OGRErr startElementTop(const char *pszName, int nLenName, void* attr);
132 :
133 : OGRErr endElementIgnoredFeature();
134 :
135 : OGRErr startElementBoundedBy(const char *pszName, int nLenName, void* attr);
136 : OGRErr endElementBoundedBy();
137 :
138 : OGRErr startElementFeatureAttribute(const char *pszName, int nLenName, void* attr);
139 : OGRErr endElementFeature();
140 :
141 : OGRErr startElementCityGMLGenericAttr(const char *pszName, int nLenName, void* attr);
142 : OGRErr endElementCityGMLGenericAttr();
143 :
144 : OGRErr startElementGeometry(const char *pszName, int nLenName, void* attr);
145 : CPLXMLNode* ParseAIXMElevationPoint(CPLXMLNode*);
146 : OGRErr endElementGeometry();
147 : OGRErr dataHandlerGeometry(const char *data, int nLen);
148 :
149 : OGRErr endElementAttribute();
150 : OGRErr dataHandlerAttribute(const char *data, int nLen);
151 :
152 : OGRErr startElementDefault(const char *pszName, int nLenName, void* attr);
153 : OGRErr endElementDefault();
154 :
155 : protected:
156 : GMLReader *m_poReader;
157 :
158 : int nStackDepth;
159 : HandlerState stateStack[STACK_SIZE];
160 :
161 : std::string osFID;
162 : virtual const char* GetFID(void* attr) = 0;
163 :
164 : virtual CPLXMLNode* AddAttributes(CPLXMLNode* psNode, void* attr) = 0;
165 :
166 : OGRErr startElement(const char *pszName, int nLenName, void* attr);
167 : OGRErr endElement();
168 : OGRErr dataHandler(const char *data, int nLen);
169 :
170 : int IsGeometryElement( const char *pszElement );
171 :
172 : public:
173 : GMLHandler( GMLReader *poReader );
174 : virtual ~GMLHandler();
175 :
176 : virtual char* GetAttributeValue(void* attr, const char* pszAttributeName) = 0;
177 : };
178 :
179 :
180 : #if defined(HAVE_XERCES)
181 :
182 : // This works around problems with math.h on some platforms #defining INFINITY
183 : #ifdef INFINITY
184 : #undef INFINITY
185 : #define INFINITY INFINITY_XERCES
186 : #endif
187 :
188 : #include <util/PlatformUtils.hpp>
189 : #include <sax2/DefaultHandler.hpp>
190 : #include <sax2/ContentHandler.hpp>
191 : #include <sax2/SAX2XMLReader.hpp>
192 : #include <sax2/XMLReaderFactory.hpp>
193 : #include <sax2/Attributes.hpp>
194 : #include <sax/InputSource.hpp>
195 : #include <util/BinInputStream.hpp>
196 :
197 : #ifdef XERCES_CPP_NAMESPACE_USE
198 : XERCES_CPP_NAMESPACE_USE
199 : #endif
200 :
201 : /************************************************************************/
202 : /* GMLBinInputStream */
203 : /************************************************************************/
204 : class GMLBinInputStream : public BinInputStream
205 : {
206 : VSILFILE* fp;
207 : XMLCh emptyString;
208 :
209 : public :
210 :
211 : GMLBinInputStream(VSILFILE* fp);
212 : virtual ~GMLBinInputStream();
213 :
214 : #if XERCES_VERSION_MAJOR >= 3
215 : virtual XMLFilePos curPos() const;
216 : virtual XMLSize_t readBytes(XMLByte* const toFill, const XMLSize_t maxToRead);
217 : virtual const XMLCh* getContentType() const ;
218 : #else
219 : virtual unsigned int curPos() const;
220 : virtual unsigned int readBytes(XMLByte* const toFill, const unsigned int maxToRead);
221 : #endif
222 : };
223 :
224 : /************************************************************************/
225 : /* GMLInputSource */
226 : /************************************************************************/
227 :
228 : class GMLInputSource : public InputSource
229 : {
230 : GMLBinInputStream* binInputStream;
231 :
232 : public:
233 : GMLInputSource(VSILFILE* fp,
234 : MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
235 : virtual ~GMLInputSource();
236 :
237 : virtual BinInputStream* makeStream() const;
238 : };
239 :
240 :
241 : /************************************************************************/
242 : /* XMLCh / char translation functions - trstring.cpp */
243 : /************************************************************************/
244 : int tr_strcmp( const char *, const XMLCh * );
245 : void tr_strcpy( XMLCh *, const char * );
246 : void tr_strcpy( char *, const XMLCh * );
247 : char *tr_strdup( const XMLCh * );
248 : int tr_strlen( const XMLCh * );
249 :
250 : /************************************************************************/
251 : /* GMLXercesHandler */
252 : /************************************************************************/
253 : class GMLXercesHandler : public DefaultHandler, public GMLHandler
254 0 : {
255 : int m_nEntityCounter;
256 :
257 : public:
258 : GMLXercesHandler( GMLReader *poReader );
259 :
260 : void startElement(
261 : const XMLCh* const uri,
262 : const XMLCh* const localname,
263 : const XMLCh* const qname,
264 : const Attributes& attrs
265 : );
266 : void endElement(
267 : const XMLCh* const uri,
268 : const XMLCh* const localname,
269 : const XMLCh* const qname
270 : );
271 : #if XERCES_VERSION_MAJOR >= 3
272 : void characters( const XMLCh *const chars,
273 : const XMLSize_t length );
274 : #else
275 : void characters( const XMLCh *const chars,
276 : const unsigned int length );
277 : #endif
278 :
279 : void fatalError(const SAXParseException&);
280 :
281 : void startEntity (const XMLCh *const name);
282 :
283 : virtual const char* GetFID(void* attr);
284 : virtual CPLXMLNode* AddAttributes(CPLXMLNode* psNode, void* attr);
285 : virtual char* GetAttributeValue(void* attr, const char* pszAttributeName);
286 : };
287 :
288 : #endif
289 :
290 :
291 : #if defined(HAVE_EXPAT)
292 :
293 : #include "ogr_expat.h"
294 :
295 : /************************************************************************/
296 : /* GMLExpatHandler */
297 : /************************************************************************/
298 : class GMLExpatHandler : public GMLHandler
299 200 : {
300 : XML_Parser m_oParser;
301 : int m_bStopParsing;
302 : int m_nDataHandlerCounter;
303 :
304 : public:
305 : GMLExpatHandler( GMLReader *poReader, XML_Parser oParser );
306 :
307 253 : int HasStoppedParsing() { return m_bStopParsing; }
308 :
309 253 : void ResetDataHandlerCounter() { m_nDataHandlerCounter = 0; }
310 :
311 : virtual const char* GetFID(void* attr);
312 : virtual CPLXMLNode* AddAttributes(CPLXMLNode* psNode, void* attr);
313 : virtual char* GetAttributeValue(void* attr, const char* pszAttributeName);
314 :
315 : static void XMLCALL startElementCbk(void *pUserData, const char *pszName,
316 : const char **ppszAttr);
317 :
318 : static void XMLCALL endElementCbk(void *pUserData, const char *pszName);
319 :
320 : static void XMLCALL dataHandlerCbk(void *pUserData, const char *data, int nLen);
321 : };
322 :
323 : #endif
324 :
325 : /************************************************************************/
326 : /* GMLReadState */
327 : /************************************************************************/
328 :
329 : class GMLReadState
330 : {
331 : std::vector<std::string> aosPathComponents;
332 :
333 : public:
334 : GMLReadState();
335 : ~GMLReadState();
336 :
337 : void PushPath( const char *pszElement, int nLen = -1 );
338 : void PopPath();
339 :
340 899822 : const char *GetLastComponent() const {
341 899822 : return ( m_nPathLength == 0 ) ? "" : aosPathComponents[m_nPathLength-1].c_str();
342 : }
343 :
344 :
345 6905 : size_t GetLastComponentLen() const {
346 6905 : return ( m_nPathLength == 0 ) ? 0: aosPathComponents[m_nPathLength-1].size();
347 : }
348 :
349 : void Reset();
350 :
351 : GMLFeature *m_poFeature;
352 : GMLReadState *m_poParentState;
353 :
354 : std::string osPath; // element path ... | as separator.
355 : int m_nPathLength;
356 : };
357 :
358 : /************************************************************************/
359 : /* GMLReader */
360 : /************************************************************************/
361 :
362 : class GMLReader : public IGMLReader
363 : {
364 : private:
365 : static int m_bXercesInitialized;
366 : static int m_nInstanceCount;
367 : int m_bClassListLocked;
368 :
369 : int m_nClassCount;
370 : GMLFeatureClass **m_papoClass;
371 :
372 : char *m_pszFilename;
373 :
374 : int bUseExpatReader;
375 :
376 : GMLHandler *m_poGMLHandler;
377 :
378 : #if defined(HAVE_XERCES)
379 : SAX2XMLReader *m_poSAXReader;
380 : XMLPScanToken m_oToFill;
381 : GMLFeature *m_poCompleteFeature;
382 : GMLInputSource *m_GMLInputSource;
383 : int m_bEOF;
384 : int SetupParserXerces();
385 : GMLFeature *NextFeatureXerces();
386 : #endif
387 :
388 : #if defined(HAVE_EXPAT)
389 : XML_Parser oParser;
390 : GMLFeature ** ppoFeatureTab;
391 : int nFeatureTabLength;
392 : int nFeatureTabIndex;
393 : int nFeatureTabAlloc;
394 : int SetupParserExpat();
395 : GMLFeature *NextFeatureExpat();
396 : char *pabyBuf;
397 : #endif
398 :
399 : VSILFILE* fpGML;
400 : int m_bReadStarted;
401 :
402 : GMLReadState *m_poState;
403 : GMLReadState *m_poRecycledState;
404 :
405 : int m_bStopParsing;
406 :
407 : int SetupParser();
408 : void CleanupParser();
409 :
410 : int m_bFetchAllGeometries;
411 :
412 : int m_bInvertAxisOrderIfLatLong;
413 : int m_bConsiderEPSGAsURN;
414 : int m_bGetSecondaryGeometryOption;
415 :
416 : int ParseFeatureType(CPLXMLNode *psSchemaNode,
417 : const char* pszName,
418 : const char *pszType);
419 :
420 : char *m_pszGlobalSRSName;
421 : int m_bCanUseGlobalSRSName;
422 :
423 : char *m_pszFilteredClassName;
424 :
425 : int m_bSequentialLayers;
426 :
427 : std::string osElemPath;
428 :
429 : int m_bFaceHoleNegative;
430 :
431 : int ParseXMLHugeFile( const char *pszOutputFilename,
432 : const int bSqliteIsTempFile,
433 : const int iSqliteCacheMB );
434 :
435 :
436 : public:
437 : GMLReader(int bExpatReader, int bInvertAxisOrderIfLatLong,
438 : int bConsiderEPSGAsURN, int bGetSecondaryGeometryOption);
439 : virtual ~GMLReader();
440 :
441 0 : int IsClassListLocked() const { return m_bClassListLocked; }
442 149 : void SetClassListLocked( int bFlag )
443 149 : { m_bClassListLocked = bFlag; }
444 :
445 : void SetSourceFile( const char *pszFilename );
446 : void SetFP( VSILFILE* fp );
447 : const char* GetSourceFileName();
448 :
449 907 : int GetClassCount() const { return m_nClassCount; }
450 : GMLFeatureClass *GetClass( int i ) const;
451 : GMLFeatureClass *GetClass( const char *pszName ) const;
452 :
453 : int AddClass( GMLFeatureClass *poClass );
454 : void ClearClasses();
455 :
456 : GMLFeature *NextFeature();
457 :
458 : int LoadClasses( const char *pszFile = NULL );
459 : int SaveClasses( const char *pszFile = NULL );
460 :
461 : int ResolveXlinks( const char *pszFile,
462 : int* pbOutIsTempFile,
463 : char **papszSkip = NULL,
464 : const int bStrict = FALSE );
465 :
466 : int HugeFileResolver( const char *pszFile,
467 : int pbSqliteIsTempFile,
468 : int iSqliteCacheMB );
469 :
470 : int PrescanForSchema(int bGetExtents = TRUE );
471 : int PrescanForTemplate( void );
472 : int ReArrangeTemplateClasses( GFSTemplateList *pCC );
473 : void ResetReading();
474 :
475 : // ---
476 :
477 66363 : GMLReadState *GetState() const { return m_poState; }
478 : void PopState();
479 : void PushState( GMLReadState * );
480 :
481 : int GetFeatureElementIndex( const char *pszElement, int nLen );
482 : int GetAttributeElementIndex( const char *pszElement, int nLen );
483 : int IsCityGMLGenericAttributeElement( const char *pszElement, void* attr );
484 :
485 : void PushFeature( const char *pszElement,
486 : const char *pszFID,
487 : int nClassIndex );
488 :
489 : void SetFeaturePropertyDirectly( const char *pszElement,
490 : char *pszValue,
491 : int iPropertyIn );
492 :
493 25 : int HasStoppedParsing() { return m_bStopParsing; }
494 :
495 3520 : int FetchAllGeometries() { return m_bFetchAllGeometries; }
496 :
497 : void SetGlobalSRSName( const char* pszGlobalSRSName ) ;
498 1072 : const char* GetGlobalSRSName() { return m_pszGlobalSRSName; }
499 :
500 1199 : int CanUseGlobalSRSName() { return m_bCanUseGlobalSRSName; }
501 :
502 : int SetFilteredClassName(const char* pszClassName);
503 2508 : const char* GetFilteredClassName() { return m_pszFilteredClassName; }
504 :
505 17 : int IsSequentialLayers() const { return m_bSequentialLayers == TRUE; }
506 : };
507 :
508 : #endif /* _CPL_GMLREADERP_H_INCLUDED */
|