Line data Source code
1 : /**********************************************************************
2 : *
3 : * Project: GML Reader
4 : * Purpose: Implementation of GMLHandler class.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : **********************************************************************
8 : * Copyright (c) 2002, Frank Warmerdam
9 : * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "cpl_port.h"
15 : #include "gmlreader.h"
16 : #include "gmlreaderp.h"
17 :
18 : #include <algorithm>
19 : #include <climits>
20 : #include <cstddef>
21 : #include <cstdlib>
22 : #include <cstring>
23 : #include <memory>
24 : #include <string>
25 : #include <vector>
26 :
27 : #include "cpl_conv.h"
28 : #include "cpl_error.h"
29 : #include "cpl_hash_set.h"
30 : #include "cpl_minixml.h"
31 : #include "cpl_string.h"
32 : #include "cpl_vsi.h"
33 : #ifdef HAVE_EXPAT
34 : #include "expat.h"
35 : #include "expat_external.h"
36 : #endif
37 : #include "ogr_core.h"
38 : #ifdef HAVE_XERCES
39 : #include "ogr_xerces.h"
40 : #endif
41 :
42 : #ifdef HAVE_XERCES
43 :
44 : /************************************************************************/
45 : /* GMLXercesHandler() */
46 : /************************************************************************/
47 :
48 2 : GMLXercesHandler::GMLXercesHandler(GMLReader *poReader)
49 2 : : GMLHandler(poReader), m_nEntityCounter(0)
50 : {
51 2 : }
52 :
53 : /************************************************************************/
54 : /* GMLXercesHandlerDealWithError() */
55 : /************************************************************************/
56 :
57 939 : static void GMLXercesHandlerDealWithError(OGRErr eErr)
58 : {
59 939 : if (eErr == OGRERR_NOT_ENOUGH_MEMORY)
60 : {
61 0 : throw SAXNotSupportedException("Out of memory");
62 : }
63 939 : else if (eErr != OGRERR_NONE)
64 : {
65 0 : throw SAXNotSupportedException("Other error during parsing");
66 : }
67 939 : }
68 :
69 : /************************************************************************/
70 : /* startElement() */
71 : /************************************************************************/
72 :
73 19 : void GMLXercesHandler::startElement(const XMLCh *const /*uri*/,
74 : const XMLCh *const localname,
75 : const XMLCh *const /*qname*/,
76 : const Attributes &attrs)
77 : {
78 19 : m_nEntityCounter = 0;
79 :
80 19 : transcode(localname, m_osElement);
81 :
82 19 : GMLXercesHandlerDealWithError(GMLHandler::startElement(
83 19 : m_osElement.c_str(), static_cast<int>(m_osElement.size()),
84 : const_cast<Attributes *>(&attrs)));
85 19 : }
86 :
87 : /************************************************************************/
88 : /* endElement() */
89 : /************************************************************************/
90 15 : void GMLXercesHandler::endElement(const XMLCh *const /*uri*/,
91 : const XMLCh *const /*localname*/,
92 : const XMLCh *const /*qname */)
93 : {
94 15 : m_nEntityCounter = 0;
95 :
96 15 : GMLXercesHandlerDealWithError(GMLHandler::endElement());
97 15 : }
98 :
99 : /************************************************************************/
100 : /* characters() */
101 : /************************************************************************/
102 :
103 905 : void GMLXercesHandler::characters(const XMLCh *const chars_in,
104 : const XMLSize_t length)
105 :
106 : {
107 905 : transcode(chars_in, m_osCharacters, static_cast<int>(length));
108 905 : GMLXercesHandlerDealWithError(GMLHandler::dataHandler(
109 905 : m_osCharacters.c_str(), static_cast<int>(m_osCharacters.size())));
110 905 : }
111 :
112 : /************************************************************************/
113 : /* fatalError() */
114 : /************************************************************************/
115 :
116 0 : void GMLXercesHandler::fatalError(const SAXParseException &exception)
117 :
118 : {
119 0 : CPLString osMsg;
120 0 : transcode(exception.getMessage(), osMsg);
121 0 : CPLError(CE_Failure, CPLE_AppDefined,
122 : "XML Parsing Error: %s at line %d, column %d\n", osMsg.c_str(),
123 0 : static_cast<int>(exception.getLineNumber()),
124 0 : static_cast<int>(exception.getColumnNumber()));
125 0 : }
126 :
127 : /************************************************************************/
128 : /* startEntity() */
129 : /************************************************************************/
130 :
131 1001 : void GMLXercesHandler::startEntity(const XMLCh *const /* name */)
132 : {
133 1001 : m_nEntityCounter++;
134 1001 : if (m_nEntityCounter > 1000 && !m_poReader->HasStoppedParsing())
135 : {
136 : throw SAXNotSupportedException(
137 1 : "File probably corrupted (million laugh pattern)");
138 : }
139 1000 : }
140 :
141 : /************************************************************************/
142 : /* GetFID() */
143 : /************************************************************************/
144 :
145 1 : const char *GMLXercesHandler::GetFID(void *attr)
146 : {
147 1 : const Attributes *attrs = static_cast<const Attributes *>(attr);
148 1 : const XMLCh achFID[] = {'f', 'i', 'd', '\0'};
149 1 : int nFIDIndex = attrs->getIndex(achFID);
150 1 : if (nFIDIndex != -1)
151 : {
152 1 : transcode(attrs->getValue(nFIDIndex), m_osFID);
153 1 : return m_osFID.c_str();
154 : }
155 : else
156 : {
157 0 : const XMLCh achGMLID[] = {'g', 'm', 'l', ':', 'i', 'd', '\0'};
158 0 : nFIDIndex = attrs->getIndex(achGMLID);
159 0 : if (nFIDIndex != -1)
160 : {
161 0 : transcode(attrs->getValue(nFIDIndex), m_osFID);
162 0 : return m_osFID.c_str();
163 : }
164 : }
165 :
166 0 : m_osFID.resize(0);
167 0 : return nullptr;
168 : }
169 :
170 : /************************************************************************/
171 : /* AddAttributes() */
172 : /************************************************************************/
173 :
174 2 : CPLXMLNode *GMLXercesHandler::AddAttributes(CPLXMLNode *psNode, void *attr)
175 : {
176 2 : const Attributes *attrs = static_cast<const Attributes *>(attr);
177 :
178 2 : CPLXMLNode *psLastChild = nullptr;
179 :
180 5 : for (unsigned int i = 0; i < attrs->getLength(); i++)
181 : {
182 3 : transcode(attrs->getQName(i), m_osAttrName);
183 3 : transcode(attrs->getValue(i), m_osAttrValue);
184 :
185 : CPLXMLNode *psChild =
186 3 : CPLCreateXMLNode(nullptr, CXT_Attribute, m_osAttrName.c_str());
187 3 : CPLCreateXMLNode(psChild, CXT_Text, m_osAttrValue.c_str());
188 :
189 3 : if (psLastChild == nullptr)
190 1 : psNode->psChild = psChild;
191 : else
192 2 : psLastChild->psNext = psChild;
193 3 : psLastChild = psChild;
194 : }
195 :
196 2 : return psLastChild;
197 : }
198 :
199 : /************************************************************************/
200 : /* GetAttributeValue() */
201 : /************************************************************************/
202 :
203 8 : char *GMLXercesHandler::GetAttributeValue(void *attr,
204 : const char *pszAttributeName)
205 : {
206 8 : const Attributes *attrs = static_cast<const Attributes *>(attr);
207 8 : for (unsigned int i = 0; i < attrs->getLength(); i++)
208 : {
209 0 : transcode(attrs->getQName(i), m_osAttrName);
210 0 : if (m_osAttrName == pszAttributeName)
211 : {
212 0 : transcode(attrs->getValue(i), m_osAttrValue);
213 0 : return CPLStrdup(m_osAttrValue);
214 : }
215 : }
216 8 : return nullptr;
217 : }
218 :
219 : /************************************************************************/
220 : /* GetAttributeByIdx() */
221 : /************************************************************************/
222 :
223 9 : char *GMLXercesHandler::GetAttributeByIdx(void *attr, unsigned int idx,
224 : char **ppszKey)
225 : {
226 9 : const Attributes *attrs = static_cast<const Attributes *>(attr);
227 9 : if (idx >= attrs->getLength())
228 : {
229 9 : *ppszKey = nullptr;
230 9 : return nullptr;
231 : }
232 0 : transcode(attrs->getQName(idx), m_osAttrName);
233 0 : transcode(attrs->getValue(idx), m_osAttrValue);
234 :
235 0 : *ppszKey = CPLStrdup(m_osAttrName);
236 0 : return CPLStrdup(m_osAttrValue);
237 : }
238 :
239 : #endif
240 :
241 : #ifdef HAVE_EXPAT
242 :
243 : /************************************************************************/
244 : /* GMLExpatHandler() */
245 : /************************************************************************/
246 :
247 585 : GMLExpatHandler::GMLExpatHandler(GMLReader *poReader, XML_Parser oParser)
248 : : GMLHandler(poReader), m_oParser(oParser), m_bStopParsing(false),
249 585 : m_nDataHandlerCounter(0)
250 : {
251 585 : }
252 :
253 : /************************************************************************/
254 : /* GMLExpatHandler::DealWithError() */
255 : /************************************************************************/
256 :
257 367850 : void GMLExpatHandler::DealWithError(OGRErr eErr)
258 : {
259 367850 : if (eErr != OGRERR_NONE)
260 : {
261 2 : m_bStopParsing = true;
262 2 : XML_StopParser(m_oParser, static_cast<XML_Bool>(false));
263 2 : if (eErr == OGRERR_NOT_ENOUGH_MEMORY)
264 0 : CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
265 : }
266 367850 : }
267 :
268 : /************************************************************************/
269 : /* startElementCbk() */
270 : /************************************************************************/
271 :
272 60897 : void XMLCALL GMLExpatHandler::startElementCbk(void *pUserData,
273 : const char *pszName,
274 : const char **ppszAttr)
275 :
276 : {
277 60897 : GMLExpatHandler *pThis = static_cast<GMLExpatHandler *>(pUserData);
278 60897 : if (pThis->m_bStopParsing)
279 0 : return;
280 :
281 60897 : const char *pszIter = pszName;
282 60897 : char ch = '\0';
283 798415 : while ((ch = *pszIter) != '\0')
284 : {
285 737518 : if (ch == ':')
286 47717 : pszName = pszIter + 1;
287 737518 : pszIter++;
288 : }
289 :
290 60897 : pThis->DealWithError(pThis->GMLHandler::startElement(
291 60897 : pszName, static_cast<int>(pszIter - pszName), ppszAttr));
292 : }
293 :
294 : /************************************************************************/
295 : /* endElementCbk() */
296 : /************************************************************************/
297 60779 : void XMLCALL GMLExpatHandler::endElementCbk(void *pUserData,
298 : const char * /* pszName */)
299 : {
300 60779 : GMLExpatHandler *pThis = static_cast<GMLExpatHandler *>(pUserData);
301 60779 : if (pThis->m_bStopParsing)
302 1 : return;
303 :
304 60778 : pThis->DealWithError(pThis->GMLHandler::endElement());
305 : }
306 :
307 : /************************************************************************/
308 : /* dataHandlerCbk() */
309 : /************************************************************************/
310 :
311 246176 : void XMLCALL GMLExpatHandler::dataHandlerCbk(void *pUserData, const char *data,
312 : int nLen)
313 :
314 : {
315 246176 : GMLExpatHandler *pThis = static_cast<GMLExpatHandler *>(pUserData);
316 246176 : if (pThis->m_bStopParsing)
317 0 : return;
318 :
319 246176 : pThis->m_nDataHandlerCounter++;
320 : // The size of the buffer that is fetched and that Expat parses is
321 : // PARSER_BUF_SIZE bytes. If the dataHandlerCbk() callback is called
322 : // more than PARSER_BUF_SIZE times, this means that one byte in the
323 : // file expands to more XML text fragments, which is the sign of a
324 : // likely abuse of <!ENTITY>
325 : // Note: the counter is zeroed by ResetDataHandlerCounter() before each
326 : // new XML parsing.
327 246176 : if (pThis->m_nDataHandlerCounter >= PARSER_BUF_SIZE)
328 : {
329 1 : CPLError(CE_Failure, CPLE_AppDefined,
330 : "File probably corrupted (million laugh pattern)");
331 1 : pThis->m_bStopParsing = true;
332 1 : XML_StopParser(pThis->m_oParser, static_cast<XML_Bool>(false));
333 1 : return;
334 : }
335 :
336 246175 : pThis->DealWithError(pThis->GMLHandler::dataHandler(data, nLen));
337 : }
338 :
339 : /************************************************************************/
340 : /* GetFID() */
341 : /************************************************************************/
342 :
343 3287 : const char *GMLExpatHandler::GetFID(void *attr)
344 : {
345 3287 : const char *const *papszIter = static_cast<const char *const *>(attr);
346 3305 : while (*papszIter)
347 : {
348 1791 : if (strcmp(*papszIter, "fid") == 0 || strcmp(*papszIter, "gml:id") == 0)
349 : {
350 1773 : return papszIter[1];
351 : }
352 18 : papszIter += 2;
353 : }
354 1514 : return nullptr;
355 : }
356 :
357 : /************************************************************************/
358 : /* AddAttributes() */
359 : /************************************************************************/
360 :
361 34070 : CPLXMLNode *GMLExpatHandler::AddAttributes(CPLXMLNode *psNode, void *attr)
362 : {
363 34070 : const char **papszIter = static_cast<const char **>(attr);
364 :
365 34070 : CPLXMLNode *psLastChild = nullptr;
366 :
367 49176 : while (*papszIter)
368 : {
369 : CPLXMLNode *psChild =
370 15106 : CPLCreateXMLNode(nullptr, CXT_Attribute, papszIter[0]);
371 15106 : CPLCreateXMLNode(psChild, CXT_Text, papszIter[1]);
372 :
373 15106 : if (psLastChild == nullptr)
374 14076 : psNode->psChild = psChild;
375 : else
376 1030 : psLastChild->psNext = psChild;
377 15106 : psLastChild = psChild;
378 :
379 15106 : papszIter += 2;
380 : }
381 :
382 34070 : return psLastChild;
383 : }
384 :
385 : /************************************************************************/
386 : /* GetAttributeValue() */
387 : /************************************************************************/
388 :
389 11128 : char *GMLExpatHandler::GetAttributeValue(void *attr,
390 : const char *pszAttributeName)
391 : {
392 11128 : const char *const *papszIter = static_cast<const char *const *>(attr);
393 11742 : while (*papszIter)
394 : {
395 861 : if (strcmp(*papszIter, pszAttributeName) == 0)
396 : {
397 247 : return CPLStrdup(papszIter[1]);
398 : }
399 614 : papszIter += 2;
400 : }
401 10881 : return nullptr;
402 : }
403 :
404 : /************************************************************************/
405 : /* GetAttributeByIdx() */
406 : /************************************************************************/
407 :
408 : // CAUTION: should be called with increasing idx starting from 0 and
409 : // no attempt to read beyond end of list.
410 13880 : char *GMLExpatHandler::GetAttributeByIdx(void *attr, unsigned int idx,
411 : char **ppszKey)
412 : {
413 13880 : const char *const *papszIter = static_cast<const char *const *>(attr);
414 13880 : if (papszIter[2 * idx] == nullptr)
415 : {
416 13258 : *ppszKey = nullptr;
417 13258 : return nullptr;
418 : }
419 622 : *ppszKey = CPLStrdup(papszIter[2 * idx]);
420 622 : return CPLStrdup(papszIter[2 * idx + 1]);
421 : }
422 :
423 : #endif
424 :
425 : static const char *const apszGMLGeometryElements[] = {
426 : "BoundingBox", /* ows:BoundingBox */
427 : "CompositeCurve",
428 : "CompositeSurface",
429 : "Shell", /* CityGML 3 */
430 : "Curve",
431 : "GeometryCollection", /* OGR < 1.8.0 bug... */
432 : "LineString",
433 : "MultiCurve",
434 : "MultiGeometry",
435 : "MultiLineString",
436 : "MultiPoint",
437 : "MultiPolygon",
438 : "MultiSurface",
439 : "Point",
440 : "Polygon",
441 : "PolygonPatch",
442 : "PolyhedralSurface",
443 : "SimplePolygon", /* GML 3.3 compact encoding */
444 : "SimpleRectangle", /* GML 3.3 compact encoding */
445 : "SimpleTriangle", /* GML 3.3 compact encoding */
446 : "SimpleMultiPoint", /* GML 3.3 compact encoding */
447 : "Solid",
448 : "Surface",
449 : "Tin",
450 : "TopoCurve",
451 : "TopoSurface",
452 : "Triangle",
453 : "TriangulatedSurface"};
454 :
455 : #define GML_GEOMETRY_TYPE_COUNT \
456 : static_cast<int>(sizeof(apszGMLGeometryElements) / \
457 : sizeof(apszGMLGeometryElements[0]))
458 :
459 107 : bool OGRGMLIsGeometryElement(const char *pszElement)
460 : {
461 3089 : for (const auto &pszGMLElement : apszGMLGeometryElements)
462 : {
463 2983 : if (strcmp(pszElement, pszGMLElement) == 0)
464 1 : return true;
465 : }
466 106 : return false;
467 : }
468 :
469 : struct _GeometryNamesStruct
470 : {
471 : unsigned long nHash;
472 : const char *pszName;
473 : };
474 :
475 : /************************************************************************/
476 : /* GMLHandler() */
477 : /************************************************************************/
478 :
479 587 : GMLHandler::GMLHandler(GMLReader *poReader)
480 : : pasGeometryNames(static_cast<GeometryNamesStruct *>(
481 1174 : CPLMalloc(GML_GEOMETRY_TYPE_COUNT * sizeof(GeometryNamesStruct)))),
482 : m_nSRSDimensionIfMissing(
483 587 : atoi(CPLGetConfigOption("GML_SRS_DIMENSION_IF_MISSING", "0"))),
484 1174 : m_poReader(poReader), eAppSchemaType(APPSCHEMA_GENERIC), nStackDepth(0)
485 : {
486 17023 : for (int i = 0; i < GML_GEOMETRY_TYPE_COUNT; i++)
487 : {
488 16436 : pasGeometryNames[i].pszName = apszGMLGeometryElements[i];
489 32872 : pasGeometryNames[i].nHash =
490 16436 : CPLHashSetHashStr(pasGeometryNames[i].pszName);
491 : }
492 587 : std::sort(pasGeometryNames, pasGeometryNames + GML_GEOMETRY_TYPE_COUNT,
493 99790 : [](const GeometryNamesStruct &a, const GeometryNamesStruct &b)
494 99790 : { return a.nHash < b.nHash; });
495 :
496 587 : stateStack[0] = STATE_TOP;
497 587 : }
498 :
499 : /************************************************************************/
500 : /* ~GMLHandler() */
501 : /************************************************************************/
502 :
503 587 : GMLHandler::~GMLHandler()
504 :
505 : {
506 587 : if (apsXMLNode.size() >= 2 && apsXMLNode[1].psNode != nullptr)
507 4 : CPLDestroyXMLNode(apsXMLNode[1].psNode);
508 :
509 587 : CPLFree(m_pszCurField);
510 587 : CPLFree(m_pszGeometry);
511 587 : CPLFree(m_pszCityGMLGenericAttrName);
512 587 : CPLFree(m_pszHref);
513 587 : CPLFree(m_pszUom);
514 587 : CPLFree(m_pszValue);
515 587 : CPLFree(m_pszKieli);
516 587 : CPLFree(pasGeometryNames);
517 587 : }
518 :
519 : /************************************************************************/
520 : /* startElement() */
521 : /************************************************************************/
522 :
523 60916 : OGRErr GMLHandler::startElement(const char *pszName, int nLenName, void *attr)
524 : {
525 : OGRErr eRet;
526 60916 : switch (stateStack[nStackDepth])
527 : {
528 585 : case STATE_TOP:
529 585 : eRet = startElementTop(pszName, nLenName, attr);
530 585 : break;
531 7167 : case STATE_DEFAULT:
532 7167 : eRet = startElementDefault(pszName, nLenName, attr);
533 7167 : break;
534 13397 : case STATE_FEATURE:
535 13397 : eRet = startElementFeatureAttribute(pszName, nLenName, attr);
536 13397 : break;
537 3400 : case STATE_PROPERTY:
538 3400 : eRet = startElementFeatureAttribute(pszName, nLenName, attr);
539 3400 : break;
540 165 : case STATE_FEATUREPROPERTY:
541 165 : eRet = startElementFeatureProperty(pszName, nLenName, attr);
542 165 : break;
543 29873 : case STATE_GEOMETRY:
544 29873 : eRet = startElementGeometry(pszName, nLenName, attr);
545 29873 : break;
546 4003 : case STATE_IGNORED_FEATURE:
547 4003 : eRet = OGRERR_NONE;
548 4003 : break;
549 1194 : case STATE_BOUNDED_BY:
550 1194 : eRet = startElementBoundedBy(pszName, nLenName, attr);
551 1194 : break;
552 1090 : case STATE_BOUNDED_BY_IN_FEATURE:
553 1090 : eRet = startElementGeometry(pszName, nLenName, attr);
554 1090 : break;
555 42 : case STATE_CITYGML_ATTRIBUTE:
556 42 : eRet = startElementCityGMLGenericAttr(pszName, nLenName, attr);
557 42 : break;
558 0 : default:
559 0 : eRet = OGRERR_NONE;
560 0 : break;
561 : }
562 60916 : m_nDepth++;
563 60916 : if (m_nDepth == 64)
564 : {
565 : // Avoid performance issues on files like
566 : // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=21737
567 3 : if (m_nUnlimitedDepth < 0)
568 : {
569 3 : m_nUnlimitedDepth = EQUAL(
570 : CPLGetConfigOption("OGR_GML_NESTING_LEVEL", ""), "UNLIMITED");
571 : }
572 3 : if (!m_nUnlimitedDepth)
573 : {
574 1 : CPLError(CE_Failure, CPLE_NotSupported,
575 : "Too deep XML nesting level (%d). "
576 : "Set the OGR_GML_NESTING_LEVEL configuration option to "
577 : "UNLIMITED to remove that limitation.",
578 : m_nDepth);
579 1 : eRet = OGRERR_FAILURE;
580 : }
581 : }
582 60916 : return eRet;
583 : }
584 :
585 : /************************************************************************/
586 : /* endElement() */
587 : /************************************************************************/
588 :
589 60793 : OGRErr GMLHandler::endElement()
590 : {
591 60793 : m_nDepth--;
592 60793 : switch (stateStack[nStackDepth])
593 : {
594 0 : case STATE_TOP:
595 0 : break;
596 3942 : case STATE_DEFAULT:
597 3942 : return endElementDefault();
598 5958 : case STATE_FEATURE:
599 5958 : return endElementFeature();
600 10678 : case STATE_PROPERTY:
601 10678 : return endElementAttribute();
602 189 : case STATE_FEATUREPROPERTY:
603 189 : return endElementFeatureProperty();
604 32951 : case STATE_GEOMETRY:
605 32951 : return endElementGeometry();
606 4348 : case STATE_IGNORED_FEATURE:
607 4348 : return endElementIgnoredFeature();
608 1477 : case STATE_BOUNDED_BY:
609 1477 : return endElementBoundedBy();
610 1166 : case STATE_BOUNDED_BY_IN_FEATURE:
611 1166 : return endElementBoundedByInFeature();
612 84 : case STATE_CITYGML_ATTRIBUTE:
613 84 : return endElementCityGMLGenericAttr();
614 0 : default:
615 0 : break;
616 : }
617 0 : return OGRERR_NONE;
618 : }
619 :
620 : /************************************************************************/
621 : /* dataHandler() */
622 : /************************************************************************/
623 :
624 247080 : OGRErr GMLHandler::dataHandler(const char *data, int nLen)
625 : {
626 247080 : switch (stateStack[nStackDepth])
627 : {
628 131434 : case STATE_TOP:
629 : case STATE_DEFAULT:
630 : case STATE_FEATURE:
631 131434 : break;
632 14325 : case STATE_PROPERTY:
633 14325 : return dataHandlerAttribute(data, nLen);
634 625 : case STATE_FEATUREPROPERTY:
635 625 : break;
636 92268 : case STATE_GEOMETRY:
637 92268 : return dataHandlerGeometry(data, nLen);
638 5876 : case STATE_IGNORED_FEATURE:
639 : case STATE_BOUNDED_BY:
640 5876 : break;
641 2414 : case STATE_BOUNDED_BY_IN_FEATURE:
642 2414 : return dataHandlerGeometry(data, nLen);
643 138 : case STATE_CITYGML_ATTRIBUTE:
644 138 : return dataHandlerAttribute(data, nLen);
645 0 : default:
646 0 : break;
647 : }
648 137935 : return OGRERR_NONE;
649 : }
650 :
651 : #define PUSH_STATE(val) \
652 : do \
653 : { \
654 : nStackDepth++; \
655 : CPLAssert(nStackDepth < STACK_SIZE); \
656 : stateStack[nStackDepth] = val; \
657 : } while (false)
658 : #define POP_STATE() nStackDepth--
659 :
660 : /************************************************************************/
661 : /* startElementBoundedBy() */
662 : /************************************************************************/
663 :
664 1194 : OGRErr GMLHandler::startElementBoundedBy(const char *pszName, int /*nLenName*/,
665 : void *attr)
666 : {
667 1194 : if (m_nDepth == 2 && strcmp(pszName, "Envelope") == 0)
668 : {
669 136 : char *pszGlobalSRSName = GetAttributeValue(attr, "srsName");
670 136 : m_poReader->SetGlobalSRSName(pszGlobalSRSName);
671 136 : CPLFree(pszGlobalSRSName);
672 :
673 136 : if (m_nSRSDimensionIfMissing == 0)
674 : {
675 : char *pszGlobalSRSDimension =
676 132 : GetAttributeValue(attr, "srsDimension");
677 132 : if (pszGlobalSRSDimension)
678 15 : m_nSRSDimensionIfMissing = atoi(pszGlobalSRSDimension);
679 132 : CPLFree(pszGlobalSRSDimension);
680 : }
681 : }
682 :
683 1194 : return OGRERR_NONE;
684 : }
685 :
686 : /************************************************************************/
687 : /* startElementGeometry() */
688 : /************************************************************************/
689 :
690 34073 : OGRErr GMLHandler::startElementGeometry(const char *pszName, int nLenName,
691 : void *attr)
692 : {
693 35163 : if (stateStack[nStackDepth] == STATE_BOUNDED_BY_IN_FEATURE &&
694 1090 : apsXMLNode.empty())
695 : {
696 1 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid <boundedBy> construct");
697 1 : return OGRERR_FAILURE;
698 : }
699 :
700 : /* Create new XML Element */
701 : CPLXMLNode *psCurNode =
702 34072 : static_cast<CPLXMLNode *>(CPLCalloc(sizeof(CPLXMLNode), 1));
703 34072 : psCurNode->eType = CXT_Element;
704 34072 : psCurNode->pszValue = static_cast<char *>(CPLMalloc(nLenName + 1));
705 34072 : memcpy(psCurNode->pszValue, pszName, nLenName + 1);
706 :
707 : /* Attach element as the last child of its parent */
708 34072 : NodeLastChild &sNodeLastChild = apsXMLNode.back();
709 34072 : CPLXMLNode *psLastChildParent = sNodeLastChild.psLastChild;
710 :
711 34072 : if (psLastChildParent == nullptr)
712 : {
713 18163 : CPLXMLNode *psParent = sNodeLastChild.psNode;
714 18163 : if (psParent)
715 14975 : psParent->psChild = psCurNode;
716 : }
717 : else
718 : {
719 15909 : psLastChildParent->psNext = psCurNode;
720 : }
721 34072 : sNodeLastChild.psLastChild = psCurNode;
722 :
723 : /* Add attributes to the element */
724 34072 : CPLXMLNode *psLastChildCurNode = AddAttributes(psCurNode, attr);
725 49181 : for (CPLXMLNode *psIter = psCurNode->psChild; psIter;
726 15109 : psIter = psIter->psNext)
727 : {
728 15109 : if (psIter->eType == CXT_Attribute &&
729 15109 : strcmp(psIter->pszValue, "xlink:href") == 0 &&
730 1090 : psIter->psChild->pszValue && psIter->psChild->pszValue[0] == '#')
731 : {
732 1090 : m_oMapElementToSubstitute[psIter->psChild->pszValue + 1] =
733 : psCurNode;
734 : }
735 : }
736 :
737 : /* Some CityGML lack a srsDimension="3" in posList, such as in */
738 : /* http://www.citygml.org/fileadmin/count.php?f=fileadmin%2Fcitygml%2Fdocs%2FFrankfurt_Street_Setting_LOD3.zip
739 : */
740 : /* So we have to add it manually */
741 71535 : if (strcmp(pszName, "posList") == 0 &&
742 34952 : CPLGetXMLValue(psCurNode, "srsDimension", nullptr) == nullptr &&
743 880 : m_nSRSDimensionIfMissing != 0)
744 : {
745 : CPLXMLNode *psChild =
746 118 : CPLCreateXMLNode(nullptr, CXT_Attribute, "srsDimension");
747 118 : CPLCreateXMLNode(psChild, CXT_Text,
748 118 : (m_nSRSDimensionIfMissing == 3) ? "3" : "2");
749 :
750 118 : if (psLastChildCurNode == nullptr)
751 118 : psCurNode->psChild = psChild;
752 : else
753 0 : psLastChildCurNode->psNext = psChild;
754 118 : psLastChildCurNode = psChild;
755 : }
756 :
757 : /* Push the element on the stack */
758 : NodeLastChild sNewNodeLastChild;
759 34072 : sNewNodeLastChild.psNode = psCurNode;
760 34072 : sNewNodeLastChild.psLastChild = psLastChildCurNode;
761 34072 : apsXMLNode.push_back(sNewNodeLastChild);
762 :
763 34072 : if (m_pszGeometry)
764 : {
765 0 : CPLFree(m_pszGeometry);
766 0 : m_pszGeometry = nullptr;
767 0 : m_nGeomAlloc = 0;
768 0 : m_nGeomLen = 0;
769 : }
770 :
771 34072 : return OGRERR_NONE;
772 : }
773 :
774 : /************************************************************************/
775 : /* startElementCityGMLGenericAttr() */
776 : /************************************************************************/
777 :
778 42 : OGRErr GMLHandler::startElementCityGMLGenericAttr(const char *pszName,
779 : int /*nLenName*/,
780 : void * /*attr*/)
781 : {
782 42 : if (strcmp(pszName, "value") == 0)
783 : {
784 42 : if (m_pszCurField)
785 : {
786 0 : CPLFree(m_pszCurField);
787 0 : m_pszCurField = nullptr;
788 0 : m_nCurFieldLen = 0;
789 0 : m_nCurFieldAlloc = 0;
790 : }
791 42 : m_bInCurField = true;
792 : }
793 :
794 42 : return OGRERR_NONE;
795 : }
796 :
797 : /************************************************************************/
798 : /* DealWithAttributes() */
799 : /************************************************************************/
800 :
801 13267 : void GMLHandler::DealWithAttributes(const char *pszName, int nLenName,
802 : void *attr)
803 : {
804 13267 : GMLReadState *poState = m_poReader->GetState();
805 13267 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
806 :
807 13267 : for (unsigned int idx = 0; true; idx++)
808 : {
809 13889 : char *pszAttrKey = nullptr;
810 :
811 13889 : char *pszAttrVal = GetAttributeByIdx(attr, idx, &pszAttrKey);
812 13889 : if (pszAttrVal == nullptr)
813 13267 : break;
814 :
815 622 : int nAttrIndex = 0;
816 622 : const char *pszAttrKeyNoNS = strchr(pszAttrKey, ':');
817 622 : if (pszAttrKeyNoNS != nullptr)
818 149 : pszAttrKeyNoNS++;
819 :
820 : /* If attribute is referenced by the .gfs */
821 771 : if (poClass->IsSchemaLocked() &&
822 50 : ((pszAttrKeyNoNS != nullptr &&
823 50 : (nAttrIndex = m_poReader->GetAttributeElementIndex(
824 116 : pszName, nLenName, pszAttrKeyNoNS)) != -1) ||
825 116 : ((nAttrIndex = m_poReader->GetAttributeElementIndex(
826 : pszName, nLenName, pszAttrKey)) != -1)))
827 : {
828 67 : nAttrIndex = FindRealPropertyByCheckingConditions(nAttrIndex, attr);
829 67 : if (nAttrIndex >= 0)
830 : {
831 65 : m_poReader->SetFeaturePropertyDirectly(nullptr, pszAttrVal,
832 : nAttrIndex);
833 65 : pszAttrVal = nullptr;
834 : }
835 : }
836 :
837 : /* Hard-coded historic cases */
838 555 : else if (strcmp(pszAttrKey, "xlink:href") == 0)
839 : {
840 14 : if ((m_bReportHref || m_poReader->ReportAllAttributes()) &&
841 7 : m_bInCurField)
842 : {
843 4 : CPLFree(m_pszHref);
844 4 : m_pszHref = pszAttrVal;
845 4 : pszAttrVal = nullptr;
846 : }
847 9 : else if ((!poClass->IsSchemaLocked() &&
848 9 : (m_bReportHref || m_poReader->ReportAllAttributes())) ||
849 3 : (poClass->IsSchemaLocked() &&
850 3 : (nAttrIndex = m_poReader->GetAttributeElementIndex(
851 6 : (std::string(pszName) + "_href").c_str(),
852 : nLenName + 5)) != -1))
853 : {
854 3 : poState->PushPath(pszName, nLenName);
855 3 : CPLString osPropNameHref = poState->osPath + "_href";
856 3 : poState->PopPath();
857 3 : m_poReader->SetFeaturePropertyDirectly(osPropNameHref,
858 : pszAttrVal, nAttrIndex);
859 3 : pszAttrVal = nullptr;
860 : }
861 : }
862 548 : else if (strcmp(pszAttrKey, "uom") == 0)
863 : {
864 20 : CPLFree(m_pszUom);
865 20 : m_pszUom = pszAttrVal;
866 20 : pszAttrVal = nullptr;
867 : }
868 528 : else if (strcmp(pszAttrKey, "value") == 0)
869 : {
870 10 : CPLFree(m_pszValue);
871 10 : m_pszValue = pszAttrVal;
872 10 : pszAttrVal = nullptr;
873 : }
874 : else /* Get language in 'kieli' attribute of 'teksti' element */
875 518 : if (eAppSchemaType == APPSCHEMA_MTKGML && nLenName == 6 &&
876 7 : strcmp(pszName, "teksti") == 0 &&
877 7 : strcmp(pszAttrKey, "kieli") == 0)
878 : {
879 7 : CPLFree(m_pszKieli);
880 7 : m_pszKieli = pszAttrVal;
881 7 : pszAttrVal = nullptr;
882 : }
883 :
884 : /* Should we report all attributes ? */
885 519 : else if (m_poReader->ReportAllAttributes() &&
886 8 : !poClass->IsSchemaLocked())
887 : {
888 8 : poState->PushPath(pszName, nLenName);
889 8 : CPLString osPropName = poState->osPath;
890 8 : poState->PopPath();
891 :
892 8 : m_poReader->SetFeaturePropertyDirectly(
893 : CPLSPrintf("%s@%s", osPropName.c_str(),
894 : pszAttrKeyNoNS ? pszAttrKeyNoNS : pszAttrKey),
895 : pszAttrVal, -1);
896 8 : pszAttrVal = nullptr;
897 : }
898 :
899 622 : CPLFree(pszAttrKey);
900 622 : CPLFree(pszAttrVal);
901 622 : }
902 :
903 : #if 0
904 : if( poClass->IsSchemaLocked() )
905 : {
906 : poState->PushPath( pszName, nLenName );
907 : CPLString osPath = poState->osPath;
908 : poState->PopPath();
909 : /* Find fields that match an attribute that is missing */
910 : for(int i=0; i < poClass->GetPropertyCount(); i++ )
911 : {
912 : GMLPropertyDefn* poProp = poClass->GetProperty(i);
913 : const char* pszSrcElement = poProp->GetSrcElement();
914 : if( poProp->GetType() == OFTStringList &&
915 : poProp->GetSrcElementLen() > osPath.size() &&
916 : strncmp(pszSrcElement, osPath, osPath.size()) == 0 &&
917 : pszSrcElement[osPath.size()] == '@' )
918 : {
919 : char* pszAttrVal = GetAttributeValue(attr, pszSrcElement + osPath.size() + 1);
920 : if( pszAttrVal == NULL )
921 : {
922 : const char* pszCond = poProp->GetCondition();
923 : if( pszCond == NULL || IsConditionMatched(pszCond, attr) )
924 : {
925 : m_poReader->SetFeaturePropertyDirectly( NULL, CPLStrdup(""), i );
926 : }
927 : }
928 : else
929 : CPLFree(pszAttrVal);
930 : }
931 : }
932 : }
933 : #endif
934 13267 : }
935 :
936 : /************************************************************************/
937 : /* IsConditionMatched() */
938 : /************************************************************************/
939 :
940 : /* FIXME! 'and' / 'or' operators are evaluated left to right, without */
941 : /* and precedence rules between them ! */
942 :
943 15 : bool GMLHandler::IsConditionMatched(const char *pszCondition, void *attr)
944 : {
945 15 : if (pszCondition == nullptr)
946 0 : return true;
947 :
948 15 : bool bSyntaxError = false;
949 30 : CPLString osCondAttr, osCondVal;
950 15 : const char *pszIter = pszCondition;
951 15 : bool bOpEqual = true;
952 19 : while (*pszIter == ' ')
953 4 : pszIter++;
954 15 : if (*pszIter != '@')
955 0 : bSyntaxError = true;
956 : else
957 : {
958 15 : pszIter++;
959 75 : while (*pszIter != '\0' && *pszIter != ' ' && *pszIter != '!' &&
960 65 : *pszIter != '=')
961 : {
962 60 : osCondAttr += *pszIter;
963 60 : pszIter++;
964 : }
965 15 : while (*pszIter == ' ')
966 0 : pszIter++;
967 :
968 15 : if (*pszIter == '!')
969 : {
970 10 : bOpEqual = false;
971 10 : pszIter++;
972 : }
973 :
974 15 : if (*pszIter != '=')
975 0 : bSyntaxError = true;
976 : else
977 : {
978 15 : pszIter++;
979 15 : while (*pszIter == ' ')
980 0 : pszIter++;
981 15 : if (*pszIter != '\'')
982 0 : bSyntaxError = true;
983 : else
984 : {
985 15 : pszIter++;
986 45 : while (*pszIter != '\0' && *pszIter != '\'')
987 : {
988 30 : osCondVal += *pszIter;
989 30 : pszIter++;
990 : }
991 15 : if (*pszIter != '\'')
992 0 : bSyntaxError = true;
993 : else
994 : {
995 15 : pszIter++;
996 21 : while (*pszIter == ' ')
997 6 : pszIter++;
998 : }
999 : }
1000 : }
1001 : }
1002 :
1003 15 : if (bSyntaxError)
1004 : {
1005 0 : CPLError(CE_Failure, CPLE_NotSupported,
1006 : "Invalid condition : %s. Must be of the form "
1007 : "@attrname[!]='attrvalue' [and|or other_cond]*. "
1008 : "'and' and 'or' operators cannot be mixed",
1009 : pszCondition);
1010 0 : return false;
1011 : }
1012 :
1013 15 : char *pszVal = GetAttributeValue(attr, osCondAttr);
1014 15 : if (pszVal == nullptr)
1015 0 : pszVal = CPLStrdup("");
1016 28 : const bool bCondMet = (bOpEqual && strcmp(pszVal, osCondVal) == 0) ||
1017 13 : (!bOpEqual && strcmp(pszVal, osCondVal) != 0);
1018 15 : CPLFree(pszVal);
1019 15 : if (*pszIter == '\0')
1020 9 : return bCondMet;
1021 :
1022 6 : if (STARTS_WITH(pszIter, "and"))
1023 : {
1024 6 : pszIter += 3;
1025 6 : if (!bCondMet)
1026 2 : return false;
1027 4 : return IsConditionMatched(pszIter, attr);
1028 : }
1029 :
1030 0 : if (STARTS_WITH(pszIter, "or"))
1031 : {
1032 0 : pszIter += 2;
1033 0 : if (bCondMet)
1034 0 : return true;
1035 0 : return IsConditionMatched(pszIter, attr);
1036 : }
1037 :
1038 0 : CPLError(
1039 : CE_Failure, CPLE_NotSupported,
1040 : "Invalid condition : %s. Must be of the form @attrname[!]='attrvalue' "
1041 : "[and|or other_cond]*. 'and' and 'or' operators cannot be mixed",
1042 : pszCondition);
1043 0 : return false;
1044 : }
1045 :
1046 : /************************************************************************/
1047 : /* FindRealPropertyByCheckingConditions() */
1048 : /************************************************************************/
1049 :
1050 3078 : int GMLHandler::FindRealPropertyByCheckingConditions(int nIdx, void *attr)
1051 : {
1052 3078 : GMLReadState *poState = m_poReader->GetState();
1053 3078 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
1054 :
1055 3078 : GMLPropertyDefn *poProp = poClass->GetProperty(nIdx);
1056 3078 : const char *pszCond = poProp->GetCondition();
1057 3078 : if (pszCond != nullptr && !IsConditionMatched(pszCond, attr))
1058 : {
1059 : /* try other attributes with same source element, but with different */
1060 : /* condition */
1061 4 : const char *pszSrcElement = poProp->GetSrcElement();
1062 4 : nIdx = -1;
1063 11 : for (int i = m_nAttributeIndex + 1; i < poClass->GetPropertyCount();
1064 : i++)
1065 : {
1066 9 : poProp = poClass->GetProperty(i);
1067 9 : if (strcmp(poProp->GetSrcElement(), pszSrcElement) == 0)
1068 : {
1069 5 : pszCond = poProp->GetCondition();
1070 5 : if (IsConditionMatched(pszCond, attr))
1071 : {
1072 2 : nIdx = i;
1073 2 : break;
1074 : }
1075 : }
1076 : }
1077 : }
1078 3078 : return nIdx;
1079 : }
1080 :
1081 : /************************************************************************/
1082 : /* startElementFeatureAttribute() */
1083 : /************************************************************************/
1084 :
1085 16797 : OGRErr GMLHandler::startElementFeatureAttribute(const char *pszName,
1086 : int nLenName, void *attr)
1087 : {
1088 : /* Reset flag */
1089 16797 : m_bInCurField = false;
1090 :
1091 16797 : GMLReadState *poState = m_poReader->GetState();
1092 :
1093 : /* -------------------------------------------------------------------- */
1094 : /* If we are collecting geometry, or if we determine this is a */
1095 : /* geometry element then append to the geometry info. */
1096 : /* -------------------------------------------------------------------- */
1097 16797 : if (IsGeometryElement(pszName))
1098 : {
1099 : bool bReadGeometry;
1100 :
1101 : /* If the <GeometryElementPath> is defined in the .gfs, use it */
1102 : /* to read the appropriate geometry element */
1103 3115 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
1104 3115 : m_nGeometryPropertyIndex = 0;
1105 4409 : if (poClass->IsSchemaLocked() &&
1106 1294 : poClass->GetGeometryPropertyCount() == 0)
1107 : {
1108 0 : bReadGeometry = false;
1109 : }
1110 4409 : else if (poClass->IsSchemaLocked() &&
1111 4409 : poClass->GetGeometryPropertyCount() == 1 &&
1112 1132 : poClass->GetGeometryProperty(0)->GetSrcElement()[0] == '\0')
1113 : {
1114 70 : bReadGeometry = true;
1115 : }
1116 4269 : else if (poClass->IsSchemaLocked() &&
1117 1224 : poClass->GetGeometryPropertyCount() > 0)
1118 : {
1119 1224 : m_nGeometryPropertyIndex =
1120 1224 : poClass->GetGeometryPropertyIndexBySrcElement(
1121 : poState->osPath.c_str());
1122 1224 : bReadGeometry = (m_nGeometryPropertyIndex >= 0);
1123 : }
1124 1821 : else if (m_poReader->FetchAllGeometries())
1125 : {
1126 0 : bReadGeometry = true;
1127 : }
1128 1821 : else if (!poClass->IsSchemaLocked() && m_poReader->IsWFSJointLayer())
1129 : {
1130 12 : m_nGeometryPropertyIndex =
1131 12 : poClass->GetGeometryPropertyIndexBySrcElement(
1132 : poState->osPath.c_str());
1133 12 : if (m_nGeometryPropertyIndex < 0)
1134 : {
1135 4 : const char *pszElement = poState->osPath.c_str();
1136 4 : CPLString osFieldName;
1137 : /* Strip member| prefix. Should always be true normally */
1138 4 : if (STARTS_WITH(pszElement, "member|"))
1139 4 : osFieldName = pszElement + strlen("member|");
1140 :
1141 : /* Replace layer|property by layer_property */
1142 4 : size_t iPos = osFieldName.find('|');
1143 4 : if (iPos != std::string::npos)
1144 4 : osFieldName[iPos] = '.';
1145 :
1146 8 : poClass->AddGeometryProperty(new GMLGeometryPropertyDefn(
1147 4 : osFieldName, poState->osPath.c_str(), wkbUnknown, -1,
1148 8 : true));
1149 4 : m_nGeometryPropertyIndex = poClass->GetGeometryPropertyCount();
1150 : }
1151 12 : bReadGeometry = true;
1152 : }
1153 : else
1154 : {
1155 : /* AIXM special case: for RouteSegment, we only want to read Curve
1156 : * geometries */
1157 : /* not 'start' and 'end' geometries */
1158 1821 : if (eAppSchemaType == APPSCHEMA_AIXM &&
1159 12 : strcmp(poState->m_poFeature->GetClass()->GetName(),
1160 : "RouteSegment") == 0)
1161 0 : bReadGeometry = strcmp(pszName, "Curve") == 0;
1162 :
1163 : // AIXM special case: we want to read both horizontalProjection_location and
1164 : // horizontalProjection_linearExtent
1165 1821 : else if (eAppSchemaType == APPSCHEMA_AIXM &&
1166 12 : STARTS_WITH(poState->m_poFeature->GetClass()->GetName(),
1167 : "horizontalProjection_") == 0)
1168 12 : bReadGeometry =
1169 12 : STARTS_WITH(pszName, "horizontalProjection_") == 0;
1170 :
1171 : /* For Inspire objects : the "main" geometry is in a <geometry>
1172 : * element */
1173 1797 : else if (m_bAlreadyFoundGeometry)
1174 2 : bReadGeometry = false;
1175 1795 : else if (strcmp(poState->osPath.c_str(), "geometry") == 0)
1176 : {
1177 8 : m_bAlreadyFoundGeometry = true;
1178 8 : bReadGeometry = true;
1179 8 : m_nGeometryPropertyIndex =
1180 8 : poClass->GetGeometryPropertyIndexBySrcElement(
1181 : poState->osPath.c_str());
1182 8 : if (m_nGeometryPropertyIndex < 0)
1183 : {
1184 6 : poClass->AddGeometryProperty(new GMLGeometryPropertyDefn(
1185 3 : "geometry", poState->osPath.c_str(), wkbUnknown, -1,
1186 3 : true));
1187 3 : m_nGeometryPropertyIndex =
1188 3 : poClass->GetGeometryPropertyCount();
1189 : }
1190 : }
1191 : else
1192 : {
1193 3574 : if (!poClass->IsSchemaLocked() &&
1194 1787 : poClass->IsConsistentSingleGeomElemPath())
1195 : {
1196 : const std::string &osGeomElemPath =
1197 1777 : poClass->GetSingleGeomElemPath();
1198 1777 : if (osGeomElemPath.empty())
1199 : {
1200 413 : poClass->SetSingleGeomElemPath(poState->osPath);
1201 : }
1202 1364 : else if (poState->osPath != osGeomElemPath)
1203 : {
1204 5 : poClass->SetConsistentSingleGeomElemPath(false);
1205 5 : poClass->SetSingleGeomElemPath(std::string());
1206 : }
1207 : }
1208 1787 : bReadGeometry = true;
1209 : }
1210 : }
1211 3115 : if (bReadGeometry)
1212 : {
1213 3110 : m_nGeometryDepth = m_nDepth;
1214 :
1215 3110 : CPLAssert(apsXMLNode.empty());
1216 :
1217 : NodeLastChild sNodeLastChild;
1218 3110 : sNodeLastChild.psNode = nullptr;
1219 3110 : sNodeLastChild.psLastChild = nullptr;
1220 3110 : apsXMLNode.push_back(sNodeLastChild);
1221 :
1222 3110 : PUSH_STATE(STATE_GEOMETRY);
1223 :
1224 3110 : return startElementGeometry(pszName, nLenName, attr);
1225 : }
1226 : }
1227 13682 : else if (
1228 12325 : (nLenName == 9 || nLenName == 8) &&
1229 2534 : (strcmp(pszName, "boundedBy") == 0 ||
1230 27373 : strcmp(pszName, "boundary") == 0) &&
1231 : // We ignore the UseBBOX() flag for CityGML, since CityGML
1232 : // has elements like bldg:boundedBy or 'boundary', which are not a simple
1233 : // rectangular bbox. This is needed to read properly
1234 : // autotest/ogr/data/gml/citygml_lod2_713_5322.xml
1235 : // (this is a workaround of not being namespace aware)
1236 967 : (eAppSchemaType == APPSCHEMA_CITYGML || m_poReader->UseBBOX()))
1237 : {
1238 78 : m_inBoundedByDepth = m_nDepth;
1239 :
1240 78 : CPLAssert(apsXMLNode.empty());
1241 :
1242 : NodeLastChild sNodeLastChild;
1243 78 : sNodeLastChild.psNode = nullptr;
1244 78 : sNodeLastChild.psLastChild = nullptr;
1245 78 : apsXMLNode.push_back(sNodeLastChild);
1246 :
1247 78 : PUSH_STATE(STATE_BOUNDED_BY_IN_FEATURE);
1248 :
1249 78 : return OGRERR_NONE;
1250 : }
1251 :
1252 : /* -------------------------------------------------------------------- */
1253 : /* Is it a CityGML generic attribute ? */
1254 : /* -------------------------------------------------------------------- */
1255 13740 : else if (eAppSchemaType == APPSCHEMA_CITYGML &&
1256 136 : m_poReader->IsCityGMLGenericAttributeElement(pszName, attr))
1257 : {
1258 42 : CPLFree(m_pszCityGMLGenericAttrName);
1259 42 : m_pszCityGMLGenericAttrName = GetAttributeValue(attr, "name");
1260 42 : m_inCityGMLGenericAttrDepth = m_nDepth;
1261 :
1262 42 : PUSH_STATE(STATE_CITYGML_ATTRIBUTE);
1263 :
1264 42 : return OGRERR_NONE;
1265 : }
1266 :
1267 13562 : else if (m_poReader->IsWFSJointLayer() && m_nDepth == m_nDepthFeature + 1)
1268 : {
1269 : }
1270 :
1271 13438 : else if (m_poReader->IsWFSJointLayer() && m_nDepth == m_nDepthFeature + 2)
1272 : {
1273 124 : const char *pszFID = GetFID(attr);
1274 124 : if (pszFID)
1275 : {
1276 124 : poState->PushPath(pszName, nLenName);
1277 248 : CPLString osPropPath = poState->osPath + "@id";
1278 124 : poState->PopPath();
1279 124 : m_poReader->SetFeaturePropertyDirectly(osPropPath,
1280 : CPLStrdup(pszFID), -1);
1281 : }
1282 : }
1283 :
1284 : /* -------------------------------------------------------------------- */
1285 : /* If it is (or at least potentially is) a simple attribute, */
1286 : /* then start collecting it. */
1287 : /* -------------------------------------------------------------------- */
1288 13314 : else if ((m_nAttributeIndex = m_poReader->GetAttributeElementIndex(
1289 13314 : pszName, nLenName)) != -1)
1290 : {
1291 10763 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
1292 16832 : if (poClass->IsSchemaLocked() &&
1293 3035 : (poClass->GetProperty(m_nAttributeIndex)->GetType() ==
1294 3034 : GMLPT_FeatureProperty ||
1295 3034 : poClass->GetProperty(m_nAttributeIndex)->GetType() ==
1296 : GMLPT_FeaturePropertyList))
1297 : {
1298 24 : m_nAttributeDepth = m_nDepth;
1299 24 : PUSH_STATE(STATE_FEATUREPROPERTY);
1300 : }
1301 : else
1302 : {
1303 : /* Is this a property with a condition on an attribute value ? */
1304 10739 : if (poClass->IsSchemaLocked())
1305 : {
1306 3011 : m_nAttributeIndex = FindRealPropertyByCheckingConditions(
1307 : m_nAttributeIndex, attr);
1308 : }
1309 :
1310 10739 : if (m_nAttributeIndex >= 0)
1311 : {
1312 10739 : if (m_pszCurField)
1313 : {
1314 1277 : CPLFree(m_pszCurField);
1315 1277 : m_pszCurField = nullptr;
1316 1277 : m_nCurFieldLen = 0;
1317 1277 : m_nCurFieldAlloc = 0;
1318 : }
1319 10739 : m_bInCurField = true;
1320 :
1321 10739 : char *pszXSINil = GetAttributeValue(attr, "xsi:nil");
1322 10739 : if (pszXSINil)
1323 : {
1324 23 : if (EQUAL(pszXSINil, "true"))
1325 23 : m_poReader->SetFeaturePropertyDirectly(
1326 : pszName, CPLStrdup(OGR_GML_NULL), -1);
1327 23 : CPLFree(pszXSINil);
1328 : }
1329 : else
1330 : {
1331 10716 : DealWithAttributes(pszName, nLenName, attr);
1332 : }
1333 :
1334 10739 : if (stateStack[nStackDepth] != STATE_PROPERTY)
1335 : {
1336 8839 : m_nAttributeDepth = m_nDepth;
1337 8839 : PUSH_STATE(STATE_PROPERTY);
1338 : }
1339 : }
1340 : /*else
1341 : {
1342 : DealWithAttributes(pszName, nLenName, attr);
1343 : }*/
1344 : }
1345 : }
1346 : else
1347 : {
1348 2551 : DealWithAttributes(pszName, nLenName, attr);
1349 : }
1350 :
1351 13567 : poState->PushPath(pszName, nLenName);
1352 :
1353 13567 : return OGRERR_NONE;
1354 : }
1355 :
1356 : /************************************************************************/
1357 : /* startElementTop() */
1358 : /************************************************************************/
1359 :
1360 585 : OGRErr GMLHandler::startElementTop(const char *pszName, int /*nLenName*/,
1361 : void *attr)
1362 : {
1363 585 : if (strcmp(pszName, "CityModel") == 0)
1364 : {
1365 10 : eAppSchemaType = APPSCHEMA_CITYGML;
1366 : // Default to 3D geometries for CityGML (#6989)
1367 10 : if (m_nSRSDimensionIfMissing == 0)
1368 10 : m_nSRSDimensionIfMissing = 3;
1369 : }
1370 575 : else if (strcmp(pszName, "AIXMBasicMessage") == 0)
1371 : {
1372 7 : eAppSchemaType = APPSCHEMA_AIXM;
1373 7 : m_bReportHref = true;
1374 : }
1375 568 : else if (strcmp(pszName, "Maastotiedot") == 0)
1376 : {
1377 8 : eAppSchemaType = APPSCHEMA_MTKGML;
1378 :
1379 8 : char *pszSRSName = GetAttributeValue(attr, "srsName");
1380 8 : m_poReader->SetGlobalSRSName(pszSRSName);
1381 8 : CPLFree(pszSRSName);
1382 :
1383 8 : m_bReportHref = true;
1384 :
1385 : /* the schemas of MTKGML don't have (string) width, so don't set it */
1386 8 : m_poReader->SetWidthFlag(false);
1387 : }
1388 :
1389 585 : stateStack[0] = STATE_DEFAULT;
1390 :
1391 585 : return OGRERR_NONE;
1392 : }
1393 :
1394 : /************************************************************************/
1395 : /* startElementDefault() */
1396 : /************************************************************************/
1397 :
1398 7167 : OGRErr GMLHandler::startElementDefault(const char *pszName, int nLenName,
1399 : void *attr)
1400 :
1401 : {
1402 : /* -------------------------------------------------------------------- */
1403 : /* Is it a feature? If so push a whole new state, and return. */
1404 : /* -------------------------------------------------------------------- */
1405 : int nClassIndex;
1406 7167 : const char *pszFilteredClassName = nullptr;
1407 :
1408 7167 : if (nLenName == 9 && strcmp(pszName, "boundedBy") == 0)
1409 : {
1410 283 : m_inBoundedByDepth = m_nDepth;
1411 :
1412 283 : PUSH_STATE(STATE_BOUNDED_BY);
1413 :
1414 283 : return OGRERR_NONE;
1415 : }
1416 :
1417 7705 : else if (m_poReader->ShouldLookForClassAtAnyLevel() &&
1418 821 : (pszFilteredClassName = m_poReader->GetFilteredClassName()) !=
1419 : nullptr)
1420 : {
1421 821 : if (strcmp(pszName, pszFilteredClassName) == 0)
1422 : {
1423 49 : m_poReader->PushFeature(pszName, GetFID(attr),
1424 49 : m_poReader->GetFilteredClassIndex());
1425 :
1426 49 : m_nDepthFeature = m_nDepth;
1427 :
1428 49 : PUSH_STATE(STATE_FEATURE);
1429 :
1430 49 : return OGRERR_NONE;
1431 : }
1432 : }
1433 :
1434 : /* WFS 2.0 GetFeature documents have a wfs:FeatureCollection */
1435 : /* as a wfs:member of the top wfs:FeatureCollection. We don't want this */
1436 : /* wfs:FeatureCollection to be recognized as a feature */
1437 6091 : else if ((!(nLenName == static_cast<int>(strlen("FeatureCollection")) &&
1438 12112 : strcmp(pszName, "FeatureCollection") == 0)) &&
1439 6049 : (nClassIndex = m_poReader->GetFeatureElementIndex(
1440 : pszName, nLenName, eAppSchemaType)) != -1)
1441 : {
1442 3458 : m_bAlreadyFoundGeometry = false;
1443 :
1444 3458 : pszFilteredClassName = m_poReader->GetFilteredClassName();
1445 3458 : if (pszFilteredClassName != nullptr &&
1446 377 : strcmp(pszName, pszFilteredClassName) != 0)
1447 : {
1448 345 : m_nDepthFeature = m_nDepth;
1449 :
1450 345 : PUSH_STATE(STATE_IGNORED_FEATURE);
1451 :
1452 345 : return OGRERR_NONE;
1453 : }
1454 : else
1455 : {
1456 3113 : if (eAppSchemaType == APPSCHEMA_MTKGML)
1457 : {
1458 22 : m_poReader->PushFeature(pszName, nullptr, nClassIndex);
1459 :
1460 22 : char *pszGID = GetAttributeValue(attr, "gid");
1461 22 : if (pszGID)
1462 21 : m_poReader->SetFeaturePropertyDirectly("gid", pszGID, -1,
1463 : GMLPT_String);
1464 : }
1465 : else
1466 3091 : m_poReader->PushFeature(pszName, GetFID(attr), nClassIndex);
1467 :
1468 3113 : m_nDepthFeature = m_nDepth;
1469 :
1470 3113 : PUSH_STATE(STATE_FEATURE);
1471 :
1472 3113 : return OGRERR_NONE;
1473 : }
1474 : }
1475 :
1476 : /* -------------------------------------------------------------------- */
1477 : /* Push the element onto the current state's path. */
1478 : /* -------------------------------------------------------------------- */
1479 3377 : m_poReader->GetState()->PushPath(pszName, nLenName);
1480 :
1481 3377 : return OGRERR_NONE;
1482 : }
1483 :
1484 : /************************************************************************/
1485 : /* endElementIgnoredFeature() */
1486 : /************************************************************************/
1487 :
1488 4348 : OGRErr GMLHandler::endElementIgnoredFeature()
1489 :
1490 : {
1491 4348 : if (m_nDepth == m_nDepthFeature)
1492 : {
1493 345 : POP_STATE();
1494 : }
1495 4348 : return OGRERR_NONE;
1496 : }
1497 :
1498 : /************************************************************************/
1499 : /* endElementBoundedBy() */
1500 : /************************************************************************/
1501 1477 : OGRErr GMLHandler::endElementBoundedBy()
1502 :
1503 : {
1504 1477 : if (m_inBoundedByDepth == m_nDepth)
1505 : {
1506 283 : POP_STATE();
1507 : }
1508 :
1509 1477 : return OGRERR_NONE;
1510 : }
1511 :
1512 : /************************************************************************/
1513 : /* endElementBoundedByInFeature() */
1514 : /************************************************************************/
1515 1166 : OGRErr GMLHandler::endElementBoundedByInFeature()
1516 :
1517 : {
1518 1166 : if (m_nDepth > m_inBoundedByDepth)
1519 : {
1520 1089 : if (m_nDepth == m_inBoundedByDepth + 1)
1521 : {
1522 78 : m_nGeometryDepth = m_nDepth;
1523 : }
1524 1089 : return endElementGeometry();
1525 : }
1526 : else
1527 : {
1528 77 : POP_STATE();
1529 77 : if (apsXMLNode.size() >= 2 && apsXMLNode[1].psNode != nullptr)
1530 0 : CPLDestroyXMLNode(apsXMLNode[1].psNode);
1531 77 : apsXMLNode.clear();
1532 77 : return OGRERR_NONE;
1533 : }
1534 : }
1535 :
1536 : /************************************************************************/
1537 : /* ParseAIXMElevationProperties() */
1538 : /************************************************************************/
1539 :
1540 10 : void GMLHandler::ParseAIXMElevationProperties(const CPLXMLNode *psGML)
1541 : {
1542 10 : if (const char *pszElevation = CPLGetXMLValue(psGML, "elevation", nullptr))
1543 : {
1544 7 : m_poReader->SetFeaturePropertyDirectly("elevation",
1545 : CPLStrdup(pszElevation), -1);
1546 : const char *pszElevationUnit =
1547 7 : CPLGetXMLValue(psGML, "elevation.uom", nullptr);
1548 7 : if (pszElevationUnit)
1549 : {
1550 5 : m_poReader->SetFeaturePropertyDirectly(
1551 : "elevation_uom", CPLStrdup(pszElevationUnit), -1);
1552 : }
1553 : }
1554 :
1555 10 : if (const char *pszGeoidUndulation =
1556 10 : CPLGetXMLValue(psGML, "geoidUndulation", nullptr))
1557 : {
1558 0 : m_poReader->SetFeaturePropertyDirectly(
1559 : "geoidUndulation", CPLStrdup(pszGeoidUndulation), -1);
1560 : const char *pszGeoidUndulationUnit =
1561 0 : CPLGetXMLValue(psGML, "geoidUndulation.uom", nullptr);
1562 0 : if (pszGeoidUndulationUnit)
1563 : {
1564 0 : m_poReader->SetFeaturePropertyDirectly(
1565 : "geoidUndulation_uom", CPLStrdup(pszGeoidUndulationUnit), -1);
1566 : }
1567 : }
1568 :
1569 10 : if (const char *pszVerticalDatum =
1570 10 : CPLGetXMLValue(psGML, "verticalDatum", nullptr))
1571 : {
1572 3 : m_poReader->SetFeaturePropertyDirectly("verticalDatum",
1573 : CPLStrdup(pszVerticalDatum), -1);
1574 : }
1575 :
1576 10 : if (const char *pszVerticalAccuracy =
1577 10 : CPLGetXMLValue(psGML, "verticalAccuracy", nullptr))
1578 : {
1579 5 : m_poReader->SetFeaturePropertyDirectly(
1580 : "verticalAccuracy", CPLStrdup(pszVerticalAccuracy), -1);
1581 : const char *pszVerticalAccuracyUnit =
1582 5 : CPLGetXMLValue(psGML, "verticalAccuracy.uom", nullptr);
1583 5 : if (pszVerticalAccuracyUnit)
1584 : {
1585 3 : m_poReader->SetFeaturePropertyDirectly(
1586 : "verticalAccuracy_uom", CPLStrdup(pszVerticalAccuracyUnit), -1);
1587 : }
1588 : }
1589 10 : }
1590 :
1591 : /************************************************************************/
1592 : /* ParseAIXMElevationPoint() */
1593 : /************************************************************************/
1594 :
1595 2 : CPLXMLNode *GMLHandler::ParseAIXMElevationPoint(CPLXMLNode *psGML)
1596 : {
1597 2 : ParseAIXMElevationProperties(psGML);
1598 :
1599 2 : const char *pszPos = CPLGetXMLValue(psGML, "pos", nullptr);
1600 2 : const char *pszCoordinates = CPLGetXMLValue(psGML, "coordinates", nullptr);
1601 2 : if (pszPos != nullptr || pszCoordinates != nullptr)
1602 : {
1603 2 : CPLFree(psGML->pszValue);
1604 2 : psGML->pszValue = CPLStrdup("gml:Point");
1605 : }
1606 : else
1607 : {
1608 0 : CPLDestroyXMLNode(psGML);
1609 0 : psGML = nullptr;
1610 : }
1611 :
1612 2 : return psGML;
1613 : }
1614 :
1615 : /************************************************************************/
1616 : /* endElementGeometry() */
1617 : /************************************************************************/
1618 34040 : OGRErr GMLHandler::endElementGeometry()
1619 :
1620 : {
1621 34040 : if (m_nGeomLen)
1622 : {
1623 : CPLXMLNode *psNode =
1624 5110 : static_cast<CPLXMLNode *>(CPLCalloc(sizeof(CPLXMLNode), 1));
1625 5110 : psNode->eType = CXT_Text;
1626 5110 : psNode->pszValue = m_pszGeometry;
1627 :
1628 5110 : NodeLastChild &sNodeLastChild = apsXMLNode.back();
1629 5110 : CPLXMLNode *psLastChildParent = sNodeLastChild.psLastChild;
1630 5110 : if (psLastChildParent == nullptr)
1631 : {
1632 2404 : CPLXMLNode *psParent = sNodeLastChild.psNode;
1633 2404 : if (psParent)
1634 2404 : psParent->psChild = psNode;
1635 : }
1636 : else
1637 2706 : psLastChildParent->psNext = psNode;
1638 5110 : sNodeLastChild.psLastChild = psNode;
1639 :
1640 5110 : m_pszGeometry = nullptr;
1641 5110 : m_nGeomAlloc = 0;
1642 5110 : m_nGeomLen = 0;
1643 : }
1644 :
1645 34040 : CPLXMLNode *psThisNode = apsXMLNode.back().psNode;
1646 34040 : CPLXMLNode *psThisNodeChild = psThisNode->psChild;
1647 41003 : if (!m_oMapElementToSubstitute.empty() && psThisNodeChild &&
1648 6963 : psThisNodeChild->eType == CXT_Attribute &&
1649 42119 : strcmp(psThisNodeChild->pszValue, "gml:id") == 0 &&
1650 1116 : psThisNodeChild->psChild->pszValue)
1651 : {
1652 : auto oIter =
1653 1116 : m_oMapElementToSubstitute.find(psThisNodeChild->psChild->pszValue);
1654 1116 : if (oIter != m_oMapElementToSubstitute.end())
1655 : {
1656 165 : auto psLastChild = oIter->second->psChild;
1657 165 : if (psLastChild)
1658 : {
1659 : // CPLDebug("GML", "Substitution of xlink:href=\"#%s\" with actual content", psThisNodeChild->psChild->pszValue);
1660 165 : CPLXMLNode *psAfter = psThisNode->psNext;
1661 165 : psThisNode->psNext = nullptr;
1662 : // We can patch oIter->second as it stored as it in the current
1663 : // GMLFeature.
1664 : // Of course that would no longer be the case in case of
1665 : // cross-references between different GMLFeature, hence we clear
1666 : // m_oMapElementToSubstitute at the end of the current feature.
1667 254 : while (psLastChild->psNext)
1668 89 : psLastChild = psLastChild->psNext;
1669 165 : if (psLastChild == psThisNode)
1670 : {
1671 : /* Can happen in situations like:
1672 : <foo xlink:href="#X">
1673 : <bar gml:id="X"/>
1674 : </foo>
1675 : Do not attempt substitution as that would cause a memory
1676 : leak.
1677 : */
1678 : }
1679 : else
1680 : {
1681 164 : psLastChild->psNext = CPLCloneXMLTree(psThisNode);
1682 : }
1683 165 : psThisNode->psNext = psAfter;
1684 : }
1685 : }
1686 : }
1687 :
1688 34040 : if (m_nDepth == m_nGeometryDepth)
1689 : {
1690 3184 : m_nGeometryDepth = 0;
1691 :
1692 3184 : CPLAssert(apsXMLNode.size() == 2);
1693 3184 : CPLXMLNode *psInterestNode = apsXMLNode.back().psNode;
1694 :
1695 : /*char* pszXML = CPLSerializeXMLTree(psInterestNode);
1696 : CPLDebug("GML", "geometry = %s", pszXML);
1697 : CPLFree(pszXML);*/
1698 :
1699 3184 : apsXMLNode.pop_back();
1700 :
1701 : /* AIXM ElevatedPoint. We want to parse this */
1702 : /* a bit specially because ElevatedPoint is aixm: stuff and */
1703 : /* the srsDimension of the <gml:pos> can be set to true although */
1704 : /* they are only 2 coordinates in practice */
1705 3184 : if (eAppSchemaType == APPSCHEMA_AIXM && psInterestNode != nullptr &&
1706 12 : strcmp(psInterestNode->pszValue, "ElevatedPoint") == 0)
1707 : {
1708 2 : psInterestNode = ParseAIXMElevationPoint(psInterestNode);
1709 : }
1710 3182 : else if (eAppSchemaType == APPSCHEMA_AIXM &&
1711 10 : psInterestNode != nullptr &&
1712 10 : (strcmp(psInterestNode->pszValue, "ElevatedCurve") == 0 ||
1713 2 : strcmp(psInterestNode->pszValue, "ElevateSurface") == 0))
1714 : {
1715 8 : ParseAIXMElevationProperties(psInterestNode);
1716 : }
1717 3174 : else if (eAppSchemaType == APPSCHEMA_MTKGML &&
1718 : psInterestNode != nullptr)
1719 : {
1720 36 : if (strcmp(psInterestNode->pszValue, "Murtoviiva") == 0)
1721 : {
1722 7 : CPLFree(psInterestNode->pszValue);
1723 7 : psInterestNode->pszValue = CPLStrdup("gml:LineString");
1724 : }
1725 29 : else if (strcmp(psInterestNode->pszValue, "Alue") == 0)
1726 : {
1727 7 : CPLFree(psInterestNode->pszValue);
1728 7 : psInterestNode->pszValue = CPLStrdup("gml:Polygon");
1729 : }
1730 22 : else if (strcmp(psInterestNode->pszValue, "Piste") == 0)
1731 : {
1732 21 : CPLFree(psInterestNode->pszValue);
1733 21 : psInterestNode->pszValue = CPLStrdup("gml:Point");
1734 : }
1735 : }
1736 3138 : else if (psInterestNode != nullptr &&
1737 3138 : strcmp(psInterestNode->pszValue, "BoundingBox") == 0)
1738 : {
1739 27 : CPLFree(psInterestNode->pszValue);
1740 27 : psInterestNode->pszValue = CPLStrdup("Envelope");
1741 30 : for (CPLXMLNode *psChild = psInterestNode->psChild; psChild;
1742 3 : psChild = psChild->psNext)
1743 : {
1744 29 : if (psChild->eType == CXT_Attribute &&
1745 27 : strcmp(psChild->pszValue, "crs") == 0)
1746 : {
1747 26 : CPLFree(psChild->pszValue);
1748 26 : psChild->pszValue = CPLStrdup("srsName");
1749 26 : break;
1750 : }
1751 : }
1752 : }
1753 :
1754 3184 : GMLFeature *poGMLFeature = m_poReader->GetState()->m_poFeature;
1755 3184 : if (stateStack[nStackDepth] == STATE_BOUNDED_BY_IN_FEATURE)
1756 : {
1757 78 : if (eAppSchemaType == APPSCHEMA_CITYGML)
1758 58 : CPLDestroyXMLNode(psInterestNode);
1759 : else
1760 20 : poGMLFeature->SetBoundedByGeometry(psInterestNode);
1761 : }
1762 : else
1763 : {
1764 3106 : if (m_poReader->FetchAllGeometries())
1765 0 : poGMLFeature->AddGeometry(psInterestNode);
1766 : else
1767 : {
1768 3106 : GMLFeatureClass *poClass = poGMLFeature->GetClass();
1769 3106 : if (poClass->GetGeometryPropertyCount() > 1)
1770 : {
1771 172 : if (poGMLFeature->GetGeometryRef(m_nGeometryPropertyIndex))
1772 : {
1773 : // If we have already a geometry, setting a new one
1774 : // will invalidate nodes potentially stored in
1775 : // m_oMapElementToSubstitute, so clear it
1776 0 : m_oMapElementToSubstitute.clear();
1777 : }
1778 172 : poGMLFeature->SetGeometryDirectly(m_nGeometryPropertyIndex,
1779 : psInterestNode);
1780 : }
1781 : else
1782 : {
1783 2934 : if (poGMLFeature->GetGeometryRef(0))
1784 : {
1785 : // If we have already a geometry, setting a new one
1786 : // will invalidate nodes potentially stored in
1787 : // m_oMapElementToSubstitute, so clear it
1788 21 : m_oMapElementToSubstitute.clear();
1789 : }
1790 2934 : poGMLFeature->SetGeometryDirectly(psInterestNode);
1791 : }
1792 : }
1793 :
1794 3106 : POP_STATE();
1795 : }
1796 : }
1797 :
1798 34040 : apsXMLNode.pop_back();
1799 :
1800 34040 : return OGRERR_NONE;
1801 : }
1802 :
1803 : /************************************************************************/
1804 : /* endElementCityGMLGenericAttr() */
1805 : /************************************************************************/
1806 84 : OGRErr GMLHandler::endElementCityGMLGenericAttr()
1807 :
1808 : {
1809 84 : if (m_pszCityGMLGenericAttrName != nullptr && m_bInCurField)
1810 : {
1811 42 : if (m_pszCurField != nullptr)
1812 : {
1813 42 : m_poReader->SetFeaturePropertyDirectly(m_pszCityGMLGenericAttrName,
1814 : m_pszCurField, -1);
1815 : }
1816 42 : m_pszCurField = nullptr;
1817 42 : m_nCurFieldLen = 0;
1818 42 : m_nCurFieldAlloc = 0;
1819 42 : m_bInCurField = false;
1820 42 : CPLFree(m_pszCityGMLGenericAttrName);
1821 42 : m_pszCityGMLGenericAttrName = nullptr;
1822 : }
1823 :
1824 84 : if (m_inCityGMLGenericAttrDepth == m_nDepth)
1825 : {
1826 42 : POP_STATE();
1827 : }
1828 :
1829 84 : return OGRERR_NONE;
1830 : }
1831 :
1832 : /************************************************************************/
1833 : /* endElementAttribute() */
1834 : /************************************************************************/
1835 10678 : OGRErr GMLHandler::endElementAttribute()
1836 :
1837 : {
1838 10678 : GMLReadState *poState = m_poReader->GetState();
1839 :
1840 10678 : if (m_bInCurField)
1841 : {
1842 7935 : if (m_pszCurField == nullptr && m_poReader->IsEmptyAsNull())
1843 : {
1844 70 : if (m_pszValue != nullptr)
1845 : {
1846 10 : m_poReader->SetFeaturePropertyDirectly(poState->osPath.c_str(),
1847 : m_pszValue, -1);
1848 10 : m_pszValue = nullptr;
1849 : }
1850 : }
1851 : else
1852 : {
1853 7865 : m_poReader->SetFeaturePropertyDirectly(
1854 : poState->osPath.c_str(),
1855 7865 : m_pszCurField ? m_pszCurField : CPLStrdup(""),
1856 : m_nAttributeIndex);
1857 7865 : m_pszCurField = nullptr;
1858 : }
1859 :
1860 7935 : if (m_pszHref != nullptr)
1861 : {
1862 4 : CPLString osPropNameHref = poState->osPath + "_href";
1863 4 : m_poReader->SetFeaturePropertyDirectly(osPropNameHref, m_pszHref,
1864 : -1);
1865 4 : m_pszHref = nullptr;
1866 : }
1867 :
1868 7935 : if (m_pszUom != nullptr)
1869 : {
1870 20 : CPLString osPropNameUom = poState->osPath + "_uom";
1871 20 : m_poReader->SetFeaturePropertyDirectly(osPropNameUom, m_pszUom, -1);
1872 20 : m_pszUom = nullptr;
1873 : }
1874 :
1875 7935 : if (m_pszKieli != nullptr)
1876 : {
1877 7 : CPLString osPropName = poState->osPath + "_kieli";
1878 7 : m_poReader->SetFeaturePropertyDirectly(osPropName, m_pszKieli, -1);
1879 7 : m_pszKieli = nullptr;
1880 : }
1881 :
1882 7935 : m_nCurFieldLen = 0;
1883 7935 : m_nCurFieldAlloc = 0;
1884 7935 : m_bInCurField = false;
1885 7935 : m_nAttributeIndex = -1;
1886 :
1887 7935 : CPLFree(m_pszValue);
1888 7935 : m_pszValue = nullptr;
1889 : }
1890 :
1891 10678 : poState->PopPath();
1892 :
1893 10678 : if (m_nAttributeDepth == m_nDepth)
1894 : {
1895 8836 : POP_STATE();
1896 : }
1897 :
1898 10678 : return OGRERR_NONE;
1899 : }
1900 :
1901 : /************************************************************************/
1902 : /* startElementFeatureProperty() */
1903 : /************************************************************************/
1904 :
1905 165 : OGRErr GMLHandler::startElementFeatureProperty(const char * /*pszName*/,
1906 : int /*nLenName*/, void *attr)
1907 : {
1908 165 : if (m_nDepth == m_nAttributeDepth + 1)
1909 : {
1910 24 : const char *pszGMLId = GetFID(attr);
1911 24 : if (pszGMLId != nullptr)
1912 : {
1913 24 : m_poReader->SetFeaturePropertyDirectly(
1914 : nullptr, CPLStrdup(CPLSPrintf("#%s", pszGMLId)),
1915 : m_nAttributeIndex);
1916 : }
1917 : }
1918 :
1919 165 : return OGRERR_NONE;
1920 : }
1921 :
1922 : /************************************************************************/
1923 : /* endElementFeatureProperty() */
1924 : /************************************************************************/
1925 :
1926 189 : OGRErr GMLHandler::endElementFeatureProperty()
1927 :
1928 : {
1929 189 : if (m_nDepth == m_nAttributeDepth)
1930 : {
1931 24 : GMLReadState *poState = m_poReader->GetState();
1932 24 : poState->PopPath();
1933 :
1934 24 : POP_STATE();
1935 : }
1936 189 : return OGRERR_NONE;
1937 : }
1938 :
1939 : /************************************************************************/
1940 : /* endElementFeature() */
1941 : /************************************************************************/
1942 5958 : OGRErr GMLHandler::endElementFeature()
1943 :
1944 : {
1945 : /* -------------------------------------------------------------------- */
1946 : /* If we are collecting a feature, and this element tag matches */
1947 : /* element name for the class, then we have finished the */
1948 : /* feature, and we pop the feature read state. */
1949 : /* -------------------------------------------------------------------- */
1950 5958 : if (m_nDepth == m_nDepthFeature)
1951 : {
1952 3156 : m_oMapElementToSubstitute.clear();
1953 3156 : m_poReader->PopState();
1954 :
1955 3156 : POP_STATE();
1956 : }
1957 :
1958 : /* -------------------------------------------------------------------- */
1959 : /* Otherwise, we just pop the element off the local read states */
1960 : /* element stack. */
1961 : /* -------------------------------------------------------------------- */
1962 : else
1963 : {
1964 2802 : m_poReader->GetState()->PopPath();
1965 : }
1966 :
1967 5958 : return OGRERR_NONE;
1968 : }
1969 :
1970 : /************************************************************************/
1971 : /* endElementDefault() */
1972 : /************************************************************************/
1973 3942 : OGRErr GMLHandler::endElementDefault()
1974 :
1975 : {
1976 3942 : if (m_nDepth > 0)
1977 3368 : m_poReader->GetState()->PopPath();
1978 :
1979 3942 : return OGRERR_NONE;
1980 : }
1981 :
1982 : /************************************************************************/
1983 : /* dataHandlerAttribute() */
1984 : /************************************************************************/
1985 :
1986 14463 : OGRErr GMLHandler::dataHandlerAttribute(const char *data, int nLen)
1987 :
1988 : {
1989 14463 : if (!m_bInCurField)
1990 3476 : return OGRERR_NONE;
1991 :
1992 10987 : int nIter = 0;
1993 :
1994 : // Ignore white space.
1995 10987 : if (m_nCurFieldLen == 0)
1996 : {
1997 25616 : while (nIter < nLen)
1998 : {
1999 22538 : const char ch = data[nIter];
2000 22538 : if (!(ch == ' ' || ch == 10 || ch == 13 || ch == '\t'))
2001 7907 : break;
2002 14631 : nIter++;
2003 : }
2004 : }
2005 :
2006 10987 : const int nCharsLen = nLen - nIter;
2007 :
2008 10987 : if (nCharsLen > INT_MAX - static_cast<int>(m_nCurFieldLen) - 1)
2009 : {
2010 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
2011 : "Too much data in a single element");
2012 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2013 : }
2014 10987 : if (m_nCurFieldLen + nCharsLen + 1 > m_nCurFieldAlloc)
2015 : {
2016 9245 : if (m_nCurFieldAlloc < INT_MAX - m_nCurFieldAlloc / 3 - nCharsLen - 1)
2017 9245 : m_nCurFieldAlloc =
2018 9245 : m_nCurFieldAlloc + m_nCurFieldAlloc / 3 + nCharsLen + 1;
2019 : else
2020 0 : m_nCurFieldAlloc = m_nCurFieldLen + nCharsLen + 1;
2021 : char *pszNewCurField = static_cast<char *>(
2022 9245 : VSI_REALLOC_VERBOSE(m_pszCurField, m_nCurFieldAlloc));
2023 9245 : if (pszNewCurField == nullptr)
2024 : {
2025 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2026 : }
2027 9245 : m_pszCurField = pszNewCurField;
2028 : }
2029 10987 : memcpy(m_pszCurField + m_nCurFieldLen, data + nIter, nCharsLen);
2030 10987 : m_nCurFieldLen += nCharsLen;
2031 10987 : m_pszCurField[m_nCurFieldLen] = '\0';
2032 :
2033 10987 : return OGRERR_NONE;
2034 : }
2035 :
2036 : /************************************************************************/
2037 : /* dataHandlerGeometry() */
2038 : /************************************************************************/
2039 :
2040 94682 : OGRErr GMLHandler::dataHandlerGeometry(const char *data, int nLen)
2041 :
2042 : {
2043 94682 : int nIter = 0;
2044 :
2045 : // Ignore white space
2046 94682 : if (m_nGeomLen == 0)
2047 : {
2048 737643 : while (nIter < nLen)
2049 : {
2050 648110 : char ch = data[nIter];
2051 648110 : if (!(ch == ' ' || ch == 10 || ch == 13 || ch == '\t'))
2052 5110 : break;
2053 643000 : nIter++;
2054 : }
2055 : }
2056 :
2057 94682 : const int nCharsLen = nLen - nIter;
2058 94682 : if (nCharsLen)
2059 : {
2060 5149 : if (nCharsLen > INT_MAX - static_cast<int>(m_nGeomLen) - 1)
2061 : {
2062 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
2063 : "Too much data in a single element");
2064 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2065 : }
2066 5149 : if (m_nGeomLen + nCharsLen + 1 > m_nGeomAlloc)
2067 : {
2068 5144 : if (m_nGeomAlloc < INT_MAX - m_nGeomAlloc / 3 - nCharsLen - 1)
2069 5144 : m_nGeomAlloc = m_nGeomAlloc + m_nGeomAlloc / 3 + nCharsLen + 1;
2070 : else
2071 0 : m_nGeomAlloc = m_nGeomAlloc + nCharsLen + 1;
2072 : char *pszNewGeometry = static_cast<char *>(
2073 5144 : VSI_REALLOC_VERBOSE(m_pszGeometry, m_nGeomAlloc));
2074 5144 : if (pszNewGeometry == nullptr)
2075 : {
2076 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2077 : }
2078 5144 : m_pszGeometry = pszNewGeometry;
2079 : }
2080 5149 : memcpy(m_pszGeometry + m_nGeomLen, data + nIter, nCharsLen);
2081 5149 : m_nGeomLen += nCharsLen;
2082 5149 : m_pszGeometry[m_nGeomLen] = '\0';
2083 : }
2084 :
2085 94682 : return OGRERR_NONE;
2086 : }
2087 :
2088 : /************************************************************************/
2089 : /* IsGeometryElement() */
2090 : /************************************************************************/
2091 :
2092 16797 : bool GMLHandler::IsGeometryElement(const char *pszElement)
2093 :
2094 : {
2095 16797 : int nFirst = 0;
2096 16797 : int nLast = GML_GEOMETRY_TYPE_COUNT - 1;
2097 16797 : unsigned long nHash = CPLHashSetHashStr(pszElement);
2098 60346 : do
2099 : {
2100 77143 : const int nMiddle = (nFirst + nLast) / 2;
2101 77143 : if (nHash == pasGeometryNames[nMiddle].nHash)
2102 3068 : return strcmp(pszElement, pasGeometryNames[nMiddle].pszName) == 0;
2103 74075 : if (nHash < pasGeometryNames[nMiddle].nHash)
2104 46826 : nLast = nMiddle - 1;
2105 : else
2106 27249 : nFirst = nMiddle + 1;
2107 74075 : } while (nFirst <= nLast);
2108 :
2109 13729 : if (eAppSchemaType == APPSCHEMA_AIXM &&
2110 265 : (strcmp(pszElement, "ElevatedPoint") == 0 ||
2111 263 : strcmp(pszElement, "ElevatedCurve") == 0 ||
2112 255 : strcmp(pszElement, "ElevatedSurface") == 0))
2113 : {
2114 12 : return true;
2115 : }
2116 :
2117 13717 : if (eAppSchemaType == APPSCHEMA_MTKGML &&
2118 77 : (strcmp(pszElement, "Piste") == 0 || strcmp(pszElement, "Alue") == 0 ||
2119 49 : strcmp(pszElement, "Murtoviiva") == 0))
2120 35 : return true;
2121 :
2122 13682 : return false;
2123 : }
|