view API/Factories/Xml/WarFoundryXmlLimitParser.cs @ 443:86725e88052e

Re #380: WarFoundry chokes on zips written by Mac OS X * Add "is dot-file" check to behave like Linux and Mac and ignore files with name ".something" Also: * Fix load failure message argument order * Save some regex checks by making sure zip entry is a file first
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Dec 2011 20:40:31 +0000
parents 3c4a6403a88c
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;
		}
	}
}