From 69b15d96cebdbb7201b1849b4f62786633d70b8d Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Wed, 1 Nov 2017 10:56:09 -0700 Subject: Introduce WiX Intermediate Representation --- src/WixToolset.Data/IntermediateSection.cs | 118 +++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/WixToolset.Data/IntermediateSection.cs (limited to 'src/WixToolset.Data/IntermediateSection.cs') diff --git a/src/WixToolset.Data/IntermediateSection.cs b/src/WixToolset.Data/IntermediateSection.cs new file mode 100644 index 00000000..2b1f7375 --- /dev/null +++ b/src/WixToolset.Data/IntermediateSection.cs @@ -0,0 +1,118 @@ +// 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; + + /// + /// Section in an intermediate file. + /// + public class IntermediateSection + { + /// + /// Creates a new section as part of an intermediate. + /// + /// Identifier for section. + /// Type of section. + /// Codepage for resulting database. + public IntermediateSection(string id, SectionType type, int codepage) + { + this.Id = id; + this.Type = type; + this.Codepage = codepage; + this.Tuples = new List(); + } + + /// + /// Gets the identifier for the section. + /// + /// Section identifier. + public string Id { get; } + + /// + /// Gets the type of the section. + /// + /// Type of section. + public SectionType Type { get; } + + /// + /// Gets the codepage for the section. + /// + /// Codepage for the section. + public int Codepage { get; set; } + + /// + /// Gets and sets the identifier of the compilation of the source file containing the section. + /// + public string CompilationId { get; set; } + + /// + /// Gets and sets the identifier of the library that combined the section. + /// + public string LibraryId { get; set; } + + /// + /// Tuples in the section. + /// + public IList Tuples { get; } + + /// + /// Parse a section from the JSON data. + /// + internal static IntermediateSection Deserialize(ITupleDefinitionCreator creator, JsonObject jsonObject) + { + var codepage = jsonObject.GetValueOrDefault("codepage", 0); + var id = jsonObject.GetValueOrDefault("id"); + var type = jsonObject.GetEnumOrDefault("type", SectionType.Unknown); + var tuplesJson = jsonObject.GetValueOrDefault("tuples"); + + if (null == id && (SectionType.Unknown != type && SectionType.Fragment != type)) + { + throw new ArgumentException("JSON object is not a valid section"); + } + + if (SectionType.Unknown == type) + { + throw new ArgumentException("JSON object is not a valid section", nameof(type)); + } + + var section = new IntermediateSection(id, type, codepage); + + foreach (JsonObject tupleJson in tuplesJson) + { + var tuple = IntermediateTuple.Deserialize(creator, 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; + } + } +} -- cgit v1.2.3-55-g6feb