| 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.Data.WindowsInstaller |
| 4 | { |
| 5 | using System; |
| 6 | using System.Diagnostics; |
| 7 | using System.Globalization; |
| 8 | using System.Xml; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Field containing data for an object column in a row. |
| 12 | /// </summary> |
| 13 | public sealed class ObjectField : Field |
| 14 | { |
| 15 | /// <summary> |
| 16 | /// Instantiates a new Field. |
| 17 | /// </summary> |
| 18 | /// <param name="columnDefinition">Column definition for this field.</param> |
| 19 | internal ObjectField(ColumnDefinition columnDefinition) : |
| 20 | base(columnDefinition) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | /// <summary> |
| 25 | /// Gets or sets the index of the embedded file in a library. |
| 26 | /// </summary> |
| 27 | /// <value>The index of the embedded file.</value> |
| 28 | public int? EmbeddedFileIndex { get; set; } |
| 29 | |
| 30 | /// <summary> |
| 31 | /// Gets or sets the previous index of the embedded file in the library. |
| 32 | /// </summary> |
| 33 | /// <value>The previous index of the embedded file.</value> |
| 34 | public int? PreviousEmbeddedFileIndex { get; set; } |
| 35 | |
| 36 | /// <summary> |
| 37 | /// Gets or sets the path to the embedded cabinet of the previous file. |
| 38 | /// </summary> |
| 39 | /// <value>The path of the cabinet containing the previous file.</value> |
| 40 | public Uri PreviousBaseUri { get; set; } |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Gets the base URI of the object field. |
| 44 | /// </summary> |
| 45 | /// <value>The base URI of the object field.</value> |
| 46 | public Uri BaseUri { get; private set; } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Gets or sets the unresolved data for this field. |
| 50 | /// </summary> |
| 51 | /// <value>Unresolved Data in the field.</value> |
| 52 | public string UnresolvedData { get; set; } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Gets or sets the unresolved previous data. |
| 56 | /// </summary> |
| 57 | /// <value>The unresolved previous data.</value> |
| 58 | public string UnresolvedPreviousData { get; set; } |
| 59 | |
| 60 | /// <summary> |
| 61 | /// Parse a field from the xml. |
| 62 | /// </summary> |
| 63 | /// <param name="reader">XmlReader where the intermediate is persisted.</param> |
| 64 | internal override void Read(XmlReader reader) |
| 65 | { |
| 66 | Debug.Assert("field" == reader.LocalName); |
| 67 | |
| 68 | bool empty = reader.IsEmptyElement; |
| 69 | |
| 70 | this.BaseUri = new Uri(reader.BaseURI); |
| 71 | |
| 72 | while (reader.MoveToNextAttribute()) |
| 73 | { |
| 74 | switch (reader.LocalName) |
| 75 | { |
| 76 | case "cabinetFileId": |
| 77 | this.EmbeddedFileIndex = Convert.ToInt32(reader.Value); |
| 78 | break; |
| 79 | case "modified": |
| 80 | this.Modified = reader.Value.Equals("yes"); |
| 81 | break; |
| 82 | case "previousData": |
| 83 | this.PreviousData = reader.Value; |
| 84 | break; |
| 85 | case "unresolvedPreviousData": |
| 86 | this.UnresolvedPreviousData = reader.Value; |
| 87 | break; |
| 88 | case "unresolvedData": |
| 89 | this.UnresolvedData = reader.Value; |
| 90 | break; |
| 91 | case "previousCabinetFileId": |
| 92 | this.PreviousEmbeddedFileIndex = Convert.ToInt32(reader.Value); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (!empty) |
| 98 | { |
| 99 | bool done = false; |
| 100 | |
| 101 | while (!done && reader.Read()) |
| 102 | { |
| 103 | switch (reader.NodeType) |
| 104 | { |
| 105 | case XmlNodeType.Element: |
| 106 | throw new XmlException(); |
| 107 | case XmlNodeType.CDATA: |
| 108 | case XmlNodeType.Text: |
| 109 | if (0 < reader.Value.Length) |
| 110 | { |
| 111 | this.Data = reader.Value; |
| 112 | } |
| 113 | break; |
| 114 | case XmlNodeType.EndElement: |
| 115 | done = true; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!done) |
| 121 | { |
| 122 | throw new XmlException(); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// <summary> |
| 128 | /// Persists a field in an XML format. |
| 129 | /// </summary> |
| 130 | /// <param name="writer">XmlWriter where the Field should persist itself as XML.</param> |
| 131 | internal override void Write(XmlWriter writer) |
| 132 | { |
| 133 | writer.WriteStartElement("field", WindowsInstallerData.XmlNamespaceUri); |
| 134 | |
| 135 | if (this.EmbeddedFileIndex.HasValue) |
| 136 | { |
| 137 | writer.WriteStartAttribute("cabinetFileId"); |
| 138 | writer.WriteValue(this.EmbeddedFileIndex); |
| 139 | writer.WriteEndAttribute(); |
| 140 | } |
| 141 | |
| 142 | if (this.Modified) |
| 143 | { |
| 144 | writer.WriteAttributeString("modified", "yes"); |
| 145 | } |
| 146 | |
| 147 | if (null != this.UnresolvedPreviousData) |
| 148 | { |
| 149 | writer.WriteAttributeString("unresolvedPreviousData", this.UnresolvedPreviousData); |
| 150 | } |
| 151 | |
| 152 | if (null != this.PreviousData) |
| 153 | { |
| 154 | writer.WriteAttributeString("previousData", this.PreviousData); |
| 155 | } |
| 156 | |
| 157 | if (null != this.UnresolvedData) |
| 158 | { |
| 159 | writer.WriteAttributeString("unresolvedData", this.UnresolvedData); |
| 160 | } |
| 161 | |
| 162 | if (this.PreviousEmbeddedFileIndex.HasValue) |
| 163 | { |
| 164 | writer.WriteStartAttribute("previousCabinetFileId"); |
| 165 | writer.WriteValue(this.PreviousEmbeddedFileIndex); |
| 166 | writer.WriteEndAttribute(); |
| 167 | } |
| 168 | |
| 169 | // Convert the data to a string that will persist nicely (nulls as String.Empty). |
| 170 | string text = Convert.ToString(this.Data, CultureInfo.InvariantCulture); |
| 171 | if (this.Column.UseCData) |
| 172 | { |
| 173 | writer.WriteCData(text); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | writer.WriteString(text); |
| 178 | } |
| 179 | |
| 180 | writer.WriteEndElement(); |
| 181 | } |
| 182 | } |
| 183 | } |