From 313fe6a98f06cc4094ace211fc3a191aa0497cd6 Mon Sep 17 00:00:00 2001 From: olivierdalang Date: Sun, 1 Jun 2014 14:52:15 +0200 Subject: [PATCH] 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) --- python/core/qgsproject.sip | 4 +--- src/core/qgsproject.cpp | 19 +++++++++++++++++++ src/core/qgsproject.h | 4 +--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/python/core/qgsproject.sip b/python/core/qgsproject.sip index 8c817e14545..3e67d2c6bcc 100644 --- a/python/core/qgsproject.sip +++ b/python/core/qgsproject.sip @@ -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 must 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 must include '/'s. E.g., "/foo" not "foo". */ //@{ QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const; diff --git a/src/core/qgsproject.cpp b/src/core/qgsproject.cpp index abecfba53c2..6aa77fa57f1 100644 --- a/src/core/qgsproject.cpp +++ b/src/core/qgsproject.cpp @@ -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_ diff --git a/src/core/qgsproject.h b/src/core/qgsproject.h index 4769629aeb2..624ff68e457 100644 --- a/src/core/qgsproject.h +++ b/src/core/qgsproject.h @@ -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 must 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 must include '/'s. E.g., "/foo" not "foo". */ //@{ QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;