check validity of keys used by writeEntry and readEntry

and print a message in the console in case of invalid key

update of docstrings (obsolete note removed, note added)
This commit is contained in:
olivierdalang 2014-06-01 14:52:15 +02:00
parent 21991b0a22
commit 313fe6a98f
3 changed files with 21 additions and 6 deletions

View File

@ -175,7 +175,7 @@ class QgsProject : QObject
keys would be the familiar QSettings-like '/' delimited entries, implying
a hierarchy of keys and corresponding values
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
@note The key string must be valid xml tag names in order to be saved to the file.
*/
//@{
//! @note available in python bindings as writeEntryBool
@ -192,8 +192,6 @@ class QgsProject : QObject
keys would be the familiar QSettings-like '/' delimited entries,
implying a hierarchy of keys and corresponding values
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
*/
//@{
QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;

View File

@ -27,6 +27,7 @@
#include "qgslayertreeregistrybridge.h"
#include "qgslogger.h"
#include "qgsmaplayerregistry.h"
#include "qgsmessagelog.h"
#include "qgspluginlayer.h"
#include "qgspluginlayerregistry.h"
#include "qgsprojectfiletransform.h"
@ -67,6 +68,24 @@ QStringList makeKeyTokens_( QString const &scope, QString const &key )
// be sure to include the canonical root node
keyTokens.push_front( "properties" );
//check validy of keys since an unvalid xml name will will be dropped upon saving the xml file. If not valid, we print a message to the console.
for (int i = 0; i < keyTokens.size(); ++i){
QString keyToken = keyTokens.at(i);
//invalid chars in XML are found at http://www.w3.org/TR/REC-xml/#NT-NameChar
//note : it seems \x10000-\xEFFFF is valid, but it when added to the regexp, a lot of unwanted characters remain
QString nameCharRegexp = QString( "[^:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x2FF\\x370-\\x37D\\x37F-\\x1FFF\\x200C-\\x200D\\x2070-\\x218F\\x2C00-\\x2FEF\\x3001-\\xD7FF\\xF900-\\xFDCF\\xFDF0-\\xFFFD\\-\\.0-9\\xB7\\x0300-\\x036F\\x203F-\\x2040]" );
QString nameStartCharRegexp = QString( "^[^:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x2FF\\x370-\\x37D\\x37F-\\x1FFF\\x200C-\\x200D\\x2070-\\x218F\\x2C00-\\x2FEF\\x3001-\\xD7FF\\xF900-\\xFDCF\\xFDF0-\\xFFFD]" );
if( keyToken.contains( QRegExp(nameCharRegexp) ) || keyToken.contains( QRegExp(nameStartCharRegexp) ) ){
QString errorString = QObject::tr("Entry token invalid : '%1'. The token will not be saved to file.").arg(keyToken);
QgsMessageLog::logMessage( errorString, QString::null, QgsMessageLog::CRITICAL );
}
}
return keyTokens;
} // makeKeyTokens_

View File

@ -220,7 +220,7 @@ class CORE_EXPORT QgsProject : public QObject
keys would be the familiar QSettings-like '/' delimited entries, implying
a hierarchy of keys and corresponding values
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
@note The key string must be valid xml tag names in order to be saved to the file.
*/
//@{
//! @note available in python bindings as writeEntryBool
@ -237,8 +237,6 @@ class CORE_EXPORT QgsProject : public QObject
keys would be the familiar QSettings-like '/' delimited entries,
implying a hierarchy of keys and corresponding values
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
*/
//@{
QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;