aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data.WindowsInstaller/SubStorage.cs
blob: e136bfe98cd3c0316733242d6af9c6ec2b684610 (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
// 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.Data
{
    using System.Xml;

    /// <summary>
    /// Substorage inside an output.
    /// </summary>
    public sealed class SubStorage
    {
        /// <summary>
        /// Instantiate a new substorage.
        /// </summary>
        /// <param name="name">The substorage name.</param>
        /// <param name="data">The substorage data.</param>
        public SubStorage(string name, Output data)
        {
            this.Name = name;
            this.Data = data;
        }

        /// <summary>
        /// Gets the substorage name.
        /// </summary>
        /// <value>The substorage name.</value>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the substorage data.
        /// </summary>
        /// <value>The substorage data.</value>
        public Output Data { get; private set; }

        /// <summary>
        /// Creates a SubStorage from the XmlReader.
        /// </summary>
        /// <param name="reader">Reader to get data from.</param>
        /// <returns>New SubStorage object.</returns>
        internal static SubStorage Read(XmlReader reader)
        {
            if (!reader.LocalName.Equals("subStorage" == reader.LocalName))
            {
                throw new XmlException();
            }

            Output data = null;
            bool empty = reader.IsEmptyElement;
            string name = null;

            while (reader.MoveToNextAttribute())
            {
                switch (reader.LocalName)
                {
                    case "name":
                        name = reader.Value;
                        break;
                }
            }

            if (!empty)
            {
                bool done = false;

                while (!done && reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            switch (reader.LocalName)
                            {
                                case "wixOutput":
                                    data = Output.Read(reader, true);
                                    break;
                                default:
                                    throw new XmlException();
                            }
                            break;
                        case XmlNodeType.EndElement:
                            done = true;
                            break;
                    }
                }

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

            return new SubStorage(name, data);
        }

        /// <summary>
        /// Persists a SubStorage in an XML format.
        /// </summary>
        /// <param name="writer">XmlWriter where the SubStorage should persist itself as XML.</param>
        internal void Write(XmlWriter writer)
        {
            writer.WriteStartElement("subStorage", Output.XmlNamespaceUri);

            writer.WriteAttributeString("name", this.Name);

            this.Data.Write(writer);

            writer.WriteEndElement();
        }
    }
}