Line | |
---|
1 | using System;
|
---|
2 | using System.Xml;
|
---|
3 | using System.IO;
|
---|
4 | using System.Collections.Generic;
|
---|
5 |
|
---|
6 | namespace SharpVectors.Xml
|
---|
7 | {
|
---|
8 | /// <summary>
|
---|
9 | /// Used to redirect external DTDs to local copies.
|
---|
10 | /// </summary>
|
---|
11 | public sealed class LocalDtdXmlUrlResolver : XmlUrlResolver
|
---|
12 | {
|
---|
13 | private Dictionary<string, string> knownDtds;
|
---|
14 |
|
---|
15 | public LocalDtdXmlUrlResolver()
|
---|
16 | : base()
|
---|
17 | {
|
---|
18 | knownDtds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
---|
19 | }
|
---|
20 |
|
---|
21 | public void AddDtd(string publicIdentifier, string localFile)
|
---|
22 | {
|
---|
23 | knownDtds.Add(publicIdentifier, localFile);
|
---|
24 | }
|
---|
25 |
|
---|
26 | public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
|
---|
27 | {
|
---|
28 | if (absoluteUri != null && knownDtds.ContainsKey(absoluteUri.AbsoluteUri))
|
---|
29 | {
|
---|
30 | // ignore the known DTDs
|
---|
31 | return Stream.Null;
|
---|
32 | }
|
---|
33 | else if (absoluteUri == null)
|
---|
34 | {
|
---|
35 | // ignore null URIs
|
---|
36 | return Stream.Null;
|
---|
37 | }
|
---|
38 | else
|
---|
39 | {
|
---|
40 | return base.GetEntity(absoluteUri, role, ofObjectToReturn);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override Uri ResolveUri(Uri baseUri, string relativeUri)
|
---|
45 | {
|
---|
46 | if (relativeUri.StartsWith("#"))
|
---|
47 | return null;
|
---|
48 | else if (relativeUri.IndexOf("-//") > -1)
|
---|
49 | return null;
|
---|
50 | else
|
---|
51 | return base.ResolveUri(baseUri, relativeUri);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.