// 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
{
#if false
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Xml;
///
/// Section in an object file.
///
public sealed class Section
{
///
/// Creates a new section as part of an intermediate.
///
/// Identifier for section.
/// Type of section.
/// Codepage for resulting database.
public Section(string id, SectionType type, int codepage)
{
this.Id = id;
this.Type = type;
this.Codepage = codepage;
this.Tables = new TableIndexedCollection();
}
///
/// Gets the identifier for the section.
///
/// Section identifier.
public string Id { get; private set; }
///
/// Gets the type of the section.
///
/// Type of section.
public SectionType Type { get; private set; }
///
/// Gets the codepage for the section.
///
/// Codepage for the section.
public int Codepage { get; private set; }
///
/// Gets the tables in the section.
///
/// Tables in section.
public TableIndexedCollection Tables { get; private set; }
///
/// Gets the source line information of the file containing this section.
///
/// The source line information of the file containing this section.
public SourceLineNumber SourceLineNumbers { get; private set; }
///
/// Gets the identity of the intermediate the section is contained within.
///
public string IntermediateId { get; internal set; }
///
/// Gets the identity of the library when the section is contained within one.
///
public string LibraryId { get; internal set; }
///
/// Ensures a table is added to the section's table collection.
///
/// Table definition for the table.
/// Table in the section.
public Table EnsureTable(TableDefinition tableDefinition)
{
Table table;
if (!this.Tables.TryGetTable(tableDefinition.Name, out table))
{
table = new Table(this, tableDefinition);
this.Tables.Add(table);
}
return table;
}
///
/// Parse a section from the xml.
///
/// XmlReader where the intermediate is persisted.
/// TableDefinitions to use in the intermediate.
/// The parsed Section.
internal static Section Read(XmlReader reader, TableDefinitionCollection tableDefinitions)
{
Debug.Assert("section" == reader.LocalName);
int codepage = 0;
bool empty = reader.IsEmptyElement;
string id = null;
SectionType type = SectionType.Unknown;
while (reader.MoveToNextAttribute())
{
switch (reader.Name)
{
case "codepage":
codepage = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture);
break;
case "id":
id = reader.Value;
break;
case "type":
switch (reader.Value)
{
case "bundle":
type = SectionType.Bundle;
break;
case "fragment":
type = SectionType.Fragment;
break;
case "module":
type = SectionType.Module;
break;
case "patchCreation":
type = SectionType.PatchCreation;
break;
case "product":
type = SectionType.Product;
break;
case "patch":
type = SectionType.Patch;
break;
default:
throw new XmlException();
}
break;
}
}
if (null == id && (SectionType.Unknown != type && SectionType.Fragment != type))
{
throw new XmlException();
}
if (SectionType.Unknown == type)
{
throw new XmlException();
}
Section section = new Section(id, type, codepage);
section.SourceLineNumbers = SourceLineNumber.CreateFromUri(reader.BaseURI);
List
tables = new List();
if (!empty)
{
bool done = false;
while (!done && reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
switch (reader.LocalName)
{
case "table":
tables.Add(Table.Read(reader, section, tableDefinitions));
break;
default:
throw new XmlException();
}
break;
case XmlNodeType.EndElement:
done = true;
break;
}
}
if (!done)
{
throw new XmlException();
}
}
section.Tables = new TableIndexedCollection(tables);
return section;
}
///
/// Persist the Section to an XmlWriter.
///
/// XmlWriter which reference will be persisted to.
internal void Write(XmlWriter writer)
{
writer.WriteStartElement("section", Intermediate.XmlNamespaceUri);
if (null != this.Id)
{
writer.WriteAttributeString("id", this.Id);
}
switch (this.Type)
{
case SectionType.Bundle:
writer.WriteAttributeString("type", "bundle");
break;
case SectionType.Fragment:
writer.WriteAttributeString("type", "fragment");
break;
case SectionType.Module:
writer.WriteAttributeString("type", "module");
break;
case SectionType.Product:
writer.WriteAttributeString("type", "product");
break;
case SectionType.PatchCreation:
writer.WriteAttributeString("type", "patchCreation");
break;
case SectionType.Patch:
writer.WriteAttributeString("type", "patch");
break;
}
if (0 != this.Codepage)
{
writer.WriteAttributeString("codepage", this.Codepage.ToString());
}
// save the rows in table order
foreach (Table table in this.Tables.OrderBy(t => t.Name))
{
table.Write(writer);
}
writer.WriteEndElement();
}
}
#endif
}