From 9d17f25521d675d356ecde4a383d82ae28070df9 Mon Sep 17 00:00:00 2001 From: Radim Blazek Date: Tue, 3 Dec 2013 22:26:30 +0100 Subject: [PATCH] Convert WMS GetFeatureInfo GML to UTF-8 supported by Expat, fixes #9082 --- src/core/qgsgml.h | 5 ++++- src/core/qgsgmlschema.h | 1 + src/providers/wms/qgswmsprovider.cpp | 18 +++++++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/core/qgsgml.h b/src/core/qgsgml.h index 2f82ea50796a..496d7abb9cbc 100644 --- a/src/core/qgsgml.h +++ b/src/core/qgsgml.h @@ -50,6 +50,7 @@ class CORE_EXPORT QgsGml : public QObject ~QgsGml(); /** Does the Http GET request to the wfs server + * Supports only UTF-8, UTF-16, ISO-8859-1, ISO-8859-1 XML encodings. * @param uri GML URL * @param wkbType wkbType to retrieve * @param extent retrieved extents @@ -57,7 +58,9 @@ class CORE_EXPORT QgsGml : public QObject */ int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0 ); - /** Read from GML data. Constructor uri param is ignored */ + /** Read from GML data. Constructor uri param is ignored + * Supports only UTF-8, UTF-16, ISO-8859-1, ISO-8859-1 XML encodings. + */ int getFeatures( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent = 0 ); /** Get parsed features for given type name */ diff --git a/src/core/qgsgmlschema.h b/src/core/qgsgmlschema.h index cefe71107471..4fa35d0ad21b 100644 --- a/src/core/qgsgmlschema.h +++ b/src/core/qgsgmlschema.h @@ -85,6 +85,7 @@ class CORE_EXPORT QgsGmlSchema: public QObject /** Guess GML schema from data if XSD does not exist. * Currently only recognizes UMN Mapserver GetFeatureInfo GML response. + * Supports only UTF-8, UTF-16, ISO-8859-1, US-ASCII XML encodings. * @param data GML data * @return true in case of success */ bool guessSchema( const QByteArray &data ); diff --git a/src/providers/wms/qgswmsprovider.cpp b/src/providers/wms/qgswmsprovider.cpp index 111bed0e9c01..33667526a772 100644 --- a/src/providers/wms/qgswmsprovider.cpp +++ b/src/providers/wms/qgswmsprovider.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #include #ifdef QGISDEBUG @@ -4376,7 +4377,18 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPoint & thePoint, Qgs } } - QgsDebugMsg( "GML (first 2000 bytes):\n" + QString::fromUtf8( mIdentifyResultBodies.value( gmlPart ).left( 2000 ) ) ); + QByteArray gmlByteArray = mIdentifyResultBodies.value( gmlPart ); + QgsDebugMsg( "GML (first 2000 bytes):\n" + gmlByteArray.left( 2000 ) ); + + // QgsGmlSchema.guessSchema() and QgsGml::getFeatures() are using Expat + // which only accepts UTF-8, UTF-16, ISO-8859-1 + // http://sourceforge.net/p/expat/bugs/498/ + QDomDocument dom; + dom.setContent( gmlByteArray ); // gets XML encoding + QTextStream stream( &gmlByteArray ); + stream.setCodec( QTextCodec::codecForName( "UTF-8" ) ); + dom.save( stream, 4, QDomNode::EncodingFromTextStream ); + QGis::WkbType wkbType; QgsGmlSchema gmlSchema; @@ -4418,7 +4430,7 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPoint & thePoint, Qgs else { // guess from GML - bool ok = gmlSchema.guessSchema( mIdentifyResultBodies.value( gmlPart ) ); + bool ok = gmlSchema.guessSchema( gmlByteArray ); if ( ! ok ) { QgsError err = gmlSchema.error(); @@ -4454,7 +4466,7 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPoint & thePoint, Qgs } QgsGml gml( featureTypeName, geometryAttribute, fields ); // TODO: avoid converting to string and back - int ret = gml.getFeatures( mIdentifyResultBodies.value( gmlPart ), &wkbType ); + int ret = gml.getFeatures( gmlByteArray, &wkbType ); #ifdef QGISDEBUG QgsDebugMsg( QString( "parsing result = %1" ).arg( ret ) ); #else