Skip to content
Permalink
Browse files

[Server][WFS] Define encodeValueToText to correctly format field values

  • Loading branch information
rldhont committed Sep 26, 2018
1 parent 6de6c08 commit cfddac586dea896a0fa1ffbed0bb337181040849
Showing with 46 additions and 2 deletions.
  1. +46 −2 src/server/services/wfs/qgswfsgetfeature.cpp
@@ -62,6 +62,8 @@ namespace QgsWfs

QString createFeatureGeoJSON( QgsFeature *feat, const createFeatureParams &params );

QString encodeValueToText( const QVariant &value );

QDomElement createFeatureGML2( QgsFeature *feat, QDomDocument &doc, const createFeatureParams &params, const QgsProject *project );

QDomElement createFeatureGML3( QgsFeature *feat, QDomDocument &doc, const createFeatureParams &params, const QgsProject *project );
@@ -1338,7 +1340,7 @@ namespace QgsWfs
QString attributeName = fields.at( idx ).name();

QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
QDomText fieldText = doc.createTextNode( featureAttributes[idx].toString() );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
fieldElem.appendChild( fieldText );
typeNameElement.appendChild( fieldElem );
}
@@ -1433,15 +1435,57 @@ namespace QgsWfs
QString attributeName = fields.at( idx ).name();

QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
QDomText fieldText = doc.createTextNode( featureAttributes[idx].toString() );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
fieldElem.appendChild( fieldText );
typeNameElement.appendChild( fieldElem );
}

return featureElement;
}

QString encodeValueToText( const QVariant &value )
{
if ( value.isNull() )
return QStringLiteral( "null" );

switch ( value.type() )
{
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::Double:
return value.toString();

case QVariant::Bool:
return value.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );

case QVariant::StringList:
case QVariant::List:
case QVariant::Map:
{
QString v = QgsJsonUtils::encodeValue( value );

//do we need CDATA
if ( v.indexOf( '<' ) != -1 || v.indexOf( '&' ) != -1 )
v.prepend( QStringLiteral( "<![CDATA[" ) ).append( QStringLiteral( "]]>" ) );

return v;
}

default:
case QVariant::String:
{
QString v = value.toString();

//do we need CDATA
if ( v.indexOf( '<' ) != -1 || v.indexOf( '&' ) != -1 )
v.prepend( QStringLiteral( "<![CDATA[" ) ).append( QStringLiteral( "]]>" ) );

return v;
}
}
}


} // namespace

0 comments on commit cfddac5

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