From 84e30aa348e0561c8e8739c2b1c7bd333f0525b8 Mon Sep 17 00:00:00 2001 From: Marco Montanari Date: Thu, 26 Sep 2013 17:37:55 +0200 Subject: [PATCH 1/4] load schema only when validating --- sld/__init__.py | 69 +++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/sld/__init__.py b/sld/__init__.py index a6a02e8..77758b9 100644 --- a/sld/__init__.py +++ b/sld/__init__.py @@ -1395,42 +1395,43 @@ def __init__(self, sld_file=None): """ super(StyledLayerDescriptor, self).__init__(None) - if StyledLayerDescriptor._cached_schema is None: - logging.debug('Storing new schema into cache.') - - localschema = NamedTemporaryFile(delete=False) - - localschema_backup_path = './StyledLayerDescriptor-backup.xsd' - try: - logging.debug('Cache hit for backup schema document.') - localschema_backup = open(localschema_backup_path, 'r') - except IOError: - logging.debug('Cache miss for backup schema document.') - localschema_backup = open(localschema_backup_path, 'w') - - schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd' - resp = urllib2.urlopen(schema_url) - localschema_backup.write(resp.read()) - resp.close() + if sld_file is not None: + + if StyledLayerDescriptor._cached_schema is None: + logging.debug('Storing new schema into cache.') + + localschema = NamedTemporaryFile(delete=False) + + localschema_backup_path = './StyledLayerDescriptor-backup.xsd' + try: + logging.debug('Cache hit for backup schema document.') + localschema_backup = open(localschema_backup_path, 'r') + except IOError: + logging.debug('Cache miss for backup schema document.') + localschema_backup = open(localschema_backup_path, 'w') + + schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd' + resp = urllib2.urlopen(schema_url) + localschema_backup.write(resp.read()) + resp.close() + localschema_backup.close() + localschema_backup = open(localschema_backup_path, 'r') + + localschema.write(localschema_backup.read()) + localschema.seek(0) localschema_backup.close() - localschema_backup = open(localschema_backup_path, 'r') - - localschema.write(localschema_backup.read()) - localschema.seek(0) - localschema_backup.close() - - self._schemadoc = parse(localschema) - localschema.close() - - StyledLayerDescriptor._cached_schema = localschema.name - else: - logging.debug('Fetching schema from cache.') - - localschema = open(StyledLayerDescriptor._cached_schema, 'r') - self._schemadoc = parse(localschema) - localschema.close() + + self._schemadoc = parse(localschema) + localschema.close() + + StyledLayerDescriptor._cached_schema = localschema.name + else: + logging.debug('Fetching schema from cache.') + + localschema = open(StyledLayerDescriptor._cached_schema, 'r') + self._schemadoc = parse(localschema) + localschema.close() - if not sld_file is None: self._node = parse(sld_file) self._schema = XMLSchema(self._schemadoc) if not self._schema.validate(self._node): From 9aa31e4b109c81893cb851be42d8bbeb5b90c463 Mon Sep 17 00:00:00 2001 From: Marco Montanari Date: Wed, 20 Aug 2014 18:07:00 +0200 Subject: [PATCH 2/4] moved xsd loading --- sld/__init__.py | 77 +++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/sld/__init__.py b/sld/__init__.py index 77758b9..81dcf14 100644 --- a/sld/__init__.py +++ b/sld/__init__.py @@ -1396,45 +1396,10 @@ def __init__(self, sld_file=None): super(StyledLayerDescriptor, self).__init__(None) if sld_file is not None: - - if StyledLayerDescriptor._cached_schema is None: - logging.debug('Storing new schema into cache.') - - localschema = NamedTemporaryFile(delete=False) - - localschema_backup_path = './StyledLayerDescriptor-backup.xsd' - try: - logging.debug('Cache hit for backup schema document.') - localschema_backup = open(localschema_backup_path, 'r') - except IOError: - logging.debug('Cache miss for backup schema document.') - localschema_backup = open(localschema_backup_path, 'w') - - schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd' - resp = urllib2.urlopen(schema_url) - localschema_backup.write(resp.read()) - resp.close() - localschema_backup.close() - localschema_backup = open(localschema_backup_path, 'r') - - localschema.write(localschema_backup.read()) - localschema.seek(0) - localschema_backup.close() - - self._schemadoc = parse(localschema) - localschema.close() - - StyledLayerDescriptor._cached_schema = localschema.name - else: - logging.debug('Fetching schema from cache.') - - localschema = open(StyledLayerDescriptor._cached_schema, 'r') - self._schemadoc = parse(localschema) - localschema.close() self._node = parse(sld_file) - self._schema = XMLSchema(self._schemadoc) - if not self._schema.validate(self._node): + #self._schema = XMLSchema(self._schemadoc) + if not self.validate(self._node): logging.warn('SLD File "%s" does not validate against the SLD schema.', sld_file) else: self._node = Element("{%s}StyledLayerDescriptor" % SLDNode._nsmap['sld'], version="1.0.0", nsmap=SLDNode._nsmap) @@ -1487,6 +1452,44 @@ def validate(self): logging.debug('The node is empty, and cannot be validated.') return False + + if StyledLayerDescriptor._cached_schema is None: + logging.debug('Storing new schema into cache.') + + localschema = NamedTemporaryFile(delete=False) + + localschema_backup_path = './StyledLayerDescriptor-backup.xsd' + try: + logging.debug('Cache hit for backup schema document.') + localschema_backup = open(localschema_backup_path, 'r') + except IOError: + logging.debug('Cache miss for backup schema document.') + localschema_backup = open(localschema_backup_path, 'w') + + schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd' + resp = urllib2.urlopen(schema_url) + localschema_backup.write(resp.read()) + resp.close() + localschema_backup.close() + localschema_backup = open(localschema_backup_path, 'r') + + localschema.write(localschema_backup.read()) + localschema.seek(0) + localschema_backup.close() + + self._schemadoc = parse(localschema) + localschema.close() + + StyledLayerDescriptor._cached_schema = localschema.name + else: + logging.debug('Fetching schema from cache.') + + localschema = open(StyledLayerDescriptor._cached_schema, 'r') + self._schemadoc = parse(localschema) + localschema.close() + + + if self._schema is None: self._schema = XMLSchema(self._schemadoc) From 6bad99b688733f783d5d4879d091c53ea28313c3 Mon Sep 17 00:00:00 2001 From: Marco Montanari Date: Wed, 20 Aug 2014 18:47:16 +0200 Subject: [PATCH 3/4] tests ok --- sld/__init__.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/sld/__init__.py b/sld/__init__.py index 845d429..0b70718 100644 --- a/sld/__init__.py +++ b/sld/__init__.py @@ -1412,7 +1412,8 @@ def __init__(self, sld_file=None): if not sld_file is None: self._node = parse(sld_file) - if not self.validate(self._node): + self._load_schema() + if not self._schema.validate(self._node): logging.warn('SLD File "%s" does not validate against the SLD schema.', sld_file) else: self._node = Element("{%s}StyledLayerDescriptor" % SLDNode._nsmap['sld'], version="1.0.0", nsmap=SLDNode._nsmap) @@ -1448,22 +1449,7 @@ def normalize(self): if not self.NamedLayer is None: self.NamedLayer.normalize() - def validate(self): - """ - Validate the current file against the SLD schema. This first normalizes - the SLD document, then validates it. Any schema validation error messages - are logged at the INFO level. - - @rtype: boolean - @return: A flag indicating if the SLD is valid. - """ - self.normalize() - - if self._node is None: - logging.debug('The node is empty, and cannot be validated.') - return False - - + def _load_schema(self): if StyledLayerDescriptor._cached_schema is None: logging.debug('Storing new schema into cache.') @@ -1499,10 +1485,26 @@ def validate(self): self._schemadoc = parse(localschema) localschema.close() + self._schema = XMLSchema(self._schemadoc) + return + + def validate(self): + """ + Validate the current file against the SLD schema. This first normalizes + the SLD document, then validates it. Any schema validation error messages + are logged at the INFO level. + + @rtype: boolean + @return: A flag indicating if the SLD is valid. + """ + self.normalize() + + if self._node is None: + logging.debug('The node is empty, and cannot be validated.') + return False - if self._schema is None: - self._schema = XMLSchema(self._schemadoc) + self._load_schema() is_valid = self._schema.validate(self._node) From d462b6477a0fa1456adc78a9cc7470e9ade11893 Mon Sep 17 00:00:00 2001 From: Marco Montanari Date: Wed, 20 Aug 2014 19:16:28 +0200 Subject: [PATCH 4/4] fixed schema loading --- sld/__init__.py | 76 +++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/sld/__init__.py b/sld/__init__.py index 0b70718..e483db2 100644 --- a/sld/__init__.py +++ b/sld/__init__.py @@ -1410,6 +1410,9 @@ def __init__(self, sld_file=None): """ super(StyledLayerDescriptor, self).__init__(None) + self._schema = None + self._schemadoc = None + if not sld_file is None: self._node = parse(sld_file) self._load_schema() @@ -1417,7 +1420,6 @@ def __init__(self, sld_file=None): logging.warn('SLD File "%s" does not validate against the SLD schema.', sld_file) else: self._node = Element("{%s}StyledLayerDescriptor" % SLDNode._nsmap['sld'], version="1.0.0", nsmap=SLDNode._nsmap) - self._schema = None setattr(self.__class__, 'NamedLayer', SLDNode.makeproperty('sld', cls=NamedLayer, docstring="The named layer of the SLD.")) @@ -1450,44 +1452,44 @@ def normalize(self): self.NamedLayer.normalize() def _load_schema(self): - if StyledLayerDescriptor._cached_schema is None: - logging.debug('Storing new schema into cache.') - - localschema = NamedTemporaryFile(delete=False) - - localschema_backup_path = './StyledLayerDescriptor-backup.xsd' - try: - logging.debug('Cache hit for backup schema document.') - localschema_backup = open(localschema_backup_path, 'r') - except IOError: - logging.debug('Cache miss for backup schema document.') - localschema_backup = open(localschema_backup_path, 'w') - - schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd' - resp = urllib2.urlopen(schema_url) - localschema_backup.write(resp.read()) - resp.close() - localschema_backup.close() - localschema_backup = open(localschema_backup_path, 'r') - - localschema.write(localschema_backup.read()) - localschema.seek(0) - localschema_backup.close() - - self._schemadoc = parse(localschema) - localschema.close() - - StyledLayerDescriptor._cached_schema = localschema.name - else: - logging.debug('Fetching schema from cache.') - - localschema = open(StyledLayerDescriptor._cached_schema, 'r') - self._schemadoc = parse(localschema) - localschema.close() + if self._schema is None: + if self._schemadoc is None: + if StyledLayerDescriptor._cached_schema is None: + logging.debug('Storing new schema into cache.') + + localschema = NamedTemporaryFile(delete=False) + + localschema_backup_path = './StyledLayerDescriptor-backup.xsd' + try: + logging.debug('Cache hit for backup schema document.') + localschema_backup = open(localschema_backup_path, 'r') + except IOError: + logging.debug('Cache miss for backup schema document.') + localschema_backup = open(localschema_backup_path, 'w') + + schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd' + resp = urllib2.urlopen(schema_url) + localschema_backup.write(resp.read()) + resp.close() + localschema_backup.close() + localschema_backup = open(localschema_backup_path, 'r') + + localschema.write(localschema_backup.read()) + localschema.seek(0) + localschema_backup.close() + + self._schemadoc = parse(localschema) + localschema.close() + + StyledLayerDescriptor._cached_schema = localschema.name + else: + logging.debug('Fetching schema from cache.') - self._schema = XMLSchema(self._schemadoc) + localschema = open(StyledLayerDescriptor._cached_schema, 'r') + self._schemadoc = parse(localschema) + localschema.close() - return + self._schema = XMLSchema(self._schemadoc) def validate(self): """