1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// 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.Core.WindowsInstaller.Bind
{
using System;
using System.Globalization;
using System.IO;
using System.Text;
using WixToolset.Data;
using WixToolset.Data.WindowsInstaller;
using WixToolset.Extensibility.Services;
internal class CreateIdtFileCommand
{
public CreateIdtFileCommand(IMessaging messaging, Table table, int codepage, string intermediateFolder, bool keepAddedColumns)
{
this.Messaging = messaging;
this.Table = table;
this.Codepage = codepage;
this.IntermediateFolder = intermediateFolder;
this.KeepAddedColumns = keepAddedColumns;
}
private IMessaging Messaging { get; }
private Table Table { get; }
private int Codepage { get; set; }
private string IntermediateFolder { get; }
private bool KeepAddedColumns { get; }
public string IdtPath { get; private set; }
public void Execute()
{
// write out the table to an IDT file
Encoding encoding;
// If UTF8 encoding, use the UTF8-specific constructor to avoid writing
// the byte order mark at the beginning of the file
if (this.Codepage == Encoding.UTF8.CodePage)
{
encoding = new UTF8Encoding(false, true);
}
else
{
if (this.Codepage == 0)
{
this.Codepage = Encoding.ASCII.CodePage;
}
encoding = Encoding.GetEncoding(this.Codepage, new EncoderExceptionFallback(), new DecoderExceptionFallback());
}
this.IdtPath = Path.Combine(this.IntermediateFolder, String.Concat(this.Table.Name, ".idt"));
using (var idtWriter = new StreamWriter(this.IdtPath, false, encoding))
{
this.TableToIdtDefinition(this.Table, idtWriter, this.KeepAddedColumns);
}
}
private void TableToIdtDefinition(Table table, StreamWriter writer, bool keepAddedColumns)
{
if (table.Definition.Unreal)
{
return;
}
if (TableDefinition.MaxColumnsInRealTable < table.Definition.Columns.Count)
{
throw new WixException(ErrorMessages.TooManyColumnsInRealTable(table.Definition.Name, table.Definition.Columns.Count, TableDefinition.MaxColumnsInRealTable));
}
// Tack on the table header, and flush before we start writing bytes directly to the stream.
var header = this.TableDefinitionToIdtDefinition(table.Definition, keepAddedColumns);
writer.Write(header);
writer.Flush();
using (var binary = new BinaryWriter(writer.BaseStream, writer.Encoding, true))
{
// Create an encoding that replaces characters with question marks, and doesn't throw. We'll
// use this in case of errors
Encoding convertEncoding = Encoding.GetEncoding(writer.Encoding.CodePage);
foreach (Row row in table.Rows)
{
if (row.Redundant)
{
continue;
}
string rowString = this.RowToIdtDefinition(row, keepAddedColumns);
byte[] rowBytes;
try
{
// GetBytes will throw an exception if any character doesn't match our current encoding
rowBytes = writer.Encoding.GetBytes(rowString);
}
catch (EncoderFallbackException)
{
this.Messaging.Write(ErrorMessages.InvalidStringForCodepage(row.SourceLineNumbers, Convert.ToString(writer.Encoding.WindowsCodePage, CultureInfo.InvariantCulture)));
rowBytes = convertEncoding.GetBytes(rowString);
}
binary.Write(rowBytes, 0, rowBytes.Length);
}
}
}
private string TableDefinitionToIdtDefinition(TableDefinition definition, bool keepAddedColumns)
{
var first = true;
var columnString = new StringBuilder();
var dataString = new StringBuilder();
var tableString = new StringBuilder();
tableString.Append(definition.Name);
foreach (var column in definition.Columns)
{
// conditionally keep columns added in a transform; otherwise,
// break because columns can only be added at the end
if (column.Added && !keepAddedColumns)
{
break;
}
if (!first)
{
columnString.Append('\t');
dataString.Append('\t');
}
columnString.Append(column.Name);
dataString.Append(ColumnIdtType(column));
if (column.PrimaryKey)
{
tableString.AppendFormat("\t{0}", column.Name);
}
first = false;
}
columnString.Append("\r\n");
columnString.Append(dataString);
columnString.Append("\r\n");
columnString.Append(tableString);
columnString.Append("\r\n");
return columnString.ToString();
}
private string RowToIdtDefinition(Row row, bool keepAddedColumns)
{
var first = true;
var sb = new StringBuilder();
foreach (var field in row.Fields)
{
// Conditionally keep columns added in a transform; otherwise,
// break because columns can only be added at the end.
if (field.Column.Added && !keepAddedColumns)
{
break;
}
if (first)
{
first = false;
}
else
{
sb.Append('\t');
}
sb.Append(this.FieldToIdtValue(field));
}
sb.Append("\r\n");
return sb.ToString();
}
private string FieldToIdtValue(Field field)
{
var data = field.AsString();
if (String.IsNullOrEmpty(data))
{
return data;
}
// Special field value idt-specific escaping.
return data.Replace('\t', '\x10')
.Replace('\r', '\x11')
.Replace('\n', '\x19');
}
/// <summary>
/// Gets the type of the column in IDT format.
/// </summary>
/// <value>IDT format for column type.</value>
private static string ColumnIdtType(ColumnDefinition column)
{
char typeCharacter;
switch (column.Type)
{
case ColumnType.Number:
typeCharacter = column.Nullable ? 'I' : 'i';
break;
case ColumnType.Preserved:
case ColumnType.String:
typeCharacter = column.Nullable ? 'S' : 's';
break;
case ColumnType.Localized:
typeCharacter = column.Nullable ? 'L' : 'l';
break;
case ColumnType.Object:
typeCharacter = column.Nullable ? 'V' : 'v';
break;
default:
throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, WixDataStrings.EXP_UnknownColumnType, column.Type));
}
return String.Concat(typeCharacter, column.Length);
}
}
}
|