-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMLConfigTranslator.cs
435 lines (341 loc) · 17.5 KB
/
XMLConfigTranslator.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace YoctoVisualization
{
public static class XMLConfigTranslator
{
public static string TranslateFromV1(XmlDocument doc)
{
string XmlConfigFile = "<?xml version=\"1.0\" ?>\n<ROOT version='2'>\n";
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{ switch (node.Name)
{
case "Config":
case "PropertiesForm":
case "Sensors": XmlConfigFile += ConfigTranslate(node); break;
case "angularGaugeForm": XmlConfigFile += AngularGaugeTranslate(node); break;
case "GaugeForm": XmlConfigFile += GaugeTranslate(node); break;
case "digitalDisplayForm": XmlConfigFile += DigitalDisplayTranslate(node); break;
case "GraphForm": XmlConfigFile += GraphTranslate(node); break;
}
}
XmlConfigFile += "</ROOT>\n";
// System.IO.File.WriteAllText("c:\\tmp\\config.txt", XmlConfigFile);
return XmlConfigFile;
}
static string ConfigTranslate(XmlNode node)
{
return DumpNode(node, 1);
}
static string GraphTranslate(XmlNode node)
{
string res = " <GraphForm>\n";
foreach (XmlNode n in node.ChildNodes)
{
string prefix = n.Name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
if (prefix == "Graphs_Series") res += SeriesTranslate(n, 2);
else if (prefix == "Graph_AxisY") res += yAxisTranslate(n, 2);
else
switch (n.Name)
{
case "location":
case "size":
case "Form_Text":
case "Graph_showRecordedData":
case "Form_BackColor": res += DumpNode(n, 2); break;
case "Graph_AxisX0": res += xAxisTranslate( n, 2);break;
case "Graph_showNavigator": res +=navigatorTranslate(n, 2); break;
case "Graph_BackColor": res += renameNode(n, "Graph_bgColor1", 2);
res += renameNode(n, "Graph_bgColor2", 2); break;
}
}
return res + " </GraphForm>\n";
}
static string navigatorTranslate(XmlNode node, int indent)
{
string indentation = new String(' ', 2 * indent);
string res = indentation + "<Graph_navigator>\n";
res+= indentation + " <enabled value = \""+node.Attributes["value"].Value+ "\" />\n";
return res + indentation + "</Graph_navigator>\n";
}
static string SeriesTranslate(XmlNode node, int indent)
{
string indentation = new String(' ', 2 * indent);
string prefix = node.Name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
string res = indentation + "<Graph_series" + node.Name.Substring(prefix.Length) + ">\n";
foreach (XmlNode n in node.ChildNodes)
switch (n.Name)
{ case "DataSource_source":
case"DataSource_datatype":
res += DumpNode(n, indent + 1); break;
case "StrokeThickness": res += renameNode(n, "thickness", indent + 1); break;
case "Title": res += renameNode(n, "legend", indent + 1); break;
case "Stroke": res += renameNode(n, "color", indent + 1); break;
case "ScalesYAt": res += renameNode(n, "yAxisIndex", indent + 1); break;
}
return res + indentation + "</Graph_series" + node.Name.Substring(prefix.Length) + ">\n";
}
static string xAxisTranslate(XmlNode node, int indent)
{
string indentation = new String(' ', 2 * indent);
string res = indentation + "<Graph_xAxis>\n";
String fontname = "Arial";
String fontsize = "10";
String color = "Black";
String GridEnabled = "FALSE";
string GridColor = "LightGray";
string GridThickness = "1";
string Title = "";
foreach (XmlNode n in node.ChildNodes)
{ switch (n.Name)
{
case "initialZoom": res += DumpNode(n, 2); break;
case "Foreground": color = n.Attributes["value"].Value;break;
case "FontSize": fontsize = n.Attributes["value"].Value; break;
case "FontFamily": fontname = n.Attributes["value"].Value; break;
case "Title": Title = n.Attributes["value"].Value; break;
case "Separator" : GridEnabled = n.Attributes["value"].Value == "Disabled" ?"FALSE":"TRUE";
GridColor= n.Attributes["Color"].Value;
GridThickness = n.Attributes["Thickness"].Value;
break;
}
}
res = res + indentation + " <Legend>\n"
+ indentation + " <title value=\""+ Title + "\"/>\n"
+ indentation + " </Legend>\n"
+ indentation + " <font>\n"
+ indentation + " <name value=\"" + fontname + "\"/>\n"
+ indentation + " <size value=\"" + (Double.Parse(fontsize) * 0.75).ToString()+ "\"/>\n"
+ indentation + " <color value=\"" + color + "\"/>\n"
+ indentation + " </font>\n"
+ indentation + " <showGrid value=\"" + GridEnabled + "\"/>\n"
+ indentation + " <gridColor value=\"" + GridColor + "\"/>\n"
+ indentation + " <gridThickness value=\"" + GridThickness + "\"/>\n"
+ indentation + " <color value=\"" + color + "\"/>\n";
return res + indentation + "</Graph_xAxis>\n";
}
static string yAxisSectionTranslate(XmlNode node, int indent)
{
string indentation = new String(' ', 2 * indent);
string prefix = node.Name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
string res = indentation + "<zones" + node.Name.Substring(prefix.Length) + ">\n";
double Value = 0;
double SectionWidth = 0;
foreach (XmlNode n in node.ChildNodes)
switch (n.Name)
{
case "Value": Value = Double.Parse(n.Attributes["value"].Value); break;
case "SectionWidth": SectionWidth = Double.Parse(n.Attributes["value"].Value); break;
case "Fill": res += renameNode(n, "color", indent + 1); break;
}
res += indentation + " <visible value =\"" + ((SectionWidth > 0) ? "TRUE" : "FALSE") + "\"/>\n";
res += indentation + " <min value=\"" + Value.ToString() + "\"/>\n";
res += indentation + " <max value=\"" + (Value + SectionWidth).ToString() + "\"/>\n";
return res + indentation + "</zones" + node.Name.Substring(prefix.Length) + ">\n";
}
static string yAxisTranslate(XmlNode node, int indent)
{
string indentation = new String(' ', 2 * indent);
string prefix = node.Name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
string res = indentation + "<Graph_yAxes" + node.Name.Substring(prefix.Length) + ">\n";
String fontname = "Arial";
String fontsize = "10";
String color = "Black";
String GridEnabled = "FALSE";
string GridColor = "LightGray";
string GridThickness = "1";
string Title = "";
foreach (XmlNode n in node.ChildNodes)
{
string prefix2 = n.Name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
if (prefix2 == "Sections") res += yAxisSectionTranslate(n, 2);
else
switch (n.Name)
{
case "Foreground": color = n.Attributes["value"].Value; break;
case "FontSize": fontsize = n.Attributes["value"].Value; break;
case "FontFamily": fontname = n.Attributes["value"].Value; break;
case "Title": Title = n.Attributes["value"].Value; break;
case "Separator":
GridEnabled = n.Attributes["value"].Value == "Disabled" ? "FALSE" : "TRUE";
GridColor = n.Attributes["Color"].Value;
GridThickness = n.Attributes["Thickness"].Value;
break;
}
}
foreach (XmlNode n in node.ChildNodes)
{
switch (n.Name)
{
case "MinValue": res += renameNode(n, "min", 2); break;
case "MaxValue": res += renameNode(n, "max", 2); break;
case "ShowLabels":res += renameNode(n, "visible", 2); break;
}
}
res = res + indentation + "<Legend>\n"
+ indentation + " <title value=\"" + Title + "\"/>\n"
+ indentation + "</Legend>\n"
+ indentation + "<font>\n"
+ indentation + " <name value=\"" + fontname + "\"/>\n"
+ indentation + " <size value=\"" + (Double.Parse(fontsize) * 0.75).ToString() + "\"/>\n"
+ indentation + " <color value=\"" + color + "\"/>\n"
+ indentation + "</font>\n"
+ indentation + "<showGrid value=\"" + GridEnabled + "\"/>\n"
+ indentation + "<gridColor value=\"" + GridColor + "\"/>\n"
+ indentation + "<gridThickness value=\"" + GridThickness + "\"/>\n"
+ indentation + "<color value=\"" + color + "\"/>\n";
return res + indentation + "</Graph_yAxes" + node.Name.Substring(prefix.Length) + ">\n";
}
static string DigitalDisplayTranslate(XmlNode node)
{
string res = " <digitalDisplayForm>\n";
string color = "RGB:FF000000";
foreach (XmlNode n in node.ChildNodes)
if (n.Name == "Label_ForeColor") color = n.Attributes["value"].Value;
foreach (XmlNode n in node.ChildNodes)
{
switch (n.Name)
{
case "location":
case "size":
case "Form_Text":
case "Form_BackColor":
case "DataSource_precision":
case "DataSource_source": res += DumpNode(n, 2); break;
case "Label_Font": res += translateFont(n, "display_font", color, 2); break;
case "Label_MinValue": res += renameNode(n, "display_outOfRangeMin", 2); break;
case "Label_MaxValue": res += renameNode(n, "display_outOfRangeMax", 2); break;
case "Label_OORColor": res += renameNode(n, "display_outOfRangeColorColor", 2); break;
}
}
res += " <display_backgroundColor1 value = \"RGB:00000000\" />\n"
+ " <display_backgroundColor2 value = \"RGB:00000000\" />\n";
return res + " </digitalDisplayForm>\n";
}
static string GaugeTranslate(XmlNode node)
{ string res = " <GaugeForm>\n";
foreach (XmlNode n in node.ChildNodes)
{
switch (n.Name)
{
case "location":
case "size":
case "Form_Text":
case "Form_BackColor":
case "DataSource_precision":
case "DataSource_source": res += DumpNode(n, 2); break;
case "SolidGauge_From": res += renameNode(n, "SolidGauge_min", 2); break;
case "SolidGauge_To": res += renameNode(n, "SolidGauge_max", 2); break;
case "SolidGauge_FromColor": res += renameNode(n, "SolidGauge_color1", 2); break;
case "SolidGauge_ToColor": res += renameNode(n, "SolidGauge_color2", 2); break;
case "SolidGauge_Uses360Mode": res += " <SolidGauge_displayMode value=\"" + (n.Attributes["value"].Value == "TRUE" ? "DISPLAY360" : "DISPLAY180") + "\"/>\n";break;
case "SolidGauge_Font": res += translateFont(n, 2); break;
case "SolidGauge_Stroke": res += renameNode(n, "SolidGauge_borderColor", 2); break;
case "SolidGauge_StrokeThickness": res += renameNode(n, "SolidGauge_borderThickness", 2); break;
case "SolidGauge_GaugeBackground": res += renameNode(n, "SolidGauge_backgroundColor1", 2);
res += renameNode(n, "SolidGauge_backgroundColor2", 2);
break;
}
}
res += " <SolidGauge_thickness value = \"40\" />\n";
return res + " </GaugeForm>\n";
}
static string AngularGaugeSectionTranslate(XmlNode node, int indent)
{
string indentation = new String(' ', 2 * indent);
string prefix = node.Name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
string res = indentation + "<AngularGauge_zones" + node.Name.Substring(prefix.Length) + ">\n";
double start = 0;
double to = 0;
foreach (XmlNode n in node.ChildNodes)
switch (n.Name)
{
case "FromValue": start = Double.Parse(n.Attributes["value"].Value); res += renameNode(n, "min", indent + 1); break;
case "ToValue": to = Double.Parse(n.Attributes["value"].Value); res += renameNode(n, "max", indent + 1); break;
case "Fill": res += renameNode(n, "color", indent + 1); break;
}
res += indentation + " <visible value=\"" + ((start != to) ? "TRUE" : "FALSE") + "\"/>\n";
return res + indentation + "</AngularGauge_zones" + node.Name.Substring(prefix.Length) + ">\n";
}
static string AngularGaugeTranslate(XmlNode node)
{
string res = " <angularGaugeForm>\n";
foreach (XmlNode n in node.ChildNodes)
{
string prefix = n.Name.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
if (prefix == "AngularGauge_Sections") res += AngularGaugeSectionTranslate(n, 2);
else
switch (n.Name)
{
case "location":
case "size":
case "Form_Text":
case "Form_BackColor":
case "DataSource_source": res += DumpNode(n, 2); break;
case "SolidGauge_FromValue": res += renameNode(n, "AngularGauge_min", 2); break;
case "AngularGauge_ToValue": res += renameNode(n, "AngularGauge_max", 2); break;
case "AngularGauge_LabelsStep": res += renameNode(n, "AngularGauge_graduation", 2); break;
case "AngularGauge_TicksStrokeThickness": res += renameNode(n, "AngularGauge_graduationThickness", 2);
res += renameNode(n, "AngularGauge_subgraduationThickness", 2);
break;
case "AngularGauge_TicksForeground": res += renameNode(n, "AngularGauge_graduationColor", 2);
res += renameNode(n, "AngularGauge_subgraduationColor", 2);
break;
case "AngularGauge_NeedleFill": res += renameNode(n, "AngularGauge_needleColor", 2); break;
}
}
res += " <AngularGauge_graduationFont >\n"
+ " <name value = \"Arial\" />\n"
+ " <size value = \"10\" />\n"
+ " <color value = \"Black\" />\n"
+ " <italic value = \"FALSE\" />\n"
+ " <bold value = \"TRUE\" />\n"
+ " </AngularGauge_graduationFont>\n"
+ " <AngularGauge_needleWidth value = \"10\"/>\n"
+ " <AngularGauge_borderThickness value = \"1\"/>\n"
+ " <AngularGauge_borderThickness value = \"1\"/>\n"
+ " <AngularGauge_backgroundColor1 value = \"RGB:00000000\"/>\n"
+ " <AngularGauge_backgroundColor2 value = \"RGB:00000000\"/>\n"
+ " <AngularGauge_borderColor value = \"DarkGray\"/>\n"
+ " <AngularGauge_needleContourThickness value = \"0\"/>\n";
return res + " </angularGaugeForm>\n";
}
static string translateFont(XmlNode n, int indent)
{ return translateFont(n, n.Name, "", indent); }
static string translateFont (XmlNode n, string newname, string color, int indent)
{
string indentation = new String(' ', 2 * indent);
string res = indentation + "<" + newname + ">\n";
res += indentation + " <name value=\"" + n.Attributes["value"].Value + "\"/>\n";
res += indentation + " <size value=\"" + n.Attributes["size"].Value + "\"/>\n";
if (color!="") res += indentation + " <color value=\"" + color + "\"/>\n";
res += indentation + " <italic value=\"" + (n.Attributes["style"].Value.Contains("Italic")?"TRUE":"FALSE") + "\"/>\n";
res += indentation + " <bold value=\"" + (n.Attributes["size"].Value.Contains("Italic") ? "TRUE" : "FALSE")+ "\"/>\n";
return res + indentation+ "</" + newname + ">\n"; ;
}
static string renameNode(XmlNode n, string newname, int indent)
{
return new String(' ', 2 * indent)+ "<"+newname+" value=\""+ System.Security.SecurityElement.Escape(n.Attributes["value"].Value) + "\"/>\n";
}
static string DumpNode(XmlNode n, int indent)
{ return DumpNode( n, n.Name, indent); }
static string DumpNode(XmlNode n, string newName, int indent)
{ string indentation = new String(' ', 2*indent);
string res = indentation + "<"+ newName;
foreach (XmlAttribute a in n.Attributes)
{ res += ' ' + a.Name + "=\"" + System.Security.SecurityElement.Escape(a.Value) + "\"";
}
if (n.ChildNodes.Count == 0) res += "/>\n";
else
{
res += ">\n";
foreach (XmlNode s in n.ChildNodes) res += DumpNode(s, indent + 1);
res += indentation + "</" + newName + ">\n";
}
return res;
}
}
}