aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/IntermediateSection.cs
blob: b46d20ba220d6c35f93ac6feb2e4fe9a31cea0a9 (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
// 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;
    using System.Collections.Generic;
    using SimpleJson;

    /// <summary>
    /// Section in an intermediate file.
    /// </summary>
    public class IntermediateSection
    {
        /// <summary>
        /// Creates a new section as part of an intermediate.
        /// </summary>
        /// <param name="id">Identifier for section.</param>
        /// <param name="type">Type of section.</param>
        /// <param name="codepage">Codepage for resulting database.</param>
        public IntermediateSection(string id, SectionType type, int codepage)
        {
            this.Id = id;
            this.Type = type;
            this.Codepage = codepage;
            this.Tuples = new List<IntermediateTuple>();
        }

        /// <summary>
        /// Gets the identifier for the section.
        /// </summary>
        /// <value>Section identifier.</value>
        public string Id { get; }

        /// <summary>
        /// Gets the type of the section.
        /// </summary>
        /// <value>Type of section.</value>
        public SectionType Type { get; }

        /// <summary>
        /// Gets the codepage for the section.
        /// </summary>
        /// <value>Codepage for the section.</value>
        public int Codepage { get; set; }

        /// <summary>
        /// Gets and sets the identifier of the compilation of the source file containing the section.
        /// </summary>
        public string CompilationId { get; set; }

        /// <summary>
        /// Gets and sets the identifier of the library that combined the section.
        /// </summary>
        public string LibraryId { get; set; }

        /// <summary>
        /// Tuples in the section.
        /// </summary>
        public IList<IntermediateTuple> Tuples { get; }

        /// <summary>
        /// Parse a section from the JSON data.
        /// </summary>
        internal static IntermediateSection Deserialize(ITupleDefinitionCreator creator, Uri baseUri, JsonObject jsonObject)
        {
            var codepage = jsonObject.GetValueOrDefault("codepage", 0);
            var id = jsonObject.GetValueOrDefault<string>("id");
            var type = jsonObject.GetEnumOrDefault("type", SectionType.Unknown);

            if (SectionType.Unknown == type)
            {
                throw new ArgumentException("JSON object is not a valid section, unknown section type", nameof(type));
            }

            var section = new IntermediateSection(id, type, codepage);

            var tuplesJson = jsonObject.GetValueOrDefault<JsonArray>("tuples");

            foreach (JsonObject tupleJson in tuplesJson)
            {
                var tuple = IntermediateTuple.Deserialize(creator, baseUri, tupleJson);
                section.Tuples.Add(tuple);
            }

            return section;
        }

        internal JsonObject Serialize()
        {
            var jsonObject = new JsonObject
            {
                { "type", this.Type.ToString().ToLowerInvariant() },
                { "codepage", this.Codepage }
            };

            if (!String.IsNullOrEmpty(this.Id))
            {
                jsonObject.Add("id", this.Id);
            }

            var tuplesJson = new JsonArray(this.Tuples.Count);

            foreach (var tuple in this.Tuples)
            {
                var tupleJson = tuple.Serialize();
                tuplesJson.Add(tupleJson);
            }

            jsonObject.Add("tuples", tuplesJson);

            return jsonObject;
        }
    }
}