-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProcessPart.cs
291 lines (266 loc) · 10.4 KB
/
ProcessPart.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
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
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.Design.Serialization;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Xml;
namespace OpenKNXproducer
{
public class ParamEntry
{
private string mValue;
private bool mIsNumeric;
private int mValueInt;
public string Value
{
get { return mValue; }
set
{
mValue = value;
mIsNumeric = int.TryParse(value, out mValueInt);
}
}
public string Name;
public bool WasReplaced;
public bool IsNumeric { get { return mIsNumeric; } }
public int ValueInt { get { return mValueInt; } }
public int Digits;
public int Increment = 1;
public bool AsLetter = false;
private readonly bool IsList = false;
private readonly string[] mValues;
private int mValuesPosition;
public ParamEntry(string iName, XmlNode iNode)
{
Name = iName;
Value = iNode.NodeAttr("value");
if (!int.TryParse(iNode.NodeAttr("digits", "0"), out Digits))
Digits = 0;
if (!int.TryParse(iNode.NodeAttr("increment", "1"), out Increment))
Increment = 1;
AsLetter = iNode.NodeAttr("asLetter") == "true";
string lList = iNode.NodeAttr("list", " ");
string lValues = iNode.NodeAttr("values");
if (lValues != "")
{
mValues = lValues.Split(lList);
IsList = true;
mValuesPosition = 0;
Value = mValues[0];
}
}
public string FormattedValue(int iOffset)
{
string lResult = mValue;
if (IsNumeric)
{
int lValue = mValueInt + iOffset;
if (AsLetter)
{
// be careful: A is 0!!!
lResult = "";
lValue++;
while (--lValue >= 0)
{
lResult = (char)('A' + lValue % 26) + lResult;
lValue /= 26;
}
lResult = lResult.PadLeft(Digits, 'A');
}
else
{
lResult = lValue.ToString(Digits == 0 ? "D" : $"D{Digits}");
}
}
return lResult;
}
public void Next()
{
if (IsList)
{
mValuesPosition = (mValuesPosition + Increment) % mValues.Length;
Value = mValues[mValuesPosition];
}
else if (IsNumeric)
{
mValueInt += Increment;
}
}
}
public class ProcessPart
{
private readonly static Dictionary<string, ProcessPart> sParts = new();
private readonly Dictionary<string, ParamEntry> mParams = new();
readonly XmlNode mPartNode;
readonly XmlDocument[] mDocuments;
private ProcessInclude mInclude;
public string IncludeName { get { return mInclude.XmlFileName; } }
readonly string mName;
public readonly int Instances;
public bool AddParam(XmlNode iNode)
{
bool lResult = false;
string lName = iNode.NodeAttr("name");
lName = lName.Trim('%');
lName = lName.Trim();
lName = $"%{lName}%";
if (lName != "" && !mParams.ContainsKey(lName))
{
mParams[lName] = new(lName, iNode);
lResult = true;
}
return lResult;
}
public void ParseParams(XmlNodeList iParamNodes)
{
// config consists of a list of name-value pairs to be replaced in document
foreach (XmlNode lNode in iParamNodes)
{
if (lNode.NodeType == XmlNodeType.Comment) continue;
AddParam(lNode);
}
}
protected ProcessPart(XmlNode iIncludeNode, string iName, int iInstances)
{
mPartNode = iIncludeNode;
mName = iName;
Instances = iInstances;
mDocuments = new XmlDocument[Instances];
sParts.Add(iName, this);
}
public static bool ParseInstance(XmlNode iNode, out string eName, out int eInstance)
{
eName = iNode.NodeAttr("name");
bool lResult = int.TryParse(iNode.NodeAttr("instance", "1"), out eInstance);
return lResult && eName != "";
}
public static ProcessPart GetPart(XmlNode iPartNode)
{
string lName = iPartNode.NodeAttr("name");
return GetPart(lName);
}
public static ProcessPart GetPart(string iName)
{
ProcessPart lPart = null;
if (sParts.ContainsKey(iName))
lPart = sParts[iName];
return lPart;
}
public static ProcessPart Factory(XmlNode iPartNode)
{
ProcessPart lPart;
string lName = iPartNode.NodeAttr("name");
if (sParts.ContainsKey(lName))
lPart = sParts[lName];
else
{
_ = int.TryParse(iPartNode.NodeAttr("instances", "1"), out int lInstances);
lPart = new(iPartNode, lName, lInstances);
}
return lPart;
}
public static void Init(XmlNode iPartNode, ProcessInclude iInclude)
{
ProcessPart lPart = Factory(iPartNode);
lPart.mInclude = iInclude;
for (int lInstance = 0; lInstance < lPart.Instances; lInstance++)
lPart.mDocuments[lInstance] = (XmlDocument)iInclude.GetDocument().CloneNode(true);
lPart.Process();
}
public static XmlDocument GetDocument(XmlNode iIncludeNode)
{
// instances in xml are 1-based
bool lSuccess = ParseInstance(iIncludeNode, out string lName, out int lInstance);
XmlDocument lDocument = null;
if (sParts.ContainsKey(lName))
{
ProcessPart lPart = sParts[lName];
if (lInstance > 0 && lInstance <= lPart.Instances)
lDocument = lPart.mDocuments[lInstance - 1];
}
return lDocument;
}
private static void ReplaceAttribute(XmlAttribute iAttr, ParamEntry iParam)
{
string lAttr = iAttr.Value.Replace(iParam.Name, iParam.FormattedValue(0));
if (iParam.IsNumeric)
{
bool lContinue;
Regex lParamRegex = new(iParam.Name[..^1] + @"([+-]\d{1,3})%");
do
{
Match lMatch = lParamRegex.Match(lAttr);
lContinue = lMatch.Success;
if (lContinue)
{
int lOffset = int.Parse(lMatch.Groups[1].Value);
lAttr = lAttr.Replace(lMatch.Captures[0].Value, iParam.FormattedValue(lOffset));
}
} while (lContinue);
}
iAttr.Value = lAttr;
}
public void Process()
{
XmlNamespaceManager nsmgr = new(mPartNode.OwnerDocument.NameTable);
nsmgr.AddNamespace("op", ProcessInclude.cOwnNamespace);
// first parse all params
XmlNodeList lParamNodes = mPartNode.SelectNodes("//op:param", nsmgr);
ParseParams(lParamNodes);
// now replace them in part include
for (int lInstance = 0; lInstance < Instances; lInstance++)
{
foreach (var lEntry in mParams)
{
ParamEntry lParam = lEntry.Value;
XmlNodeList lAttrs = mDocuments[lInstance].SelectNodes($"//*/@*[contains(.,'{lParam.Name[..^1]}')]");
foreach (XmlAttribute lAttr in lAttrs)
ReplaceAttribute(lAttr, lParam);
lParam.Next();
}
}
}
public static List<XmlNode> Preprocess(XmlNodeList iNodes, XmlDocument iDocument)
{
List<XmlNode> lIncludeNodes = new();
foreach (XmlNode lNode in iNodes)
{
lIncludeNodes.Add(lNode);
if (lNode.LocalName == "part")
{
// we found a part declaration
// at this point there is no include available, but we need the instances information
ProcessPart.Factory(lNode);
}
else if (lNode.LocalName == "usePart")
{
string lName = lNode.NodeAttr("name");
string lInstance = lNode.NodeAttr("instance");
if (lName != "" && lInstance == "")
{
// We have a usepart include without a specific instance
ProcessPart lPart = ProcessPart.GetPart(lName);
int lInstances = lPart.Instances;
_ = int.TryParse(lNode.NodeAttr("instanceFrom", "1"), out int lFrom);
_ = int.TryParse(lNode.NodeAttr("instanceTo", lInstances.ToString()), out int lTo);
// we change the current include node to a node including the first instance
XmlAttribute lAttribute = iDocument.CreateAttribute("instance");
lAttribute.Value = lFrom.ToString();
lNode.Attributes.Append(lAttribute);
XmlNode lLastInsert = lNode;
// now we add all remaining instances as includes
for (int lCount = lFrom + 1; lCount <= lTo; lCount++)
{
XmlNode lNewInclude = lNode.CloneNode(true);
// XmlNode lNewInclude = lNode.OwnerDocument.ImportNode(lNode, true);
lNode.ParentNode.InsertAfter(lNewInclude, lLastInsert);
lNewInclude.Attributes["instance"].Value = lCount.ToString();
lIncludeNodes.Add(lNewInclude);
lLastInsert = lNewInclude;
}
}
}
}
return lIncludeNodes;
}
}
}