// 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.IO;
using System.Xml;
///
/// Pdb generated by the binder.
///
public sealed class Pdb
{
public const string XmlNamespaceUri = "http://wixtoolset.org/schemas/v4/wixpdb";
private static readonly Version CurrentVersion = new Version("4.0.0.0");
///
/// Creates a new empty pdb object.
///
/// The source line information for the pdb.
public Pdb()
{
}
///
/// Gets or sets the output that is a part of this pdb.
///
/// Type of the output.
public Output Output { get; set; }
///
/// Loads a pdb from a path on disk.
///
/// Path to pdb file saved on disk.
/// Suppresses wix.dll version mismatch check.
/// Pdb pdb.
public static Pdb Load(string path, bool suppressVersionCheck)
{
using (FileStream stream = File.OpenRead(path))
using (FileStructure fs = FileStructure.Read(stream))
{
if (FileFormat.Wixpdb != fs.FileFormat)
{
throw new WixUnexpectedFileFormatException(path, FileFormat.Wixpdb, fs.FileFormat);
}
Uri uri = new Uri(Path.GetFullPath(path));
using (XmlReader reader = XmlReader.Create(fs.GetDataStream(), null, uri.AbsoluteUri))
{
try
{
reader.MoveToContent();
return Pdb.Read(reader, suppressVersionCheck);
}
catch (XmlException xe)
{
throw new WixCorruptFileException(path, fs.FileFormat, xe);
}
}
}
}
///
/// Saves a pdb to a path on disk.
///
/// Path to save pdb file to on disk.
public void Save(string path)
{
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));
using (FileStream stream = File.Create(path))
using (FileStructure fs = FileStructure.Create(stream, FileFormat.Wixpdb, null))
using (XmlWriter writer = XmlWriter.Create(fs.GetDataStream()))
{
writer.WriteStartDocument();
this.Write(writer);
writer.WriteEndDocument();
}
}
///
/// Processes an XmlReader and builds up the pdb object.
///
/// Reader to get data from.
/// Suppresses wix.dll version mismatch check.
/// The Pdb represented by the Xml.
internal static Pdb Read(XmlReader reader, bool suppressVersionCheck)
{
if ("wixPdb" != reader.LocalName)
{
throw new XmlException();
}
bool empty = reader.IsEmptyElement;
Pdb pdb = new Pdb();
Version version = null;
while (reader.MoveToNextAttribute())
{
switch (reader.LocalName)
{
case "version":
version = new Version(reader.Value);
break;
}
}
if (!suppressVersionCheck && null != version && !Pdb.CurrentVersion.Equals(version))
{
throw new WixException(WixDataErrors.VersionMismatch(SourceLineNumber.CreateFromUri(reader.BaseURI), "wixPdb", version.ToString(), Pdb.CurrentVersion.ToString()));
}
// loop through the rest of the pdb building up the Output object
if (!empty)
{
bool done = false;
// loop through all the fields in a row
while (!done && reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
switch (reader.LocalName)
{
case "wixOutput":
pdb.Output = Output.Read(reader, suppressVersionCheck);
break;
default:
throw new XmlException();
}
break;
case XmlNodeType.EndElement:
done = true;
break;
}
}
if (!done)
{
throw new XmlException();
}
}
return pdb;
}
///
/// Persists a pdb in an XML format.
///
/// XmlWriter where the Pdb should persist itself as XML.
internal void Write(XmlWriter writer)
{
writer.WriteStartElement("wixPdb", XmlNamespaceUri);
writer.WriteAttributeString("version", Pdb.CurrentVersion.ToString());
this.Output.Write(writer);
writer.WriteEndElement();
}
}
}