changeset 119:645e71b9a809

Re #49: XmlResourceResolver fails with HTML DTD on Windows * Rebuild resolver to handle relative and absolute URIs, with absolute mapping to resource ID
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Sep 2012 11:51:43 +0100
parents e53bf3356914
children 780169621672
files Xml/XmlResourceResolver.cs
diffstat 1 files changed, 31 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/Xml/XmlResourceResolver.cs	Fri Aug 31 20:40:30 2012 +0100
+++ b/Xml/XmlResourceResolver.cs	Sat Sep 01 11:51:43 2012 +0100
@@ -10,38 +10,40 @@
 	public class XmlResourceResolver : XmlUrlResolver
 	{
 		private Assembly assm;
-		private Dictionary<string, string> relativeToUriMap = new Dictionary<string, string>();
+		private Dictionary<string, string> relativeToAbsoluteMap = new Dictionary<string, string>();
+		private Dictionary<string, string> absoluteToResourceMap = new Dictionary<string, string>();
 
 		public XmlResourceResolver(Assembly assembly)
 		{
 			assm = assembly;
+		}
+
+		public void AddMapping(string relativeUri, string absoluteUri, string resourceName)
+		{
+			relativeToAbsoluteMap.Add(relativeUri, absoluteUri);
+			AddMapping(absoluteUri, resourceName);
 		}
 
-		public void AddMapping(string relativeURI, string resourceName)
-		{
-			string key = MakeKey(relativeURI);
-			relativeToUriMap[key] = resourceName;
-		}
-
-		private string MakeKey(string uri)
-		{
-			return "resource:///" + Uri.EscapeUriString(uri);
+		public void AddMapping(string absoluteUri, string resourceName)
+		{
+			absoluteToResourceMap[absoluteUri] = resourceName;
 		}
 
 		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);
+			Uri resolved;
+
+			if (relativeToAbsoluteMap.ContainsKey(relativeUri))
+			{
+				resolved = new Uri(relativeToAbsoluteMap[relativeUri], UriKind.Absolute);
+			}
+			else if (absoluteToResourceMap.ContainsKey(relativeUri))
+			{
+				resolved = new Uri(relativeUri, UriKind.Absolute);
+			}
+			else
+			{
+				resolved = base.ResolveUri(baseUri, relativeUri);
 			}
 
 			return resolved;
@@ -49,28 +51,13 @@
 
 		public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
 		{
-			string absoluteUriString = absoluteUri.ToString();
-			string key = null;
-
-			if (relativeToUriMap.ContainsKey(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;
+			string absoluteUriString = absoluteUri == null ? null : absoluteUri.ToString();
+			Console.WriteLine("Getting entity " + absoluteUriString);
+
+			if (absoluteToResourceMap.ContainsKey(absoluteUriString))
+			{
+				string file = absoluteToResourceMap[absoluteUriString];
+				return assm.GetManifestResourceStream(file);
 			}
 
 			return base.GetEntity(absoluteUri, role, ofObjectToReturn);