00001 //------------------------------------------------------------------------------ 00002 // <copyright file="XmlUrlResolver.cs" company="Microsoft"> 00003 // 00004 // Copyright (c) 2002 Microsoft Corporation. All rights reserved. 00005 // 00006 // The use and distribution terms for this software are contained in the file 00007 // named license.txt, which can be found in the root of this distribution. 00008 // By using this software in any fashion, you are agreeing to be bound by the 00009 // terms of this license. 00010 // 00011 // You must not remove this notice, or any other, from this software. 00012 // 00013 // </copyright> 00014 //------------------------------------------------------------------------------ 00015 00016 namespace System.Xml 00017 { 00018 using System; 00019 using System.IO; 00020 using System.Net; 00021 using System.Text; 00022 00028 public class XmlUrlResolver : XmlResolver { 00029 private const int NOTPREFIXED = -1; 00030 private const int PREFIXED = 1; 00031 private const int ABSOLUTENOTPREFIXED = 2; 00032 private const int SYSTEMROOTMISSING = 3; 00033 00034 static XmlDownloadManager _DownloadManager = new XmlDownloadManager(); 00035 ICredentials _credentials; 00036 00037 // Construction 00038 00045 public XmlUrlResolver() { 00046 } 00047 00048 //UE attension 00053 public override ICredentials Credentials { 00054 set { _credentials = value; } 00055 } 00056 00057 // Resource resolution 00058 00064 public override Object GetEntity(Uri absoluteUri, 00065 string role, 00066 Type ofObjectToReturn) { 00067 if (ofObjectToReturn == null || ofObjectToReturn == typeof(System.IO.Stream)) { 00068 return _DownloadManager.GetStream(absoluteUri, _credentials); 00069 } 00070 else { 00071 throw new XmlException(Res.Xml_UnsupportedClass); 00072 } 00073 } 00074 00079 public override Uri ResolveUri(Uri baseUri, string relativeUri) { 00080 if (null == relativeUri) { 00081 relativeUri = String.Empty; 00082 } 00083 int prefixed = IsPrefixed(relativeUri); 00084 if (prefixed == PREFIXED) { 00085 if (relativeUri.StartsWith("file:")) 00086 return new Uri(Escape(relativeUri), true); 00087 else 00088 return new Uri(relativeUri); 00089 } 00090 else if (prefixed == ABSOLUTENOTPREFIXED) { 00091 if (relativeUri.StartsWith("file:")) 00092 return new Uri(Escape(relativeUri), true); 00093 else 00094 return new Uri(Escape("file://" + relativeUri), true); 00095 } 00096 else if (prefixed == SYSTEMROOTMISSING) { 00097 // we have gotten a path like "/foo/bar.xml" we should use the drive letter from the baseUri if available. 00098 if (null == baseUri) 00099 return new Uri(Escape(Path.GetFullPath(relativeUri)), true); 00100 else 00101 if ("file" == baseUri.Scheme) { 00102 return new Uri(Escape(Path.GetFullPath(Path.GetPathRoot(baseUri.LocalPath) + relativeUri)), true); 00103 } 00104 return new Uri(baseUri, relativeUri); 00105 } 00106 else if (baseUri != null) { 00107 if (baseUri.Scheme == "file"){ 00108 baseUri = new Uri(Escape(baseUri.ToString()), true); 00109 return new Uri(baseUri, Escape(relativeUri), true); 00110 } 00111 else 00112 return new Uri(baseUri, relativeUri); 00113 } 00114 else { 00115 return new Uri(Escape(Path.GetFullPath(relativeUri)), true); 00116 } 00117 } 00118 00119 private static string Escape(string path) { 00120 StringBuilder b = new StringBuilder(); 00121 char c; 00122 for (int i = 0; i < path.Length; i++) { 00123 c = path[i]; 00124 if (c == '\\') 00125 b.Append('/'); 00126 else if (c == '#') 00127 b.Append("%23"); 00128 else 00129 b.Append(c); 00130 } 00131 return b.ToString(); 00132 } 00133 00134 internal static string UnEscape(string path) { 00135 if (null != path && path.StartsWith("file")) { 00136 return path.Replace("%23", "#"); 00137 } 00138 return path; 00139 } 00140 00141 00142 private static int IsPrefixed(string uri) { 00143 if (uri != null && uri.IndexOf("://") >= 0) 00144 return PREFIXED; 00145 else if (uri.IndexOf(":\\") > 0) 00146 return ABSOLUTENOTPREFIXED; 00147 else if (uri.Length > 1 && uri[0] == '\\' && uri[1] != '\\') 00148 return SYSTEMROOTMISSING; 00149 else 00150 return NOTPREFIXED; 00151 } 00152 } 00153 }