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 diff
     1.1 --- a/Xml/XmlResourceResolver.cs	Fri Aug 31 20:40:30 2012 +0100
     1.2 +++ b/Xml/XmlResourceResolver.cs	Sat Sep 01 11:51:43 2012 +0100
     1.3 @@ -10,38 +10,40 @@
     1.4  	public class XmlResourceResolver : XmlUrlResolver
     1.5  	{
     1.6  		private Assembly assm;
     1.7 -		private Dictionary<string, string> relativeToUriMap = new Dictionary<string, string>();
     1.8 +		private Dictionary<string, string> relativeToAbsoluteMap = new Dictionary<string, string>();
     1.9 +		private Dictionary<string, string> absoluteToResourceMap = new Dictionary<string, string>();
    1.10  
    1.11  		public XmlResourceResolver(Assembly assembly)
    1.12  		{
    1.13  			assm = assembly;
    1.14 +		}
    1.15 +
    1.16 +		public void AddMapping(string relativeUri, string absoluteUri, string resourceName)
    1.17 +		{
    1.18 +			relativeToAbsoluteMap.Add(relativeUri, absoluteUri);
    1.19 +			AddMapping(absoluteUri, resourceName);
    1.20  		}
    1.21  
    1.22 -		public void AddMapping(string relativeURI, string resourceName)
    1.23 -		{
    1.24 -			string key = MakeKey(relativeURI);
    1.25 -			relativeToUriMap[key] = resourceName;
    1.26 -		}
    1.27 -
    1.28 -		private string MakeKey(string uri)
    1.29 -		{
    1.30 -			return "resource:///" + Uri.EscapeUriString(uri);
    1.31 +		public void AddMapping(string absoluteUri, string resourceName)
    1.32 +		{
    1.33 +			absoluteToResourceMap[absoluteUri] = resourceName;
    1.34  		}
    1.35  
    1.36  		public override Uri ResolveUri(Uri baseUri, string relativeUri)
    1.37  		{
    1.38 -			Uri resolved;
    1.39 -			if (relativeToUriMap.ContainsKey(relativeUri))
    1.40 -			{
    1.41 -				resolved = new Uri(MakeKey(relativeUri));
    1.42 -			}
    1.43 -			else if (relativeToUriMap.ContainsKey(MakeKey(relativeUri)))
    1.44 -			{
    1.45 -				resolved = new Uri(relativeUri);
    1.46 -			}
    1.47 -			else
    1.48 -			{
    1.49 -				resolved = base.ResolveUri(baseUri, relativeUri);
    1.50 +			Uri resolved;
    1.51 +
    1.52 +			if (relativeToAbsoluteMap.ContainsKey(relativeUri))
    1.53 +			{
    1.54 +				resolved = new Uri(relativeToAbsoluteMap[relativeUri], UriKind.Absolute);
    1.55 +			}
    1.56 +			else if (absoluteToResourceMap.ContainsKey(relativeUri))
    1.57 +			{
    1.58 +				resolved = new Uri(relativeUri, UriKind.Absolute);
    1.59 +			}
    1.60 +			else
    1.61 +			{
    1.62 +				resolved = base.ResolveUri(baseUri, relativeUri);
    1.63  			}
    1.64  
    1.65  			return resolved;
    1.66 @@ -49,28 +51,13 @@
    1.67  
    1.68  		public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    1.69  		{
    1.70 -			string absoluteUriString = absoluteUri.ToString();
    1.71 -			string key = null;
    1.72 -
    1.73 -			if (relativeToUriMap.ContainsKey(absoluteUriString))
    1.74 -			{
    1.75 -				key = absoluteUriString;
    1.76 -			}
    1.77 -			else
    1.78 -			{
    1.79 -				string createdKey = MakeKey(absoluteUriString);
    1.80 -
    1.81 -				if (relativeToUriMap.ContainsKey(createdKey))
    1.82 -				{
    1.83 -					key = createdKey;
    1.84 -				}
    1.85 -			}
    1.86 -
    1.87 -			if (key != null)
    1.88 -			{
    1.89 -				string file = relativeToUriMap[key];
    1.90 -				Stream stream = assm.GetManifestResourceStream(file);
    1.91 -				return stream;
    1.92 +			string absoluteUriString = absoluteUri == null ? null : absoluteUri.ToString();
    1.93 +			Console.WriteLine("Getting entity " + absoluteUriString);
    1.94 +
    1.95 +			if (absoluteToResourceMap.ContainsKey(absoluteUriString))
    1.96 +			{
    1.97 +				string file = absoluteToResourceMap[absoluteUriString];
    1.98 +				return assm.GetManifestResourceStream(file);
    1.99  			}
   1.100  
   1.101  			return base.GetEntity(absoluteUri, role, ofObjectToReturn);