# HG changeset patch # User IBBoard # Date 1236893717 0 # Node ID f9846f896df381056d708805172ca7f62e9dbc99 # Parent 28e99aa0053fd06fd860e8f878b1978b92df9454 Re #32 - Migrate WarFoundry files to using Schemas * Add missing spaces to Cats and Core XSD * Fix some incorrect namespaces in Race XSD * Copy schemas to output dir on build * Make WarFoundryXmlFactory validate against Schemas * Make WarFoundryLoader handle failed file loads slightly differently so that we can log out as a warning * Correctly structure "simpleContent" sections of Race XSD Still to do: * Work out why Race XSD doesn't like core:nonNegativeDecimal but appears to be fine with core:percentage * Migrate test files to define namespaces and make sure they match the structure diff -r 28e99aa0053f -r f9846f896df3 IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Mon Mar 09 20:45:45 2009 +0000 +++ b/IBBoard.WarFoundry.API.csproj Thu Mar 12 21:35:17 2009 +0000 @@ -1,4 +1,4 @@ - + Debug @@ -44,6 +44,18 @@ + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + diff -r 28e99aa0053f -r f9846f896df3 api/Factories/Xml/WarFoundryXmlFactory.cs --- a/api/Factories/Xml/WarFoundryXmlFactory.cs Mon Mar 09 20:45:45 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Thu Mar 12 21:35:17 2009 +0000 @@ -25,6 +25,7 @@ public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory { private static WarFoundryXmlFactory factory; + private XmlReaderSettings settings; private Dictionary extraData = new Dictionary(); private XmlResolver xmlResolver; @@ -266,12 +267,7 @@ protected XmlDocument CreateXmlDocumentFromStream(Stream stream) { XmlDocument doc = new XmlDocument(); - XmlReaderSettings settings = new XmlReaderSettings(); - settings.XmlResolver = xmlResolver; - settings.ValidationType = ValidationType.DTD; - settings.ProhibitDtd = false; - settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); - XmlReader reader = XmlReader.Create(stream, settings); + XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); try { @@ -289,6 +285,51 @@ } return doc; + } + + /// + /// Lazy-getter for XML reader settings. May throw a if there is a problem with the translation schema. + /// + /// + /// A with the default values for validating the translation document against the translation schema + /// + private XmlReaderSettings GetReaderSettings() + { + 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); + } + } + + return settings; + } + + private void ValidationEventMethod(object sender, ValidationEventArgs e) + { + throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Exception.Message, e.Exception); } protected XmlDocument CreateXmlDocumentFromString(string xmlString) @@ -512,11 +553,5 @@ return new EquipmentItem(id, name, cost, min, max, armourType, race); } - - private void ValidationEventMethod(object sender, ValidationEventArgs e) - { - //TODO: Fire a validation failure event - LogNotifier.WarnFormat(GetType(), "Validation Error: {0}", e.Message); - } } } \ No newline at end of file diff -r 28e99aa0053f -r f9846f896df3 api/WarFoundryLoader.cs --- a/api/WarFoundryLoader.cs Mon Mar 09 20:45:45 2009 +0000 +++ b/api/WarFoundryLoader.cs Thu Mar 12 21:35:17 2009 +0000 @@ -275,18 +275,26 @@ foreach (FileInfo file in gameSystemFiles.Keys) { + FileLoadFailure failure = null; + try { bool loaded = LoadObject(file, gameSystemFiles[file]); if (!loaded) { - fails.Add(new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}")); + failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}"); } } catch (Exception ex) { - fails.Add(new FileLoadFailure(file, ex.Message)); + failure = new FileLoadFailure(file, ex.Message); + } + + if (failure!=null) + { + fails.Add(failure); + LogNotifier.Warn(GetType(), failure.Message); } } @@ -299,18 +307,26 @@ foreach (FileInfo file in raceFiles.Keys) { + FileLoadFailure failure = null; + try { bool loaded = LoadObject(file, raceFiles[file]); if (!loaded) { - fails.Add(new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}")); + failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}"); } } catch (Exception ex) { - fails.Add(new FileLoadFailure(file, ex.Message)); + failure = new FileLoadFailure(file, ex.Message); + } + + if (failure!=null) + { + fails.Add(failure); + LogNotifier.Warn(GetType(), failure.Message); } } diff -r 28e99aa0053f -r f9846f896df3 dtds/race.xsd --- a/dtds/race.xsd Mon Mar 09 20:45:45 2009 +0000 +++ b/dtds/race.xsd Thu Mar 12 21:35:17 2009 +0000 @@ -28,7 +28,7 @@ - + @@ -39,7 +39,7 @@ - + @@ -48,9 +48,10 @@ - + + + - @@ -89,9 +90,10 @@ - + + + - @@ -108,9 +110,10 @@ - + + + - @@ -119,7 +122,7 @@ - + @@ -132,7 +135,7 @@ - + @@ -140,7 +143,7 @@ - + @@ -162,7 +165,7 @@ - + diff -r 28e99aa0053f -r f9846f896df3 dtds/warfoundry-cats.xsd --- a/dtds/warfoundry-cats.xsd Mon Mar 09 20:45:45 2009 +0000 +++ b/dtds/warfoundry-cats.xsd Thu Mar 12 21:35:17 2009 +0000 @@ -1,6 +1,6 @@ +xmlns="http://ibboard.co.uk/warfoundry/cats" elementFormDefault="qualified"> diff -r 28e99aa0053f -r f9846f896df3 dtds/warfoundry-core.xsd --- a/dtds/warfoundry-core.xsd Mon Mar 09 20:45:45 2009 +0000 +++ b/dtds/warfoundry-core.xsd Thu Mar 12 21:35:17 2009 +0000 @@ -1,6 +1,6 @@ +xmlns="http://ibboard.co.uk/warfoundry/core" elementFormDefault="qualified">