| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.BootstrapperApplicationApi |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Xml.XPath; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Utility class for reading BootstrapperApplicationData.xml. |
| 11 | /// </summary> |
| 12 | public class BootstrapperApplicationData : IBootstrapperApplicationData |
| 13 | { |
| 14 | /// <summary> |
| 15 | /// The default file name for BootstrapperApplicationData. |
| 16 | /// </summary> |
| 17 | public const string DefaultFileName = "BootstrapperApplicationData.xml"; |
| 18 | |
| 19 | /// <summary> |
| 20 | /// The XML namespace for BootstrapperApplicationData. |
| 21 | /// </summary> |
| 22 | public const string XMLNamespace = "http://wixtoolset.org/schemas/v4/BootstrapperApplicationData"; |
| 23 | |
| 24 | /// <summary> |
| 25 | /// The default path of where the BA was extracted to. |
| 26 | /// </summary> |
| 27 | public static readonly DirectoryInfo DefaultFolder; |
| 28 | |
| 29 | /// <summary> |
| 30 | /// The default path to BootstrapperApplicationData.xml. |
| 31 | /// </summary> |
| 32 | public static readonly FileInfo DefaultFile; |
| 33 | |
| 34 | static BootstrapperApplicationData() |
| 35 | { |
| 36 | DefaultFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); |
| 37 | DefaultFile = new FileInfo(Path.Combine(DefaultFolder.FullName, DefaultFileName)); |
| 38 | } |
| 39 | |
| 40 | /// <inheritdoc/> |
| 41 | public FileInfo BADataFile { get; private set; } |
| 42 | |
| 43 | /// <inheritdoc/> |
| 44 | public IBundleInfo Bundle { get; private set; } |
| 45 | |
| 46 | /// <summary> |
| 47 | /// Uses the default location for BootstrapperApplicationData.xml. |
| 48 | /// </summary> |
| 49 | public BootstrapperApplicationData() : this(DefaultFile) { } |
| 50 | |
| 51 | /// <summary> |
| 52 | /// Uses the given file for BootstrapperApplicationData.xml. |
| 53 | /// </summary> |
| 54 | /// <param name="baDataFile"></param> |
| 55 | public BootstrapperApplicationData(FileInfo baDataFile) |
| 56 | { |
| 57 | this.BADataFile = baDataFile; |
| 58 | |
| 59 | using (FileStream fs = this.BADataFile.OpenRead()) |
| 60 | { |
| 61 | this.Bundle = BundleInfo.ParseBundleFromStream(fs); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// <summary> |
| 66 | /// Utility method for parsing BootstrapperApplicationData.xml. |
| 67 | /// </summary> |
| 68 | /// <param name="node"></param> |
| 69 | /// <param name="attributeName"></param> |
| 70 | /// <returns></returns> |
| 71 | public static string GetAttribute(XPathNavigator node, string attributeName) |
| 72 | { |
| 73 | XPathNavigator attribute = node.SelectSingleNode("@" + attributeName); |
| 74 | |
| 75 | if (attribute == null) |
| 76 | { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | return attribute.Value; |
| 81 | } |
| 82 | |
| 83 | /// <summary> |
| 84 | /// Utility method for parsing BootstrapperApplicationData.xml. |
| 85 | /// </summary> |
| 86 | /// <param name="node"></param> |
| 87 | /// <param name="attributeName"></param> |
| 88 | /// <returns></returns> |
| 89 | public static bool? GetYesNoAttribute(XPathNavigator node, string attributeName) |
| 90 | { |
| 91 | string attributeValue = GetAttribute(node, attributeName); |
| 92 | |
| 93 | if (attributeValue == null) |
| 94 | { |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | return attributeValue.Equals("yes", StringComparison.InvariantCulture); |
| 99 | } |
| 100 | } |
| 101 | } |