forked from JeremyDurnell/locbaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocBamlOptions.cs
More file actions
283 lines (262 loc) · 11 KB
/
LocBamlOptions.cs
File metadata and controls
283 lines (262 loc) · 11 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
namespace BamlLocalization
{
internal sealed class LocBamlOptions
{
internal string Input;
internal string Output;
internal CultureInfo CultureInfo;
internal string Translations;
internal bool ToParse;
internal bool ToGenerate;
internal bool HasNoLogo;
internal bool IsVerbose;
internal TranslationFileType TranslationFileType;
internal BinaryFileType InputType;
internal ArrayList AssemblyPaths;
internal Assembly[] Assemblies;
/// <summary>
/// return true if the operation succeeded.
/// otherwise, return false
/// </summary>
internal string CheckAndSetDefault()
{
// we validate the options here and also set default
// if we can
// Rule #1: One and only one action at a time
// i.e. Can't parse and generate at the same time
// Must do one of them
if ((ToParse && ToGenerate) ||
(!ToParse && !ToGenerate))
return StringLoader.Get("MustChooseOneAction");
// Rule #2: Must have an input
if (string.IsNullOrEmpty(Input))
{
return StringLoader.Get("InputFileRequired");
}
else
{
if (!File.Exists(Input))
{
return StringLoader.Get("FileNotFound", Input);
}
string extension = Path.GetExtension(Input);
InputType = BinaryFileTypeHelper.GetBinaryFileTypeFromExtension(extension);
if (InputType == BinaryFileType.NONE)
{
return StringLoader.Get("FileTypeNotSupported", extension);
}
}
if (ToGenerate)
{
// Rule #3: before generation, we must have Culture string
if (CultureInfo == null && InputType != BinaryFileType.BAML)
{
// if we are not generating baml,
return StringLoader.Get("CultureNameNeeded", InputType.ToString());
}
// Rule #4: before generation, we must have translation file
if (string.IsNullOrEmpty(Translations))
{
return StringLoader.Get("TranslationNeeded");
}
else
{
string extension = Path.GetExtension(Translations);
if (!File.Exists(Translations))
{
return StringLoader.Get("TranslationNotFound", Translations);
}
else
{
TranslationFileType = TranslationFileTypeHelpers.GetTranslationFileTypeFromExtension(extension);
}
}
}
// Rule #5: If the output file name is empty, we act accordingly
if (string.IsNullOrEmpty(Output))
{
// Rule #5.1: If it is parse, we default to [input file name].csv
if (ToParse)
{
string fileName = Path.GetFileNameWithoutExtension(Input);
Output = fileName + TranslationFileType.CSV.GetExtension();
TranslationFileType = TranslationFileType.CSV;
}
else
{
// Rule #5.2: If it is generating, and the output can't be empty
return StringLoader.Get("OutputDirectoryNeeded");
}
}
else
{
// output isn't null, we will determind the Output file type
// Rule #6: if it is parsing. It will be .csv or .txt.
if (ToParse)
{
string fileName;
string outputDir;
if (Directory.Exists(Output))
{
// the output is actually a directory name
fileName = string.Empty;
outputDir = Output;
}
else
{
// get the extension
fileName = Path.GetFileName(Output);
outputDir = Path.GetDirectoryName(Output);
}
// Rule #6.1: if it is just the output directory
// we append the input file name as the output + csv as default
if (string.IsNullOrEmpty(fileName))
{
TranslationFileType = TranslationFileType.CSV;
Output = outputDir
+ Path.DirectorySeparatorChar
+ Path.GetFileName(Input)
+ TranslationFileType.GetExtension();
}
else
{
// Rule #6.2: if we have file name, check the extension.
string extension = Path.GetExtension(Output);
TranslationFileType = TranslationFileTypeHelpers.GetTranslationFileTypeFromExtension(extension);
}
}
else
{
// it is to generate. And Output should point to the directory name.
if (!Directory.Exists(Output))
return StringLoader.Get("OutputDirectoryError", Output);
}
}
// Rule #7: if the input assembly path is not null
if (AssemblyPaths != null && AssemblyPaths.Count > 0)
{
Assemblies = new Assembly[AssemblyPaths.Count];
for (int i = 0; i < Assemblies.Length; i++)
{
string errorMsg = null;
try
{
// Read file contents into a byte array or else we will hold the file open
byte[] fileContents = File.ReadAllBytes((string)AssemblyPaths[i]);
Assemblies[i] = Assembly.Load(fileContents);
}
catch (ArgumentException argumentError)
{
errorMsg = argumentError.Message;
}
catch (BadImageFormatException formatError)
{
errorMsg = formatError.Message;
}
catch (FileNotFoundException fileError)
{
errorMsg = fileError.Message;
}
catch (PathTooLongException pathError)
{
errorMsg = pathError.Message;
}
catch (SecurityException securityError)
{
errorMsg = securityError.Message;
}
if (errorMsg != null)
{
return errorMsg; // return error message when loading this assembly
}
}
}
// if we come to this point, we are all fine, return null error message
return null;
}
/// <summary>
/// Write message line depending on IsVerbose flag
/// </summary>
internal void WriteLine(string message)
{
if (IsVerbose)
{
Console.WriteLine(message);
}
}
/// <summary>
/// Write the message depending on IsVerbose flag
/// </summary>
internal void Write(string message)
{
if (IsVerbose)
{
Console.Write(message);
}
}
/// <summary>
/// Factory method to get an object for writing the translation file
/// </summary>
public ITranslationWriter GetTranslationWriter()
{
switch (TranslationFileType)
{
case TranslationFileType.CSV:
case TranslationFileType.TXT:
// ResourceTextWriter will dispose of the stream when it is disposed of
return new ResourceTextWriter(TranslationFileType, new FileStream(Output, FileMode.Create));
case TranslationFileType.XLIFF:
// ResourceXliffWriter will dispose of the stream when it is disposed of
if (File.Exists(Output))
{
Xliff1_2.XliffObject existingFile;
try
{
using (FileStream stream = new FileStream(Output, FileMode.Open))
{
existingFile = Xliff1_2.XliffObject.Deserialize(stream);
}
}
catch (Exception e)
{
throw new Exception("Error reading existing XLIFF file: " + e.Message, e);
}
return new ResourceXliffWriter(this, new FileStream(Output, FileMode.Create), existingFile);
}
else
{
return new ResourceXliffWriter(this, new FileStream(Output, FileMode.Create));
}
default:
throw new Exception("Unknown translation file type");
}
}
public TranslationDictionariesReader GetTranslationsDictionary()
{
switch (TranslationFileType)
{
case TranslationFileType.CSV:
case TranslationFileType.TXT:
Stream input = File.OpenRead(Translations);
using (ResourceTextReader reader = new ResourceTextReader(TranslationFileType, input))
{
return new TranslationDictionariesReader(reader);
}
case TranslationFileType.XLIFF:
using (Stream xlfInput = File.OpenRead(Translations))
{
Xliff1_2.XliffObject xliff = Xliff1_2.XliffObject.Deserialize(xlfInput);
return new TranslationDictionariesReader(xliff);
}
default:
throw new Exception("Unknown translation file type");
}
}
}
}