Skip to content
Permalink
Browse files

GML parser: fix parsing of typenames and geometry names with non-ASCI…

…I character (#16009)
  • Loading branch information
rouault committed Jan 14, 2017
1 parent b412ceb commit 6de4fceec285ab3f271b2ff6d6bf209dd6fface0
Showing with 67 additions and 5 deletions.
  1. +16 −5 src/core/qgsgml.cpp
  2. +2 −0 src/core/qgsgml.h
  3. +49 −0 tests/src/core/testqgsgml.cpp
@@ -277,10 +277,12 @@ QgsGmlStreamingParser::QgsGmlStreamingParser( const QString& typeName,
: mTypeName( typeName )
, mTypeNameBA( mTypeName.toUtf8() )
, mTypeNamePtr( mTypeNameBA.constData() )
, mTypeNameUTF8Len( strlen( mTypeNamePtr ) )
, mWkbType( QgsWkbTypes::Unknown )
, mGeometryAttribute( geometryAttribute )
, mGeometryAttributeBA( geometryAttribute.toUtf8() )
, mGeometryAttributePtr( mGeometryAttributeBA.constData() )
, mGeometryAttributeUTF8Len( strlen( mGeometryAttributePtr ) )
, mFields( fields )
, mIsException( false )
, mTruncatedResponse( false )
@@ -315,6 +317,7 @@ QgsGmlStreamingParser::QgsGmlStreamingParser( const QString& typeName,
mTypeName = mTypeName.mid( index + 1 );
mTypeNameBA = mTypeName.toUtf8();
mTypeNamePtr = mTypeNameBA.constData();
mTypeNameUTF8Len = strlen( mTypeNamePtr );
}

mParser = XML_ParserCreateNS( nullptr, NS_SEPARATOR );
@@ -340,8 +343,10 @@ QgsGmlStreamingParser::QgsGmlStreamingParser( const QList<LayerProperties>& laye
bool invertAxisOrientation )
: mLayerProperties( layerProperties )
, mTypeNamePtr( nullptr )
, mTypeNameUTF8Len( 0 )
, mWkbType( QgsWkbTypes::Unknown )
, mGeometryAttributePtr( nullptr )
, mGeometryAttributeUTF8Len( 0 )
, mFields( fields )
, mIsException( false )
, mTruncatedResponse( false )
@@ -397,13 +402,15 @@ QgsGmlStreamingParser::QgsGmlStreamingParser( const QList<LayerProperties>& laye
mGeometryAttribute = mLayerProperties[0].mGeometryAttribute;
mGeometryAttributeBA = mGeometryAttribute.toUtf8();
mGeometryAttributePtr = mGeometryAttributeBA.constData();
mGeometryAttributeUTF8Len = strlen( mGeometryAttributePtr );
int index = mTypeName.indexOf( ':' );
if ( index != -1 && index < mTypeName.length() )
{
mTypeName = mTypeName.mid( index + 1 );
}
mTypeNameBA = mTypeName.toUtf8();
mTypeNamePtr = mTypeNameBA.constData();
mTypeNameUTF8Len = strlen( mTypeNamePtr );
}

mEndian = QgsApplication::endian();
@@ -542,7 +549,7 @@ void QgsGmlStreamingParser::startElement( const XML_Char* el, const XML_Char** a
}
}
}
else if ( localNameLen == mGeometryAttribute.size() &&
else if ( localNameLen == static_cast<int>( mGeometryAttributeUTF8Len ) &&
memcmp( pszLocalName, mGeometryAttributePtr, localNameLen ) == 0 )
{
mParseModeStack.push( QgsGmlStreamingParser::Geometry );
@@ -606,6 +613,7 @@ void QgsGmlStreamingParser::startElement( const XML_Char* el, const XML_Char** a
}
mGeometryAttributeBA = mGeometryAttribute.toUtf8();
mGeometryAttributePtr = mGeometryAttributeBA.constData();
mGeometryAttributeUTF8Len = strlen( mGeometryAttributePtr );
mParseModeStack.push( QgsGmlStreamingParser::FeatureTuple );
QString id;
if ( mGMLNameSpaceURI.isEmpty() )
@@ -634,7 +642,8 @@ void QgsGmlStreamingParser::startElement( const XML_Char* el, const XML_Char** a
}
}
else if ( theParseMode == None &&
localNameLen == mTypeName.size() && memcmp( pszLocalName, mTypeNamePtr, mTypeName.size() ) == 0 )
localNameLen == static_cast<int>( mTypeNameUTF8Len ) &&
memcmp( pszLocalName, mTypeNamePtr, mTypeNameUTF8Len ) == 0 )
{
Q_ASSERT( !mCurrentFeature );
mCurrentFeature = new QgsFeature( mFeatureCount );
@@ -857,7 +866,8 @@ void QgsGmlStreamingParser::endElement( const XML_Char* el )

setAttribute( mAttributeName, mStringCash );
}
else if ( theParseMode == Geometry && localNameLen == mGeometryAttribute.size() &&
else if ( theParseMode == Geometry &&
localNameLen == static_cast<int>( mGeometryAttributeUTF8Len ) &&
memcmp( pszLocalName, mGeometryAttributePtr, localNameLen ) == 0 )
{
mParseModeStack.pop();
@@ -935,8 +945,9 @@ void QgsGmlStreamingParser::endElement( const XML_Char* el )
}
else if (( theParseMode == Tuple && !mTypeNamePtr &&
LOCALNAME_EQUALS( "Tuple" ) ) ||
( theParseMode == Feature && localNameLen == mTypeName.size() &&
memcmp( pszLocalName, mTypeNamePtr, mTypeName.size() ) == 0 ) )
( theParseMode == Feature &&
localNameLen == static_cast<int>( mTypeNameUTF8Len ) &&
memcmp( pszLocalName, mTypeNamePtr, mTypeNameUTF8Len ) == 0 ) )
{
Q_ASSERT( mCurrentFeature );
if ( !mCurrentFeature->hasGeometry() )
@@ -250,6 +250,7 @@ class CORE_EXPORT QgsGmlStreamingParser
QString mTypeName;
QByteArray mTypeNameBA;
const char* mTypeNamePtr;
size_t mTypeNameUTF8Len;

QgsWkbTypes::Type mWkbType;

@@ -259,6 +260,7 @@ class CORE_EXPORT QgsGmlStreamingParser
QString mGeometryAttribute;
QByteArray mGeometryAttributeBA;
const char* mGeometryAttributePtr;
size_t mGeometryAttributeUTF8Len;

QgsFields mFields;
QMap<QString, QPair<int, QgsField> > mThematicAttributes;
@@ -76,6 +76,7 @@ class TestQgsGML : public QObject
void testPartialFeature();
void testThroughOGRGeometry();
void testThroughOGRGeometry_urn_EPSG_4326();
void testAccents();
};

