// 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
{
private readonly List symbols;
///
/// Creates a new section as part of an intermediate.
///
/// Identifier for section.
/// Type of section.
/// Codepage for resulting database.
/// Optional compilation identifier
public IntermediateSection(string id, SectionType type, int codepage, string compilationId = null)
{
this.Id = id;
this.Type = type;
this.Codepage = codepage;
this.CompilationId = compilationId;
this.symbols = 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; }
///
/// Gets and sets the identifier of the compilation of the source file containing the section.
///
public string CompilationId { get; }
///
/// Gets and sets the identifier of the library that combined the section.
///
public string LibraryId { get; private set; }
///
/// Symbols in the section.
///
public IReadOnlyCollection Symbols => this.symbols;
///
/// Adds a symbol to the section.
///
/// Type of IntermediateSymbol to add to the section.
/// Symbol to add to the section.
/// Symbol added to the section.
public T AddSymbol(T symbol) where T : IntermediateSymbol
{
this.symbols.Add(symbol);
return symbol;
}
///
/// Assigns the section to a library.
///
/// Identifier of the library.
public void AssignToLibrary(string libraryId)
{
this.LibraryId = libraryId;
}
///
/// Removes a symbol from the section.
///
/// Symbol to remove.
/// True if the symbol was removed; otherwise false.
public bool RemoveSymbol(IntermediateSymbol symbol)
{
return this.symbols.Remove(symbol);
}
///
/// Parse a section from the JSON data.
///
internal static IntermediateSection Deserialize(ISymbolDefinitionCreator creator, Uri baseUri, JsonObject jsonObject)
{
var codepage = jsonObject.GetValueOrDefault("codepage", 0);
var id = jsonObject.GetValueOrDefault("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 symbolsJson = jsonObject.GetValueOrDefault("symbols");
foreach (JsonObject symbolJson in symbolsJson)
{
var symbol = IntermediateSymbol.Deserialize(creator, baseUri, symbolJson);
section.symbols.Add(symbol);
}
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 symbolsJson = new JsonArray(this.Symbols.Count);
foreach (var symbol in this.Symbols)
{
var symbolJson = symbol.Serialize();
symbolsJson.Add(symbolJson);
}
jsonObject.Add("symbols", symbolsJson);
return jsonObject;
}
}
}