view Xml/XmlResourceResolver.cs @ 115:de0ed24eb961

* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise * Clean up code (and some automatic changes) * Use XML resources in TranslationXmlLoader
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Jun 2012 20:06:50 +0100
parents
children 07660ac09a5f
line wrap: on
line source

using System;
using System.Xml;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace IBBoard.Xml
{
	public class XmlResourceResolver : XmlUrlResolver
	{
		private Assembly assm;
		private Dictionary<string, string> relativeToUriMap = new Dictionary<string, string>();

		public XmlResourceResolver(Assembly assembly)
		{
			assm = assembly;
		}

		public void AddMapping(string relativeURI, string resourceName)
		{
			relativeToUriMap[relativeURI] = resourceName;
		}

		public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
		{
			string absoluteUriString = absoluteUri.ToString();

			if (relativeToUriMap.ContainsKey(absoluteUriString))
			{
				string file = relativeToUriMap[absoluteUriString];
				Stream stream = assm.GetManifestResourceStream(file);
				return stream;
			}

			return base.GetEntity(absoluteUri, role, ofObjectToReturn);
		}
	}
}