| 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.Core.WindowsInstaller.ExtensibilityServices |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Xml.Linq; |
| 8 | using WixToolset.Core.WindowsInstaller.Decompile; |
| 9 | using WixToolset.Data.WindowsInstaller; |
| 10 | using WixToolset.Extensibility.Services; |
| 11 | |
| 12 | internal class WindowsInstallerDecompilerHelper : IWindowsInstallerDecompilerHelper |
| 13 | { |
| 14 | public WindowsInstallerDecompilerHelper(IServiceProvider _) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | private Dictionary<string, XElement> IndexedElements { get; } = new Dictionary<string, XElement>(); |
| 19 | |
| 20 | #region IWindowsInstallerDecompilerHelper interfaces |
| 21 | |
| 22 | public XElement RootElement { get; set; } |
| 23 | |
| 24 | public XElement AddElementToRoot(string name, params object[] content) |
| 25 | { |
| 26 | var element = new XElement(Names.WxsNamespace + name, content); |
| 27 | |
| 28 | this.RootElement.Add(element); |
| 29 | |
| 30 | return element; |
| 31 | } |
| 32 | |
| 33 | public XElement AddElementToRoot(XName name, params object[] content) |
| 34 | { |
| 35 | var element = new XElement(name, content); |
| 36 | |
| 37 | this.RootElement.Add(element); |
| 38 | |
| 39 | return element; |
| 40 | } |
| 41 | |
| 42 | public XElement AddElementToRoot(XElement element) |
| 43 | { |
| 44 | this.RootElement.Add(element); |
| 45 | |
| 46 | return element; |
| 47 | } |
| 48 | |
| 49 | public XElement CreateElement(string name, params object[] content) |
| 50 | { |
| 51 | return new XElement(Names.WxsNamespace + name, content); |
| 52 | } |
| 53 | |
| 54 | public XElement GetIndexedElement(Row row) |
| 55 | { |
| 56 | return this.GetIndexedElement(row.TableDefinition.Name, row.GetPrimaryKey()); |
| 57 | } |
| 58 | |
| 59 | public XElement GetIndexedElement(string table, string primaryKey) |
| 60 | { |
| 61 | return this.TryGetIndexedElement(table, primaryKey, out var element) ? element : null; |
| 62 | } |
| 63 | |
| 64 | public XElement GetIndexedElement(string table, string primaryKey1, string primaryKey2) |
| 65 | { |
| 66 | return this.TryGetIndexedElement(table, primaryKey1, primaryKey2, out var element) ? element : null; |
| 67 | } |
| 68 | |
| 69 | public XElement GetIndexedElement(string table, string primaryKey1, string primaryKey2, string primaryKey3) |
| 70 | { |
| 71 | return this.TryGetIndexedElement(table, primaryKey1, primaryKey2, primaryKey3, out var element) ? element : null; |
| 72 | } |
| 73 | |
| 74 | public XElement GetIndexedElement(string table, string[] primaryKeys) |
| 75 | { |
| 76 | return this.TryGetIndexedElement(table, primaryKeys, out var element) ? element : null; |
| 77 | } |
| 78 | |
| 79 | public void IndexElement(Row row, XElement element) |
| 80 | { |
| 81 | this.IndexElement(row.Table.Name, row.GetPrimaryKey(), element); |
| 82 | } |
| 83 | |
| 84 | public void IndexElement(string table, string primaryKey, XElement element) |
| 85 | { |
| 86 | var key = String.Concat(table, ':', primaryKey); |
| 87 | this.IndexedElements.Add(key, element); |
| 88 | } |
| 89 | |
| 90 | public void IndexElement(string table, string primaryKey1, string primaryKey2, XElement element) |
| 91 | { |
| 92 | this.IndexElement(table, String.Join("/", primaryKey1, primaryKey2), element); |
| 93 | } |
| 94 | |
| 95 | public void IndexElement(string table, string primaryKey1, string primaryKey2, string primaryKey3, XElement element) |
| 96 | { |
| 97 | this.IndexElement(table, String.Join("/", primaryKey1, primaryKey2, primaryKey3), element); |
| 98 | } |
| 99 | |
| 100 | public void IndexElement(string table, string[] primaryKeys, XElement element) |
| 101 | { |
| 102 | this.IndexElement(table, String.Join("/", primaryKeys), element); |
| 103 | } |
| 104 | |
| 105 | public bool TryGetIndexedElement(Row row, out XElement element) |
| 106 | { |
| 107 | return this.TryGetIndexedElement(row.TableDefinition.Name, row.GetPrimaryKey(), out element); |
| 108 | } |
| 109 | |
| 110 | public bool TryGetIndexedElement(string table, string primaryKey, out XElement element) |
| 111 | { |
| 112 | var key = String.Concat(table, ':', primaryKey); |
| 113 | return this.IndexedElements.TryGetValue(key, out element); |
| 114 | } |
| 115 | |
| 116 | public bool TryGetIndexedElement(string table, string primaryKey1, string primaryKey2, out XElement element) |
| 117 | { |
| 118 | return this.TryGetIndexedElement(table, String.Join("/", primaryKey1, primaryKey2), out element); |
| 119 | } |
| 120 | |
| 121 | public bool TryGetIndexedElement(string table, string primaryKey1, string primaryKey2, string primaryKey3, out XElement element) |
| 122 | { |
| 123 | return this.TryGetIndexedElement(table, String.Join("/", primaryKey1, primaryKey2, primaryKey3), out element); |
| 124 | } |
| 125 | |
| 126 | public bool TryGetIndexedElement(string table, string[] primaryKeys, out XElement element) |
| 127 | { |
| 128 | return this.TryGetIndexedElement(table, String.Join("/", primaryKeys), out element); |
| 129 | } |
| 130 | |
| 131 | #endregion |
| 132 | } |
| 133 | } |