1 : /******************************************************************************
2 : * $Id: gmlreaderp.h 18188 2009-12-05 22:19:00Z chaitanya $
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 :
86 : #ifdef XERCES_CPP_NAMESPACE_USE
87 : XERCES_CPP_NAMESPACE_USE
88 : #endif
89 :
90 :
91 : /************************************************************************/
92 : /* XMLCh / char translation functions - trstring.cpp */
93 : /************************************************************************/
94 : int tr_strcmp( const char *, const XMLCh * );
95 : void tr_strcpy( XMLCh *, const char * );
96 : void tr_strcpy( char *, const XMLCh * );
97 : char *tr_strdup( const XMLCh * );
98 : int tr_strlen( const XMLCh * );
99 :
100 : /************************************************************************/
101 : /* GMLXercesHandler */
102 : /************************************************************************/
103 : class GMLXercesHandler : public DefaultHandler, public GMLHandler
104 : {
105 : int m_nEntityCounter;
106 :
107 : public:
108 : GMLXercesHandler( GMLReader *poReader );
109 :
110 : void startElement(
111 : const XMLCh* const uri,
112 : const XMLCh* const localname,
113 : const XMLCh* const qname,
114 : const Attributes& attrs
115 : );
116 : void endElement(
117 : const XMLCh* const uri,
118 : const XMLCh* const localname,
119 : const XMLCh* const qname
120 : );
121 : #if XERCES_VERSION_MAJOR >= 3
122 : void characters( const XMLCh *const chars,
123 : const XMLSize_t length );
124 : #else
125 : void characters( const XMLCh *const chars,
126 : const unsigned int length );
127 : #endif
128 :
129 : void fatalError(const SAXParseException&);
130 :
131 : void startEntity (const XMLCh *const name);
132 :
133 : virtual char* GetFID(void* attr);
134 : virtual char* GetAttributes(void* attr);
135 : };
136 :
137 : #elif defined(HAVE_EXPAT)
138 :
139 : #include "ogr_expat.h"
140 :
141 : /************************************************************************/
142 : /* GMLExpatHandler */
143 : /************************************************************************/
144 : class GMLExpatHandler : public GMLHandler
145 46 : {
146 : XML_Parser m_oParser;
147 : int m_bStopParsing;
148 : int m_nDataHandlerCounter;
149 :
150 : public:
151 : GMLExpatHandler( GMLReader *poReader, XML_Parser oParser );
152 :
153 : virtual OGRErr startElement(const char *pszName, void* attr);
154 : virtual OGRErr endElement(const char *pszName);
155 : virtual OGRErr dataHandler(const char *data, int nLen);
156 :
157 23 : int HasStoppedParsing() { return m_bStopParsing; }
158 :
159 23 : void ResetDataHandlerCounter() { m_nDataHandlerCounter = 0; }
160 : int GetDataHandlerCounter() { return m_nDataHandlerCounter; }
161 :
162 : virtual char* GetFID(void* attr);
163 : virtual char* GetAttributes(void* attr);
164 : };
165 :
166 : #endif
167 :
168 : /************************************************************************/
169 : /* GMLReadState */
170 : /************************************************************************/
171 :
172 : class GMLReadState
173 : {
174 : void RebuildPath();
175 :
176 : public:
177 : GMLReadState();
178 : ~GMLReadState();
179 :
180 : void PushPath( const char *pszElement );
181 : void PopPath();
182 :
183 : int MatchPath( const char *pszPathInput );
184 : const char *GetPath() const { return m_pszPath; }
185 : const char *GetLastComponent() const;
186 :
187 : GMLFeature *m_poFeature;
188 : GMLReadState *m_poParentState;
189 :
190 : char *m_pszPath; // element path ... | as separator.
191 :
192 : int m_nPathLength;
193 : char **m_papszPathComponents;
194 : };
195 :
196 : /************************************************************************/
197 : /* GMLReader */
198 : /************************************************************************/
199 :
200 : class GMLReader : public IGMLReader
201 : {
202 : private:
203 : static int m_bXercesInitialized;
204 : static int m_nInstanceCount;
205 : int m_bClassListLocked;
206 :
207 : int m_nClassCount;
208 : GMLFeatureClass **m_papoClass;
209 :
210 : char *m_pszFilename;
211 :
212 : #if HAVE_XERCES == 1
213 : GMLXercesHandler *m_poGMLHandler;
214 : SAX2XMLReader *m_poSAXReader;
215 : XMLPScanToken m_oToFill;
216 : GMLFeature *m_poCompleteFeature;
217 : #else
218 : GMLExpatHandler *m_poGMLHandler;
219 : FILE* fpGML;
220 : XML_Parser oParser;
221 : GMLFeature ** ppoFeatureTab;
222 : int nFeatureTabLength;
223 : int nFeatureTabIndex;
224 : #endif
225 : int m_bReadStarted;
226 :
227 : GMLReadState *m_poState;
228 :
229 : int m_bStopParsing;
230 :
231 : int SetupParser();
232 : void CleanupParser();
233 :
234 : public:
235 : GMLReader();
236 : virtual ~GMLReader();
237 :
238 56 : int IsClassListLocked() const { return m_bClassListLocked; }
239 17 : void SetClassListLocked( int bFlag )
240 17 : { m_bClassListLocked = bFlag; }
241 :
242 : void SetSourceFile( const char *pszFilename );
243 :
244 194 : int GetClassCount() const { return m_nClassCount; }
245 : GMLFeatureClass *GetClass( int i ) const;
246 : GMLFeatureClass *GetClass( const char *pszName ) const;
247 :
248 : int AddClass( GMLFeatureClass *poClass );
249 : void ClearClasses();
250 :
251 : GMLFeature *NextFeature();
252 :
253 : int LoadClasses( const char *pszFile = NULL );
254 : int SaveClasses( const char *pszFile = NULL );
255 :
256 : int ParseXSD( const char *pszFile );
257 :
258 : int PrescanForSchema(int bGetExtents = TRUE );
259 : void ResetReading();
260 :
261 : // ---
262 :
263 1583 : GMLReadState *GetState() const { return m_poState; }
264 : void PopState();
265 : void PushState( GMLReadState * );
266 :
267 : int IsFeatureElement( const char *pszElement );
268 : int IsAttributeElement( const char *pszElement );
269 :
270 : void PushFeature( const char *pszElement,
271 : const char *pszFID );
272 :
273 : void SetFeatureProperty( const char *pszElement,
274 : const char *pszValue );
275 :
276 7 : int HasStoppedParsing() { return m_bStopParsing; }
277 :
278 : };
279 :
280 : #endif /* _CPL_GMLREADERP_H_INCLUDED */
|