comparison API/Factories/Xml/WarFoundryXmlLimitParser.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
1 // This file (WarFoundryXmlLimitParser.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2010 IBBoard
2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
4 using System.Collections.Generic;
5 using System.Xml;
6 using IBBoard.Limits;
7 using IBBoard.Xml;
8
9 namespace IBBoard.WarFoundry.API.Factories.Xml
10 {
11 public class WarFoundryXmlLimitParser
12 {
13 public ILimit GetMinLimit(XmlElement elem)
14 {
15 XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:minLimit/*[1]");
16 return GetLimitFromElement(limitElem);
17 }
18
19 public ILimit GetMaxLimit(XmlElement equipSlot)
20 {
21 XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(equipSlot, "race:maxLimit/*[1]");
22 return GetLimitFromElement(limitElem);
23 }
24
25 public ILimit GetLimitFromElement(XmlElement limitElem)
26 {
27 ILimit limit = null;
28
29 if (limitElem != null)
30 {
31 switch (limitElem.LocalName)
32 {
33 case "percentageLimit":
34 double limitPercent = XmlTools.GetDoubleValueFromAttribute(limitElem, "limit");
35 bool roundUp = limitElem.GetAttribute("round").Equals("up");
36 limit = new SimpleRoundedPercentageLimit(limitPercent, roundUp);
37 break;
38 case "sizeConstrainedLimit":
39 limit = new NumericSizeConstrainedLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
40 break;
41 case "absoluteLimit":
42 limit = new AbsoluteNumericLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
43 break;
44 case "unitSizeLimit":
45 limit = new SimpleRoundedPercentageLimit(100);
46 break;
47 case "compositeMaxLimit":
48 ICollection<ILimit> maxSubLimits = GetSubLimits(limitElem);
49 limit = new CompositeMaximumLimit(maxSubLimits);
50 break;
51 case "compositeMinLimit":
52 ICollection<ILimit> minSubLimits = GetSubLimits(limitElem);
53 limit = new CompositeMinimumLimit(minSubLimits);
54 break;
55 default:
56 //TODO: Warn of missing handler for when we've extended the limit list
57 break;
58 }
59 }
60
61 return limit;
62 }
63
64 private ICollection<ILimit> GetSubLimits(XmlElement limitElem)
65 {
66 XmlNodeList subLimitNodes = GetSubLimitElements(limitElem);
67 ICollection<ILimit> limits = new List<ILimit>();
68
69 foreach (XmlNode node in subLimitNodes)
70 {
71 if (node is XmlElement)
72 {
73 ILimit limit = GetLimitFromElement((XmlElement)node);
74
75 if (limit != null)
76 {
77 limits.Add(limit);
78 }
79 }
80 }
81
82 return limits;
83 }
84
85 private XmlNodeList GetSubLimitElements(XmlElement limitElem)
86 {
87 return limitElem.ChildNodes;
88 }
89 }
90 }
91