1 : /******************************************************************************
2 : * $Id: gmlreaderp.h 20029 2010-07-11 18:38:23Z 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 :
36 : class GMLReader;
37 :
38 : /************************************************************************/
39 : /* GMLHandler */
40 : /************************************************************************/
41 : class GMLHandler
42 : {
43 : char *m_pszCurField;
44 :
45 : char *m_pszGeometry;
46 : int m_nGeomAlloc;
47 : int m_nGeomLen;
48 :
49 : int m_nGeometryDepth;
50 :
51 : int m_nDepth;
52 : int m_nDepthFeature;
53 :
54 : protected:
55 : GMLReader *m_poReader;
56 :
57 : public:
58 : GMLHandler( GMLReader *poReader );
59 : virtual ~GMLHandler();
60 :
61 : virtual OGRErr startElement(const char *pszName, void* attr);
62 : virtual OGRErr endElement(const char *pszName);
63 : virtual OGRErr dataHandler(const char *data, int nLen);
64 : virtual char* GetFID(void* attr) = 0;
65 : virtual char* GetAttributes(void* attr) = 0;
66 :
67 : int IsGeometryElement( const char *pszElement );
68 : };
69 :
70 :
71 : #if HAVE_XERCES == 1
72 :
73 : // This works around problems with math.h on some platforms #defining INFINITY
74 : #ifdef INFINITY
75 : #undef INFINITY
76 : #define INFINITY INFINITY_XERCES
77 : #endif
78 :
79 : #include <util/PlatformUtils.hpp>
80 : #include <sax2/DefaultHandler.hpp>
81 : #include <sax2/ContentHandler.hpp>
82 : #include <sax2/SAX2XMLReader.hpp>
83 : #include <sax2/XMLReaderFactory.hpp>
84 : #include <sax2/Attributes.hpp>
85 : #include <sax/InputSource.hpp>
86 : #include <util/BinInputStream.hpp>
87 :
88 : #ifdef XERCES_CPP_NAMESPACE_USE
89 : XERCES_CPP_NAMESPACE_USE
90 : #endif
91 :
92 : /************************************************************************/
93 : /* GMLBinInputStream */
94 : /************************************************************************/
95 : class GMLBinInputStream : public BinInputStream
96 : {
97 : FILE* fp;
98 : XMLCh emptyString;
99 :
100 : public :
101 :
102 : GMLBinInputStream(FILE* fp);
103 : virtual ~GMLBinInputStream();
104 :
105 : #if XERCES_VERSION_MAJOR >= 3
106 : virtual XMLFilePos curPos() const;
107 : virtual XMLSize_t readBytes(XMLByte* const toFill, const XMLSize_t maxToRead);
108 : virtual const XMLCh* getContentType() const ;
109 : #else
110 : virtual unsigned int curPos() const;
111 : virtual unsigned int readBytes(XMLByte* const toFill, const unsigned int maxToRead);
112 : #endif
113 : };
114 :
115 : /************************************************************************/
116 : /* GMLInputSource */
117 : /************************************************************************/
118 :
119 : class GMLInputSource : public InputSource
120 : {
121 : GMLBinInputStream* binInputStream;
122 :
123 : public:
124 : GMLInputSource(FILE* fp,
125 : MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
126 : virtual ~GMLInputSource();
127 :
128 : virtual BinInputStream* makeStream() const;
129 : };
130 :
131 :
132 : /************************************************************************/
133 : /* XMLCh / char translation functions - trstring.cpp */
134 : /************************************************************************/
135 : int tr_strcmp( const char *, const XMLCh * );
136 : void tr_strcpy( XMLCh *, const char * );
137 : void tr_strcpy( char *, const XMLCh * );
138 : char *tr_strdup( const XMLCh * );
139 : int tr_strlen( const XMLCh * );
140 :
141 : /************************************************************************/
142 : /* GMLXercesHandler */
143 : /************************************************************************/
144 : class GMLXercesHandler : public DefaultHandler, public GMLHandler
145 27 : {
146 : int m_nEntityCounter;
147 :
148 : public:
149 : GMLXercesHandler( GMLReader *poReader );
150 :
151 : void startElement(
152 : const XMLCh* const uri,
153 : const XMLCh* const localname,
154 : const XMLCh* const qname,
155 : const Attributes& attrs
156 : );
157 : void endElement(
158 : const XMLCh* const uri,
159 : const XMLCh* const localname,
160 : const XMLCh* const qname
161 : );
162 : #if XERCES_VERSION_MAJOR >= 3
163 : void characters( const XMLCh *const chars,
164 : const XMLSize_t length );
165 : #else
166 : void characters( const XMLCh *const chars,
167 : const unsigned int length );
168 : #endif
169 :
170 : void fatalError(const SAXParseException&);
171 :
172 : void startEntity (const XMLCh *const name);
173 :
174 : virtual char* GetFID(void* attr);
175 : virtual char* GetAttributes(void* attr);
176 : };
177 :
178 : #elif defined(HAVE_EXPAT)
179 :
180 : #include "ogr_expat.h"
181 :
182 : /************************************************************************/
183 : /* GMLExpatHandler */
184 : /************************************************************************/
185 : class GMLExpatHandler : public GMLHandler
186 : {
187 : XML_Parser m_oParser;
188 : int m_bStopParsing;
189 : int m_nDataHandlerCounter;
190 :
191 : public:
192 : GMLExpatHandler( GMLReader *poReader, XML_Parser oParser );
193 :
194 : virtual OGRErr startElement(const char *pszName, void* attr);
195 : virtual OGRErr endElement(const char *pszName);
196 : virtual OGRErr dataHandler(const char *data, int nLen);
197 :
198 : int HasStoppedParsing() { return m_bStopParsing; }
199 :
200 : void ResetDataHandlerCounter() { m_nDataHandlerCounter = 0; }
201 : int GetDataHandlerCounter() { return m_nDataHandlerCounter; }
202 :
203 : virtual char* GetFID(void* attr);
204 : virtual char* GetAttributes(void* attr);
205 : };
206 :
207 : #endif
208 :
209 : /************************************************************************/
210 : /* GMLReadState */
211 : /************************************************************************/
212 :
213 : class GMLReadState
214 : {
215 : void RebuildPath();
216 :
217 : public:
218 : GMLReadState();
219 : ~GMLReadState();
220 :
221 : void PushPath( const char *pszElement );
222 : void PopPath();
223 :
224 : int MatchPath( const char *pszPathInput );
225 : const char *GetPath() const { return m_pszPath; }
226 : const char *GetLastComponent() const;
227 :
228 : GMLFeature *m_poFeature;
229 : GMLReadState *m_poParentState;
230 :
231 : char *m_pszPath; // element path ... | as separator.
232 :
233 : int m_nPathLength;
234 : char **m_papszPathComponents;
235 : };
236 :
237 : /************************************************************************/
238 : /* GMLReader */
239 : /************************************************************************/
240 :
241 : class GMLReader : public IGMLReader
242 : {
243 : private:
244 : static int m_bXercesInitialized;
245 : static int m_nInstanceCount;
246 : int m_bClassListLocked;
247 :
248 : int m_nClassCount;
249 : GMLFeatureClass **m_papoClass;
250 :
251 : char *m_pszFilename;
252 :
253 : #if HAVE_XERCES == 1
254 : GMLXercesHandler *m_poGMLHandler;
255 : SAX2XMLReader *m_poSAXReader;
256 : XMLPScanToken m_oToFill;
257 : GMLFeature *m_poCompleteFeature;
258 : GMLInputSource *m_GMLInputSource;
259 : #else
260 : GMLExpatHandler *m_poGMLHandler;
261 : XML_Parser oParser;
262 : GMLFeature ** ppoFeatureTab;
263 : int nFeatureTabLength;
264 : int nFeatureTabIndex;
265 : #endif
266 : FILE* fpGML;
267 : int m_bReadStarted;
268 :
269 : GMLReadState *m_poState;
270 :
271 : int m_bStopParsing;
272 :
273 : int SetupParser();
274 : void CleanupParser();
275 :
276 : public:
277 : GMLReader();
278 : virtual ~GMLReader();
279 :
280 69 : int IsClassListLocked() const { return m_bClassListLocked; }
281 20 : void SetClassListLocked( int bFlag )
282 20 : { m_bClassListLocked = bFlag; }
283 :
284 : void SetSourceFile( const char *pszFilename );
285 : const char* GetSourceFileName();
286 :
287 211 : int GetClassCount() const { return m_nClassCount; }
288 : GMLFeatureClass *GetClass( int i ) const;
289 : GMLFeatureClass *GetClass( const char *pszName ) const;
290 :
291 : int AddClass( GMLFeatureClass *poClass );
292 : void ClearClasses();
293 :
294 : GMLFeature *NextFeature();
295 :
296 : int LoadClasses( const char *pszFile = NULL );
297 : int SaveClasses( const char *pszFile = NULL );
298 :
299 : int ParseXSD( const char *pszFile );
300 :
301 : int ResolveXlinks( const char *pszFile,
302 : int* pbOutIsTempFile,
303 : char **papszSkip = NULL,
304 : const int bStrict = FALSE );
305 :
306 : int PrescanForSchema(int bGetExtents = TRUE );
307 : void ResetReading();
308 :
309 : // ---
310 :
311 1766 : GMLReadState *GetState() const { return m_poState; }
312 : void PopState();
313 : void PushState( GMLReadState * );
314 :
315 : int IsFeatureElement( const char *pszElement );
316 : int IsAttributeElement( const char *pszElement );
317 :
318 : void PushFeature( const char *pszElement,
319 : const char *pszFID );
320 :
321 : void SetFeatureProperty( const char *pszElement,
322 : const char *pszValue );
323 :
324 8 : int HasStoppedParsing() { return m_bStopParsing; }
325 :
326 : };
327 :
328 : #endif /* _CPL_GMLREADERP_H_INCLUDED */
|