aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Rows/WixActionRowCollection.cs
blob: d72198ee52de4e32bfa657903e8e8ab48a2d125f (plain)
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
// 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.Core.WindowsInstaller.Rows
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Globalization;
    using System.Xml;
    using WixToolset.Data.Tuples;
    using WixToolset.Data.WindowsInstaller.Rows;

    /// <summary>
    /// A collection of action rows sorted by their sequence table and action name.
    /// </summary>
    internal sealed class WixActionRowCollection : ICollection
    {
        private SortedList collection;

        /// <summary>
        /// Creates a new action table object.
        /// </summary>
        public WixActionRowCollection()
        {
            this.collection = new SortedList();
        }

        /// <summary>
        /// Gets the number of items in the collection.
        /// </summary>
        /// <value>Number of items in collection.</value>
        public int Count
        {
            get { return this.collection.Count; }
        }

        /// <summary>
        /// Gets if the collection has been synchronized.
        /// </summary>
        /// <value>True if the collection has been synchronized.</value>
        public bool IsSynchronized
        {
            get { return this.collection.IsSynchronized; }
        }

        /// <summary>
        /// Gets the object used to synchronize the collection.
        /// </summary>
        /// <value>Oject used the synchronize the collection.</value>
        public object SyncRoot
        {
            get { return this; }
        }

        /// <summary>
        /// Get an ActionRow by its sequence table and action name.
        /// </summary>
        /// <param name="sequenceTable">The sequence table of the ActionRow.</param>
        /// <param name="action">The action name of the ActionRow.</param>
        public WixActionRow this[SequenceTable sequenceTable, string action]
        {
            get { return (WixActionRow)this.collection[GetKey(sequenceTable, action)]; }
        }

        /// <summary>
        /// Add an ActionRow to the collection.
        /// </summary>
        /// <param name="actionRow">The ActionRow to add.</param>
        /// <param name="overwrite">true to overwrite an existing ActionRow; false otherwise.</param>
        public void Add(WixActionRow actionRow, bool overwrite)
        {
            string key = GetKey(actionRow.SequenceTable, actionRow.Action);

            if (overwrite)
            {
                this.collection[key] = actionRow;
            }
            else
            {
                this.collection.Add(key, actionRow);
            }
        }

        /// <summary>
        /// Add an ActionRow to the collection.
        /// </summary>
        /// <param name="actionRow">The ActionRow to add.</param>
        public void Add(WixActionRow actionRow)
        {
            this.Add(actionRow, false);
        }

        /// <summary>
        /// Determines if the collection contains an ActionRow with a specific sequence table and name.
        /// </summary>
        /// <param name="sequenceTable">The sequence table of the ActionRow.</param>
        /// <param name="action">The action name of the ActionRow.</param>
        /// <returns>true if the ActionRow was found; false otherwise.</returns>
        public bool Contains(SequenceTable sequenceTable, string action)
        {
            return this.collection.Contains(GetKey(sequenceTable, action));
        }

        /// <summary>
        /// Copies the collection into an array.
        /// </summary>
        /// <param name="array">Array to copy the collection into.</param>
        /// <param name="index">Index to start copying from.</param>
        public void CopyTo(System.Array array, int index)
        {
            this.collection.Values.CopyTo(array, index);
        }

        /// <summary>
        /// Gets the enumerator for the collection.
        /// </summary>
        /// <returns>The enumerator for the collection.</returns>
        public IEnumerator GetEnumerator()
        {
            return this.collection.Values.GetEnumerator();
        }

        /// <summary>
        /// Remove an ActionRow from the collection.
        /// </summary>
        /// <param name="sequenceTable">The sequence table of the ActionRow.</param>
        /// <param name="action">The action name of the ActionRow.</param>
        public void Remove(SequenceTable sequenceTable, string action)
        {
            this.collection.Remove(GetKey(sequenceTable, action));
        }

        /// <summary>
        /// Load an action table from an XmlReader.
        /// </summary>
        /// <param name="reader">Reader to get data from.</param>
        /// <returns>The ActionRowCollection represented by the xml.</returns>
        internal static WixActionRowCollection Load(XmlReader reader)
        {
            reader.MoveToContent();

            return Parse(reader);
        }

        /// <summary>
        /// Creates a new action table object and populates it from an Xml reader.
        /// </summary>
        /// <param name="reader">Reader to get data from.</param>
        /// <returns>The parsed ActionTable.</returns>
        private static WixActionRowCollection Parse(XmlReader reader)
        {
            if (!reader.LocalName.Equals("actions"))
            {
                throw new XmlException();
            }

            WixActionRowCollection actionRows = new WixActionRowCollection();
            bool empty = reader.IsEmptyElement;

            while (reader.MoveToNextAttribute())
            {
            }

            if (!empty)
            {
                bool done = false;

                // loop through all the fields in a row
                while (!done && reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                        switch (reader.LocalName)
                        {
                            case "action":
                                WixActionRow[] parsedActionRows = ParseActions(reader);

                                foreach (WixActionRow actionRow in parsedActionRows)
                                {
                                    actionRows.Add(actionRow);
                                }
                                break;
                            default:
                                throw new XmlException();
                        }
                            break;
                        case XmlNodeType.EndElement:
                            done = true;
                            break;
                    }
                }

                if (!done)
                {
                    throw new XmlException();
                }
            }

            return actionRows;
        }

        /// <summary>
        /// Get the key for storing an ActionRow.
        /// </summary>
        /// <param name="sequenceTable">The sequence table of the ActionRow.</param>
        /// <param name="action">The action name of the ActionRow.</param>
        /// <returns>The string key.</returns>
        private static string GetKey(SequenceTable sequenceTable, string action)
        {
            return GetKey(sequenceTable.ToString(), action);
        }

        /// <summary>
        /// Get the key for storing an ActionRow.
        /// </summary>
        /// <param name="sequenceTable">The sequence table of the ActionRow.</param>
        /// <param name="action">The action name of the ActionRow.</param>
        /// <returns>The string key.</returns>
        private static string GetKey(string sequenceTable, string action)
        {
            return String.Concat(sequenceTable, '/', action);
        }

        /// <summary>
        /// Parses ActionRows from the Xml reader.
        /// </summary>
        /// <param name="reader">Xml reader that contains serialized ActionRows.</param>
        /// <returns>The parsed ActionRows.</returns>
        internal static WixActionRow[] ParseActions(XmlReader reader)
        {
            Debug.Assert("action" == reader.LocalName);

            string id = null;
            string condition = null;
            bool empty = reader.IsEmptyElement;
            int sequence = int.MinValue;
            int sequenceCount = 0;
            SequenceTable[] sequenceTables = new SequenceTable[Enum.GetValues(typeof(SequenceTable)).Length];

            while (reader.MoveToNextAttribute())
            {
                switch (reader.Name)
                {
                    case "name":
                        id = reader.Value;
                        break;
                    case "AdminExecuteSequence":
                        if (reader.Value.Equals("yes"))
                        {
                            sequenceTables[sequenceCount] = SequenceTable.AdminExecuteSequence;
                            ++sequenceCount;
                        }
                        break;
                    case "AdminUISequence":
                        if (reader.Value.Equals("yes"))
                        {
                            sequenceTables[sequenceCount] = SequenceTable.AdminUISequence;
                            ++sequenceCount;
                        }
                        break;
                    case "AdvtExecuteSequence":
                        if (reader.Value.Equals("yes"))
                        {
                            sequenceTables[sequenceCount] = SequenceTable.AdvtExecuteSequence;
                            ++sequenceCount;
                        }
                        break;
                    case "condition":
                        condition = reader.Value;
                        break;
                    case "InstallExecuteSequence":
                        if (reader.Value.Equals("yes"))
                        {
                            sequenceTables[sequenceCount] = SequenceTable.InstallExecuteSequence;
                            ++sequenceCount;
                        }
                        break;
                    case "InstallUISequence":
                        if (reader.Value.Equals("yes"))
                        {
                            sequenceTables[sequenceCount] = SequenceTable.InstallUISequence;
                            ++sequenceCount;
                        }
                        break;
                    case "sequence":
                        sequence = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
                        break;
                }
            }

            if (null == id)
            {
                throw new XmlException();
            }

            if (int.MinValue == sequence)
            {
                throw new XmlException();
            }
            else if (1 > sequence)
            {
                throw new XmlException();
            }

            if (0 == sequenceCount)
            {
                throw new XmlException();
            }

            if (!empty && reader.Read() && XmlNodeType.EndElement != reader.MoveToContent())
            {
                throw new XmlException();
            }

            // create the actions
            WixActionRow[] actionRows = new WixActionRow[sequenceCount];
            for (int i = 0; i < sequenceCount; i++)
            {
                //WixActionRow actionRow = new WixActionRow(sequenceTables[i], id, condition, sequence);
                //actionRows[i] = actionRow;
                throw new NotImplementedException();
            }

            return actionRows;
        }
    }
}