changeset 118:e53bf3356914

Re #49: XmlResourceResolver fails with HTML DTD on Windows * Make sure that we're always using a conventional format URI
author IBBoard <dev@ibboard.co.uk>
date Fri, 31 Aug 2012 20:40:30 +0100
parents e9f3a4ddf772
children 645e71b9a809
files Xml/XmlResourceResolver.cs
diffstat 1 files changed, 42 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Xml/XmlResourceResolver.cs	Sat Jul 14 15:24:17 2012 +0100
+++ b/Xml/XmlResourceResolver.cs	Fri Aug 31 20:40:30 2012 +0100
@@ -19,16 +19,56 @@
 
 		public void AddMapping(string relativeURI, string resourceName)
 		{
-			relativeToUriMap[relativeURI] = resourceName;
+			string key = MakeKey(relativeURI);
+			relativeToUriMap[key] = resourceName;
+		}
+
+		private string MakeKey(string uri)
+		{
+			return "resource:///" + Uri.EscapeUriString(uri);
+		}
+
+		public override Uri ResolveUri(Uri baseUri, string relativeUri)
+		{
+			Uri resolved;
+			if (relativeToUriMap.ContainsKey(relativeUri))
+			{
+				resolved = new Uri(MakeKey(relativeUri));
+			}
+			else if (relativeToUriMap.ContainsKey(MakeKey(relativeUri)))
+			{
+				resolved = new Uri(relativeUri);
+			}
+			else
+			{
+				resolved = base.ResolveUri(baseUri, relativeUri);
+			}
+
+			return resolved;
 		}
 
 		public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
 		{
 			string absoluteUriString = absoluteUri.ToString();
+			string key = null;
 
 			if (relativeToUriMap.ContainsKey(absoluteUriString))
 			{
-				string file = relativeToUriMap[absoluteUriString];
+				key = absoluteUriString;
+			}
+			else
+			{
+				string createdKey = MakeKey(absoluteUriString);
+
+				if (relativeToUriMap.ContainsKey(createdKey))
+				{
+					key = createdKey;
+				}
+			}
+
+			if (key != null)
+			{
+				string file = relativeToUriMap[key];
 				Stream stream = assm.GetManifestResourceStream(file);
 				return stream;
 			}