Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
olivierdalang committed Jun 1, 2014
1 parent 21991b0 commit 313fe6a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 1 addition & 3 deletions python/core/qgsproject.sip
Expand Up @@ -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
Expand All @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsproject.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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_

Expand Down
4 changes: 1 addition & 3 deletions src/core/qgsproject.h
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 313fe6a

Please sign in to comment.