// 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.Diagnostics;
using System.Globalization;
using System.Xml;
///
/// Field containing data for an object column in a row.
///
public sealed class ObjectField : Field
{
///
/// Instantiates a new Field.
///
/// Column definition for this field.
internal ObjectField(ColumnDefinition columnDefinition) :
base(columnDefinition)
{
}
///
/// Gets or sets the index of the embedded file in a library.
///
/// The index of the embedded file.
public int? EmbeddedFileIndex { get; set; }
///
/// Gets or sets the previous index of the embedded file in the library.
///
/// The previous index of the embedded file.
public int? PreviousEmbeddedFileIndex { get; set; }
///
/// Gets or sets the path to the embedded cabinet of the previous file.
///
/// The path of the cabinet containing the previous file.
public Uri PreviousBaseUri { get; set; }
///
/// Gets the base URI of the object field.
///
/// The base URI of the object field.
public Uri BaseUri { get; private set; }
///
/// Gets or sets the unresolved data for this field.
///
/// Unresolved Data in the field.
public string UnresolvedData { get; set; }
///
/// Gets or sets the unresolved previous data.
///
/// The unresolved previous data.
public string UnresolvedPreviousData { get; set; }
///
/// Parse a field from the xml.
///
/// XmlReader where the intermediate is persisted.
internal override void Read(XmlReader reader)
{
Debug.Assert("field" == reader.LocalName);
bool empty = reader.IsEmptyElement;
this.BaseUri = new Uri(reader.BaseURI);
while (reader.MoveToNextAttribute())
{
switch (reader.LocalName)
{
case "cabinetFileId":
this.EmbeddedFileIndex = Convert.ToInt32(reader.Value);
break;
case "modified":
this.Modified = reader.Value.Equals("yes");
break;
case "previousData":
this.PreviousData = reader.Value;
break;
case "unresolvedPreviousData":
this.UnresolvedPreviousData = reader.Value;
break;
case "unresolvedData":
this.UnresolvedData = reader.Value;
break;
case "previousCabinetFileId":
this.PreviousEmbeddedFileIndex = Convert.ToInt32(reader.Value);
break;
}
}
if (!empty)
{
bool done = false;
while (!done && reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
throw new XmlException();
case XmlNodeType.CDATA:
case XmlNodeType.Text:
if (0 < reader.Value.Length)
{
this.Data = reader.Value;
}
break;
case XmlNodeType.EndElement:
done = true;
break;
}
}
if (!done)
{
throw new XmlException();
}
}
}
///
/// Persists a field in an XML format.
///
/// XmlWriter where the Field should persist itself as XML.
internal override void Write(XmlWriter writer)
{
writer.WriteStartElement("field", Intermediate.XmlNamespaceUri);
if (this.EmbeddedFileIndex.HasValue)
{
writer.WriteStartAttribute("cabinetFileId");
writer.WriteValue(this.EmbeddedFileIndex);
writer.WriteEndAttribute();
}
if (this.Modified)
{
writer.WriteAttributeString("modified", "yes");
}
if (null != this.UnresolvedPreviousData)
{
writer.WriteAttributeString("unresolvedPreviousData", this.UnresolvedPreviousData);
}
if (null != this.PreviousData)
{
writer.WriteAttributeString("previousData", this.PreviousData);
}
if (null != this.UnresolvedData)
{
writer.WriteAttributeString("unresolvedData", this.UnresolvedData);
}
if (this.PreviousEmbeddedFileIndex.HasValue)
{
writer.WriteStartAttribute("previousCabinetFileId");
writer.WriteValue(this.PreviousEmbeddedFileIndex);
writer.WriteEndAttribute();
}
// Convert the data to a string that will persist nicely (nulls as String.Empty).
string text = Convert.ToString(this.Data, CultureInfo.InvariantCulture);
if (this.Column.UseCData)
{
writer.WriteCData(text);
}
else
{
writer.WriteString(text);
}
writer.WriteEndElement();
}
}
}