Skip to content

Commit d795e50

Browse files
committed
Merge pull request #469 from olivierdalang/SanitizeEntryNames
Warning about invalid entry names
2 parents c6460a3 + 0132a3e commit d795e50

File tree

5 files changed

+135
-6
lines changed

5 files changed

+135
-6
lines changed

python/core/qgsproject.sip

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class QgsProject : QObject
150150
keys would be the familiar QSettings-like '/' delimited entries, implying
151151
a hierarchy of keys and corresponding values
152152

153-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
153+
@note The key string must be valid xml tag names in order to be saved to the file.
154154
*/
155155
//@{
156156
//! @note available in python bindings as writeEntryBool
@@ -167,8 +167,6 @@ class QgsProject : QObject
167167
keys would be the familiar QSettings-like '/' delimited entries,
168168
implying a hierarchy of keys and corresponding values
169169

170-
171-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
172170
*/
173171
//@{
174172
QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = 0 ) const;

src/core/qgsproject.cpp

Lines changed: 19 additions & 0 deletions
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"
@@ -66,6 +67,24 @@ QStringList makeKeyTokens_( QString const &scope, QString const &key )
6667
// be sure to include the canonical root node
6768
keyTokens.push_front( "properties" );
6869

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

src/core/qgsproject.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class CORE_EXPORT QgsProject : public QObject
197197
keys would be the familiar QSettings-like '/' delimited entries, implying
198198
a hierarchy of keys and corresponding values
199199
200-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
200+
@note The key string must be valid xml tag names in order to be saved to the file.
201201
*/
202202
//@{
203203
//! @note available in python bindings as writeEntryBool
@@ -214,8 +214,6 @@ class CORE_EXPORT QgsProject : public QObject
214214
keys would be the familiar QSettings-like '/' delimited entries,
215215
implying a hierarchy of keys and corresponding values
216216
217-
218-
@note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
219217
*/
220218
//@{
221219
QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = 0 ) const;

tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
3030
ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
3131
ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
3232
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
33+
ADD_PYTHON_TEST(PyQgsProject test_qgsproject.py)
3334
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
3435
ADD_PYTHON_TEST(PyQgsField test_qgsfield.py)
3536
ADD_PYTHON_TEST(PyQgsFontUtils test_qgsfontutils.py)

tests/src/python/test_qgsproject.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsProject.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Sebastian Dietrich'
10+
__date__ = '19/11/2015'
11+
__copyright__ = 'Copyright 2015, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis
16+
17+
from qgis.core import QgsProject, QgsMessageLog
18+
19+
from utilities import getQgisTestApp, TestCase, unittest
20+
21+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
22+
23+
24+
class TestQgsProject(TestCase):
25+
26+
def __init__(self, methodName):
27+
"""Run once on class initialisation."""
28+
unittest.TestCase.__init__(self, methodName)
29+
self.messageCaught = False
30+
31+
def test_makeKeyTokens_(self):
32+
# see http://www.w3.org/TR/REC-xml/#d0e804 for a list of valid characters
33+
34+
invalidTokens = []
35+
validTokens = []
36+
37+
# all test tokens will be generated by prepending or inserting characters to this token
38+
validBase = u"valid";
39+
40+
# some invalid characters, not allowed anywhere in a token
41+
# note that '/' must not be added here because it is taken as a separator by makeKeyTokens_()
42+
invalidChars = u"+*,;<>|!$%()=?#\x01";
43+
44+
# generate the characters that are allowed at the start of a token (and at every other position)
45+
validStartChars = u":_";
46+
charRanges = [
47+
(ord(u'a'), ord(u'z')),
48+
(ord(u'A'), ord(u'Z')),
49+
(0x00F8, 0x02FF),
50+
(0x0370, 0x037D),
51+
(0x037F, 0x1FFF),
52+
(0x200C, 0x200D),
53+
(0x2070, 0x218F),
54+
(0x2C00, 0x2FEF),
55+
(0x3001, 0xD7FF),
56+
(0xF900, 0xFDCF),
57+
(0xFDF0, 0xFFFD),
58+
#(0x10000, 0xEFFFF), while actually valid, these are not yet accepted by makeKeyTokens_()
59+
]
60+
for r in charRanges:
61+
for c in range(r[0], r[1]):
62+
validStartChars += unichr(c)
63+
64+
# generate the characters that are only allowed inside a token, not at the start
65+
validInlineChars = u"-.\xB7";
66+
charRanges = [
67+
(ord(u'0'), ord(u'9')),
68+
(0x0300, 0x036F),
69+
(0x203F, 0x2040),
70+
]
71+
for r in charRanges:
72+
for c in range(r[0], r[1]):
73+
validInlineChars += unichr(c)
74+
75+
# test forbidden start characters
76+
for c in invalidChars + validInlineChars:
77+
invalidTokens.append(c + validBase)
78+
79+
# test forbidden inline characters
80+
for c in invalidChars:
81+
invalidTokens.append(validBase[:4] + c + validBase[4:])
82+
83+
# test each allowed start character
84+
for c in validStartChars:
85+
validTokens.append(c + validBase)
86+
87+
# test each allowed inline character
88+
for c in validInlineChars:
89+
validTokens.append(validBase[:4] + c + validBase[4:]);
90+
91+
logger = QgsMessageLog.instance()
92+
logger.messageReceived.connect(self.catchMessage)
93+
prj = QgsProject.instance()
94+
95+
for token in validTokens:
96+
self.messageCaught = False
97+
prj.readEntry("test", token)
98+
myMessage = "valid token '%s' not accepted" % (token)
99+
assert not self.messageCaught, myMessage
100+
101+
for token in invalidTokens:
102+
self.messageCaught = False
103+
prj.readEntry("test", token)
104+
myMessage = "invalid token '%s' accepted" % (token)
105+
assert self.messageCaught, myMessage
106+
107+
logger.messageReceived.disconnect(self.catchMessage)
108+
109+
def catchMessage(self):
110+
self.messageCaught = True
111+
112+
if __name__ == '__main__':
113+
unittest.main()

0 commit comments

Comments
 (0)