Skip to content

Commit 313fe6a

Browse files
committed
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)
1 parent 21991b0 commit 313fe6a

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

python/core/qgsproject.sip

+1-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class QgsProject : QObject
175175
keys would be the familiar QSettings-like '/' delimited entries, implying
176176
a hierarchy of keys and corresponding values
177177

178-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
178+
@note The key string must be valid xml tag names in order to be saved to the file.
179179
*/
180180
//@{
181181
//! @note available in python bindings as writeEntryBool
@@ -192,8 +192,6 @@ class QgsProject : QObject
192192
keys would be the familiar QSettings-like '/' delimited entries,
193193
implying a hierarchy of keys and corresponding values
194194

195-
196-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
197195
*/
198196
//@{
199197
QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;

src/core/qgsproject.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgslayertreeregistrybridge.h"
2828
#include "qgslogger.h"
2929
#include "qgsmaplayerregistry.h"
30+
#include "qgsmessagelog.h"
3031
#include "qgspluginlayer.h"
3132
#include "qgspluginlayerregistry.h"
3233
#include "qgsprojectfiletransform.h"
@@ -67,6 +68,24 @@ QStringList makeKeyTokens_( QString const &scope, QString const &key )
6768
// be sure to include the canonical root node
6869
keyTokens.push_front( "properties" );
6970

71+
//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.
72+
for (int i = 0; i < keyTokens.size(); ++i){
73+
QString keyToken = keyTokens.at(i);
74+
75+
//invalid chars in XML are found at http://www.w3.org/TR/REC-xml/#NT-NameChar
76+
//note : it seems \x10000-\xEFFFF is valid, but it when added to the regexp, a lot of unwanted characters remain
77+
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]" );
78+
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]" );
79+
80+
if( keyToken.contains( QRegExp(nameCharRegexp) ) || keyToken.contains( QRegExp(nameStartCharRegexp) ) ){
81+
82+
QString errorString = QObject::tr("Entry token invalid : '%1'. The token will not be saved to file.").arg(keyToken);
83+
QgsMessageLog::logMessage( errorString, QString::null, QgsMessageLog::CRITICAL );
84+
85+
}
86+
87+
}
88+
7089
return keyTokens;
7190
} // makeKeyTokens_
7291

src/core/qgsproject.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class CORE_EXPORT QgsProject : public QObject
220220
keys would be the familiar QSettings-like '/' delimited entries, implying
221221
a hierarchy of keys and corresponding values
222222
223-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
223+
@note The key string must be valid xml tag names in order to be saved to the file.
224224
*/
225225
//@{
226226
//! @note available in python bindings as writeEntryBool
@@ -237,8 +237,6 @@ class CORE_EXPORT QgsProject : public QObject
237237
keys would be the familiar QSettings-like '/' delimited entries,
238238
implying a hierarchy of keys and corresponding values
239239
240-
241-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
242240
*/
243241
//@{
244242
QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;

0 commit comments

Comments
 (0)