Mercurial > repos > snowblizz-super-API-ideas
changeset 41:422ddd5fedd1
Re #48 - Require schemas to validate
* Restructure schema caching so that it warns instead of exceptioning - allows a missing schema to not kill an unrelated file load
* Fix exception handling for GameSystems while working out where exceptions were being caught
Current situation: Missing Race/System schema stops file loading but missing cat/core schema doesn't
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 21 Mar 2009 20:52:26 +0000 |
parents | 3664eee50390 |
children | d0d44434b557 |
files | api/Factories/Xml/WarFoundryXmlFactory.cs api/WarFoundryLoader.cs |
diffstat | 2 files changed, 35 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs Sat Mar 21 17:04:33 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Sat Mar 21 20:52:26 2009 +0000 @@ -308,36 +308,44 @@ { if (settings == null) { - try - { - settings = new XmlReaderSettings(); - settings.ValidationType = ValidationType.Schema; - settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; - settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); - XmlSchemaSet cache = new XmlSchemaSet(); - cache.Add("http://ibboard.co.uk/warfoundry/core", IBBoard.Constants.ExecutablePath + "/dtds/warfoundry-core.xsd"); - cache.Add("http://ibboard.co.uk/warfoundry/cats", IBBoard.Constants.ExecutablePath + "/dtds/warfoundry-cats.xsd"); - cache.Add("http://ibboard.co.uk/warfoundry/race", IBBoard.Constants.ExecutablePath + "/dtds/race.xsd"); - cache.Add("http://ibboard.co.uk/warfoundry/system", IBBoard.Constants.ExecutablePath + "/dtds/system.xsd"); - settings.Schemas.Add(cache); - } - catch (DirectoryNotFoundException ex) - { - throw new InvalidDataException("Problem validating schema for WarFoundry data: " + ex.Message, ex); - } - catch (XmlSchemaException ex) - { - throw new InvalidDataException("Problem validating schema for WarFoundry data: " + ex.Message, ex); - } - catch (XmlException ex) - { - throw new InvalidDataException("Problem reading data for schema: " + ex.Message, ex); - } + settings = new XmlReaderSettings(); + settings.ValidationType = ValidationType.Schema; + settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; + settings.ProhibitDtd = true; + settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); + XmlSchemaSet cache = new XmlSchemaSet(); + string path = IBBoard.Constants.ExecutablePath + "/dtds/"; + string nsBase = "http://ibboard.co.uk/warfoundry/"; + AddSchemaToCache(cache, nsBase + "core", path + "warfoundry-core.xsd"); + AddSchemaToCache(cache, nsBase + "cats", path + "warfoundry-cats.xsd"); + AddSchemaToCache(cache, nsBase + "race", path + "race.xsd"); + AddSchemaToCache(cache, nsBase + "system", path + "system.xsd"); + settings.Schemas.Add(cache); } return settings; } + private void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation) + { + try + { + cache.Add(xmlNamespace, schemaLocation); + } + catch (IOException ex) + { + LogNotifier.Warn(GetType(), "Problem reading schema: " + ex.Message, ex); + } + catch (XmlSchemaException ex) + { + LogNotifier.Warn(GetType(), "Problem validating schema for WarFoundry data: " + ex.Message, ex); + } + catch (XmlException ex) + { + LogNotifier.Warn(GetType(), "Problem reading data for schema: " + ex.Message, ex); + } + } + private void ValidationEventMethod(object sender, ValidationEventArgs e) { throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Exception.Message, e.Exception);
--- a/api/WarFoundryLoader.cs Sat Mar 21 17:04:33 2009 +0000 +++ b/api/WarFoundryLoader.cs Sat Mar 21 20:52:26 2009 +0000 @@ -285,7 +285,7 @@ if (!loaded) { - failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}"); + failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as GameSystem using {1}"); } } catch (Exception ex) @@ -296,7 +296,7 @@ if (failure!=null) { fails.Add(failure); - LogNotifier.Warn(GetType(), failure.Message); + LogNotifier.Warn(GetType(), failure.Message, failure.Exception); } }