Mercurial > repos > IBBoard.WarFoundry.API
comparison api/Factories/Xml/WarFoundryXmlFactory.cs @ 10:607c3232d689
Re #11 - Documentation
* Document Stats and SystemStats at class level to explain the difference
* Document WarFoundryXmlFactory at class level
Re #13 - XPath for XML loading
* Load categories using XPath - needs fixing so we can do a proper "categories/cat" query
Re #10 - Refactoring for readability
* Fix method signature for getting number from attribute added in r19
* Refactor more code in to GetExtraData method
* Separate out loading of categories for GameSystem
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Jan 2009 20:43:36 +0000 |
parents | 6ad505b6c36e |
children | 5a1df00b0359 |
comparison
equal
deleted
inserted
replaced
9:6ad505b6c36e | 10:607c3232d689 |
---|---|
34 using ICSharpCode.SharpZipLib.Zip; | 34 using ICSharpCode.SharpZipLib.Zip; |
35 | 35 |
36 namespace IBBoard.WarFoundry.API.Factories.Xml | 36 namespace IBBoard.WarFoundry.API.Factories.Xml |
37 { | 37 { |
38 /// <summary> | 38 /// <summary> |
39 /// Summary description for WarFoundryXmlFactory. | 39 /// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load. |
40 /// </summary> | 40 /// </summary> |
41 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory | 41 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory |
42 { | 42 { |
43 private Dictionary<IWarFoundryObject, XmlDocument> extraData = new Dictionary<IWarFoundryObject, XmlDocument>(); | 43 private Dictionary<IWarFoundryObject, XmlDocument> extraData = new Dictionary<IWarFoundryObject, XmlDocument>(); |
44 private XmlResolver xmlResolver; | 44 private XmlResolver xmlResolver; |
109 catch(FormatException) | 109 catch(FormatException) |
110 { | 110 { |
111 throw new FormatException("Attribute 'maxPoints' of army '"+name+"' was not a valid number"); | 111 throw new FormatException("Attribute 'maxPoints' of army '"+name+"' was not a valid number"); |
112 } | 112 } |
113 | 113 |
114 Army army = new Army(race, name, points, file);//, this); | 114 Army army = new Army(race, name, points, file); |
115 extraData[army] = elem.OwnerDocument; | 115 extraData[army] = elem.OwnerDocument; |
116 return army; | 116 return army; |
117 } | 117 } |
118 | 118 |
119 protected override Stream GetGameSystemDataStream (ZipFile file) | 119 protected override Stream GetGameSystemDataStream (ZipFile file) |
131 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) | 131 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) |
132 { | 132 { |
133 string id = elem.GetAttribute("id"); | 133 string id = elem.GetAttribute("id"); |
134 string name = elem.GetAttribute("name"); | 134 string name = elem.GetAttribute("name"); |
135 GameSystem system = new GameSystem(id, name, this); | 135 GameSystem system = new GameSystem(id, name, this); |
136 //system.SourceZipFile = file.; | |
137 extraData[system] = elem.OwnerDocument; | 136 extraData[system] = elem.OwnerDocument; |
138 return system; | 137 return system; |
139 } | 138 } |
140 | 139 |
141 protected override Stream GetRaceDataStream (ZipFile file) | 140 protected override Stream GetRaceDataStream (ZipFile file) |
155 string id = elem.GetAttribute("id"); | 154 string id = elem.GetAttribute("id"); |
156 string subid = elem.GetAttribute("subid"); | 155 string subid = elem.GetAttribute("subid"); |
157 string systemID = elem.GetAttribute("system"); | 156 string systemID = elem.GetAttribute("system"); |
158 string name = elem.GetAttribute("name"); | 157 string name = elem.GetAttribute("name"); |
159 Race race = new Race(id, subid, name, systemID, this); | 158 Race race = new Race(id, subid, name, systemID, this); |
160 //race.SourceZipFile = file; //TODO reference source file | |
161 extraData[race] = elem.OwnerDocument; | 159 extraData[race] = elem.OwnerDocument; |
162 return race; | 160 return race; |
163 } | 161 } |
164 | 162 |
165 public override void CompleteLoading(IWarFoundryStagedLoadObject obj) | 163 public override void CompleteLoading(IWarFoundryStagedLoadObject obj) |
174 { | 172 { |
175 CompleteLoading((Race)obj); | 173 CompleteLoading((Race)obj); |
176 } | 174 } |
177 } | 175 } |
178 | 176 |
179 public XmlDocument GetExtraData(IWarFoundryObject obj) | 177 public XmlNode GetExtraData(IWarFoundryObject obj) |
180 { | 178 { |
181 XmlDocument extra = null; | 179 XmlDocument extra = null; |
182 extraData.TryGetValue(obj, out extra); | 180 extraData.TryGetValue(obj, out extra); |
183 return extra; | 181 XmlNode node = null; |
182 | |
183 if (extra !=null) | |
184 { | |
185 node = extra.LastChild; | |
186 } | |
187 | |
188 return node; | |
184 } | 189 } |
185 | 190 |
186 public void CompleteLoading(GameSystem system) | 191 public void CompleteLoading(GameSystem system) |
187 { | 192 { |
188 if (system.IsFullyLoaded) | 193 if (system.IsFullyLoaded) |
189 { | 194 { |
190 LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID); | 195 LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID); |
191 return; | 196 return; |
192 } | 197 } |
193 | 198 |
194 XmlDocument extra = GetExtraData(system); | 199 XmlNode elem = GetExtraData(system); |
195 XmlNode elem = extra.LastChild; | |
196 | |
197 XmlNode catsElem = elem.FirstChild; | 200 XmlNode catsElem = elem.FirstChild; |
198 WarFoundryObject tempObj; | 201 LoadCategoriesForSystem(system, elem); |
199 | |
200 foreach (XmlElement cat in catsElem.ChildNodes) | |
201 { | |
202 tempObj = CreateObjectFromElement(cat); | |
203 | |
204 if (tempObj is Category) | |
205 { | |
206 system.AddCategory((Category)tempObj); | |
207 } | |
208 else | |
209 { | |
210 LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml); | |
211 } | |
212 } | |
213 | 202 |
214 XmlElement statsElem = (XmlElement)catsElem.NextSibling; | 203 XmlElement statsElem = (XmlElement)catsElem.NextSibling; |
215 LoadSystemStatsFromElement(statsElem, system); | 204 LoadSystemStatsFromElement(statsElem, system); |
216 string defaultStatsID = statsElem.GetAttribute("defaultStats"); | 205 string defaultStatsID = statsElem.GetAttribute("defaultStats"); |
217 | 206 |
218 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); | 207 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); |
219 system.StandardSystemStatsID = defaultStatsID; | 208 system.StandardSystemStatsID = defaultStatsID; |
220 system.SetAsFullyLoaded(); | 209 system.SetAsFullyLoaded(); |
221 } | 210 } |
222 | 211 |
212 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) | |
213 { | |
214 WarFoundryObject tempObj; | |
215 | |
216 foreach (XmlElement cat in elem.SelectNodes("//"+WarFoundryXmlElementName.CATEGORY_ELEMENT.Value)) | |
217 { | |
218 tempObj = CreateObjectFromElement(cat); | |
219 | |
220 if (tempObj is Category) | |
221 { | |
222 system.AddCategory((Category)tempObj); | |
223 } | |
224 else | |
225 { | |
226 LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml); | |
227 } | |
228 } | |
229 } | |
230 | |
223 public void CompleteLoading(Race race) | 231 public void CompleteLoading(Race race) |
224 { | 232 { |
225 if (race.IsFullyLoaded) | 233 if (race.IsFullyLoaded) |
226 { | 234 { |
227 LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID); | 235 LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID); |
228 return; | 236 return; |
229 } | 237 } |
230 | 238 |
231 XmlDocument extra = GetExtraData(race); | 239 XmlNode elem = GetExtraData(race); |
232 XmlNode elem = extra.LastChild; | |
233 XmlNode colNode = elem.FirstChild; | 240 XmlNode colNode = elem.FirstChild; |
234 | 241 |
235 Dictionary<string, UnitType> unitTypes = new Dictionary<string, UnitType>(); | 242 Dictionary<string, UnitType> unitTypes = new Dictionary<string, UnitType>(); |
236 | 243 |
237 foreach (XmlElement node in colNode.ChildNodes) | 244 foreach (XmlElement node in colNode.ChildNodes) |
367 incAmount = GetIntValueFromAttribute(elem, "incAmount"); | 374 incAmount = GetIntValueFromAttribute(elem, "incAmount"); |
368 | 375 |
369 return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount); | 376 return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount); |
370 } | 377 } |
371 | 378 |
372 private GetIntValueFromAttribute(XmlElement elem, string attributeName) | 379 private int GetIntValueFromAttribute(XmlElement elem, string attributeName) |
373 { | 380 { |
374 try | 381 try |
375 { | 382 { |
376 return int.Parse(elem.GetAttribute(attributeName)); | 383 return int.Parse(elem.GetAttribute(attributeName)); |
377 } | 384 } |