Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load schema only when validating #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions sld/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,50 +1410,16 @@ 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, 'rb')
except IOError:
logging.debug('Cache miss for backup schema document.')
localschema_backup = open(localschema_backup_path, 'wb')

schema_url = 'http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd'
resp = urlopen(schema_url)
localschema_backup.write(resp.read())
resp.close()
localschema_backup.close()
localschema_backup = open(localschema_backup_path, 'rb')

localschema.write(localschema_backup.read())
localschema.close()
localschema_backup.close()

localschema = open(localschema.name, 'rt')
self._schemadoc = parse(localschema)
localschema.close()

StyledLayerDescriptor._cached_schema = localschema.name
else:
logging.debug('Fetching schema from cache.')

localschema = open(StyledLayerDescriptor._cached_schema, 'rt')
self._schemadoc = parse(localschema)
localschema.close()
self._schema = None
self._schemadoc = None

if not sld_file is None:
self._node = parse(sld_file)
self._schema = XMLSchema(self._schemadoc)
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)
self._schema = None

setattr(self.__class__, 'NamedLayer', SLDNode.makeproperty('sld', cls=NamedLayer,
docstring="The named layer of the SLD."))
Expand Down Expand Up @@ -1485,6 +1451,46 @@ def normalize(self):
if not self.NamedLayer is None:
self.NamedLayer.normalize()

def _load_schema(self):
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.')

localschema = open(StyledLayerDescriptor._cached_schema, 'r')
self._schemadoc = parse(localschema)
localschema.close()

self._schema = XMLSchema(self._schemadoc)

def validate(self):
"""
Validate the current file against the SLD schema. This first normalizes
Expand All @@ -1500,8 +1506,7 @@ def validate(self):
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)

Expand Down