view 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
line wrap: on
line source

//  This file (WarFoundryXmlLimitParser.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2010 IBBoard
// 
//  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.
using System.Collections.Generic;
using System.Xml;
using IBBoard.Limits;
using IBBoard.Xml;

namespace IBBoard.WarFoundry.API.Factories.Xml
{
	public class WarFoundryXmlLimitParser
	{
		public ILimit GetMinLimit(XmlElement elem)
		{
			XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:minLimit/*[1]");
			return GetLimitFromElement(limitElem);
		}

		public ILimit GetMaxLimit(XmlElement equipSlot)
		{
			XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(equipSlot, "race:maxLimit/*[1]");
			return GetLimitFromElement(limitElem);
		}

		public ILimit GetLimitFromElement(XmlElement limitElem)
		{
			ILimit limit = null;
			
			if (limitElem != null)
			{
				switch (limitElem.LocalName)
				{
					case "percentageLimit":
						double limitPercent = XmlTools.GetDoubleValueFromAttribute(limitElem, "limit");
						bool roundUp = limitElem.GetAttribute("round").Equals("up");
						limit = new SimpleRoundedPercentageLimit(limitPercent, roundUp);
						break;
					case "sizeConstrainedLimit":
						limit = new NumericSizeConstrainedLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
						break;
					case "absoluteLimit":
						limit = new AbsoluteNumericLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
						break;
					case "unitSizeLimit":
						limit = new SimpleRoundedPercentageLimit(100);
						break;
					case "compositeMaxLimit":
						ICollection<ILimit> maxSubLimits = GetSubLimits(limitElem);
						limit = new CompositeMaximumLimit(maxSubLimits);
						break;
					case "compositeMinLimit":
						ICollection<ILimit> minSubLimits = GetSubLimits(limitElem);
						limit = new CompositeMinimumLimit(minSubLimits);
						break;
					default:
						//TODO: Warn of missing handler for when we've extended the limit list
						break;
				}
			}
			
			return limit;
		}

		private ICollection<ILimit> GetSubLimits(XmlElement limitElem)
		{
			XmlNodeList subLimitNodes = GetSubLimitElements(limitElem);
			ICollection<ILimit> limits = new List<ILimit>();
			
			foreach (XmlNode node in subLimitNodes)
			{
				if (node is XmlElement)
				{
					ILimit limit = GetLimitFromElement((XmlElement)node);
					
					if (limit != null)
					{
						limits.Add(limit);
					}
				}
			}
			
			return limits;
		}

		private XmlNodeList GetSubLimitElements(XmlElement limitElem)
		{
			return limitElem.ChildNodes;
		}
	}
}