forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXHtmlUrlResolver.cs
79 lines (76 loc) · 2.97 KB
/
XHtmlUrlResolver.cs
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
using CYQ.Data.Properties;
using CYQ.Data.Tool;
using System;
using System.IO;
namespace CYQ.Data.Xml
{
internal class ResolverDtd
{
public static void Resolver(ref System.Xml.XmlDocument xmlDoc)
{
xmlDoc.XmlResolver = XHtmlUrlResolver.Instance;
}
}
internal class XHtmlUrlResolver : System.Xml.XmlUrlResolver
{
private static XHtmlUrlResolver _Resolver = null;
public static XHtmlUrlResolver Instance
{
get
{
if (_Resolver == null)
{
_Resolver = new XHtmlUrlResolver();
}
return _Resolver;
}
}
private string dtdUri = null;
public string DtdUri
{
get
{
if (dtdUri == null)
{
dtdUri = AppConfig.XHtml.DtdUri;
string folder = Path.GetDirectoryName(dtdUri);
//检测文件是否存在
if (!Directory.Exists(folder))//有异常直接抛
{
Directory.CreateDirectory(folder);
}
string[] items = new string[] { "/xhtml1-transitional.dtd", "/xhtml-lat1.ent", "/xhtml-special.ent", "/xhtml-symbol.ent" };
if (!File.Exists(folder + items[0]))
{
using (FileStream fs = File.Create(folder + items[0]))
{
fs.Write(Resources.xhtml1_transitional, 0, Resources.xhtml1_transitional.Length);
}
using (FileStream fs = File.Create(folder + items[1]))
{
fs.Write(Resources.xhtml_lat1, 0, Resources.xhtml_lat1.Length);
}
using (FileStream fs = File.Create(folder + items[2]))
{
fs.Write(Resources.xhtml_special, 0, Resources.xhtml_special.Length);
}
using (FileStream fs = File.Create(folder + items[3]))
{
fs.Write(Resources.xhtml_symbol, 0, Resources.xhtml_symbol.Length);
}
}
}
return dtdUri;
}
}
///assemblyShortName;component/resourceLocation, 例如"/SilverlightLibraryAssembly;component/image.png"。 请注意,需要使用前导斜杠和 component 关键字(后跟一个斜杠)。
public override Uri ResolveUri(Uri baseUri, string relativeUri)
{
if (relativeUri.Contains("xhtml1-transitional.dtd") && DtdUri != null)
{
relativeUri = DtdUri;
}
return base.ResolveUri(baseUri, relativeUri);
}
}
}