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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Util
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WixToolset.Data;
using WixToolset.Data.WindowsInstaller;
using WixToolset.Extensibility;
using WixToolset.Util.Symbols;
/// <summary>
/// The decompiler for the WiX Toolset Utility Extension.
/// </summary>
internal sealed class UtilDecompiler : BaseWindowsInstallerDecompilerExtension
{
public override IReadOnlyCollection<TableDefinition> TableDefinitions => UtilTableDefinitions.All;
private static readonly Dictionary<string, XName> CustomActionMapping = new Dictionary<string, XName>()
{
{ "Wix4BroadcastEnvironmentChange_X86", UtilConstants.BroadcastEnvironmentChange },
{ "Wix4BroadcastEnvironmentChange_X64", UtilConstants.BroadcastEnvironmentChange },
{ "Wix4BroadcastEnvironmentChange_ARM64", UtilConstants.BroadcastEnvironmentChange },
{ "Wix4BroadcastSettingChange_X86", UtilConstants.BroadcastSettingChange },
{ "Wix4BroadcastSettingChange_X64", UtilConstants.BroadcastSettingChange },
{ "Wix4BroadcastSettingChange_ARM64", UtilConstants.BroadcastSettingChange },
{ "Wix4CheckRebootRequired_X86", UtilConstants.CheckRebootRequired },
{ "Wix4CheckRebootRequired_X64", UtilConstants.CheckRebootRequired },
{ "Wix4CheckRebootRequired_ARM64", UtilConstants.CheckRebootRequired },
{ "Wix4QueryNativeMachine_X86", UtilConstants.QueryNativeMachine },
{ "Wix4QueryNativeMachine_X64", UtilConstants.QueryNativeMachine },
{ "Wix4QueryNativeMachine_ARM64", UtilConstants.QueryNativeMachine },
{ "Wix4QueryOsDriverInfo_X86", UtilConstants.QueryWindowsDriverInfo },
{ "Wix4QueryOsDriverInfo_X64", UtilConstants.QueryWindowsDriverInfo },
{ "Wix4QueryOsDriverInfo_ARM64", UtilConstants.QueryWindowsDriverInfo },
{ "Wix4QueryOsInfo_X86", UtilConstants.QueryWindowsSuiteInfo },
{ "Wix4QueryOsInfo_X64", UtilConstants.QueryWindowsSuiteInfo },
{ "Wix4QueryOsInfo_ARM64", UtilConstants.QueryWindowsSuiteInfo },
};
private IReadOnlyCollection<string> customActionNames;
/// <summary>
/// Called at the beginning of the decompilation of a database.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
public override void PreDecompileTables(TableIndexedCollection tables)
{
this.RememberCustomActionNames(tables);
this.CleanupSecureCustomProperties(tables);
this.CleanupInternetShortcutRemoveFileTables(tables);
}
private void RememberCustomActionNames(TableIndexedCollection tables)
{
var customActionTable = tables["CustomAction"];
this.customActionNames = customActionTable?.Rows.Select(r => r.GetPrimaryKey()).Distinct().ToList() ?? (IReadOnlyCollection<string>)Array.Empty<string>();
}
/// <summary>
/// Decompile the SecureCustomProperties field to PropertyRefs for known extension properties.
/// </summary>
/// <remarks>
/// If we've referenced any of the suite or directory properties, add
/// a PropertyRef to refer to the Property (and associated custom action)
/// from the extension's library. Then remove the property from
/// SecureCustomExtensions property so later decompilation won't create
/// new Property elements.
/// </remarks>
/// <param name="tables">The collection of all tables.</param>
private void CleanupSecureCustomProperties(TableIndexedCollection tables)
{
var propertyTable = tables["Property"];
if (null != propertyTable)
{
foreach (var row in propertyTable.Rows)
{
if ("SecureCustomProperties" == row[0].ToString())
{
var remainingProperties = new StringBuilder();
var secureCustomProperties = row[1].ToString().Split(';');
foreach (var property in secureCustomProperties)
{
if (property.StartsWith("WIX_SUITE_", StringComparison.Ordinal) || property.StartsWith("WIX_DIR_", StringComparison.Ordinal)
|| property.StartsWith("WIX_ACCOUNT_", StringComparison.Ordinal))
{
this.DecompilerHelper.AddElementToRoot("PropertyRef", new XAttribute("Id", property));
}
else
{
if (0 < remainingProperties.Length)
{
remainingProperties.Append(";");
}
remainingProperties.Append(property);
}
}
row[1] = remainingProperties.ToString();
break;
}
}
}
}
/// <summary>
/// Remove RemoveFile rows that the InternetShortcut compiler extension adds for us.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
private void CleanupInternetShortcutRemoveFileTables(TableIndexedCollection tables)
{
// index the WixInternetShortcut table
var wixInternetShortcutTable = tables["WixInternetShortcut"];
var wixInternetShortcuts = new Hashtable();
if (null != wixInternetShortcutTable)
{
foreach (var row in wixInternetShortcutTable.Rows)
{
wixInternetShortcuts.Add(row.GetPrimaryKey(), row);
}
}
// remove the RemoveFile rows with primary keys that match the WixInternetShortcut table's
var removeFileTable = tables["RemoveFile"];
if (null != removeFileTable)
{
for (var i = removeFileTable.Rows.Count - 1; 0 <= i; i--)
{
if (null != wixInternetShortcuts[removeFileTable.Rows[i][0]])
{
removeFileTable.Rows.RemoveAt(i);
}
}
}
}
/// <summary>
/// Decompiles an extension table.
/// </summary>
/// <param name="table">The table to decompile.</param>
public override bool TryDecompileTable(Table table)
{
switch (table.Name)
{
case "WixCloseApplication":
case "Wix4CloseApplication":
this.DecompileWixCloseApplicationTable(table);
break;
case "WixRemoveFolderEx":
case "Wix4RemoveFolderEx":
this.DecompileWixRemoveFolderExTable(table);
break;
case "WixRestartResource":
case "Wix4RestartResource":
this.DecompileWixRestartResourceTable(table);
break;
case "FileShare":
case "Wix4FileShare":
this.DecompileFileShareTable(table);
break;
case "FileSharePermissions":
case "Wix4FileSharePermissions":
this.DecompileFileSharePermissionsTable(table);
break;
case "WixInternetShortcut":
case "Wix4InternetShortcut":
this.DecompileWixInternetShortcutTable(table);
break;
case "Group":
case "Wix4Group":
this.DecompileGroupTable(table);
break;
case "Group6":
case "Wix6Group":
this.DecompileGroup6Table(table);
break;
case "GroupGroup":
case "Wix6GroupGroup":
this.DecompileGroupGroup6Table(table);
break;
case "Perfmon":
case "Wix4Perfmon":
this.DecompilePerfmonTable(table);
break;
case "PerfmonManifest":
case "Wix4PerfmonManifest":
this.DecompilePerfmonManifestTable(table);
break;
case "EventManifest":
case "Wix4EventManifest":
this.DecompileEventManifestTable(table);
break;
case "SecureObjects":
case "Wix4SecureObjects":
this.DecompileSecureObjectsTable(table);
break;
case "ServiceConfig":
case "Wix4ServiceConfig":
this.DecompileServiceConfigTable(table);
break;
case "User":
case "Wix4User":
this.DecompileUserTable(table);
break;
case "UserGroup":
case "Wix4UserGroup":
this.DecompileUserGroupTable(table);
break;
case "XmlConfig":
case "Wix4XmlConfig":
this.DecompileXmlConfigTable(table);
break;
case "XmlFile":
case "Wix4XmlFile":
// XmlFile decompilation has been moved to FinalizeXmlFileTable function
break;
default:
return false;
}
return true;
}
/// <summary>
/// Finalize decompilation.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
public override void PostDecompileTables(TableIndexedCollection tables)
{
this.FinalizeCustomActions();
this.FinalizePerfmonTable(tables);
this.FinalizePerfmonManifestTable(tables);
this.FinalizeSecureObjectsTable(tables);
this.FinalizeServiceConfigTable(tables);
this.FinalizeXmlConfigTable(tables);
this.FinalizeXmlFileTable(tables);
this.FinalizeEventManifestTable(tables);
}
/// <summary>
/// Decompile the WixCloseApplication table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileWixCloseApplicationTable(Table table)
{
foreach (var row in table.Rows)
{
var attribute = row.FieldAsNullableInteger(4) ?? 0x2;
this.DecompilerHelper.AddElementToRoot(UtilConstants.CloseApplicationName,
new XAttribute("Id", row.FieldAsString(0)),
new XAttribute("Target", row.FieldAsString(1)),
AttributeIfNotNull("Description", row, 2),
AttributeIfNotNull("Content", row, 3),
AttributeIfNotNull("CloseMessage", 0x1 == (attribute & 0x1)),
AttributeIfNotNull("RebootPrompt", 0x2 == (attribute & 0x2)),
AttributeIfNotNull("ElevatedCloseMessage", 0x4 == (attribute & 0x4)),
NumericAttributeIfNotNull("Sequence", row, 5),
AttributeIfNotNull("Property", row, 6)
);
}
}
/// <summary>
/// Decompile the WixRemoveFolderEx table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileWixRemoveFolderExTable(Table table)
{
foreach (var row in table.Rows)
{
var on = String.Empty;
var installMode = row.FieldAsInteger(3);
switch (installMode)
{
case (int)WixRemoveFolderExInstallMode.Install:
on = "install";
break;
case (int)WixRemoveFolderExInstallMode.Uninstall:
on = "uninstall";
break;
case (int)WixRemoveFolderExInstallMode.Both:
on = "both";
break;
default:
this.Messaging.Write(WarningMessages.UnrepresentableColumnValue(row.SourceLineNumbers, table.Name, "InstallMode", installMode));
break;
}
var removeFolder = new XElement(UtilConstants.RemoveFolderExName,
AttributeIfNotNull("Id", row, 0),
AttributeIfNotNull("Property", row, 2),
AttributeIfNotNull("On", on)
);
// Add to the appropriate Component or section element.
var componentId = row.FieldAsString(1);
if (this.DecompilerHelper.TryGetIndexedElement("Component", componentId, out var component))
{
component.Add(removeFolder);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Component_", componentId, "Component"));
}
}
}
/// <summary>
/// Decompile the WixRestartResource table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileWixRestartResourceTable(Table table)
{
foreach (var row in table.Rows)
{
var restartResource = new XElement(UtilConstants.RestartResourceName,
new XAttribute("Id", row.FieldAsString(0)));
// Determine the resource type and set accordingly.
var resource = row.FieldAsString(2);
var attributes = row.FieldAsInteger(3);
var type = (WixRestartResourceAttributes)attributes;
switch (type)
{
case WixRestartResourceAttributes.Filename:
restartResource.Add(new XAttribute("Path", resource));
break;
case WixRestartResourceAttributes.ProcessName:
restartResource.Add(new XAttribute("ProcessName", resource));
break;
case WixRestartResourceAttributes.ServiceName:
restartResource.Add(new XAttribute("ServiceName", resource));
break;
default:
this.Messaging.Write(WarningMessages.UnrepresentableColumnValue(row.SourceLineNumbers, table.Name, "Attributes", attributes));
break;
}
// Add to the appropriate Component or section element.
var componentId = row.FieldAsString(1);
if (!String.IsNullOrEmpty(componentId))
{
if (this.DecompilerHelper.TryGetIndexedElement("Component", componentId, out var component))
{
component.Add(restartResource);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Component_", componentId, "Component"));
}
}
else
{
this.DecompilerHelper.AddElementToRoot(restartResource);
}
}
}
/// <summary>
/// Decompile the FileShare table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileFileShareTable(Table table)
{
foreach (var row in table.Rows)
{
var fileShare = new XElement(UtilConstants.FileShareName,
new XAttribute("Id", row.FieldAsString(0)),
new XAttribute("Name", row.FieldAsString(1)),
AttributeIfNotNull("Description", row, 3)
);
// the Directory_ column is set by the parent Component
// the User_ and Permissions columns are deprecated
if (this.DecompilerHelper.TryGetIndexedElement("Component", row.FieldAsString(2), out var component))
{
component.Add(fileShare);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Component_", (string)row[2], "Component"));
}
this.DecompilerHelper.IndexElement(row, fileShare);
}
}
/// <summary>
/// Decompile the FileSharePermissions table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileFileSharePermissionsTable(Table table)
{
foreach (var row in table.Rows)
{
var fileSharePermission = new XElement(UtilConstants.FileSharePermissionName,
new XAttribute("User", row.FieldAsString(1)));
this.AddPermissionAttributes(fileSharePermission, row, 2, UtilConstants.FolderPermissions);
if (this.DecompilerHelper.TryGetIndexedElement("Wix4FileShare", row.FieldAsString(0), out var fileShare) ||
this.DecompilerHelper.TryGetIndexedElement("FileShare", row.FieldAsString(0), out fileShare))
{
fileShare.Add(fileSharePermission);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "FileShare_", (string)row[0], "Wix4FileShare"));
}
}
}
/// <summary>
/// Decompile the Group table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileGroupTable(Table table)
{
foreach (var row in table.Rows)
{
var group = new XElement(UtilConstants.GroupName,
new XAttribute("Id", row.FieldAsString(0)),
new XAttribute("Name", row.FieldAsString(2)),
AttributeIfNotNull("Domain", row, 3)
);
this.DecompilerHelper.AddElementToRoot(group);
this.DecompilerHelper.IndexElement(row, group);
}
}
/// <summary>
/// Decompile the Group6 table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileGroup6Table(Table table)
{
foreach (var row in table.Rows)
{
var groupId = row.FieldAsString(0);
XElement group;
if (this.DecompilerHelper.TryGetIndexedElement("Group", groupId, out group)
|| this.DecompilerHelper.TryGetIndexedElement("Wix4Group", groupId, out group))
{
var attributes = (Group6SymbolAttributes)(row.FieldAsNullableInteger(2) ?? 0);
group.Add(AttributeIfNotNull("Comment", row, 1));
group.Add(AttributeIfTrue("FailIfExists", ((attributes & Group6SymbolAttributes.FailIfExists) != 0)));
group.Add(AttributeIfTrue("UpdateIfExists", ((attributes & Group6SymbolAttributes.UpdateIfExists) != 0)));
group.Add(AttributeIfTrue("DontRemoveOnUninstall", ((attributes & Group6SymbolAttributes.DontRemoveOnUninstall) != 0)));
group.Add(AttributeIfTrue("DontCreateGroup", ((attributes & Group6SymbolAttributes.DontCreateGroup) != 0)));
group.Add(AttributeIfTrue("NonVital", ((attributes & Group6SymbolAttributes.NonVital) != 0)));
group.Add(AttributeIfTrue("RemoveComment", ((attributes & Group6SymbolAttributes.RemoveComment) != 0)));
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Group_", groupId, "Group"));
}
}
}
/// <summary>
/// Decompile the GroupGroup6 table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileGroupGroup6Table(Table table)
{
foreach (var row in table.Rows)
{
var parentId = row.FieldAsString(0);
XElement parentGroup;
var parentExists = (this.DecompilerHelper.TryGetIndexedElement("Group", parentId, out parentGroup)
|| this.DecompilerHelper.TryGetIndexedElement("Wix4Group", parentId, out parentGroup));
var childId = row.FieldAsString(1);
XElement childGroup;
var childExists = (this.DecompilerHelper.TryGetIndexedElement("Group", childId, out childGroup)
|| this.DecompilerHelper.TryGetIndexedElement("Wix4Group", childId, out childGroup));
if (parentExists && childExists)
{
childGroup.Add(new XElement(UtilConstants.GroupRefName, new XAttribute("Id", parentId)));
}
else
{
if(!parentExists)
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Parent_", parentId, "Group"));
}
if (!childExists)
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Child_", childId, "Group"));
}
}
}
}
/// <summary>
/// Decompile the WixInternetShortcut table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileWixInternetShortcutTable(Table table)
{
foreach (var row in table.Rows)
{
var type = String.Empty;
var shortcutType = (UtilCompiler.InternetShortcutType)row.FieldAsInteger(5);
switch (shortcutType)
{
case UtilCompiler.InternetShortcutType.Link:
type = "link";
break;
case UtilCompiler.InternetShortcutType.Url:
type = "url";
break;
}
var internetShortcut = new XElement(UtilConstants.InternetShortcutName,
new XAttribute("Id", row.FieldAsString(0)),
new XAttribute("Directory", row.FieldAsString(2)),
new XAttribute("Name", Path.GetFileNameWithoutExtension(row.FieldAsString(3))), // remove .lnk/.url extension because compiler extension adds it back for us
new XAttribute("Type", type),
new XAttribute("Target", row.FieldAsString(4)),
new XAttribute("IconFile", row.FieldAsString(6)),
NumericAttributeIfNotNull("IconIndex", row, 7)
);
var componentId = row.FieldAsString(1);
if (this.DecompilerHelper.TryGetIndexedElement("Component", componentId, out var component))
{
component.Add(internetShortcut);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Component_", componentId, "Component"));
}
this.DecompilerHelper.IndexElement(row, internetShortcut);
}
}
/// <summary>
/// Decompile the Perfmon table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompilePerfmonTable(Table table)
{
foreach (var row in table.Rows)
{
this.DecompilerHelper.IndexElement(row, new XElement(UtilConstants.PerfCounterName, new XAttribute("Name", row.FieldAsString(2))));
}
}
/// <summary>
/// Decompile the PerfmonManifest table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompilePerfmonManifestTable(Table table)
{
foreach (var row in table.Rows)
{
this.DecompilerHelper.IndexElement(row, new XElement(UtilConstants.PerfCounterManifestName, new XAttribute("ResourceFileDirectory", row.FieldAsString(2))));
}
}
/// <summary>
/// Decompile the EventManifest table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileEventManifestTable(Table table)
{
foreach (var row in table.Rows)
{
this.DecompilerHelper.IndexElement(row, new XElement(UtilConstants.EventManifestName));
}
}
/// <summary>
/// Decompile the SecureObjects table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileSecureObjectsTable(Table table)
{
foreach (var row in table.Rows)
{
var permissionEx = new XElement(UtilConstants.PermissionExName,
AttributeIfNotNull("Domain", row, 2),
AttributeIfNotNull("User", row, 3)
);
string[] specialPermissions;
switch ((string)row[1])
{
case "CreateFolder":
specialPermissions = UtilConstants.FolderPermissions;
break;
case "File":
specialPermissions = UtilConstants.FilePermissions;
break;
case "Registry":
specialPermissions = UtilConstants.RegistryPermissions;
break;
case "ServiceInstall":
specialPermissions = UtilConstants.ServicePermissions;
break;
default:
this.Messaging.Write(WarningMessages.IllegalColumnValue(row.SourceLineNumbers, row.Table.Name, row.Fields[1].Column.Name, row[1]));
return;
}
this.AddPermissionAttributes(permissionEx, row, 4, specialPermissions);
this.DecompilerHelper.IndexElement(row, permissionEx);
}
}
/// <summary>
/// Decompile the ServiceConfig table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileServiceConfigTable(Table table)
{
foreach (var row in table.Rows)
{
var serviceConfig = new XElement(UtilConstants.ServiceConfigName,
new XAttribute("ServiceName", row.FieldAsString(0)),
AttributeIfNotNull("FirstFailureActionType", row, 3),
AttributeIfNotNull("SecondFailureActionType", row, 4),
AttributeIfNotNull("ThirdFailureActionType", row, 5),
NumericAttributeIfNotNull("ResetPeriodInDays", row, 6),
NumericAttributeIfNotNull("RestartServiceDelayInSeconds", row, 7),
AttributeIfNotNull("ProgramCommandLine", row, 8),
AttributeIfNotNull("RebootMessage", row, 9)
);
this.DecompilerHelper.IndexElement(row, serviceConfig);
}
}
/// <summary>
/// Decompile the User table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileUserTable(Table table)
{
foreach (var row in table.Rows)
{
var attributes = row.FieldAsNullableInteger(6) ?? 0;
var user = new XElement(UtilConstants.UserName,
new XAttribute("Id", row.FieldAsString(0)),
new XAttribute("Name", row.FieldAsString(2)),
AttributeIfNotNull("Domain", row, 3),
AttributeIfNotNull("Password", row, 4),
AttributeIfNotNull("Comment", row, 5),
AttributeIfTrue("PasswordNeverExpires", UtilCompiler.UserDontExpirePasswrd == (attributes & UtilCompiler.UserDontExpirePasswrd)),
AttributeIfTrue("CanNotChangePassword", UtilCompiler.UserPasswdCantChange == (attributes & UtilCompiler.UserPasswdCantChange)),
AttributeIfTrue("PasswordExpired", UtilCompiler.UserPasswdChangeReqdOnLogin == (attributes & UtilCompiler.UserPasswdChangeReqdOnLogin)),
AttributeIfTrue("Disabled", UtilCompiler.UserDisableAccount == (attributes & UtilCompiler.UserDisableAccount)),
AttributeIfTrue("FailIfExists", UtilCompiler.UserFailIfExists == (attributes & UtilCompiler.UserFailIfExists)),
AttributeIfTrue("UpdateIfExists", UtilCompiler.UserUpdateIfExists == (attributes & UtilCompiler.UserUpdateIfExists)),
AttributeIfTrue("LogonAsService", UtilCompiler.UserLogonAsService == (attributes & UtilCompiler.UserLogonAsService)),
AttributeIfTrue("LogonAsBatchJob", UtilCompiler.UserLogonAsBatchJob == (attributes & UtilCompiler.UserLogonAsBatchJob)),
AttributeIfTrue("RemoveComment", UtilCompiler.UserRemoveComment == (attributes & UtilCompiler.UserRemoveComment))
);
if (UtilCompiler.UserDontRemoveOnUninstall == (attributes & UtilCompiler.UserDontRemoveOnUninstall))
{
user.Add(new XAttribute("RemoveOnUninstall", "no"));
}
if (UtilCompiler.UserDontCreateUser == (attributes & UtilCompiler.UserDontCreateUser))
{
user.Add(new XAttribute("CreateUser", "no"));
}
if (UtilCompiler.UserNonVital == (attributes & UtilCompiler.UserNonVital))
{
user.Add(new XAttribute("Vital", "no"));
}
var componentId = row.FieldAsString(1);
if (!String.IsNullOrEmpty(componentId))
{
if (this.DecompilerHelper.TryGetIndexedElement("Component", componentId, out var component))
{
component.Add(user);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Component_", componentId, "Component"));
}
}
else
{
this.DecompilerHelper.AddElementToRoot(user);
}
this.DecompilerHelper.IndexElement(row, user);
}
}
/// <summary>
/// Decompile the UserGroup table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileUserGroupTable(Table table)
{
foreach (var row in table.Rows)
{
var userId = row.FieldAsString(0);
if (this.DecompilerHelper.TryGetIndexedElement("User", userId, out var user))
{
user.Add(new XElement(UtilConstants.GroupRefName, new XAttribute("Id", row.FieldAsString(1))));
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(), "Group_", userId, "Group"));
}
}
}
/// <summary>
/// Decompile the XmlConfig table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileXmlConfigTable(Table table)
{
foreach (var row in table.Rows)
{
var flags = row.FieldAsNullableInteger(7) ?? 0;
string node = null;
string action = null;
string on = null;
if (0x1 == (flags & 0x1))
{
node = "element";
}
else if (0x2 == (flags & 0x2))
{
node = "value";
}
else if (0x4 == (flags & 0x4))
{
node = "document";
}
if (0x10 == (flags & 0x10))
{
action = "create";
}
else if (0x20 == (flags & 0x20))
{
action = "delete";
}
if (0x100 == (flags & 0x100))
{
on = "install";
}
else if (0x200 == (flags & 0x200))
{
on = "uninstall";
}
var xmlConfig = new XElement(UtilConstants.XmlConfigName,
new XAttribute("Id", row.FieldAsString(0)),
new XAttribute("File", row.FieldAsString(1)),
AttributeIfNotNull("ElementId", row, 2),
AttributeIfNotNull("ElementPath", row, 3),
AttributeIfNotNull("VerifyPath", row, 4),
AttributeIfNotNull("Name", row, 5),
AttributeIfNotNull("Value", row, 6),
AttributeIfNotNull("Node", node),
AttributeIfNotNull("Action", action),
AttributeIfNotNull("On", on),
AttributeIfTrue("PreserveModifiedDate", 0x00001000 == (flags & 0x00001000)),
NumericAttributeIfNotNull("Sequence", row, 9)
);
this.DecompilerHelper.IndexElement(row, xmlConfig);
}
}
private void FinalizeCustomActions()
{
foreach (var customActionName in this.customActionNames)
{
if (CustomActionMapping.TryGetValue(customActionName, out var elementName))
{
this.DecompilerHelper.AddElementToRoot(elementName);
}
}
}
/// <summary>
/// Finalize the Perfmon table.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
/// <remarks>
/// Since the PerfCounter element nests under a File element, but
/// the Perfmon table does not have a foreign key relationship with
/// the File table (instead it has a formatted string that usually
/// refers to a file row - but doesn't have to), the nesting must
/// be inferred during finalization.
/// </remarks>
private void FinalizePerfmonTable(TableIndexedCollection tables)
{
if (tables.TryGetTable("Perfmon", out var perfmonTable))
{
foreach (var row in perfmonTable.Rows)
{
var formattedFile = row.FieldAsString(1);
// try to "de-format" the File column's value to determine the proper parent File element
if ((formattedFile.StartsWith("[#", StringComparison.Ordinal) || formattedFile.StartsWith("[!", StringComparison.Ordinal))
&& formattedFile.EndsWith("]", StringComparison.Ordinal))
{
var fileId = formattedFile.Substring(2, formattedFile.Length - 3);
if (this.DecompilerHelper.TryGetIndexedElement("File", fileId, out var file))
{
var perfCounter = this.DecompilerHelper.GetIndexedElement(row);
file.Add(perfCounter);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, perfmonTable.Name, row.GetPrimaryKey(), "File", formattedFile, "File"));
}
}
else
{
this.Messaging.Write(UtilErrors.IllegalFileValueInPerfmonOrManifest(formattedFile, "Perfmon"));
}
}
}
}
/// <summary>
/// Finalize the PerfmonManifest table.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
private void FinalizePerfmonManifestTable(TableIndexedCollection tables)
{
if (tables.TryGetTable("PerfmonManifest", out var perfmonManifestTable))
{
foreach (var row in perfmonManifestTable.Rows)
{
var formattedFile = row.FieldAsString(1);
// try to "de-format" the File column's value to determine the proper parent File element
if ((formattedFile.StartsWith("[#", StringComparison.Ordinal) || formattedFile.StartsWith("[!", StringComparison.Ordinal))
&& formattedFile.EndsWith("]", StringComparison.Ordinal))
{
var perfCounterManifest = this.DecompilerHelper.GetIndexedElement(row);
var fileId = formattedFile.Substring(2, formattedFile.Length - 3);
if (this.DecompilerHelper.TryGetIndexedElement("File", fileId, out var file))
{
file.Add(perfCounterManifest);
}
else
{
var resourceFileDirectory = perfCounterManifest.Attribute("ResourceFileDirectory")?.Value;
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, resourceFileDirectory, row.GetPrimaryKey(), "File", formattedFile, "File"));
}
}
else
{
this.Messaging.Write(UtilErrors.IllegalFileValueInPerfmonOrManifest(formattedFile, "PerfmonManifest"));
}
}
}
}
/// <summary>
/// Finalize the SecureObjects table.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
/// <remarks>
/// Nests the PermissionEx elements below their parent elements. There are no declared foreign
/// keys for the parents of the SecureObjects table.
/// </remarks>
private void FinalizeSecureObjectsTable(TableIndexedCollection tables)
{
var createFolderElementsByDirectoryId = new Dictionary<string, List<XElement>>();
// index the CreateFolder table because the foreign key to this table from the
// LockPermissions table is only part of the primary key of this table
if (tables.TryGetTable("CreateFolder", out var createFolderTable))
{
foreach (var row in createFolderTable.Rows)
{
var directoryId = row.FieldAsString(0);
if (!createFolderElementsByDirectoryId.TryGetValue(directoryId, out var createFolderElements))
{
createFolderElements = new List<XElement>();
createFolderElementsByDirectoryId.Add(directoryId, createFolderElements);
}
var createFolder = this.DecompilerHelper.GetIndexedElement(row);
createFolderElements.Add(createFolder);
}
}
if (tables.TryGetTable("SecureObjects", out var secureObjectsTable))
{
foreach (var row in secureObjectsTable.Rows)
{
var id = row.FieldAsString(0);
var table = row.FieldAsString(1);
var permissionEx = this.DecompilerHelper.GetIndexedElement(row);
if (table == "CreateFolder")
{
if (createFolderElementsByDirectoryId.TryGetValue(id, out var createFolderElements))
{
foreach (var createFolder in createFolderElements)
{
createFolder.Add(permissionEx);
}
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, "SecureObjects", row.GetPrimaryKey(), "LockObject", id, table));
}
}
else
{
var parentElement = this.DecompilerHelper.GetIndexedElement(table, id);
if (parentElement != null)
{
parentElement.Add(permissionEx);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, "SecureObjects", row.GetPrimaryKey(), "LockObject", id, table));
}
}
}
}
}
/// <summary>
/// Finalize the ServiceConfig table.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
/// <remarks>
/// Since there is no foreign key from the ServiceName column to the
/// ServiceInstall table, this relationship must be handled late.
/// </remarks>
private void FinalizeServiceConfigTable(TableIndexedCollection tables)
{
//var serviceInstalls = new Hashtable();
var serviceInstallElementsByName = new Dictionary<string, List<XElement>>();
// index the ServiceInstall table because the foreign key used by the ServiceConfig
// table is actually the ServiceInstall.Name, not the ServiceInstall.ServiceInstall
// this is unfortunate because the service Name is not guaranteed to be unique, so
// decompiler must assume there could be multiple matches and add the ServiceConfig to each
// TODO: the Component column information should be taken into acount to accurately identify
// the correct column to use
if (tables.TryGetTable("ServiceInstall", out var serviceInstallTable))
{
foreach (var row in serviceInstallTable.Rows)
{
var name = row.FieldAsString(1);
if (!serviceInstallElementsByName.TryGetValue(name, out var serviceInstallElements))
{
serviceInstallElements = new List<XElement>();
serviceInstallElementsByName.Add(name, serviceInstallElements);
}
var serviceInstall = this.DecompilerHelper.GetIndexedElement(row);
serviceInstallElements.Add(serviceInstall);
}
}
if (tables.TryGetTable("ServiceConfig", out var serviceConfigTable))
{
foreach (var row in serviceConfigTable.Rows)
{
var serviceConfig = this.DecompilerHelper.GetIndexedElement(row);
if (row.FieldAsInteger(2) == 0)
{
var componentId = row.FieldAsString(1);
if (this.DecompilerHelper.TryGetIndexedElement("Component", componentId, out var component))
{
component.Add(serviceConfig);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, serviceConfigTable.Name, row.GetPrimaryKey(), "Component_", componentId, "Component"));
}
}
else
{
var name = row.FieldAsString(0);
if (serviceInstallElementsByName.TryGetValue(name, out var serviceInstallElements))
{
foreach (var serviceInstall in serviceInstallElements)
{
serviceInstall.Add(serviceConfig);
}
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, serviceConfigTable.Name, row.GetPrimaryKey(), "ServiceName", name, "ServiceInstall"));
}
}
}
}
}
/// <summary>
/// Finalize the XmlConfig table.
/// </summary>
/// <param name="tables">Collection of all tables.</param>
private void FinalizeXmlConfigTable(TableIndexedCollection tables)
{
if (tables.TryGetTable("Wix4XmlConfig", out var xmlConfigTable))
{
foreach (var row in xmlConfigTable.Rows)
{
var xmlConfig = this.DecompilerHelper.GetIndexedElement(row);
if (null != row[2])
{
var id = row.FieldAsString(2);
if (this.DecompilerHelper.TryGetIndexedElement("Wix4XmlConfig", id, out var parentXmlConfig))
{
parentXmlConfig.Add(xmlConfig);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, xmlConfigTable.Name, row.GetPrimaryKey(), "ElementPath", (string)row[2], "XmlConfig"));
}
}
else
{
var componentId = row.FieldAsString(8);
if (this.DecompilerHelper.TryGetIndexedElement("Component", componentId, out var component))
{
component.Add(xmlConfig);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, xmlConfigTable.Name, row.GetPrimaryKey(), "Component_", componentId, "Component"));
}
}
}
}
}
/// <summary>
/// Finalize the XmlFile table.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
/// <remarks>
/// Some of the XmlFile table rows are compiler generated from util:EventManifest node
/// These rows should not be appended to component.
/// </remarks>
private void FinalizeXmlFileTable(TableIndexedCollection tables)
{
if (tables.TryGetTable("XmlFile", out var xmlFileTable))
{
var eventManifestTable = tables["EventManifest"];
foreach (var row in xmlFileTable.Rows)
{
var manifestGenerated = false;
var xmlFileConfigId = (string)row[0];
if (null != eventManifestTable)
{
foreach (var emrow in eventManifestTable.Rows)
{
var formattedFile = (string)emrow[1];
if ((formattedFile.StartsWith("[#", StringComparison.Ordinal) || formattedFile.StartsWith("[!", StringComparison.Ordinal))
&& formattedFile.EndsWith("]", StringComparison.Ordinal))
{
var fileId = formattedFile.Substring(2, formattedFile.Length - 3);
if (String.Equals(String.Concat("Config_", fileId, "ResourceFile"), xmlFileConfigId))
{
if (this.DecompilerHelper.TryGetIndexedElement(emrow, out var eventManifest))
{
eventManifest.Add(new XAttribute("ResourceFile", row.FieldAsString(4)));
}
manifestGenerated = true;
}
else if (String.Equals(String.Concat("Config_", fileId, "MessageFile"), xmlFileConfigId))
{
if (this.DecompilerHelper.TryGetIndexedElement(emrow, out var eventManifest))
{
eventManifest.Add(new XAttribute("MessageFile", row.FieldAsString(4)));
}
manifestGenerated = true;
}
}
}
}
if (manifestGenerated)
{
continue;
}
var action = "setValue";
var flags = row.FieldAsInteger(5);
if (0x1 == (flags & 0x1) && 0x2 == (flags & 0x2))
{
this.Messaging.Write(WarningMessages.IllegalColumnValue(row.SourceLineNumbers, xmlFileTable.Name, row.Fields[5].Column.Name, row[5]));
}
else if (0x1 == (flags & 0x1))
{
action = "createElement";
}
else if (0x2 == (flags & 0x2))
{
action = "deleteValue";
}
var selectionLanguage = (0x100 == (flags & 0x100)) ? "XPath" : null;
var preserveModifiedDate = 0x00001000 == (flags & 0x00001000);
var permanent = 0x00010000 == (flags & 0x00010000);
if (this.DecompilerHelper.TryGetIndexedElement("Component", row.FieldAsString(6), out var component))
{
var xmlFile = new XElement(UtilConstants.XmlFileName,
AttributeIfNotNull("Id", row, 0),
AttributeIfNotNull("File", row, 1),
AttributeIfNotNull("ElementPath", row, 2),
AttributeIfNotNull("Name", row, 3),
AttributeIfNotNull("Value", row, 4),
AttributeIfNotNull("Action", action),
AttributeIfNotNull("SelectionLanguage", selectionLanguage),
AttributeIfTrue("PreserveModifiedDate", preserveModifiedDate),
AttributeIfTrue("Permanent", permanent),
NumericAttributeIfNotNull("Sequence", row, 7)
);
component.Add(xmlFile);
}
else
{
this.Messaging.Write(WarningMessages.ExpectedForeignRow(row.SourceLineNumbers, xmlFileTable.Name, row.GetPrimaryKey(), "Component_", (string)row[6], "Component"));
}
}
}
}
/// <summary>
/// Finalize the eventManifest table.
/// This function must be called after FinalizeXmlFileTable
/// </summary>
/// <param name="tables">The collection of all tables.</param>
private void FinalizeEventManifestTable(TableIndexedCollection tables)
{
if (tables.TryGetTable("EventManifest", out var eventManifestTable))
{
foreach (var row in eventManifestTable.Rows)
{
var eventManifest = this.DecompilerHelper.GetIndexedElement(row);
var formattedFile = row.FieldAsString(1);
// try to "de-format" the File column's value to determine the proper parent File element
if ((formattedFile.StartsWith("[#", StringComparison.Ordinal) || formattedFile.StartsWith("[!", StringComparison.Ordinal))
&& formattedFile.EndsWith("]", StringComparison.Ordinal))
{
var fileId = formattedFile.Substring(2, formattedFile.Length - 3);
if (this.DecompilerHelper.TryGetIndexedElement("File", fileId, out var file))
{
file.Add(eventManifest);
}
}
else
{
this.Messaging.Write(UtilErrors.IllegalFileValueInPerfmonOrManifest(formattedFile, "EventManifest"));
}
}
}
}
private void AddPermissionAttributes(XElement element, Row row, int column, string[] specialPermissions)
{
var permissions = row.FieldAsInteger(column);
for (var i = 0; i < 32; i++)
{
if (0 != ((permissions >> i) & 1))
{
string name = null;
if (16 > i && specialPermissions.Length > i)
{
name = specialPermissions[i];
}
else if (28 > i && UtilConstants.StandardPermissions.Length > (i - 16))
{
name = UtilConstants.StandardPermissions[i - 16];
}
else if (0 <= (i - 28) && UtilConstants.GenericPermissions.Length > (i - 28))
{
name = UtilConstants.GenericPermissions[i - 28];
}
if (!String.IsNullOrEmpty(name))
{
element.Add(new XAttribute(name, "yes"));
}
else
{
this.Messaging.Write(WarningMessages.UnknownPermission(row.SourceLineNumbers, row.Table.Name, row.GetPrimaryKey(), i));
}
}
}
}
private static XAttribute AttributeIfNotNull(string name, string value)
{
return value == null ? null : new XAttribute(name, value);
}
private static XAttribute AttributeIfNotNull(string name, bool value)
{
return new XAttribute(name, value ? "yes" : "no");
}
private static XAttribute AttributeIfNotNull(string name, Row row, int field)
{
if (row[field] != null)
{
return new XAttribute(name, row.FieldAsString(field));
}
return null;
}
private static XAttribute NumericAttributeIfNotNull(string name, Row row, int field)
{
if (row[field] != null)
{
return new XAttribute(name, row.FieldAsInteger(field));
}
return null;
}
private static XAttribute AttributeIfTrue(string name, bool value)
{
return value ? new XAttribute(name, "yes") : null;
}
}
internal static class XElementExtensions
{
public static XElement AttributeIfNotNull(this XElement element, string name, Row row, int field)
{
if (row[field] != null)
{
element.Add(new XAttribute(name, row.FieldAsString(field)));
}
return element;
}
public static XElement NumericAttributeIfNotNull(this XElement element, string name, Row row, int field)
{
if (row[field] != null)
{
element.Add(new XAttribute(name, row.FieldAsInteger(field)));
}
return element;
}
}
}
|