const QString data1( "<myns:FeatureCollection "
@@ -1103,5 +1104,53 @@ void TestQgsGML::testThroughOGRGeometry_urn_EPSG_4326()
delete features[0].first;
}

void TestQgsGML::testAccents()
{
QgsFields fields;
QgsGmlStreamingParser gmlParser( QString::fromUtf8( QByteArray( "my\xc3\xa9typename" ) ),
QString::fromUtf8( QByteArray( "my\xc3\xa9geom" ) ),
fields );
QCOMPARE( gmlParser.processData( QByteArray( "<myns:FeatureCollection "
"xmlns:myns='http://myns' "
"xmlns:gml='http://www.opengis.net/gml'>"
"<gml:featureMember>"
"<myns:my\xc3\xa9typename fid='mytypename.1'>"
"<myns:my\xc3\xa9geom>"
"<gml:MultiSurface srsName='EPSG:27700'>"
"<gml:surfaceMember>"
"<gml:Polygon srsName='EPSG:27700'>"
"<gml:exterior>"
"<gml:LinearRing>"
"<gml:posList>0 0 0 10 10 10 10 0 0 0</gml:posList>"
"</gml:LinearRing>"
"</gml:exterior>"
"</gml:Polygon>"
"</gml:surfaceMember>"
"<gml:surfaceMember>"
"<gml:Polygon srsName='EPSG:27700'>"
"<gml:exterior>"
"<gml:LinearRing>"
"<gml:posList>0 0 0 10 10 10 10 0 0 0</gml:posList>"
"</gml:LinearRing>"
"</gml:exterior>"
"</gml:Polygon>"
"</gml:surfaceMember>"
"</gml:MultiSurface>"
"</myns:my\xc3\xa9geom>"
"</myns:my\xc3\xa9typename>"
"</gml:featureMember>"
"</myns:FeatureCollection>" ), true ), true );
QCOMPARE( gmlParser.wkbType(), QgsWkbTypes::MultiPolygon );
QVector<QgsGmlStreamingParser::QgsGmlFeaturePtrGmlIdPair> features = gmlParser.getAndStealReadyFeatures();
QCOMPARE( features.size(), 1 );
QVERIFY( features[0].first->hasGeometry() );
QCOMPARE( features[0].first->geometry().wkbType(), QgsWkbTypes::MultiPolygon );
QgsMultiPolygon multi = features[0].first->geometry().asMultiPolygon();
QCOMPARE( multi.size(), 2 );
QCOMPARE( multi[0].size(), 1 );
QCOMPARE( multi[0][0].size(), 5 );
delete features[0].first;
}

QGSTEST_MAIN( TestQgsGML )
#include "testqgsgml.moc"

0 comments on commit 6de4fce

Please sign in to comment.
You can’t perform that action at this time.