diff options
Diffstat (limited to 'src/internal/TablesAndTuples/WixTableDefinition.cs')
-rw-r--r-- | src/internal/TablesAndTuples/WixTableDefinition.cs | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/internal/TablesAndTuples/WixTableDefinition.cs b/src/internal/TablesAndTuples/WixTableDefinition.cs new file mode 100644 index 00000000..61dcbb0a --- /dev/null +++ b/src/internal/TablesAndTuples/WixTableDefinition.cs | |||
@@ -0,0 +1,169 @@ | |||
1 | using System.Collections.Generic; | ||
2 | using System.Linq; | ||
3 | using System.Xml; | ||
4 | |||
5 | namespace TablesAndSymbols | ||
6 | { | ||
7 | class WixTableDefinition | ||
8 | { | ||
9 | public WixTableDefinition(string name, IEnumerable<WixColumnDefinition> columns, bool unreal, bool symbolless, string symbolDefinitionName, bool? symbolIdIsPrimaryKey) | ||
10 | { | ||
11 | this.Name = name; | ||
12 | this.VariableName = name.Replace("_", ""); | ||
13 | this.Unreal = unreal; | ||
14 | this.Columns = columns?.ToArray(); | ||
15 | this.Symbolless = symbolless; | ||
16 | this.SymbolDefinitionName = symbolless ? null : symbolDefinitionName ?? this.VariableName; | ||
17 | this.SymbolIdIsPrimaryKey = symbolIdIsPrimaryKey ?? DeriveSymbolIdIsPrimaryKey(this.Columns); | ||
18 | } | ||
19 | |||
20 | public string Name { get; } | ||
21 | |||
22 | public string VariableName { get; } | ||
23 | |||
24 | public string SymbolDefinitionName { get; } | ||
25 | |||
26 | public bool Unreal { get; } | ||
27 | |||
28 | public WixColumnDefinition[] Columns { get; } | ||
29 | |||
30 | public bool SymbolIdIsPrimaryKey { get; } | ||
31 | |||
32 | public bool Symbolless { get; } | ||
33 | |||
34 | static WixTableDefinition Read(XmlReader reader) | ||
35 | { | ||
36 | var empty = reader.IsEmptyElement; | ||
37 | string name = null; | ||
38 | string symbolDefinitionName = null; | ||
39 | var unreal = false; | ||
40 | bool? symbolIdIsPrimaryKey = null; | ||
41 | var symbolless = false; | ||
42 | |||
43 | while (reader.MoveToNextAttribute()) | ||
44 | { | ||
45 | switch (reader.LocalName) | ||
46 | { | ||
47 | case "name": | ||
48 | name = reader.Value; | ||
49 | break; | ||
50 | case "symbolDefinitionName": | ||
51 | symbolDefinitionName = reader.Value; | ||
52 | break; | ||
53 | case "symbolIdIsPrimaryKey": | ||
54 | symbolIdIsPrimaryKey = reader.Value.Equals("yes"); | ||
55 | break; | ||
56 | case "symbolless": | ||
57 | symbolless = reader.Value.Equals("yes"); | ||
58 | break; | ||
59 | case "unreal": | ||
60 | unreal = reader.Value.Equals("yes"); | ||
61 | break; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | if (null == name) | ||
66 | { | ||
67 | throw new XmlException(); | ||
68 | } | ||
69 | |||
70 | var columns = new List<WixColumnDefinition>(); | ||
71 | |||
72 | // parse the child elements | ||
73 | if (!empty) | ||
74 | { | ||
75 | var done = false; | ||
76 | |||
77 | while (!done && reader.Read()) | ||
78 | { | ||
79 | switch (reader.NodeType) | ||
80 | { | ||
81 | case XmlNodeType.Element: | ||
82 | switch (reader.LocalName) | ||
83 | { | ||
84 | case "columnDefinition": | ||
85 | var columnDefinition = WixColumnDefinition.Read(reader); | ||
86 | columns.Add(columnDefinition); | ||
87 | break; | ||
88 | default: | ||
89 | throw new XmlException(); | ||
90 | } | ||
91 | break; | ||
92 | case XmlNodeType.EndElement: | ||
93 | done = true; | ||
94 | break; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | if (!done) | ||
99 | { | ||
100 | throw new XmlException(); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | return new WixTableDefinition(name, columns.ToArray(), unreal, symbolless, symbolDefinitionName, symbolIdIsPrimaryKey); | ||
105 | } | ||
106 | |||
107 | static bool DeriveSymbolIdIsPrimaryKey(WixColumnDefinition[] columns) | ||
108 | { | ||
109 | return columns[0].PrimaryKey && | ||
110 | columns[0].Type == ColumnType.String && | ||
111 | columns[0].Category == ColumnCategory.Identifier && | ||
112 | !columns[0].Name.EndsWith("_") && | ||
113 | (columns.Length == 1 || !columns.Skip(1).Any(t => t.PrimaryKey)); | ||
114 | } | ||
115 | |||
116 | public static List<WixTableDefinition> LoadCollection(string inputPath) | ||
117 | { | ||
118 | using (var reader = XmlReader.Create(inputPath)) | ||
119 | { | ||
120 | reader.MoveToContent(); | ||
121 | |||
122 | if ("tableDefinitions" != reader.LocalName) | ||
123 | { | ||
124 | throw new XmlException(); | ||
125 | } | ||
126 | |||
127 | var empty = reader.IsEmptyElement; | ||
128 | var tableDefinitions = new List<WixTableDefinition>(); | ||
129 | |||
130 | while (reader.MoveToNextAttribute()) | ||
131 | { | ||
132 | } | ||
133 | |||
134 | // parse the child elements | ||
135 | if (!empty) | ||
136 | { | ||
137 | var done = false; | ||
138 | |||
139 | while (!done && reader.Read()) | ||
140 | { | ||
141 | switch (reader.NodeType) | ||
142 | { | ||
143 | case XmlNodeType.Element: | ||
144 | switch (reader.LocalName) | ||
145 | { | ||
146 | case "tableDefinition": | ||
147 | tableDefinitions.Add(WixTableDefinition.Read(reader)); | ||
148 | break; | ||
149 | default: | ||
150 | throw new XmlException(); | ||
151 | } | ||
152 | break; | ||
153 | case XmlNodeType.EndElement: | ||
154 | done = true; | ||
155 | break; | ||
156 | } | ||
157 | } | ||
158 | |||
159 | if (!done) | ||
160 | { | ||
161 | throw new XmlException(); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | return tableDefinitions; | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | } | ||