1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
//------------------------------------------------------------------------------
// <copyright file="XmlPreloadedResolver.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System.IO;
using System.Xml;
#if !SILVERLIGHT
using System.Net;
#endif
using System.Text;
using System.Xml.Utils;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.Versioning;
namespace System.Xml.Resolvers {
//
// XmlPreloadedResolver is an XmlResolver that which can be pre-loaded with data.
// By default it contains well-known DTDs for XHTML 1.0 and RSS 0.91.
// Custom mappings of URIs to data can be added with the Add method.
//
public partial class XmlPreloadedResolver : XmlResolver {
//
// PreloadedData class
//
abstract class PreloadedData {
// Returns preloaded data as Stream; Stream must always be supported
internal abstract Stream AsStream();
// Returns preloaded data as TextReader, or throws when not supported
internal virtual TextReader AsTextReader() {
throw new XmlException(Res.GetString(Res.Xml_UnsupportedClass));
}
// Returns true for types that are supported for this preloaded data; Stream must always be supported
internal virtual bool SupportsType(Type type) {
if (type == null || type == typeof(Stream)) {
return true;
}
return false;
}
};
//
// XmlKnownDtdData class
//
class XmlKnownDtdData : PreloadedData {
internal string publicId;
internal string systemId;
private string resourceName;
internal XmlKnownDtdData(string publicId, string systemId, string resourceName) {
this.publicId = publicId;
this.systemId = systemId;
this.resourceName = resourceName;
}
internal override Stream AsStream() {
Assembly asm = Assembly.GetExecutingAssembly();
return asm.GetManifestResourceStream(resourceName);
}
}
class ByteArrayChunk : PreloadedData {
byte[] array;
int offset;
int length;
internal ByteArrayChunk(byte[] array)
: this(array, 0, array.Length) {
}
internal ByteArrayChunk(byte[] array, int offset, int length) {
this.array = array;
this.offset = offset;
this.length = length;
}
internal override Stream AsStream() {
return new MemoryStream(array, offset, length);
}
}
class StringData : PreloadedData {
string str;
internal StringData(string str) {
this.str = str;
}
internal override Stream AsStream() {
return new MemoryStream(Encoding.Unicode.GetBytes(str));
}
internal override TextReader AsTextReader() {
return new StringReader(str);
}
internal override bool SupportsType(Type type) {
if (type == typeof(TextReader)) {
return true;
}
return base.SupportsType(type);
}
}
//
// Fields
//
XmlResolver fallbackResolver;
Dictionary<Uri, PreloadedData> mappings;
XmlKnownDtds preloadedDtds;
//
// Static/constant fiels
//
static XmlKnownDtdData[] Xhtml10_Dtd = new XmlKnownDtdData[] {
new XmlKnownDtdData( "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "xhtml1-strict.dtd" ),
new XmlKnownDtdData( "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1-transitional.dtd" ),
new XmlKnownDtdData( "-//W3C//DTD XHTML 1.0 Frameset//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd", "xhtml1-frameset.dtd" ),
new XmlKnownDtdData( "-//W3C//ENTITIES Latin 1 for XHTML//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent", "xhtml-lat1.ent" ),
new XmlKnownDtdData( "-//W3C//ENTITIES Symbols for XHTML//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent", "xhtml-symbol.ent" ),
new XmlKnownDtdData( "-//W3C//ENTITIES Special for XHTML//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent", "xhtml-special.ent" ),
};
static XmlKnownDtdData[] Rss091_Dtd = new XmlKnownDtdData[] {
new XmlKnownDtdData( "-//Netscape Communications//DTD RSS 0.91//EN", "http://my.netscape.com/publish/formats/rss-0.91.dtd", "rss-0.91.dtd" ),
};
//
// Constructors
//
public XmlPreloadedResolver()
: this(null) {
}
public XmlPreloadedResolver(XmlKnownDtds preloadedDtds)
: this(null, preloadedDtds, null) {
}
public XmlPreloadedResolver(XmlResolver fallbackResolver)
: this(fallbackResolver, XmlKnownDtds.All, null) {
}
public XmlPreloadedResolver(XmlResolver fallbackResolver, XmlKnownDtds preloadedDtds)
: this (fallbackResolver, preloadedDtds, null) {
}
public XmlPreloadedResolver(XmlResolver fallbackResolver, XmlKnownDtds preloadedDtds, IEqualityComparer<Uri> uriComparer) {
this.fallbackResolver = fallbackResolver;
this.mappings = new Dictionary<Uri, PreloadedData>(16, uriComparer);
this.preloadedDtds = preloadedDtds;
// load known DTDs
if (preloadedDtds != 0) {
if ((preloadedDtds & XmlKnownDtds.Xhtml10) != 0) {
AddKnownDtd(Xhtml10_Dtd);
}
if ((preloadedDtds & XmlKnownDtds.Rss091) != 0) {
AddKnownDtd(Rss091_Dtd);
}
}
}
#if !SILVERLIGHT
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
#endif
public override Uri ResolveUri(Uri baseUri, string relativeUri) {
// 1) special-case well-known public IDs
// 2) To make FxCop happy we need to use StartsWith() overload that takes StringComparison ->
// .StartsWith(string) is equal to .StartsWith(string, StringComparison.CurrentCulture);
if (relativeUri != null && relativeUri.StartsWith("-//", StringComparison.CurrentCulture)) {
// 1) XHTML 1.0 public IDs
// 2) To make FxCop happy we need to use StartsWith() overload that takes StringComparison ->
// .StartsWith(string) is equal to .StartsWith(string, StringComparison.CurrentCulture);
if ((preloadedDtds & XmlKnownDtds.Xhtml10) != 0 && relativeUri.StartsWith("-//W3C//", StringComparison.CurrentCulture)) {
for (int i = 0; i < Xhtml10_Dtd.Length; i++) {
if (relativeUri == Xhtml10_Dtd[i].publicId) {
return new Uri(relativeUri, UriKind.Relative);
}
}
}
// RSS 0.91 public IDs
if ((preloadedDtds & XmlKnownDtds.Rss091) != 0) {
Debug.Assert(Rss091_Dtd.Length == 1);
if (relativeUri == Rss091_Dtd[0].publicId) {
return new Uri(relativeUri, UriKind.Relative);
}
}
}
return base.ResolveUri(baseUri, relativeUri);
}
public override Object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn) {
if (absoluteUri == null) {
throw new ArgumentNullException("absoluteUri");
}
PreloadedData data;
if (!mappings.TryGetValue(absoluteUri, out data)) {
if (fallbackResolver != null) {
return fallbackResolver.GetEntity(absoluteUri, role, ofObjectToReturn);
}
throw new XmlException(Res.GetString(Res.Xml_CannotResolveUrl, absoluteUri.ToString()));
}
if (ofObjectToReturn == null || ofObjectToReturn == typeof(Stream) || ofObjectToReturn == typeof(Object)) {
return data.AsStream();
}
else if (ofObjectToReturn == typeof(TextReader)) {
return data.AsTextReader();
}
else {
throw new XmlException(Res.GetString(Res.Xml_UnsupportedClass));
}
}
#if !SILVERLIGHT
public override ICredentials Credentials {
set {
if (fallbackResolver != null) {
fallbackResolver.Credentials = value;
}
}
}
#endif
public override bool SupportsType(Uri absoluteUri, Type type) {
if (absoluteUri == null) {
throw new ArgumentNullException("absoluteUri");
}
PreloadedData data;
if (!mappings.TryGetValue(absoluteUri, out data)) {
if (fallbackResolver != null) {
return fallbackResolver.SupportsType(absoluteUri, type);
}
return base.SupportsType(absoluteUri, type);
}
return data.SupportsType(type);
}
public void Add(Uri uri, byte[] value) {
if (uri == null) {
throw new ArgumentNullException("uri");
}
if (value == null) {
throw new ArgumentNullException("value");
}
Add(uri, new ByteArrayChunk(value, 0, value.Length));
}
public void Add(Uri uri, byte[] value, int offset, int count) {
if (uri == null) {
throw new ArgumentNullException("uri");
}
if (value == null) {
throw new ArgumentNullException("value");
}
if (count < 0) {
throw new ArgumentOutOfRangeException("count");
}
if (offset < 0) {
throw new ArgumentOutOfRangeException("offset");
}
if (value.Length - offset < count) {
throw new ArgumentOutOfRangeException("count");
}
Add(uri, new ByteArrayChunk(value, offset, count));
}
public void Add(Uri uri, Stream value) {
if (uri == null) {
throw new ArgumentNullException("uri");
}
if (value == null) {
throw new ArgumentNullException("value");
}
if (value.CanSeek) {
// stream of known length -> allocate the byte array and read all data into it
int size = checked((int)value.Length);
byte[] bytes = new byte[size];
value.Read(bytes, 0, size);
Add(uri, new ByteArrayChunk(bytes));
}
else {
// stream of unknown length -> read into memory stream and then get internal the byte array
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[4096];
int read;
while ((read = value.Read(buffer, 0, buffer.Length)) > 0) {
ms.Write(buffer, 0, read);
}
int size = checked((int)ms.Position);
byte[] bytes = new byte[size];
Array.Copy(ms.GetBuffer(), bytes, size);
Add(uri, new ByteArrayChunk(bytes));
}
}
public void Add(Uri uri, string value) {
if (uri == null) {
throw new ArgumentNullException("uri");
}
if (value == null) {
throw new ArgumentNullException("value");
}
Add(uri, new StringData(value));
}
public IEnumerable<Uri> PreloadedUris {
get {
// read-only collection of keys
return mappings.Keys;
}
}
public void Remove(Uri uri) {
if (uri == null) {
throw new ArgumentNullException("uri");
}
mappings.Remove(uri);
}
//
// Private implementation methods
//
private void Add(Uri uri, PreloadedData data) {
Debug.Assert(uri != null);
// override if exists
if (mappings.ContainsKey(uri)) {
mappings[uri] = data;
}
else {
mappings.Add(uri, data);
}
}
private void AddKnownDtd(XmlKnownDtdData[] dtdSet) {
for (int i = 0; i < dtdSet.Length; i++) {
XmlKnownDtdData dtdInfo = dtdSet[i];
mappings.Add(new Uri(dtdInfo.publicId, UriKind.RelativeOrAbsolute), dtdInfo);
mappings.Add(new Uri(dtdInfo.systemId, UriKind.RelativeOrAbsolute), dtdInfo);
}
}
}
}
|