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 599 : GMLExpatHandler::GMLExpatHandler(GMLReader *poReader, XML_Parser oParser)
248 : : GMLHandler(poReader), m_oParser(oParser), m_bStopParsing(false),
249 599 : m_nDataHandlerCounter(0)
250 : {
251 599 : }
252 :
253 : /************************************************************************/
254 : /* GMLExpatHandler::DealWithError() */
255 : /************************************************************************/
256 :
257 368518 : void GMLExpatHandler::DealWithError(OGRErr eErr)
258 : {
259 368518 : 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 368518 : }
267 :
268 : /************************************************************************/
269 : /* startElementCbk() */
270 : /************************************************************************/
271 :
272 61035 : void XMLCALL GMLExpatHandler::startElementCbk(void *pUserData,
273 : const char *pszName,
274 : const char **ppszAttr)
275 :
276 : {
277 61035 : GMLExpatHandler *pThis = static_cast<GMLExpatHandler *>(pUserData);
278 61035 : if (pThis->m_bStopParsing)
279 0 : return;
280 :
281 61035 : const char *pszIter = pszName;
282 61035 : char ch = '\0';
283 800328 : while ((ch = *pszIter) != '\0')
284 : {
285 739293 : if (ch == ':')
286 47855 : pszName = pszIter + 1;
287 739293 : pszIter++;
288 : }
289 :
290 61035 : pThis->DealWithError(pThis->GMLHandler::startElement(
291 61035 : pszName, static_cast<int>(pszIter - pszName), ppszAttr));
292 : }
293 :
294 : /************************************************************************/
295 : /* endElementCbk() */
296 : /************************************************************************/
297 60917 : void XMLCALL GMLExpatHandler::endElementCbk(void *pUserData,
298 : const char * /* pszName */)
299 : {
300 60917 : GMLExpatHandler *pThis = static_cast<GMLExpatHandler *>(pUserData);
301 60917 : if (pThis->m_bStopParsing)
302 1 : return;
303 :
304 60916 : pThis->DealWithError(pThis->GMLHandler::endElement());
305 : }
306 :
307 : /************************************************************************/
308 : /* dataHandlerCbk() */
309 : /************************************************************************/
310 :
311 246568 : void XMLCALL GMLExpatHandler::dataHandlerCbk(void *pUserData, const char *data,
312 : int nLen)
313 :
314 : {
315 246568 : GMLExpatHandler *pThis = static_cast<GMLExpatHandler *>(pUserData);
316 246568 : if (pThis->m_bStopParsing)
317 0 : return;
318 :
319 246568 : 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 246568 : 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 246567 : pThis->DealWithError(pThis->GMLHandler::dataHandler(data, nLen));
337 : }
338 :
339 : /************************************************************************/
340 : /* GetFID() */
341 : /************************************************************************/
342 :
343 3328 : const char *GMLExpatHandler::GetFID(void *attr)
344 : {
345 3328 : const char *const *papszIter = static_cast<const char *const *>(attr);
346 3346 : while (*papszIter)
347 : {
348 1832 : if (strcmp(*papszIter, "fid") == 0 || strcmp(*papszIter, "gml:id") == 0)
349 : {
350 1814 : return papszIter[1];
351 : }
352 18 : papszIter += 2;
353 : }
354 1514 : return nullptr;
355 : }
356 :
357 : /************************************************************************/
358 : /* AddAttributes() */
359 : /************************************************************************/
360 :
361 34076 : CPLXMLNode *GMLExpatHandler::AddAttributes(CPLXMLNode *psNode, void *attr)
362 : {
363 34076 : const char **papszIter = static_cast<const char **>(attr);
364 :
365 34076 : CPLXMLNode *psLastChild = nullptr;
366 :
367 49188 : while (*papszIter)
368 : {
369 : CPLXMLNode *psChild =
370 15112 : CPLCreateXMLNode(nullptr, CXT_Attribute, papszIter[0]);
371 15112 : CPLCreateXMLNode(psChild, CXT_Text, papszIter[1]);
372 :
373 15112 : if (psLastChild == nullptr)
374 14079 : psNode->psChild = psChild;
375 : else
376 1033 : psLastChild->psNext = psChild;
377 15112 : psLastChild = psChild;
378 :
379 15112 : papszIter += 2;
380 : }
381 :
382 34076 : return psLastChild;
383 : }
384 :
385 : /************************************************************************/
386 : /* GetAttributeValue() */
387 : /************************************************************************/
388 :
389 11158 : char *GMLExpatHandler::GetAttributeValue(void *attr,
390 : const char *pszAttributeName)
391 : {
392 11158 : const char *const *papszIter = static_cast<const char *const *>(attr);
393 11772 : while (*papszIter)
394 : {
395 861 : if (strcmp(*papszIter, pszAttributeName) == 0)
396 : {
397 247 : return CPLStrdup(papszIter[1]);
398 : }
399 614 : papszIter += 2;
400 : }
401 10911 : 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 13910 : char *GMLExpatHandler::GetAttributeByIdx(void *attr, unsigned int idx,
411 : char **ppszKey)
412 : {
413 13910 : const char *const *papszIter = static_cast<const char *const *>(attr);
414 13910 : if (papszIter[2 * idx] == nullptr)
415 : {
416 13288 : *ppszKey = nullptr;
417 13288 : 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 601 : GMLHandler::GMLHandler(GMLReader *poReader)
480 : : pasGeometryNames(static_cast<GeometryNamesStruct *>(
481 1202 : CPLMalloc(GML_GEOMETRY_TYPE_COUNT * sizeof(GeometryNamesStruct)))),
482 : m_nSRSDimensionIfMissing(
483 601 : atoi(CPLGetConfigOption("GML_SRS_DIMENSION_IF_MISSING", "0"))),
484 1202 : m_poReader(poReader), eAppSchemaType(APPSCHEMA_GENERIC), nStackDepth(0)
485 : {
486 17429 : for (int i = 0; i < GML_GEOMETRY_TYPE_COUNT; i++)
487 : {
488 16828 : pasGeometryNames[i].pszName = apszGMLGeometryElements[i];
489 33656 : pasGeometryNames[i].nHash =
490 16828 : CPLHashSetHashStr(pasGeometryNames[i].pszName);
491 : }
492 601 : std::sort(pasGeometryNames, pasGeometryNames + GML_GEOMETRY_TYPE_COUNT,
493 102170 : [](const GeometryNamesStruct &a, const GeometryNamesStruct &b)
494 102170 : { return a.nHash < b.nHash; });
495 :
496 601 : stateStack[0] = STATE_TOP;
497 601 : }
498 :
499 : /************************************************************************/
500 : /* ~GMLHandler() */
501 : /************************************************************************/
502 :
503 601 : GMLHandler::~GMLHandler()
504 :
505 : {
506 601 : if (apsXMLNode.size() >= 2 && apsXMLNode[1].psNode != nullptr)
507 4 : CPLDestroyXMLNode(apsXMLNode[1].psNode);
508 :
509 601 : CPLFree(m_pszCurField);
510 601 : CPLFree(m_pszGeometry);
511 601 : CPLFree(m_pszCityGMLGenericAttrName);
512 601 : CPLFree(m_pszHref);
513 601 : CPLFree(m_pszUom);
514 601 : CPLFree(m_pszValue);
515 601 : CPLFree(m_pszKieli);
516 601 : CPLFree(pasGeometryNames);
517 601 : }
518 :
519 : /************************************************************************/
520 : /* startElement() */
521 : /************************************************************************/
522 :
523 61054 : OGRErr GMLHandler::startElement(const char *pszName, int nLenName, void *attr)
524 : {
525 : OGRErr eRet;
526 61054 : switch (stateStack[nStackDepth])
527 : {
528 599 : case STATE_TOP:
529 599 : eRet = startElementTop(pszName, nLenName, attr);
530 599 : break;
531 7249 : case STATE_DEFAULT:
532 7249 : eRet = startElementDefault(pszName, nLenName, attr);
533 7249 : break;
534 13427 : case STATE_FEATURE:
535 13427 : eRet = startElementFeatureAttribute(pszName, nLenName, attr);
536 13427 : break;
537 3409 : case STATE_PROPERTY:
538 3409 : eRet = startElementFeatureAttribute(pszName, nLenName, attr);
539 3409 : break;
540 165 : case STATE_FEATUREPROPERTY:
541 165 : eRet = startElementFeatureProperty(pszName, nLenName, attr);
542 165 : break;
543 29876 : case STATE_GEOMETRY:
544 29876 : eRet = startElementGeometry(pszName, nLenName, attr);
545 29876 : 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 61054 : m_nDepth++;
563 61054 : 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 61054 : return eRet;
583 : }
584 :
585 : /************************************************************************/
586 : /* endElement() */
587 : /************************************************************************/
588 :
589 60931 : OGRErr GMLHandler::endElement()
590 : {
591 60931 : m_nDepth--;
592 60931 : switch (stateStack[nStackDepth])
593 : {
594 0 : case STATE_TOP:
595 0 : break;
596 3997 : case STATE_DEFAULT:
597 3997 : return endElementDefault();
598 5999 : case STATE_FEATURE:
599 5999 : return endElementFeature();
600 10714 : case STATE_PROPERTY:
601 10714 : return endElementAttribute();
602 189 : case STATE_FEATUREPROPERTY:
603 189 : return endElementFeatureProperty();
604 32957 : case STATE_GEOMETRY:
605 32957 : 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 247472 : OGRErr GMLHandler::dataHandler(const char *data, int nLen)
625 : {
626 247472 : switch (stateStack[nStackDepth])
627 : {
628 131772 : case STATE_TOP:
629 : case STATE_DEFAULT:
630 : case STATE_FEATURE:
631 131772 : break;
632 14364 : case STATE_PROPERTY:
633 14364 : return dataHandlerAttribute(data, nLen);
634 625 : case STATE_FEATUREPROPERTY:
635 625 : break;
636 92283 : case STATE_GEOMETRY:
637 92283 : 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 138273 : 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 34079 : OGRErr GMLHandler::startElementGeometry(const char *pszName, int nLenName,
691 : void *attr)
692 : {
693 35169 : 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 34078 : static_cast<CPLXMLNode *>(CPLCalloc(sizeof(CPLXMLNode), 1));
703 34078 : psCurNode->eType = CXT_Element;
704 34078 : psCurNode->pszValue = static_cast<char *>(CPLMalloc(nLenName + 1));
705 34078 : memcpy(psCurNode->pszValue, pszName, nLenName + 1);
706 :
707 : /* Attach element as the last child of its parent */
708 34078 : NodeLastChild &sNodeLastChild = apsXMLNode.back();
709 34078 : CPLXMLNode *psLastChildParent = sNodeLastChild.psLastChild;
710 :
711 34078 : if (psLastChildParent == nullptr)
712 : {
713 18166 : CPLXMLNode *psParent = sNodeLastChild.psNode;
714 18166 : if (psParent)
715 14975 : psParent->psChild = psCurNode;
716 : }
717 : else
718 : {
719 15912 : psLastChildParent->psNext = psCurNode;
720 : }
721 34078 : sNodeLastChild.psLastChild = psCurNode;
722 :
723 : /* Add attributes to the element */
724 34078 : CPLXMLNode *psLastChildCurNode = AddAttributes(psCurNode, attr);
725 49193 : for (CPLXMLNode *psIter = psCurNode->psChild; psIter;
726 15115 : psIter = psIter->psNext)
727 : {
728 15115 : if (psIter->eType == CXT_Attribute &&
729 15115 : 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 71547 : if (strcmp(pszName, "posList") == 0 &&
742 34958 : 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 34078 : sNewNodeLastChild.psNode = psCurNode;
760 34078 : sNewNodeLastChild.psLastChild = psLastChildCurNode;
761 34078 : apsXMLNode.push_back(sNewNodeLastChild);
762 :
763 34078 : 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 34078 : 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 13297 : void GMLHandler::DealWithAttributes(const char *pszName, int nLenName,
802 : void *attr)
803 : {
804 13297 : GMLReadState *poState = m_poReader->GetState();
805 13297 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
806 :
807 13297 : for (unsigned int idx = 0; true; idx++)
808 : {
809 13919 : char *pszAttrKey = nullptr;
810 :
811 13919 : char *pszAttrVal = GetAttributeByIdx(attr, idx, &pszAttrKey);
812 13919 : if (pszAttrVal == nullptr)
813 13297 : 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 13297 : }
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 3086 : int GMLHandler::FindRealPropertyByCheckingConditions(int nIdx, void *attr)
1051 : {
1052 3086 : GMLReadState *poState = m_poReader->GetState();
1053 3086 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
1054 :
1055 3086 : GMLPropertyDefn *poProp = poClass->GetProperty(nIdx);
1056 3086 : const char *pszCond = poProp->GetCondition();
1057 3086 : 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 3086 : return nIdx;
1079 : }
1080 :
1081 : /************************************************************************/
1082 : /* startElementFeatureAttribute() */
1083 : /************************************************************************/
1084 :
1085 16836 : OGRErr GMLHandler::startElementFeatureAttribute(const char *pszName,
1086 : int nLenName, void *attr)
1087 : {
1088 :
1089 16836 : GMLReadState *poState = m_poReader->GetState();
1090 :
1091 19648 : if (m_bInCurField && m_nAttributeIndex >= 0 &&
1092 19648 : std::string_view(pszName, nLenName) == "timePosition")
1093 : {
1094 6 : poState->PushPath(pszName, nLenName);
1095 6 : return OGRERR_NONE;
1096 : }
1097 :
1098 : /* Reset flag */
1099 16830 : m_bInCurField = false;
1100 :
1101 : /* -------------------------------------------------------------------- */
1102 : /* If we are collecting geometry, or if we determine this is a */
1103 : /* geometry element then append to the geometry info. */
1104 : /* -------------------------------------------------------------------- */
1105 16830 : if (IsGeometryElement(pszName))
1106 : {
1107 : bool bReadGeometry;
1108 :
1109 : /* If the <GeometryElementPath> is defined in the .gfs, use it */
1110 : /* to read the appropriate geometry element */
1111 3118 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
1112 3118 : m_nGeometryPropertyIndex = 0;
1113 4412 : if (poClass->IsSchemaLocked() &&
1114 1294 : poClass->GetGeometryPropertyCount() == 0)
1115 : {
1116 0 : bReadGeometry = false;
1117 : }
1118 4412 : else if (poClass->IsSchemaLocked() &&
1119 4412 : poClass->GetGeometryPropertyCount() == 1 &&
1120 1132 : poClass->GetGeometryProperty(0)->GetSrcElement()[0] == '\0')
1121 : {
1122 70 : bReadGeometry = true;
1123 : }
1124 4272 : else if (poClass->IsSchemaLocked() &&
1125 1224 : poClass->GetGeometryPropertyCount() > 0)
1126 : {
1127 1224 : m_nGeometryPropertyIndex =
1128 1224 : poClass->GetGeometryPropertyIndexBySrcElement(
1129 : poState->osPath.c_str());
1130 1224 : bReadGeometry = (m_nGeometryPropertyIndex >= 0);
1131 : }
1132 1824 : else if (m_poReader->FetchAllGeometries())
1133 : {
1134 0 : bReadGeometry = true;
1135 : }
1136 1824 : else if (!poClass->IsSchemaLocked() && m_poReader->IsWFSJointLayer())
1137 : {
1138 12 : m_nGeometryPropertyIndex =
1139 12 : poClass->GetGeometryPropertyIndexBySrcElement(
1140 : poState->osPath.c_str());
1141 12 : if (m_nGeometryPropertyIndex < 0)
1142 : {
1143 4 : const char *pszElement = poState->osPath.c_str();
1144 4 : CPLString osFieldName;
1145 : /* Strip member| prefix. Should always be true normally */
1146 4 : if (STARTS_WITH(pszElement, "member|"))
1147 4 : osFieldName = pszElement + strlen("member|");
1148 :
1149 : /* Replace layer|property by layer_property */
1150 4 : size_t iPos = osFieldName.find('|');
1151 4 : if (iPos != std::string::npos)
1152 4 : osFieldName[iPos] = '.';
1153 :
1154 8 : poClass->AddGeometryProperty(new GMLGeometryPropertyDefn(
1155 4 : osFieldName, poState->osPath.c_str(), wkbUnknown, -1,
1156 8 : true));
1157 4 : m_nGeometryPropertyIndex = poClass->GetGeometryPropertyCount();
1158 : }
1159 12 : bReadGeometry = true;
1160 : }
1161 : else
1162 : {
1163 : /* AIXM special case: for RouteSegment, we only want to read Curve
1164 : * geometries */
1165 : /* not 'start' and 'end' geometries */
1166 1824 : if (eAppSchemaType == APPSCHEMA_AIXM &&
1167 12 : strcmp(poState->m_poFeature->GetClass()->GetName(),
1168 : "RouteSegment") == 0)
1169 0 : bReadGeometry = strcmp(pszName, "Curve") == 0;
1170 :
1171 : // AIXM special case: we want to read both horizontalProjection_location and
1172 : // horizontalProjection_linearExtent
1173 1824 : else if (eAppSchemaType == APPSCHEMA_AIXM &&
1174 12 : STARTS_WITH(poState->m_poFeature->GetClass()->GetName(),
1175 : "horizontalProjection_") == 0)
1176 12 : bReadGeometry =
1177 12 : STARTS_WITH(pszName, "horizontalProjection_") == 0;
1178 :
1179 : /* For Inspire objects : the "main" geometry is in a <geometry>
1180 : * element */
1181 1800 : else if (m_bAlreadyFoundGeometry)
1182 2 : bReadGeometry = false;
1183 1798 : else if (strcmp(poState->osPath.c_str(), "geometry") == 0)
1184 : {
1185 8 : m_bAlreadyFoundGeometry = true;
1186 8 : bReadGeometry = true;
1187 8 : m_nGeometryPropertyIndex =
1188 8 : poClass->GetGeometryPropertyIndexBySrcElement(
1189 : poState->osPath.c_str());
1190 8 : if (m_nGeometryPropertyIndex < 0)
1191 : {
1192 6 : poClass->AddGeometryProperty(new GMLGeometryPropertyDefn(
1193 3 : "geometry", poState->osPath.c_str(), wkbUnknown, -1,
1194 3 : true));
1195 3 : m_nGeometryPropertyIndex =
1196 3 : poClass->GetGeometryPropertyCount();
1197 : }
1198 : }
1199 : else
1200 : {
1201 3580 : if (!poClass->IsSchemaLocked() &&
1202 1790 : poClass->IsConsistentSingleGeomElemPath())
1203 : {
1204 : const std::string &osGeomElemPath =
1205 1780 : poClass->GetSingleGeomElemPath();
1206 1780 : if (osGeomElemPath.empty())
1207 : {
1208 415 : poClass->SetSingleGeomElemPath(poState->osPath);
1209 : }
1210 1365 : else if (poState->osPath != osGeomElemPath)
1211 : {
1212 5 : poClass->SetConsistentSingleGeomElemPath(false);
1213 5 : poClass->SetSingleGeomElemPath(std::string());
1214 : }
1215 : }
1216 1790 : bReadGeometry = true;
1217 : }
1218 : }
1219 3118 : if (bReadGeometry)
1220 : {
1221 3113 : m_nGeometryDepth = m_nDepth;
1222 :
1223 3113 : CPLAssert(apsXMLNode.empty());
1224 :
1225 : NodeLastChild sNodeLastChild;
1226 3113 : sNodeLastChild.psNode = nullptr;
1227 3113 : sNodeLastChild.psLastChild = nullptr;
1228 3113 : apsXMLNode.push_back(sNodeLastChild);
1229 :
1230 3113 : PUSH_STATE(STATE_GEOMETRY);
1231 :
1232 3113 : return startElementGeometry(pszName, nLenName, attr);
1233 : }
1234 : }
1235 13712 : else if (
1236 12355 : (nLenName == 9 || nLenName == 8) &&
1237 2540 : (strcmp(pszName, "boundedBy") == 0 ||
1238 27433 : strcmp(pszName, "boundary") == 0) &&
1239 : // We ignore the UseBBOX() flag for CityGML, since CityGML
1240 : // has elements like bldg:boundedBy or 'boundary', which are not a simple
1241 : // rectangular bbox. This is needed to read properly
1242 : // autotest/ogr/data/gml/citygml_lod2_713_5322.xml
1243 : // (this is a workaround of not being namespace aware)
1244 967 : (eAppSchemaType == APPSCHEMA_CITYGML || m_poReader->UseBBOX()))
1245 : {
1246 78 : m_inBoundedByDepth = m_nDepth;
1247 :
1248 78 : CPLAssert(apsXMLNode.empty());
1249 :
1250 : NodeLastChild sNodeLastChild;
1251 78 : sNodeLastChild.psNode = nullptr;
1252 78 : sNodeLastChild.psLastChild = nullptr;
1253 78 : apsXMLNode.push_back(sNodeLastChild);
1254 :
1255 78 : PUSH_STATE(STATE_BOUNDED_BY_IN_FEATURE);
1256 :
1257 78 : return OGRERR_NONE;
1258 : }
1259 :
1260 : /* -------------------------------------------------------------------- */
1261 : /* Is it a CityGML generic attribute ? */
1262 : /* -------------------------------------------------------------------- */
1263 13770 : else if (eAppSchemaType == APPSCHEMA_CITYGML &&
1264 136 : m_poReader->IsCityGMLGenericAttributeElement(pszName, attr))
1265 : {
1266 42 : CPLFree(m_pszCityGMLGenericAttrName);
1267 42 : m_pszCityGMLGenericAttrName = GetAttributeValue(attr, "name");
1268 42 : m_inCityGMLGenericAttrDepth = m_nDepth;
1269 :
1270 42 : PUSH_STATE(STATE_CITYGML_ATTRIBUTE);
1271 :
1272 42 : return OGRERR_NONE;
1273 : }
1274 :
1275 13592 : else if (m_poReader->IsWFSJointLayer() && m_nDepth == m_nDepthFeature + 1)
1276 : {
1277 : }
1278 :
1279 13468 : else if (m_poReader->IsWFSJointLayer() && m_nDepth == m_nDepthFeature + 2)
1280 : {
1281 124 : const char *pszFID = GetFID(attr);
1282 124 : if (pszFID)
1283 : {
1284 124 : poState->PushPath(pszName, nLenName);
1285 248 : CPLString osPropPath = poState->osPath + "@id";
1286 124 : poState->PopPath();
1287 124 : m_poReader->SetFeaturePropertyDirectly(osPropPath,
1288 : CPLStrdup(pszFID), -1);
1289 : }
1290 : }
1291 :
1292 : /* -------------------------------------------------------------------- */
1293 : /* If it is (or at least potentially is) a simple attribute, */
1294 : /* then start collecting it. */
1295 : /* -------------------------------------------------------------------- */
1296 13344 : else if ((m_nAttributeIndex = m_poReader->GetAttributeElementIndex(
1297 13344 : pszName, nLenName)) != -1)
1298 : {
1299 10793 : GMLFeatureClass *poClass = poState->m_poFeature->GetClass();
1300 16878 : if (poClass->IsSchemaLocked() &&
1301 3043 : (poClass->GetProperty(m_nAttributeIndex)->GetType() ==
1302 3042 : GMLPT_FeatureProperty ||
1303 3042 : poClass->GetProperty(m_nAttributeIndex)->GetType() ==
1304 : GMLPT_FeaturePropertyList))
1305 : {
1306 24 : m_nAttributeDepth = m_nDepth;
1307 24 : PUSH_STATE(STATE_FEATUREPROPERTY);
1308 : }
1309 : else
1310 : {
1311 : /* Is this a property with a condition on an attribute value ? */
1312 10769 : if (poClass->IsSchemaLocked())
1313 : {
1314 3019 : m_nAttributeIndex = FindRealPropertyByCheckingConditions(
1315 : m_nAttributeIndex, attr);
1316 : }
1317 :
1318 10769 : if (m_nAttributeIndex >= 0)
1319 : {
1320 10769 : if (m_pszCurField)
1321 : {
1322 1277 : CPLFree(m_pszCurField);
1323 1277 : m_pszCurField = nullptr;
1324 1277 : m_nCurFieldLen = 0;
1325 1277 : m_nCurFieldAlloc = 0;
1326 : }
1327 10769 : m_bInCurField = true;
1328 :
1329 10769 : char *pszXSINil = GetAttributeValue(attr, "xsi:nil");
1330 10769 : if (pszXSINil)
1331 : {
1332 23 : if (EQUAL(pszXSINil, "true"))
1333 23 : m_poReader->SetFeaturePropertyDirectly(
1334 : pszName, CPLStrdup(OGR_GML_NULL), -1);
1335 23 : CPLFree(pszXSINil);
1336 : }
1337 : else
1338 : {
1339 10746 : DealWithAttributes(pszName, nLenName, attr);
1340 : }
1341 :
1342 10769 : if (stateStack[nStackDepth] != STATE_PROPERTY)
1343 : {
1344 8869 : m_nAttributeDepth = m_nDepth;
1345 8869 : PUSH_STATE(STATE_PROPERTY);
1346 : }
1347 : }
1348 : /*else
1349 : {
1350 : DealWithAttributes(pszName, nLenName, attr);
1351 : }*/
1352 : }
1353 : }
1354 : else
1355 : {
1356 2551 : DealWithAttributes(pszName, nLenName, attr);
1357 : }
1358 :
1359 13597 : poState->PushPath(pszName, nLenName);
1360 :
1361 13597 : return OGRERR_NONE;
1362 : }
1363 :
1364 : /************************************************************************/
1365 : /* startElementTop() */
1366 : /************************************************************************/
1367 :
1368 599 : OGRErr GMLHandler::startElementTop(const char *pszName, int /*nLenName*/,
1369 : void *attr)
1370 : {
1371 599 : if (strcmp(pszName, "CityModel") == 0)
1372 : {
1373 10 : eAppSchemaType = APPSCHEMA_CITYGML;
1374 : // Default to 3D geometries for CityGML (#6989)
1375 10 : if (m_nSRSDimensionIfMissing == 0)
1376 10 : m_nSRSDimensionIfMissing = 3;
1377 : }
1378 589 : else if (strcmp(pszName, "AIXMBasicMessage") == 0)
1379 : {
1380 7 : eAppSchemaType = APPSCHEMA_AIXM;
1381 7 : m_bReportHref = true;
1382 : }
1383 582 : else if (strcmp(pszName, "Maastotiedot") == 0)
1384 : {
1385 8 : eAppSchemaType = APPSCHEMA_MTKGML;
1386 :
1387 8 : char *pszSRSName = GetAttributeValue(attr, "srsName");
1388 8 : m_poReader->SetGlobalSRSName(pszSRSName);
1389 8 : CPLFree(pszSRSName);
1390 :
1391 8 : m_bReportHref = true;
1392 :
1393 : /* the schemas of MTKGML don't have (string) width, so don't set it */
1394 8 : m_poReader->SetWidthFlag(false);
1395 : }
1396 :
1397 599 : stateStack[0] = STATE_DEFAULT;
1398 :
1399 599 : return OGRERR_NONE;
1400 : }
1401 :
1402 : /************************************************************************/
1403 : /* startElementDefault() */
1404 : /************************************************************************/
1405 :
1406 7249 : OGRErr GMLHandler::startElementDefault(const char *pszName, int nLenName,
1407 : void *attr)
1408 :
1409 : {
1410 : /* -------------------------------------------------------------------- */
1411 : /* Is it a feature? If so push a whole new state, and return. */
1412 : /* -------------------------------------------------------------------- */
1413 : int nClassIndex;
1414 7249 : const char *pszFilteredClassName = nullptr;
1415 :
1416 7249 : if (nLenName == 9 && strcmp(pszName, "boundedBy") == 0)
1417 : {
1418 283 : m_inBoundedByDepth = m_nDepth;
1419 :
1420 283 : PUSH_STATE(STATE_BOUNDED_BY);
1421 :
1422 283 : return OGRERR_NONE;
1423 : }
1424 :
1425 7787 : else if (m_poReader->ShouldLookForClassAtAnyLevel() &&
1426 821 : (pszFilteredClassName = m_poReader->GetFilteredClassName()) !=
1427 : nullptr)
1428 : {
1429 821 : if (strcmp(pszName, pszFilteredClassName) == 0)
1430 : {
1431 49 : m_poReader->PushFeature(pszName, GetFID(attr),
1432 49 : m_poReader->GetFilteredClassIndex());
1433 :
1434 49 : m_nDepthFeature = m_nDepth;
1435 :
1436 49 : PUSH_STATE(STATE_FEATURE);
1437 :
1438 49 : return OGRERR_NONE;
1439 : }
1440 : }
1441 :
1442 : /* WFS 2.0 GetFeature documents have a wfs:FeatureCollection */
1443 : /* as a wfs:member of the top wfs:FeatureCollection. We don't want this */
1444 : /* wfs:FeatureCollection to be recognized as a feature */
1445 6173 : else if ((!(nLenName == static_cast<int>(strlen("FeatureCollection")) &&
1446 12276 : strcmp(pszName, "FeatureCollection") == 0)) &&
1447 6131 : (nClassIndex = m_poReader->GetFeatureElementIndex(
1448 : pszName, nLenName, eAppSchemaType)) != -1)
1449 : {
1450 3499 : m_bAlreadyFoundGeometry = false;
1451 :
1452 3499 : pszFilteredClassName = m_poReader->GetFilteredClassName();
1453 3499 : if (pszFilteredClassName != nullptr &&
1454 377 : strcmp(pszName, pszFilteredClassName) != 0)
1455 : {
1456 345 : m_nDepthFeature = m_nDepth;
1457 :
1458 345 : PUSH_STATE(STATE_IGNORED_FEATURE);
1459 :
1460 345 : return OGRERR_NONE;
1461 : }
1462 : else
1463 : {
1464 3154 : if (eAppSchemaType == APPSCHEMA_MTKGML)
1465 : {
1466 22 : m_poReader->PushFeature(pszName, nullptr, nClassIndex);
1467 :
1468 22 : char *pszGID = GetAttributeValue(attr, "gid");
1469 22 : if (pszGID)
1470 21 : m_poReader->SetFeaturePropertyDirectly("gid", pszGID, -1,
1471 : GMLPT_String);
1472 : }
1473 : else
1474 3132 : m_poReader->PushFeature(pszName, GetFID(attr), nClassIndex);
1475 :
1476 3154 : m_nDepthFeature = m_nDepth;
1477 :
1478 3154 : PUSH_STATE(STATE_FEATURE);
1479 :
1480 3154 : return OGRERR_NONE;
1481 : }
1482 : }
1483 :
1484 : /* -------------------------------------------------------------------- */
1485 : /* Push the element onto the current state's path. */
1486 : /* -------------------------------------------------------------------- */
1487 3418 : m_poReader->GetState()->PushPath(pszName, nLenName);
1488 :
1489 3418 : return OGRERR_NONE;
1490 : }
1491 :
1492 : /************************************************************************/
1493 : /* endElementIgnoredFeature() */
1494 : /************************************************************************/
1495 :
1496 4348 : OGRErr GMLHandler::endElementIgnoredFeature()
1497 :
1498 : {
1499 4348 : if (m_nDepth == m_nDepthFeature)
1500 : {
1501 345 : POP_STATE();
1502 : }
1503 4348 : return OGRERR_NONE;
1504 : }
1505 :
1506 : /************************************************************************/
1507 : /* endElementBoundedBy() */
1508 : /************************************************************************/
1509 1477 : OGRErr GMLHandler::endElementBoundedBy()
1510 :
1511 : {
1512 1477 : if (m_inBoundedByDepth == m_nDepth)
1513 : {
1514 283 : POP_STATE();
1515 : }
1516 :
1517 1477 : return OGRERR_NONE;
1518 : }
1519 :
1520 : /************************************************************************/
1521 : /* endElementBoundedByInFeature() */
1522 : /************************************************************************/
1523 1166 : OGRErr GMLHandler::endElementBoundedByInFeature()
1524 :
1525 : {
1526 1166 : if (m_nDepth > m_inBoundedByDepth)
1527 : {
1528 1089 : if (m_nDepth == m_inBoundedByDepth + 1)
1529 : {
1530 78 : m_nGeometryDepth = m_nDepth;
1531 : }
1532 1089 : return endElementGeometry();
1533 : }
1534 : else
1535 : {
1536 77 : POP_STATE();
1537 77 : if (apsXMLNode.size() >= 2 && apsXMLNode[1].psNode != nullptr)
1538 0 : CPLDestroyXMLNode(apsXMLNode[1].psNode);
1539 77 : apsXMLNode.clear();
1540 77 : return OGRERR_NONE;
1541 : }
1542 : }
1543 :
1544 : /************************************************************************/
1545 : /* ParseAIXMElevationProperties() */
1546 : /************************************************************************/
1547 :
1548 10 : void GMLHandler::ParseAIXMElevationProperties(const CPLXMLNode *psGML)
1549 : {
1550 10 : if (const char *pszElevation = CPLGetXMLValue(psGML, "elevation", nullptr))
1551 : {
1552 7 : m_poReader->SetFeaturePropertyDirectly("elevation",
1553 : CPLStrdup(pszElevation), -1);
1554 : const char *pszElevationUnit =
1555 7 : CPLGetXMLValue(psGML, "elevation.uom", nullptr);
1556 7 : if (pszElevationUnit)
1557 : {
1558 5 : m_poReader->SetFeaturePropertyDirectly(
1559 : "elevation_uom", CPLStrdup(pszElevationUnit), -1);
1560 : }
1561 : }
1562 :
1563 10 : if (const char *pszGeoidUndulation =
1564 10 : CPLGetXMLValue(psGML, "geoidUndulation", nullptr))
1565 : {
1566 0 : m_poReader->SetFeaturePropertyDirectly(
1567 : "geoidUndulation", CPLStrdup(pszGeoidUndulation), -1);
1568 : const char *pszGeoidUndulationUnit =
1569 0 : CPLGetXMLValue(psGML, "geoidUndulation.uom", nullptr);
1570 0 : if (pszGeoidUndulationUnit)
1571 : {
1572 0 : m_poReader->SetFeaturePropertyDirectly(
1573 : "geoidUndulation_uom", CPLStrdup(pszGeoidUndulationUnit), -1);
1574 : }
1575 : }
1576 :
1577 10 : if (const char *pszVerticalDatum =
1578 10 : CPLGetXMLValue(psGML, "verticalDatum", nullptr))
1579 : {
1580 3 : m_poReader->SetFeaturePropertyDirectly("verticalDatum",
1581 : CPLStrdup(pszVerticalDatum), -1);
1582 : }
1583 :
1584 10 : if (const char *pszVerticalAccuracy =
1585 10 : CPLGetXMLValue(psGML, "verticalAccuracy", nullptr))
1586 : {
1587 5 : m_poReader->SetFeaturePropertyDirectly(
1588 : "verticalAccuracy", CPLStrdup(pszVerticalAccuracy), -1);
1589 : const char *pszVerticalAccuracyUnit =
1590 5 : CPLGetXMLValue(psGML, "verticalAccuracy.uom", nullptr);
1591 5 : if (pszVerticalAccuracyUnit)
1592 : {
1593 3 : m_poReader->SetFeaturePropertyDirectly(
1594 : "verticalAccuracy_uom", CPLStrdup(pszVerticalAccuracyUnit), -1);
1595 : }
1596 : }
1597 10 : }
1598 :
1599 : /************************************************************************/
1600 : /* ParseAIXMElevationPoint() */
1601 : /************************************************************************/
1602 :
1603 2 : CPLXMLNode *GMLHandler::ParseAIXMElevationPoint(CPLXMLNode *psGML)
1604 : {
1605 2 : ParseAIXMElevationProperties(psGML);
1606 :
1607 2 : const char *pszPos = CPLGetXMLValue(psGML, "pos", nullptr);
1608 2 : const char *pszCoordinates = CPLGetXMLValue(psGML, "coordinates", nullptr);
1609 2 : if (pszPos != nullptr || pszCoordinates != nullptr)
1610 : {
1611 2 : CPLFree(psGML->pszValue);
1612 2 : psGML->pszValue = CPLStrdup("gml:Point");
1613 : }
1614 : else
1615 : {
1616 0 : CPLDestroyXMLNode(psGML);
1617 0 : psGML = nullptr;
1618 : }
1619 :
1620 2 : return psGML;
1621 : }
1622 :
1623 : /************************************************************************/
1624 : /* endElementGeometry() */
1625 : /************************************************************************/
1626 34046 : OGRErr GMLHandler::endElementGeometry()
1627 :
1628 : {
1629 34046 : if (m_nGeomLen)
1630 : {
1631 : CPLXMLNode *psNode =
1632 5113 : static_cast<CPLXMLNode *>(CPLCalloc(sizeof(CPLXMLNode), 1));
1633 5113 : psNode->eType = CXT_Text;
1634 5113 : psNode->pszValue = m_pszGeometry;
1635 :
1636 5113 : NodeLastChild &sNodeLastChild = apsXMLNode.back();
1637 5113 : CPLXMLNode *psLastChildParent = sNodeLastChild.psLastChild;
1638 5113 : if (psLastChildParent == nullptr)
1639 : {
1640 2407 : CPLXMLNode *psParent = sNodeLastChild.psNode;
1641 2407 : if (psParent)
1642 2407 : psParent->psChild = psNode;
1643 : }
1644 : else
1645 2706 : psLastChildParent->psNext = psNode;
1646 5113 : sNodeLastChild.psLastChild = psNode;
1647 :
1648 5113 : m_pszGeometry = nullptr;
1649 5113 : m_nGeomAlloc = 0;
1650 5113 : m_nGeomLen = 0;
1651 : }
1652 :
1653 34046 : CPLXMLNode *psThisNode = apsXMLNode.back().psNode;
1654 34046 : CPLXMLNode *psThisNodeChild = psThisNode->psChild;
1655 41009 : if (!m_oMapElementToSubstitute.empty() && psThisNodeChild &&
1656 6963 : psThisNodeChild->eType == CXT_Attribute &&
1657 42125 : strcmp(psThisNodeChild->pszValue, "gml:id") == 0 &&
1658 1116 : psThisNodeChild->psChild->pszValue)
1659 : {
1660 : auto oIter =
1661 1116 : m_oMapElementToSubstitute.find(psThisNodeChild->psChild->pszValue);
1662 1116 : if (oIter != m_oMapElementToSubstitute.end())
1663 : {
1664 165 : auto psLastChild = oIter->second->psChild;
1665 165 : if (psLastChild)
1666 : {
1667 : // CPLDebug("GML", "Substitution of xlink:href=\"#%s\" with actual content", psThisNodeChild->psChild->pszValue);
1668 165 : CPLXMLNode *psAfter = psThisNode->psNext;
1669 165 : psThisNode->psNext = nullptr;
1670 : // We can patch oIter->second as it stored as it in the current
1671 : // GMLFeature.
1672 : // Of course that would no longer be the case in case of
1673 : // cross-references between different GMLFeature, hence we clear
1674 : // m_oMapElementToSubstitute at the end of the current feature.
1675 254 : while (psLastChild->psNext)
1676 89 : psLastChild = psLastChild->psNext;
1677 165 : if (psLastChild == psThisNode)
1678 : {
1679 : /* Can happen in situations like:
1680 : <foo xlink:href="#X">
1681 : <bar gml:id="X"/>
1682 : </foo>
1683 : Do not attempt substitution as that would cause a memory
1684 : leak.
1685 : */
1686 : }
1687 : else
1688 : {
1689 164 : psLastChild->psNext = CPLCloneXMLTree(psThisNode);
1690 : }
1691 165 : psThisNode->psNext = psAfter;
1692 : }
1693 : }
1694 : }
1695 :
1696 34046 : if (m_nDepth == m_nGeometryDepth)
1697 : {
1698 3187 : m_nGeometryDepth = 0;
1699 :
1700 3187 : CPLAssert(apsXMLNode.size() == 2);
1701 3187 : CPLXMLNode *psInterestNode = apsXMLNode.back().psNode;
1702 :
1703 : /*char* pszXML = CPLSerializeXMLTree(psInterestNode);
1704 : CPLDebug("GML", "geometry = %s", pszXML);
1705 : CPLFree(pszXML);*/
1706 :
1707 3187 : apsXMLNode.pop_back();
1708 :
1709 : /* AIXM ElevatedPoint. We want to parse this */
1710 : /* a bit specially because ElevatedPoint is aixm: stuff and */
1711 : /* the srsDimension of the <gml:pos> can be set to true although */
1712 : /* they are only 2 coordinates in practice */
1713 3187 : if (eAppSchemaType == APPSCHEMA_AIXM && psInterestNode != nullptr &&
1714 12 : strcmp(psInterestNode->pszValue, "ElevatedPoint") == 0)
1715 : {
1716 2 : psInterestNode = ParseAIXMElevationPoint(psInterestNode);
1717 : }
1718 3185 : else if (eAppSchemaType == APPSCHEMA_AIXM &&
1719 10 : psInterestNode != nullptr &&
1720 10 : (strcmp(psInterestNode->pszValue, "ElevatedCurve") == 0 ||
1721 2 : strcmp(psInterestNode->pszValue, "ElevateSurface") == 0))
1722 : {
1723 8 : ParseAIXMElevationProperties(psInterestNode);
1724 : }
1725 3177 : else if (eAppSchemaType == APPSCHEMA_MTKGML &&
1726 : psInterestNode != nullptr)
1727 : {
1728 36 : if (strcmp(psInterestNode->pszValue, "Murtoviiva") == 0)
1729 : {
1730 7 : CPLFree(psInterestNode->pszValue);
1731 7 : psInterestNode->pszValue = CPLStrdup("gml:LineString");
1732 : }
1733 29 : else if (strcmp(psInterestNode->pszValue, "Alue") == 0)
1734 : {
1735 7 : CPLFree(psInterestNode->pszValue);
1736 7 : psInterestNode->pszValue = CPLStrdup("gml:Polygon");
1737 : }
1738 22 : else if (strcmp(psInterestNode->pszValue, "Piste") == 0)
1739 : {
1740 21 : CPLFree(psInterestNode->pszValue);
1741 21 : psInterestNode->pszValue = CPLStrdup("gml:Point");
1742 : }
1743 : }
1744 3141 : else if (psInterestNode != nullptr &&
1745 3141 : strcmp(psInterestNode->pszValue, "BoundingBox") == 0)
1746 : {
1747 27 : CPLFree(psInterestNode->pszValue);
1748 27 : psInterestNode->pszValue = CPLStrdup("Envelope");
1749 30 : for (CPLXMLNode *psChild = psInterestNode->psChild; psChild;
1750 3 : psChild = psChild->psNext)
1751 : {
1752 29 : if (psChild->eType == CXT_Attribute &&
1753 27 : strcmp(psChild->pszValue, "crs") == 0)
1754 : {
1755 26 : CPLFree(psChild->pszValue);
1756 26 : psChild->pszValue = CPLStrdup("srsName");
1757 26 : break;
1758 : }
1759 : }
1760 : }
1761 :
1762 3187 : GMLFeature *poGMLFeature = m_poReader->GetState()->m_poFeature;
1763 3187 : if (stateStack[nStackDepth] == STATE_BOUNDED_BY_IN_FEATURE)
1764 : {
1765 78 : if (eAppSchemaType == APPSCHEMA_CITYGML)
1766 58 : CPLDestroyXMLNode(psInterestNode);
1767 : else
1768 20 : poGMLFeature->SetBoundedByGeometry(psInterestNode);
1769 : }
1770 : else
1771 : {
1772 3109 : if (m_poReader->FetchAllGeometries())
1773 0 : poGMLFeature->AddGeometry(psInterestNode);
1774 : else
1775 : {
1776 3109 : GMLFeatureClass *poClass = poGMLFeature->GetClass();
1777 3109 : if (poClass->GetGeometryPropertyCount() > 1)
1778 : {
1779 172 : if (poGMLFeature->GetGeometryRef(m_nGeometryPropertyIndex))
1780 : {
1781 : // If we have already a geometry, setting a new one
1782 : // will invalidate nodes potentially stored in
1783 : // m_oMapElementToSubstitute, so clear it
1784 0 : m_oMapElementToSubstitute.clear();
1785 : }
1786 172 : poGMLFeature->SetGeometryDirectly(m_nGeometryPropertyIndex,
1787 : psInterestNode);
1788 : }
1789 : else
1790 : {
1791 2937 : if (poGMLFeature->GetGeometryRef(0))
1792 : {
1793 : // If we have already a geometry, setting a new one
1794 : // will invalidate nodes potentially stored in
1795 : // m_oMapElementToSubstitute, so clear it
1796 21 : m_oMapElementToSubstitute.clear();
1797 : }
1798 2937 : poGMLFeature->SetGeometryDirectly(psInterestNode);
1799 : }
1800 : }
1801 :
1802 3109 : POP_STATE();
1803 : }
1804 : }
1805 :
1806 34046 : apsXMLNode.pop_back();
1807 :
1808 34046 : return OGRERR_NONE;
1809 : }
1810 :
1811 : /************************************************************************/
1812 : /* endElementCityGMLGenericAttr() */
1813 : /************************************************************************/
1814 84 : OGRErr GMLHandler::endElementCityGMLGenericAttr()
1815 :
1816 : {
1817 84 : if (m_pszCityGMLGenericAttrName != nullptr && m_bInCurField)
1818 : {
1819 42 : if (m_pszCurField != nullptr)
1820 : {
1821 42 : m_poReader->SetFeaturePropertyDirectly(m_pszCityGMLGenericAttrName,
1822 : m_pszCurField, -1);
1823 : }
1824 42 : m_pszCurField = nullptr;
1825 42 : m_nCurFieldLen = 0;
1826 42 : m_nCurFieldAlloc = 0;
1827 42 : m_bInCurField = false;
1828 42 : CPLFree(m_pszCityGMLGenericAttrName);
1829 42 : m_pszCityGMLGenericAttrName = nullptr;
1830 : }
1831 :
1832 84 : if (m_inCityGMLGenericAttrDepth == m_nDepth)
1833 : {
1834 42 : POP_STATE();
1835 : }
1836 :
1837 84 : return OGRERR_NONE;
1838 : }
1839 :
1840 : /************************************************************************/
1841 : /* endElementAttribute() */
1842 : /************************************************************************/
1843 10714 : OGRErr GMLHandler::endElementAttribute()
1844 :
1845 : {
1846 10714 : GMLReadState *poState = m_poReader->GetState();
1847 :
1848 10714 : if (m_bInCurField)
1849 : {
1850 7962 : if (m_pszCurField == nullptr && m_poReader->IsEmptyAsNull())
1851 : {
1852 70 : if (m_pszValue != nullptr)
1853 : {
1854 10 : m_poReader->SetFeaturePropertyDirectly(poState->osPath.c_str(),
1855 : m_pszValue, -1);
1856 10 : m_pszValue = nullptr;
1857 : }
1858 : }
1859 : else
1860 : {
1861 7892 : m_poReader->SetFeaturePropertyDirectly(
1862 : poState->osPath.c_str(),
1863 7892 : m_pszCurField ? m_pszCurField : CPLStrdup(""),
1864 : m_nAttributeIndex);
1865 7892 : m_pszCurField = nullptr;
1866 : }
1867 :
1868 7962 : if (m_pszHref != nullptr)
1869 : {
1870 4 : CPLString osPropNameHref = poState->osPath + "_href";
1871 4 : m_poReader->SetFeaturePropertyDirectly(osPropNameHref, m_pszHref,
1872 : -1);
1873 4 : m_pszHref = nullptr;
1874 : }
1875 :
1876 7962 : if (m_pszUom != nullptr)
1877 : {
1878 20 : CPLString osPropNameUom = poState->osPath + "_uom";
1879 20 : m_poReader->SetFeaturePropertyDirectly(osPropNameUom, m_pszUom, -1);
1880 20 : m_pszUom = nullptr;
1881 : }
1882 :
1883 7962 : if (m_pszKieli != nullptr)
1884 : {
1885 7 : CPLString osPropName = poState->osPath + "_kieli";
1886 7 : m_poReader->SetFeaturePropertyDirectly(osPropName, m_pszKieli, -1);
1887 7 : m_pszKieli = nullptr;
1888 : }
1889 :
1890 7962 : m_nCurFieldLen = 0;
1891 7962 : m_nCurFieldAlloc = 0;
1892 7962 : m_bInCurField = false;
1893 7962 : m_nAttributeIndex = -1;
1894 :
1895 7962 : CPLFree(m_pszValue);
1896 7962 : m_pszValue = nullptr;
1897 : }
1898 :
1899 10714 : poState->PopPath();
1900 :
1901 10714 : if (m_nAttributeDepth == m_nDepth)
1902 : {
1903 8866 : POP_STATE();
1904 : }
1905 :
1906 10714 : return OGRERR_NONE;
1907 : }
1908 :
1909 : /************************************************************************/
1910 : /* startElementFeatureProperty() */
1911 : /************************************************************************/
1912 :
1913 165 : OGRErr GMLHandler::startElementFeatureProperty(const char * /*pszName*/,
1914 : int /*nLenName*/, void *attr)
1915 : {
1916 165 : if (m_nDepth == m_nAttributeDepth + 1)
1917 : {
1918 24 : const char *pszGMLId = GetFID(attr);
1919 24 : if (pszGMLId != nullptr)
1920 : {
1921 24 : m_poReader->SetFeaturePropertyDirectly(
1922 : nullptr, CPLStrdup(CPLSPrintf("#%s", pszGMLId)),
1923 : m_nAttributeIndex);
1924 : }
1925 : }
1926 :
1927 165 : return OGRERR_NONE;
1928 : }
1929 :
1930 : /************************************************************************/
1931 : /* endElementFeatureProperty() */
1932 : /************************************************************************/
1933 :
1934 189 : OGRErr GMLHandler::endElementFeatureProperty()
1935 :
1936 : {
1937 189 : if (m_nDepth == m_nAttributeDepth)
1938 : {
1939 24 : GMLReadState *poState = m_poReader->GetState();
1940 24 : poState->PopPath();
1941 :
1942 24 : POP_STATE();
1943 : }
1944 189 : return OGRERR_NONE;
1945 : }
1946 :
1947 : /************************************************************************/
1948 : /* endElementFeature() */
1949 : /************************************************************************/
1950 5999 : OGRErr GMLHandler::endElementFeature()
1951 :
1952 : {
1953 : /* -------------------------------------------------------------------- */
1954 : /* If we are collecting a feature, and this element tag matches */
1955 : /* element name for the class, then we have finished the */
1956 : /* feature, and we pop the feature read state. */
1957 : /* -------------------------------------------------------------------- */
1958 5999 : if (m_nDepth == m_nDepthFeature)
1959 : {
1960 3197 : m_oMapElementToSubstitute.clear();
1961 3197 : m_poReader->PopState();
1962 :
1963 3197 : POP_STATE();
1964 : }
1965 :
1966 : /* -------------------------------------------------------------------- */
1967 : /* Otherwise, we just pop the element off the local read states */
1968 : /* element stack. */
1969 : /* -------------------------------------------------------------------- */
1970 : else
1971 : {
1972 2802 : m_poReader->GetState()->PopPath();
1973 : }
1974 :
1975 5999 : return OGRERR_NONE;
1976 : }
1977 :
1978 : /************************************************************************/
1979 : /* endElementDefault() */
1980 : /************************************************************************/
1981 3997 : OGRErr GMLHandler::endElementDefault()
1982 :
1983 : {
1984 3997 : if (m_nDepth > 0)
1985 3409 : m_poReader->GetState()->PopPath();
1986 :
1987 3997 : return OGRERR_NONE;
1988 : }
1989 :
1990 : /************************************************************************/
1991 : /* dataHandlerAttribute() */
1992 : /************************************************************************/
1993 :
1994 14502 : OGRErr GMLHandler::dataHandlerAttribute(const char *data, int nLen)
1995 :
1996 : {
1997 14502 : if (!m_bInCurField)
1998 3482 : return OGRERR_NONE;
1999 :
2000 11020 : int nIter = 0;
2001 :
2002 : // Ignore white space.
2003 11020 : if (m_nCurFieldLen == 0)
2004 : {
2005 25700 : while (nIter < nLen)
2006 : {
2007 22616 : const char ch = data[nIter];
2008 22616 : if (!(ch == ' ' || ch == 10 || ch == 13 || ch == '\t'))
2009 7934 : break;
2010 14682 : nIter++;
2011 : }
2012 : }
2013 :
2014 11020 : const int nCharsLen = nLen - nIter;
2015 :
2016 11020 : if (nCharsLen > INT_MAX - static_cast<int>(m_nCurFieldLen) - 1)
2017 : {
2018 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
2019 : "Too much data in a single element");
2020 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2021 : }
2022 11020 : if (m_nCurFieldLen + nCharsLen + 1 > m_nCurFieldAlloc)
2023 : {
2024 9275 : if (m_nCurFieldAlloc < INT_MAX - m_nCurFieldAlloc / 3 - nCharsLen - 1)
2025 9275 : m_nCurFieldAlloc =
2026 9275 : m_nCurFieldAlloc + m_nCurFieldAlloc / 3 + nCharsLen + 1;
2027 : else
2028 0 : m_nCurFieldAlloc = m_nCurFieldLen + nCharsLen + 1;
2029 : char *pszNewCurField = static_cast<char *>(
2030 9275 : VSI_REALLOC_VERBOSE(m_pszCurField, m_nCurFieldAlloc));
2031 9275 : if (pszNewCurField == nullptr)
2032 : {
2033 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2034 : }
2035 9275 : m_pszCurField = pszNewCurField;
2036 : }
2037 11020 : memcpy(m_pszCurField + m_nCurFieldLen, data + nIter, nCharsLen);
2038 11020 : m_nCurFieldLen += nCharsLen;
2039 11020 : m_pszCurField[m_nCurFieldLen] = '\0';
2040 :
2041 11020 : return OGRERR_NONE;
2042 : }
2043 :
2044 : /************************************************************************/
2045 : /* dataHandlerGeometry() */
2046 : /************************************************************************/
2047 :
2048 94697 : OGRErr GMLHandler::dataHandlerGeometry(const char *data, int nLen)
2049 :
2050 : {
2051 94697 : int nIter = 0;
2052 :
2053 : // Ignore white space
2054 94697 : if (m_nGeomLen == 0)
2055 : {
2056 737772 : while (nIter < nLen)
2057 : {
2058 648227 : char ch = data[nIter];
2059 648227 : if (!(ch == ' ' || ch == 10 || ch == 13 || ch == '\t'))
2060 5113 : break;
2061 643114 : nIter++;
2062 : }
2063 : }
2064 :
2065 94697 : const int nCharsLen = nLen - nIter;
2066 94697 : if (nCharsLen)
2067 : {
2068 5152 : if (nCharsLen > INT_MAX - static_cast<int>(m_nGeomLen) - 1)
2069 : {
2070 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
2071 : "Too much data in a single element");
2072 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2073 : }
2074 5152 : if (m_nGeomLen + nCharsLen + 1 > m_nGeomAlloc)
2075 : {
2076 5147 : if (m_nGeomAlloc < INT_MAX - m_nGeomAlloc / 3 - nCharsLen - 1)
2077 5147 : m_nGeomAlloc = m_nGeomAlloc + m_nGeomAlloc / 3 + nCharsLen + 1;
2078 : else
2079 0 : m_nGeomAlloc = m_nGeomAlloc + nCharsLen + 1;
2080 : char *pszNewGeometry = static_cast<char *>(
2081 5147 : VSI_REALLOC_VERBOSE(m_pszGeometry, m_nGeomAlloc));
2082 5147 : if (pszNewGeometry == nullptr)
2083 : {
2084 0 : return OGRERR_NOT_ENOUGH_MEMORY;
2085 : }
2086 5147 : m_pszGeometry = pszNewGeometry;
2087 : }
2088 5152 : memcpy(m_pszGeometry + m_nGeomLen, data + nIter, nCharsLen);
2089 5152 : m_nGeomLen += nCharsLen;
2090 5152 : m_pszGeometry[m_nGeomLen] = '\0';
2091 : }
2092 :
2093 94697 : return OGRERR_NONE;
2094 : }
2095 :
2096 : /************************************************************************/
2097 : /* IsGeometryElement() */
2098 : /************************************************************************/
2099 :
2100 16830 : bool GMLHandler::IsGeometryElement(const char *pszElement)
2101 :
2102 : {
2103 16830 : int nFirst = 0;
2104 16830 : int nLast = GML_GEOMETRY_TYPE_COUNT - 1;
2105 16830 : unsigned long nHash = CPLHashSetHashStr(pszElement);
2106 60463 : do
2107 : {
2108 77293 : const int nMiddle = (nFirst + nLast) / 2;
2109 77293 : if (nHash == pasGeometryNames[nMiddle].nHash)
2110 3071 : return strcmp(pszElement, pasGeometryNames[nMiddle].pszName) == 0;
2111 74222 : if (nHash < pasGeometryNames[nMiddle].nHash)
2112 46928 : nLast = nMiddle - 1;
2113 : else
2114 27294 : nFirst = nMiddle + 1;
2115 74222 : } while (nFirst <= nLast);
2116 :
2117 13759 : if (eAppSchemaType == APPSCHEMA_AIXM &&
2118 265 : (strcmp(pszElement, "ElevatedPoint") == 0 ||
2119 263 : strcmp(pszElement, "ElevatedCurve") == 0 ||
2120 255 : strcmp(pszElement, "ElevatedSurface") == 0))
2121 : {
2122 12 : return true;
2123 : }
2124 :
2125 13747 : if (eAppSchemaType == APPSCHEMA_MTKGML &&
2126 77 : (strcmp(pszElement, "Piste") == 0 || strcmp(pszElement, "Alue") == 0 ||
2127 49 : strcmp(pszElement, "Murtoviiva") == 0))
2128 35 : return true;
2129 :
2130 13712 : return false;
2131 : }
|