-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplacerContext.cs
188 lines (170 loc) · 5.92 KB
/
ReplacerContext.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
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
using System;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
namespace wordbookmark2pdf
{
/// <summary>
/// 上下文基类
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class ReplacerContext<T>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="filepath">模板路径</param>
/// <param name="targetDir">存放文件夹</param>
protected ReplacerContext(string filepath, string targetDir)
{
DestFolder = targetDir;
SrcFilePath = filepath;
}
/// <summary>
/// 上下文自定义对象
/// </summary>
public T Model { get; set; }
/// <summary>
/// 原模板路径
/// </summary>
public string SrcFilePath { get; private set; }
/// <summary>
/// 目标文件夹
/// </summary>
public string DestFolder { get; private set; }
/// <summary>
/// 原模板不带后缀文件名
/// </summary>
public string SrcFileNameWithoutExt { get { return Path.GetFileNameWithoutExtension(SrcFilePath); } }
/// <summary>
/// 结果文件不带后缀文件名
/// </summary>
public string DestFileNameWithoutExt { get { return Path.GetFileNameWithoutExtension(DestFilePath); } }
/// <summary>
/// 结果文件路径
/// </summary>
public string DestFilePath { get { return Path.Combine(DestFolder, $"{Path.GetFileName(SrcFilePath)}"); } }
/// <summary>
/// 书签名称
/// </summary>
public string BookmarkName { get; protected internal set; }
/// <summary>
/// 书签图片名称
/// </summary>
public string BookmarkPicUri { get; protected internal set; }
/// <summary>
/// 书签sql
/// </summary>
public string BookmarkSql { get; protected internal set; }
/// <summary>
/// 书签文本
/// </summary>
public string BookmarkText { get; protected internal set; }
/// <summary>
/// 书签配置文件解析
/// </summary>
/// <param name="logger">日志记录器</param>
/// <returns></returns>
public virtual BookMarkConfs EnsureBookMarkConf(Action<string> logger)
{
string bookMarkConfig = BookMarkConfigPath;
if (!File.Exists(bookMarkConfig))
{
logger?.Invoke($"配置文件:{bookMarkConfig}不存在");
return null;
}
var xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(bookMarkConfig);
logger?.Invoke($"配置文件:{bookMarkConfig}加载成功");
}
catch (Exception e)
{
logger?.Invoke(e.Message);
Console.WriteLine(e);
return null;
}
var bookMarkConfs = new BookMarkConfs();
var doc = xmlDoc.DocumentElement;
if (doc == null
|| doc.ChildNodes.Count == 0)
{
logger?.Invoke($"配置文件:{bookMarkConfig}不存在任何节点信息");
return null;
}
foreach (XmlNode nodeMatch in doc.ChildNodes)
{
if (nodeMatch.Attributes == null) continue;
if (nodeMatch.ChildNodes.Count > 0)
{
var templName = nodeMatch.Attributes["name"].Value;
var bookMarks = new BookMarks(templName);
bookMarkConfs.Confs.Add(templName, bookMarks);
foreach (XmlNode node in nodeMatch.ChildNodes)
{
if (node.Attributes == null) continue;
var markname = node.Attributes["name"].Value.ToUpper();
var type = node.Attributes["type"].Value.ToUpper();
var bookMark = new BookMark(type);
bookMarks.Marks.Add(markname, bookMark);
bookMark.Text = node.Attributes["text"].Value;
bookMark.Sql = node.Attributes["sql"].Value;
bookMark.PicUri = node.Attributes["url"].Value;
}
}
}
return bookMarkConfs;
}
/// <summary>
/// 书签配置文件路径
/// </summary>
protected virtual string BookMarkConfigPath
{
get { return "BookMarkConf.xml"; }
}
/// <summary>
/// 预处理书签文本(比如常用宏的处理@SYS.TIME:'yyyyMMdd')
/// </summary>
/// <param name="text">书签文本</param>
/// <returns>书签文本</returns>
public virtual string PreprocessText(string text)
{
if (!string.IsNullOrEmpty(text))
{
var pattern1 = @"(?<=@SYS.TIME:').+?(?=')";
foreach (Match match in Regex.Matches(text, pattern1))
{
var fmt = match.Value;
var time = $"{DateTime.Now.ToString(fmt)}";
text = text.Replace($"@SYS.TIME:'{fmt}'", time);// 系统当前时间
}
}
return text;
}
/// <summary>
/// 预处理书签sql
/// </summary>
/// <param name="sql">书签sql</param>
/// <returns>书签sql</returns>
public virtual string PreprocessSql(string sql)
{
return sql;
}
/// <summary>
/// 预处理书签图片URL
/// </summary>
/// <param name="url">书签图片URL</param>
/// <returns>书签图片URL</returns>
public virtual string PreprocessPicUri(string url)
{
return url;
}
/// <summary>
/// 书签sql执行使用的数据库执行命令(无sql执行可以返回空,建议实现)
/// </summary>
/// <returns></returns>
public abstract IDbCommand CreateCommand();
}
}