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
|
//
// AddinDescription.cs
//
// Author:
// Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Specialized;
using Mono.Addins.Serialization;
using Mono.Addins.Database;
namespace Mono.Addins.Description
{
/// <summary>
/// An add-in description
/// </summary>
/// <remarks>
/// This class represent an add-in manifest. It has properties for getting
/// all information, and methods for loading and saving files.
/// </remarks>
public class AddinDescription: IBinaryXmlElement
{
XmlDocument configDoc;
string configFile;
AddinDatabase ownerDatabase;
string id;
string name;
string ns;
string version;
string compatVersion;
string author;
string url;
string copyright;
string description;
string category;
string basePath;
string sourceAddinFile;
bool isroot;
bool hasUserId;
bool canWrite = true;
bool defaultEnabled = true;
AddinFlags flags = AddinFlags.None;
string domain;
ModuleDescription mainModule;
ModuleCollection optionalModules;
ExtensionNodeSetCollection nodeSets;
ConditionTypeDescriptionCollection conditionTypes;
ExtensionPointCollection extensionPoints;
ExtensionNodeDescription localizer;
object[] fileInfo;
AddinPropertyCollectionImpl properties;
internal static BinaryXmlTypeMap typeMap;
static AddinDescription ()
{
typeMap = new BinaryXmlTypeMap ();
typeMap.RegisterType (typeof(AddinDescription), "AddinDescription");
typeMap.RegisterType (typeof(Extension), "Extension");
typeMap.RegisterType (typeof(ExtensionNodeDescription), "Node");
typeMap.RegisterType (typeof(ExtensionNodeSet), "NodeSet");
typeMap.RegisterType (typeof(ExtensionNodeType), "NodeType");
typeMap.RegisterType (typeof(ExtensionPoint), "ExtensionPoint");
typeMap.RegisterType (typeof(ModuleDescription), "ModuleDescription");
typeMap.RegisterType (typeof(ConditionTypeDescription), "ConditionType");
typeMap.RegisterType (typeof(Condition), "Condition");
typeMap.RegisterType (typeof(AddinDependency), "AddinDependency");
typeMap.RegisterType (typeof(AssemblyDependency), "AssemblyDependency");
typeMap.RegisterType (typeof(NodeTypeAttribute), "NodeTypeAttribute");
typeMap.RegisterType (typeof(AddinFileInfo), "FileInfo");
typeMap.RegisterType (typeof(AddinProperty), "Property");
}
internal AddinDatabase OwnerDatabase {
get { return ownerDatabase; }
set { ownerDatabase = value; }
}
/// <summary>
/// Gets or sets the path to the main addin file.
/// </summary>
/// <value>
/// The addin file.
/// </value>
/// <remarks>
/// The add-in file can be either the main assembly of an add-in or an xml manifest.
/// </remarks>
public string AddinFile {
get { return sourceAddinFile; }
set { sourceAddinFile = value; }
}
/// <summary>
/// Gets the addin identifier.
/// </summary>
/// <value>
/// The addin identifier.
/// </value>
public string AddinId {
get { return Addin.GetFullId (Namespace, LocalId, Version); }
}
/// <summary>
/// Gets or sets the local identifier.
/// </summary>
/// <value>
/// The local identifier.
/// </value>
public string LocalId {
get { return id != null ? id : string.Empty; }
set { id = value; hasUserId = true; }
}
/// <summary>
/// Gets or sets the namespace.
/// </summary>
/// <value>
/// The namespace.
/// </value>
public string Namespace {
get { return ns != null ? ns : string.Empty; }
set { ns = value; }
}
/// <summary>
/// Gets or sets the display name of the add-in.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name {
get {
string val = Properties.GetPropertyValue ("Name");
if (val.Length > 0)
return val;
if (name != null && name.Length > 0)
return name;
if (HasUserId)
return AddinId;
else if (sourceAddinFile != null)
return Path.GetFileNameWithoutExtension (sourceAddinFile);
else
return string.Empty;
}
set { name = value; }
}
/// <summary>
/// Gets or sets the version.
/// </summary>
/// <value>
/// The version.
/// </value>
public string Version {
get { return version != null ? version : string.Empty; }
set { version = value; }
}
/// <summary>
/// Gets or sets the version of the add-in with which this add-in is backwards compatible.
/// </summary>
/// <value>
/// The compat version.
/// </value>
public string CompatVersion {
get { return compatVersion != null ? compatVersion : string.Empty; }
set { compatVersion = value; }
}
/// <summary>
/// Gets or sets the author.
/// </summary>
/// <value>
/// The author.
/// </value>
public string Author {
get {
string val = Properties.GetPropertyValue ("Author");
if (val.Length > 0)
return val;
return author ?? string.Empty;
}
set { author = value; }
}
/// <summary>
/// Gets or sets the Url where more information about the add-in can be found.
/// </summary>
/// <value>
/// The URL.
/// </value>
public string Url {
get {
string val = Properties.GetPropertyValue ("Url");
if (val.Length > 0)
return val;
return url ?? string.Empty;
}
set { url = value; }
}
/// <summary>
/// Gets or sets the copyright.
/// </summary>
/// <value>
/// The copyright.
/// </value>
public string Copyright {
get {
string val = Properties.GetPropertyValue ("Copyright");
if (val.Length > 0)
return val;
return copyright ?? string.Empty;
}
set { copyright = value; }
}
/// <summary>
/// Gets or sets the description of the add-in.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description {
get {
string val = Properties.GetPropertyValue ("Description");
if (val.Length > 0)
return val;
return description ?? string.Empty;
}
set { description = value; }
}
/// <summary>
/// Gets or sets the category of the add-in.
/// </summary>
/// <value>
/// The category.
/// </value>
public string Category {
get {
string val = Properties.GetPropertyValue ("Category");
if (val.Length > 0)
return val;
return category ?? string.Empty;
}
set { category = value; }
}
/// <summary>
/// Gets the base path for locating external files relative to the add-in.
/// </summary>
/// <value>
/// The base path.
/// </value>
public string BasePath {
get { return basePath != null ? basePath : string.Empty; }
}
internal void SetBasePath (string path)
{
basePath = path;
}
/// <summary>
/// Gets or sets a value indicating whether this instance is an add-in root.
/// </summary>
/// <value>
/// <c>true</c> if this instance is an add-in root; otherwise, <c>false</c>.
/// </value>
public bool IsRoot {
get { return isroot; }
set { isroot = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this add-in is enabled by default.
/// </summary>
/// <value>
/// <c>true</c> if enabled by default; otherwise, <c>false</c>.
/// </value>
public bool EnabledByDefault {
get { return defaultEnabled; }
set { defaultEnabled = value; }
}
/// <summary>
/// Gets or sets the add-in flags.
/// </summary>
/// <value>
/// The flags.
/// </value>
public AddinFlags Flags {
get { return flags; }
set { flags = value; }
}
internal bool HasUserId {
get { return hasUserId; }
set { hasUserId = value; }
}
/// <summary>
/// Gets a value indicating whether this add-in can be disabled.
/// </summary>
/// <value>
/// <c>true</c> if this add-in can be disabled; otherwise, <c>false</c>.
/// </value>
public bool CanDisable {
get { return (flags & AddinFlags.CantDisable) == 0 && !IsHidden; }
}
/// <summary>
/// Gets a value indicating whether this add-in can be uninstalled.
/// </summary>
/// <value>
/// <c>true</c> if this instance can be uninstalled; otherwise, <c>false</c>.
/// </value>
public bool CanUninstall {
get { return (flags & AddinFlags.CantUninstall) == 0 && !IsHidden; }
}
/// <summary>
/// Gets a value indicating whether this add-in is hidden.
/// </summary>
/// <value>
/// <c>true</c> if this add-in is hidden; otherwise, <c>false</c>.
/// </value>
public bool IsHidden {
get { return (flags & AddinFlags.Hidden) != 0; }
}
internal bool SupportsVersion (string ver)
{
return Addin.CompareVersions (ver, Version) >= 0 &&
(CompatVersion.Length == 0 || Addin.CompareVersions (ver, CompatVersion) <= 0);
}
/// <summary>
/// Gets all external files
/// </summary>
/// <value>
/// All files.
/// </value>
/// <remarks>
/// External files are data files and assemblies explicitly referenced in the Runtime section of the add-in manifest.
/// </remarks>
public StringCollection AllFiles {
get {
StringCollection col = new StringCollection ();
foreach (string s in MainModule.AllFiles)
col.Add (s);
foreach (ModuleDescription mod in OptionalModules) {
foreach (string s in mod.AllFiles)
col.Add (s);
}
return col;
}
}
/// <summary>
/// Gets all paths to be ignored by the add-in scanner.
/// </summary>
/// <value>
/// All paths to be ignored.
/// </value>
public StringCollection AllIgnorePaths {
get {
StringCollection col = new StringCollection ();
foreach (string s in MainModule.IgnorePaths)
col.Add (s);
foreach (ModuleDescription mod in OptionalModules) {
foreach (string s in mod.IgnorePaths)
col.Add (s);
}
return col;
}
}
/// <summary>
/// Gets the main module.
/// </summary>
/// <value>
/// The main module.
/// </value>
public ModuleDescription MainModule {
get {
if (mainModule == null) {
if (RootElement == null)
mainModule = new ModuleDescription ();
else
mainModule = new ModuleDescription (RootElement);
mainModule.SetParent (this);
}
return mainModule;
}
}
/// <summary>
/// Gets the optional modules.
/// </summary>
/// <value>
/// The optional modules.
/// </value>
/// <remarks>
/// Optional modules can be used to declare extensions which will be registered only if some specified
/// add-in dependencies can be satisfied. Dependencies specified in optional modules are 'soft dependencies',
/// which means that they don't need to be satisfied in order to load the add-in.
/// </remarks>
public ModuleCollection OptionalModules {
get {
if (optionalModules == null) {
optionalModules = new ModuleCollection (this);
if (RootElement != null) {
foreach (XmlElement mod in RootElement.SelectNodes ("Module"))
optionalModules.Add (new ModuleDescription (mod));
}
}
return optionalModules;
}
}
/// <summary>
/// Gets all modules (including the main module and all optional modules)
/// </summary>
/// <value>
/// All modules.
/// </value>
public ModuleCollection AllModules {
get {
ModuleCollection col = new ModuleCollection (this);
col.Add (MainModule);
foreach (ModuleDescription mod in OptionalModules)
col.Add (mod);
return col;
}
}
/// <summary>
/// Gets the extension node sets.
/// </summary>
/// <value>
/// The extension node sets.
/// </value>
public ExtensionNodeSetCollection ExtensionNodeSets {
get {
if (nodeSets == null) {
nodeSets = new ExtensionNodeSetCollection (this);
if (RootElement != null) {
foreach (XmlElement elem in RootElement.SelectNodes ("ExtensionNodeSet"))
nodeSets.Add (new ExtensionNodeSet (elem));
}
}
return nodeSets;
}
}
/// <summary>
/// Gets the extension points.
/// </summary>
/// <value>
/// The extension points.
/// </value>
public ExtensionPointCollection ExtensionPoints {
get {
if (extensionPoints == null) {
extensionPoints = new ExtensionPointCollection (this);
if (RootElement != null) {
foreach (XmlElement elem in RootElement.SelectNodes ("ExtensionPoint"))
extensionPoints.Add (new ExtensionPoint (elem));
}
}
return extensionPoints;
}
}
/// <summary>
/// Gets the condition types.
/// </summary>
/// <value>
/// The condition types.
/// </value>
public ConditionTypeDescriptionCollection ConditionTypes {
get {
if (conditionTypes == null) {
conditionTypes = new ConditionTypeDescriptionCollection (this);
if (RootElement != null) {
foreach (XmlElement elem in RootElement.SelectNodes ("ConditionType"))
conditionTypes.Add (new ConditionTypeDescription (elem));
}
}
return conditionTypes;
}
}
/// <summary>
/// Gets or sets the add-in localizer.
/// </summary>
/// <value>
/// The description of the add-in localizer for this add-in.
/// </value>
public ExtensionNodeDescription Localizer {
get { return localizer; }
set { localizer = value; }
}
/// <summary>
/// Custom properties specified in the add-in header
/// </summary>
public AddinPropertyCollection Properties {
get {
if (properties == null)
properties = new AddinPropertyCollectionImpl ();
return properties;
}
}
/// <summary>
/// Adds an extension point.
/// </summary>
/// <returns>
/// The extension point.
/// </returns>
/// <param name='path'>
/// Path that identifies the new extension point.
/// </param>
public ExtensionPoint AddExtensionPoint (string path)
{
ExtensionPoint ep = new ExtensionPoint ();
ep.Path = path;
ExtensionPoints.Add (ep);
return ep;
}
internal ExtensionNodeDescription FindExtensionNode (string path, bool lookInDeps)
{
// Look in the extensions of this add-in
foreach (Extension ext in MainModule.Extensions) {
if (path.StartsWith (ext.Path + "/")) {
string subp = path.Substring (ext.Path.Length).Trim ('/');
ExtensionNodeDescriptionCollection nodes = ext.ExtensionNodes;
ExtensionNodeDescription node = null;
foreach (string p in subp.Split ('/')) {
if (p.Length == 0) continue;
node = nodes [p];
if (node == null)
break;
nodes = node.ChildNodes;
}
if (node != null)
return node;
}
}
if (!lookInDeps || OwnerDatabase == null)
return null;
// Look in dependencies
foreach (Dependency dep in MainModule.Dependencies) {
AddinDependency adep = dep as AddinDependency;
if (adep == null) continue;
Addin ad = OwnerDatabase.GetInstalledAddin (Domain, adep.FullAddinId);
if (ad != null && ad.Description != null) {
ExtensionNodeDescription node = ad.Description.FindExtensionNode (path, false);
if (node != null)
return node;
}
}
return null;
}
XmlElement RootElement {
get {
if (configDoc != null)
return configDoc.DocumentElement;
else
return null;
}
}
/// <summary>
/// Gets or sets file where this description is stored
/// </summary>
/// <value>
/// The file path.
/// </value>
public string FileName {
get { return configFile; }
set { configFile = value; }
}
internal string Domain {
get { return domain; }
set { domain = value; }
}
internal void StoreFileInfo ()
{
ArrayList list = new ArrayList ();
foreach (string f in AllFiles) {
string file = Path.Combine (this.BasePath, f);
AddinFileInfo fi = new AddinFileInfo ();
fi.FileName = f;
fi.Timestamp = File.GetLastWriteTime (file);
list.Add (fi);
}
fileInfo = list.ToArray ();
}
internal bool FilesChanged ()
{
// Checks if the files of the add-in have changed.
if (fileInfo == null)
return true;
foreach (AddinFileInfo f in fileInfo) {
string file = Path.Combine (this.BasePath, f.FileName);
if (!File.Exists (file))
return true;
if (f.Timestamp != File.GetLastWriteTime (file))
return true;
}
return false;
}
/// <summary>
/// Saves the add-in description.
/// </summary>
/// <param name='fileName'>
/// File name where to save this instance
/// </param>
/// <remarks>
/// Saves the add-in description to the specified file and sets the FileName property.
/// </remarks>
public void Save (string fileName)
{
configFile = fileName;
Save ();
}
/// <summary>
/// Saves the add-in description.
/// </summary>
/// <exception cref='InvalidOperationException'>
/// It is thrown if FileName is not set
/// </exception>
/// <remarks>
/// The description is saved to the file specified in the FileName property.
/// </remarks>
public void Save ()
{
if (configFile == null)
throw new InvalidOperationException ("File name not specified.");
SaveXml ();
using (StreamWriter sw = new StreamWriter (configFile)) {
XmlTextWriter tw = new XmlTextWriter (sw);
tw.Formatting = Formatting.Indented;
configDoc.Save (tw);
}
}
/// <summary>
/// Generates an XML representation of the add-in description
/// </summary>
/// <returns>
/// An XML manifest.
/// </returns>
public XmlDocument SaveToXml ()
{
SaveXml ();
return configDoc;
}
void SaveXml ()
{
if (!canWrite)
throw new InvalidOperationException ("Can't write incomplete description.");
XmlElement elem;
if (configDoc == null) {
configDoc = new XmlDocument ();
configDoc.AppendChild (configDoc.CreateElement ("Addin"));
}
elem = configDoc.DocumentElement;
if (HasUserId)
elem.SetAttribute ("id", id);
else
elem.RemoveAttribute ("id");
elem.SetAttribute ("version", version);
elem.SetAttribute ("namespace", ns);
if (isroot)
elem.SetAttribute ("isroot", "true");
else
elem.RemoveAttribute ("isroot");
// Name will return the file name when HasUserId=false
if (!string.IsNullOrEmpty (name))
elem.SetAttribute ("name", name);
else
elem.RemoveAttribute ("name");
if (compatVersion != null && compatVersion.Length > 0)
elem.SetAttribute ("compatVersion", compatVersion);
else
elem.RemoveAttribute ("compatVersion");
if (defaultEnabled)
elem.RemoveAttribute ("defaultEnabled");
else
elem.SetAttribute ("defaultEnabled", "false");
if (flags != AddinFlags.None)
elem.RemoveAttribute ("flags");
else
elem.SetAttribute ("flags", flags.ToString ());
if (author != null && author.Length > 0)
elem.SetAttribute ("author", author);
else
elem.RemoveAttribute ("author");
if (url != null && url.Length > 0)
elem.SetAttribute ("url", url);
else
elem.RemoveAttribute ("url");
if (copyright != null && copyright.Length > 0)
elem.SetAttribute ("copyright", copyright);
else
elem.RemoveAttribute ("copyright");
if (description != null && description.Length > 0)
elem.SetAttribute ("description", description);
else
elem.RemoveAttribute ("description");
if (category != null && category.Length > 0)
elem.SetAttribute ("category", category);
else
elem.RemoveAttribute ("category");
if (localizer == null || localizer.Element == null) {
// Remove old element if it exists
XmlElement oldLoc = (XmlElement) elem.SelectSingleNode ("Localizer");
if (oldLoc != null)
elem.RemoveChild (oldLoc);
}
if (localizer != null)
localizer.SaveXml (elem);
if (mainModule != null) {
mainModule.Element = elem;
mainModule.SaveXml (elem);
}
if (optionalModules != null)
optionalModules.SaveXml (elem);
if (nodeSets != null)
nodeSets.SaveXml (elem);
if (extensionPoints != null)
extensionPoints.SaveXml (elem);
XmlElement oldHeader = (XmlElement) elem.SelectSingleNode ("Header");
if (properties == null || properties.Count == 0) {
if (oldHeader != null)
elem.RemoveChild (oldHeader);
} else {
if (oldHeader == null) {
oldHeader = elem.OwnerDocument.CreateElement ("Header");
if (elem.FirstChild != null)
elem.InsertBefore (elem, elem.FirstChild);
else
elem.AppendChild (oldHeader);
}
else
oldHeader.RemoveAll ();
foreach (var prop in properties) {
XmlElement propElem = elem.OwnerDocument.CreateElement (prop.Name);
if (!string.IsNullOrEmpty (prop.Locale))
propElem.SetAttribute ("locale", prop.Locale);
propElem.InnerText = prop.Value ?? string.Empty;
oldHeader.AppendChild (propElem);
}
}
}
/// <summary>
/// Load an add-in description from a file
/// </summary>
/// <param name='configFile'>
/// The file.
/// </param>
public static AddinDescription Read (string configFile)
{
AddinDescription config;
using (Stream s = File.OpenRead (configFile)) {
config = Read (s, Path.GetDirectoryName (configFile));
}
config.configFile = configFile;
return config;
}
/// <summary>
/// Load an add-in description from a stream
/// </summary>
/// <param name='stream'>
/// The stream
/// </param>
/// <param name='basePath'>
/// The path to be used to resolve relative file paths.
/// </param>
public static AddinDescription Read (Stream stream, string basePath)
{
return Read (new StreamReader (stream), basePath);
}
/// <summary>
/// Load an add-in description from a text reader
/// </summary>
/// <param name='reader'>
/// The text reader
/// </param>
/// <param name='basePath'>
/// The path to be used to resolve relative file paths.
/// </param>
public static AddinDescription Read (TextReader reader, string basePath)
{
AddinDescription config = new AddinDescription ();
try {
config.configDoc = new XmlDocument ();
config.configDoc.Load (reader);
} catch (Exception ex) {
throw new InvalidOperationException ("The add-in configuration file is invalid: " + ex.Message, ex);
}
XmlElement elem = config.configDoc.DocumentElement;
if (elem.LocalName == "ExtensionModel")
return config;
config.id = elem.GetAttribute ("id");
config.ns = elem.GetAttribute ("namespace");
config.name = elem.GetAttribute ("name");
config.version = elem.GetAttribute ("version");
config.compatVersion = elem.GetAttribute ("compatVersion");
config.author = elem.GetAttribute ("author");
config.url = elem.GetAttribute ("url");
config.copyright = elem.GetAttribute ("copyright");
config.description = elem.GetAttribute ("description");
config.category = elem.GetAttribute ("category");
config.basePath = elem.GetAttribute ("basePath");
config.domain = "global";
string s = elem.GetAttribute ("isRoot");
if (s.Length == 0) s = elem.GetAttribute ("isroot");
config.isroot = GetBool (s, false);
config.defaultEnabled = GetBool (elem.GetAttribute ("defaultEnabled"), true);
string prot = elem.GetAttribute ("flags");
if (prot.Length == 0)
config.flags = AddinFlags.None;
else
config.flags = (AddinFlags) Enum.Parse (typeof(AddinFlags), prot);
XmlElement localizerElem = (XmlElement) elem.SelectSingleNode ("Localizer");
if (localizerElem != null)
config.localizer = new ExtensionNodeDescription (localizerElem);
XmlElement headerElem = (XmlElement) elem.SelectSingleNode ("Header");
if (headerElem != null) {
foreach (XmlNode node in headerElem.ChildNodes) {
XmlElement prop = node as XmlElement;
if (prop == null)
continue;
config.Properties.SetPropertyValue (prop.LocalName, prop.GetAttribute ("locale"), prop.InnerText);
}
}
if (config.id.Length > 0)
config.hasUserId = true;
return config;
}
static bool GetBool (string s, bool defval)
{
if (s.Length == 0)
return defval;
else
return s == "true" || s == "yes";
}
internal static AddinDescription ReadBinary (FileDatabase fdb, string configFile)
{
AddinDescription description = (AddinDescription) fdb.ReadSharedObject (configFile, typeMap);
if (description != null) {
description.FileName = configFile;
description.canWrite = !fdb.IgnoreDescriptionData;
}
return description;
}
internal void SaveBinary (FileDatabase fdb, string file)
{
configFile = file;
SaveBinary (fdb);
}
internal void SaveBinary (FileDatabase fdb)
{
if (!canWrite)
throw new InvalidOperationException ("Can't write incomplete description.");
fdb.WriteSharedObject (AddinFile, FileName, typeMap, this);
// BinaryXmlReader.DumpFile (configFile);
}
/// <summary>
/// Verify this instance.
/// </summary>
/// <remarks>
/// This method checks all the definitions in the description and returns a list of errors.
/// If the returned list is empty, it means that the description is valid.
/// </remarks>
public StringCollection Verify ()
{
StringCollection errors = new StringCollection ();
if (IsRoot) {
if (OptionalModules.Count > 0)
errors.Add ("Root add-in hosts can't have optional modules.");
}
if (AddinId.Length == 0 || Version.Length == 0) {
if (ExtensionPoints.Count > 0)
errors.Add ("Add-ins which define new extension points must have an Id and Version.");
}
MainModule.Verify ("", errors);
OptionalModules.Verify ("", errors);
ExtensionNodeSets.Verify ("", errors);
ExtensionPoints.Verify ("", errors);
ConditionTypes.Verify ("", errors);
foreach (ExtensionNodeSet nset in ExtensionNodeSets) {
if (nset.Id.Length == 0)
errors.Add ("Attribute 'id' can't be empty for global node sets.");
}
string bp = null;
if (BasePath.Length > 0)
bp = BasePath;
else if (sourceAddinFile != null && sourceAddinFile.Length > 0)
bp = Path.GetDirectoryName (AddinFile);
else if (configFile != null && configFile.Length > 0)
bp = Path.GetDirectoryName (configFile);
if (bp != null) {
foreach (string file in AllFiles) {
string asmFile = Path.Combine (bp, file);
if (!File.Exists (asmFile))
errors.Add ("The file '" + asmFile + "' referenced in the manifest could not be found.");
}
}
if (localizer != null && localizer.GetAttribute ("type").Length == 0) {
errors.Add ("The attribute 'type' in the Location element is required.");
}
// Ensure that there are no duplicated properties
if (properties != null) {
HashSet<string> props = new HashSet<string> ();
foreach (var prop in properties) {
if (!props.Add (prop.Name + " " + prop.Locale))
errors.Add (string.Format ("Property {0} specified more than once", prop.Name + (prop.Locale != null ? " (" + prop.Locale + ")" : "")));
}
}
return errors;
}
internal void SetExtensionsAddinId (string addinId)
{
foreach (ExtensionPoint ep in ExtensionPoints)
ep.SetExtensionsAddinId (addinId);
foreach (ExtensionNodeSet ns in ExtensionNodeSets)
ns.SetExtensionsAddinId (addinId);
}
internal void UnmergeExternalData (Hashtable addins)
{
// Removes extension types and extension sets coming from other add-ins.
foreach (ExtensionPoint ep in ExtensionPoints)
ep.UnmergeExternalData (AddinId, addins);
foreach (ExtensionNodeSet ns in ExtensionNodeSets)
ns.UnmergeExternalData (AddinId, addins);
}
internal void MergeExternalData (AddinDescription other)
{
// Removes extension types and extension sets coming from other add-ins.
foreach (ExtensionPoint ep in other.ExtensionPoints) {
ExtensionPoint tep = ExtensionPoints [ep.Path];
if (tep != null)
tep.MergeWith (AddinId, ep);
}
foreach (ExtensionNodeSet ns in other.ExtensionNodeSets) {
ExtensionNodeSet tns = ExtensionNodeSets [ns.Id];
if (tns != null)
tns.MergeWith (AddinId, ns);
}
}
internal bool IsExtensionModel {
get { return RootElement.LocalName == "ExtensionModel"; }
}
internal static AddinDescription Merge (AddinDescription desc1, AddinDescription desc2)
{
if (!desc2.IsExtensionModel) {
AddinDescription tmp = desc1;
desc1 = desc2; desc2 = tmp;
}
((AddinPropertyCollectionImpl)desc1.Properties).AddRange (desc2.Properties);
desc1.ExtensionPoints.AddRange (desc2.ExtensionPoints);
desc1.ExtensionNodeSets.AddRange (desc2.ExtensionNodeSets);
desc1.ConditionTypes.AddRange (desc2.ConditionTypes);
desc1.OptionalModules.AddRange (desc2.OptionalModules);
foreach (string s in desc2.MainModule.Assemblies)
desc1.MainModule.Assemblies.Add (s);
foreach (string s in desc2.MainModule.DataFiles)
desc1.MainModule.DataFiles.Add (s);
desc1.MainModule.Dependencies.AddRange (desc2.MainModule.Dependencies);
desc1.MainModule.Extensions.AddRange (desc2.MainModule.Extensions);
return desc1;
}
void IBinaryXmlElement.Write (BinaryXmlWriter writer)
{
writer.WriteValue ("id", id);
writer.WriteValue ("ns", ns);
writer.WriteValue ("isroot", isroot);
writer.WriteValue ("name", name);
writer.WriteValue ("version", version);
writer.WriteValue ("compatVersion", compatVersion);
writer.WriteValue ("hasUserId", hasUserId);
writer.WriteValue ("author", author);
writer.WriteValue ("url", url);
writer.WriteValue ("copyright", copyright);
writer.WriteValue ("description", description);
writer.WriteValue ("category", category);
writer.WriteValue ("basePath", basePath);
writer.WriteValue ("sourceAddinFile", sourceAddinFile);
writer.WriteValue ("defaultEnabled", defaultEnabled);
writer.WriteValue ("domain", domain);
writer.WriteValue ("MainModule", MainModule);
writer.WriteValue ("OptionalModules", OptionalModules);
writer.WriteValue ("NodeSets", ExtensionNodeSets);
writer.WriteValue ("ExtensionPoints", ExtensionPoints);
writer.WriteValue ("ConditionTypes", ConditionTypes);
writer.WriteValue ("FilesInfo", fileInfo);
writer.WriteValue ("Localizer", localizer);
writer.WriteValue ("flags", (int)flags);
writer.WriteValue ("Properties", properties);
}
void IBinaryXmlElement.Read (BinaryXmlReader reader)
{
id = reader.ReadStringValue ("id");
ns = reader.ReadStringValue ("ns");
isroot = reader.ReadBooleanValue ("isroot");
name = reader.ReadStringValue ("name");
version = reader.ReadStringValue ("version");
compatVersion = reader.ReadStringValue ("compatVersion");
hasUserId = reader.ReadBooleanValue ("hasUserId");
author = reader.ReadStringValue ("author");
url = reader.ReadStringValue ("url");
copyright = reader.ReadStringValue ("copyright");
description = reader.ReadStringValue ("description");
category = reader.ReadStringValue ("category");
basePath = reader.ReadStringValue ("basePath");
sourceAddinFile = reader.ReadStringValue ("sourceAddinFile");
defaultEnabled = reader.ReadBooleanValue ("defaultEnabled");
domain = reader.ReadStringValue ("domain");
mainModule = (ModuleDescription) reader.ReadValue ("MainModule");
optionalModules = (ModuleCollection) reader.ReadValue ("OptionalModules", new ModuleCollection (this));
nodeSets = (ExtensionNodeSetCollection) reader.ReadValue ("NodeSets", new ExtensionNodeSetCollection (this));
extensionPoints = (ExtensionPointCollection) reader.ReadValue ("ExtensionPoints", new ExtensionPointCollection (this));
conditionTypes = (ConditionTypeDescriptionCollection) reader.ReadValue ("ConditionTypes", new ConditionTypeDescriptionCollection (this));
fileInfo = (object[]) reader.ReadValue ("FilesInfo", null);
localizer = (ExtensionNodeDescription) reader.ReadValue ("Localizer");
flags = (AddinFlags) reader.ReadInt32Value ("flags");
properties = (AddinPropertyCollectionImpl) reader.ReadValue ("Properties", new AddinPropertyCollectionImpl ());
if (mainModule != null)
mainModule.SetParent (this);
}
}
class AddinFileInfo: IBinaryXmlElement
{
string fileName;
DateTime timestamp;
public string FileName {
get {
return fileName;
}
set {
fileName = value;
}
}
public System.DateTime Timestamp {
get {
return timestamp;
}
set {
timestamp = value;
}
}
public void Read (BinaryXmlReader reader)
{
fileName = reader.ReadStringValue ("fileName");
timestamp = reader.ReadDateTimeValue ("timestamp");
}
public void Write (BinaryXmlWriter writer)
{
writer.WriteValue ("fileName", fileName);
writer.WriteValue ("timestamp", timestamp);
}
}
}
|