-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZtConfig.cls
303 lines (232 loc) · 12.4 KB
/
ZtConfig.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ZtConfig"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Class ZtConfig.
' It preserves all configuration informations and methods to read them from ZtConfig.xml.
' It distributes these informations and work to ZtConfigBasic, ZtConfigUser, and ZtConfigFinal.
'
' Zotero Tools.
' This software is under Revised ('New') BSD license.
' Copyright © 2019, Olaf Ahrens. All rights reserved.
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Private constants.
Private Const PVT_CONFIG_FILE_NAME As String = "ZtConfig.xml"
Private Const PVT_CONTROL_CHARACTER_REGEX_PATTERN As String = "(?<signsBefore>^|[^\\](?:\\{2})*)(?<whole>\{Chr\((?:(?:x(?<hexNumber>[\dabcdefABCDEF]+))|(?<decNumber>\d+)\)\}))"
Private Const PVT_RESOLVE_PROPERTY_REGEX_PATTERN As String = "(?<signsBefore>^|[^\\](?:\\{2})*)(?<whole>\{(?<propertyString>[a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+){2,3})\})"
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Private variables.
Private pvtMessageDisplay As ZtIMessageDisplayable
Private pvtBasic As ZtConfigBasic
Private pvtUserStylePresets As Collection
Private pvtUser As ZtConfigUser
Private pvtXml As MSXML2.DOMDocument60
Private pvtControlCharacterRegExp As ZtRegExp
Private pvtResolvePropertyRegExp As ZtRegExp
Private pvtFinal As ZtConfigFinal
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Constructor.
Private Sub Class_Initialize()
Set pvtXml = New MSXML2.DOMDocument60
Set pvtUserStylePresets = New Collection
Set pvtControlCharacterRegExp = New ZtRegExp
Set pvtResolvePropertyRegExp = New ZtRegExp
Set pvtBasic = New ZtConfigBasic
Set pvtUser = New ZtConfigUser
End Sub
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Friend/Public procedures and properties.
' All members that should be callable by CallByName procedure must be public.
Friend Sub Initialize(ByVal valMessageDisplay As ZtIMessageDisplayable)
Dim locXmlPathFileName As String
Set pvtMessageDisplay = valMessageDisplay
locXmlPathFileName = ThisDocument.Path & "\" & PVT_CONFIG_FILE_NAME
pvtXml.Load locXmlPathFileName
pvtXml.preserveWhiteSpace = True
pvtControlCharacterRegExp.Initialize "XML control characters", _
PVT_CONTROL_CHARACTER_REGEX_PATTERN, _
pvtMessageDisplay, _
False, _
True, _
True
pvtResolvePropertyRegExp.Initialize "XML resolve property", _
PVT_RESOLVE_PROPERTY_REGEX_PATTERN, _
pvtMessageDisplay, _
False, _
True, _
True
' Set basic config.
pvtBasic.Initialize Me, pvtXml
' Set user config.
pvtUser.Initialize Me, pvtXml
End Sub
Public Property Get Basic() As ZtConfigBasic
Set Basic = pvtBasic
End Property
Public Property Get User() As ZtConfigUser
Set User = pvtUser
End Property
Public Property Get Final() As ZtConfigFinal
Set Final = pvtFinal
End Property
Friend Property Get UserStylePresets() As Collection
Set UserStylePresets = pvtUserStylePresets
End Property
Friend Sub KeepUserStylePresets()
Dim locNode As MSXML2.IXMLDOMNode
Dim locPreset0 As ZtConfigUserStylePreset
Dim locPreset1 As ZtConfigUserStylePreset
Dim locCtr As Integer
For Each locNode In pvtXml.SelectNodes("/zoteroTools/user/stylePreset")
Set locPreset0 = New ZtConfigUserStylePreset
locPreset0.Initialize Me, locNode
If pvtUserStylePresets.Count = 0 Then
pvtUserStylePresets.Add locPreset0, locNode.Attributes.getNamedItem("name").Text
Else
locCtr = 1
For Each locPreset1 In pvtUserStylePresets
If locPreset0.Name < locPreset1.Name Then
Exit For
End If
locCtr = locCtr + 1
Next
If locCtr <= pvtUserStylePresets.Count Then
pvtUserStylePresets.Add locPreset0, locNode.Attributes.getNamedItem("name").Text, locCtr
Else
pvtUserStylePresets.Add locPreset0, locNode.Attributes.getNamedItem("name").Text
End If
End If
Next
End Sub
Friend Sub KeepFinal()
Set pvtFinal = New ZtConfigFinal
pvtFinal.Initialize Me, pvtXml
End Sub
Friend Sub RegExpFactory(ByRef refRegExp As ZtRegExp, ByVal valName As String, ByVal valPattern As String, _
Optional ByVal valGlobal As Boolean = False, Optional ByVal valIgnoreCase As Boolean = False, Optional ByVal valMultiLine As Boolean = False)
Set refRegExp = New ZtRegExp
refRegExp.Initialize valName, valPattern, pvtMessageDisplay, valGlobal, valIgnoreCase, valMultiLine
End Sub
Friend Function ResolveTextElement(ByVal valElement As MSXML2.IXMLDOMNode, Optional ByVal valClearTabAndLineBreak As Boolean = False) As String
Dim locResult As String
Dim locMatches As Collection
Dim locMatch As ZtRegMatch
locResult = valElement.Text
If valClearTabAndLineBreak Then
locResult = Replace(Replace(Replace(locResult, Chr$(9), vbNullString), Chr$(10), vbNullString), Chr$(13), vbNullString)
End If
Set locMatches = pvtControlCharacterRegExp.Execute(locResult)
Do While locMatches.Count > 0
For Each locMatch In locMatches
With locMatch
If Len(.Groups("hexNumber")) > 0 Then
locResult = Left$(locResult, .FirstIndex + Len(.Groups("signsBefore"))) & _
ChrW$(CLng("&H" & .Groups("hexNumber"))) & _
Right$(locResult, Len(locResult) - .FirstIndex - .Length)
Else
locResult = Left$(locResult, .FirstIndex + Len(.Groups("signsBefore"))) & _
ChrW$(CLng(.Groups("decNumber"))) & _
Right$(locResult, Len(locResult) - .FirstIndex - .Length)
End If
End With
Exit For
Next
Set locMatches = pvtControlCharacterRegExp.Execute(locResult)
Loop
Set locMatches = pvtResolvePropertyRegExp.Execute(locResult)
Do While locMatches.Count > 0
For Each locMatch In locMatches
With locMatch
locResult = Left$(locResult, .FirstIndex + Len(.Groups("signsBefore"))) & _
CStr(pvtResolvePropertyScript(Me, .Groups("propertyString"))) & _
Right$(locResult, Len(locResult) - .FirstIndex - .Length)
End With
Exit For
Next
Set locMatches = pvtResolvePropertyRegExp.Execute(locResult)
Loop
locResult = Replace(locResult, "\{", "{")
ResolveTextElement = locResult
End Function
Friend Sub RegPieceFactoryFromString(ByVal valText As String, ByVal valName As String, ByRef refRegPiece As ZtRegPiece)
Dim locSpecialChars As String
Dim locCharCtr As Integer
Dim locChar As String
Dim locText As String
locText = valText
locSpecialChars = "\?+^$()[]{}.|"
For locCharCtr = 1 To Len(locSpecialChars)
locChar = Mid$(locSpecialChars, locCharCtr, 1)
locText = Replace(locText, locChar, "\" & locChar)
Next
Me.RegPieceFactory locText, valName, refRegPiece
End Sub
Friend Sub RegPieceFactory(ByVal valTextWithRegExpSyntax As String, ByVal valName As String, ByRef refRegPiece As ZtRegPiece)
If refRegPiece Is Nothing Then
Set refRegPiece = New ZtRegPiece
refRegPiece.Initialize valTextWithRegExpSyntax, valName, pvtMessageDisplay
End If
End Sub
Friend Function GetGivenNameStyle(ByVal valEnumName As String) As ZtEGivenNameStyle
Select Case valEnumName
Case "GivenNameNone": GetGivenNameStyle = GivenNameNone
Case "GivenNameAbbreviated": GetGivenNameStyle = GivenNameAbbreviated
Case "GivenNameFull": GetGivenNameStyle = GivenNameFull
End Select
End Function
Friend Function GetWordStoryTypes(ByRef refEnumNames As Variant) As WdStoryType()
Dim locCtr As Integer
Dim locTypes() As WdStoryType
ReDim locTypes(UBound(refEnumNames))
For locCtr = 0 To UBound(refEnumNames)
Select Case refEnumNames(locCtr)
Case "wdCommentsStory": locTypes(locCtr) = wdCommentsStory
Case "wdEndnoteContinuationNoticeStory": locTypes(locCtr) = wdEndnoteContinuationNoticeStory
Case "wdEndnoteContinuationSeparatorStory": locTypes(locCtr) = wdEndnoteContinuationSeparatorStory
Case "wdEndnoteSeparatorStory": locTypes(locCtr) = wdEndnoteSeparatorStory
Case "wdEndnotesStory": locTypes(locCtr) = wdEndnotesStory
Case "wdEvenPagesFooterStory": locTypes(locCtr) = wdEvenPagesFooterStory
Case "wdEvenPagesHeaderStory": locTypes(locCtr) = wdEvenPagesHeaderStory
Case "wdFirstPageFooterStory": locTypes(locCtr) = wdFirstPageFooterStory
Case "wdFirstPageHeaderStory": locTypes(locCtr) = wdFirstPageHeaderStory
Case "wdFootnoteContinuationNoticeStory": locTypes(locCtr) = wdFootnoteContinuationNoticeStory
Case "wdFootnoteContinuationSeparatorStory": locTypes(locCtr) = wdFootnoteContinuationSeparatorStory
Case "wdFootnoteSeparatorStory": locTypes(locCtr) = wdFootnoteSeparatorStory
Case "wdFootnotesStory": locTypes(locCtr) = wdFootnotesStory
Case "wdMainTextStory": locTypes(locCtr) = wdMainTextStory
Case "wdPrimaryFooterStory": locTypes(locCtr) = wdPrimaryFooterStory
Case "wdPrimaryHeaderStory": locTypes(locCtr) = wdPrimaryHeaderStory
Case "wdTextFrameStory": locTypes(locCtr) = wdTextFrameStory
End Select
Next
GetWordStoryTypes = locTypes
End Function
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Private procedures and properties.
Private Function pvtResolvePropertyScript(ByVal valObject As Object, ByVal valPropertyList As String) As Variant
Dim locResult As Variant
Dim locFirstElementEndPosition As Integer
Dim locPropertyList As String
locPropertyList = valPropertyList
locFirstElementEndPosition = InStr(locPropertyList, ".")
If locFirstElementEndPosition = 0 Then
locResult = CallByName(valObject, locPropertyList, VbGet)
pvtResolvePropertyScript = locResult
Else
Set locResult = CallByName(valObject, Left$(locPropertyList, locFirstElementEndPosition - 1), VbGet)
pvtResolvePropertyScript = pvtResolvePropertyScript(locResult, Right$(locPropertyList, Len(locPropertyList) - locFirstElementEndPosition))
End If
End Function
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *