-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
497 lines (428 loc) · 16.1 KB
/
MainForm.cs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Created by SharpDevelop.
* User: virtan39
* Date: 13.8.2015
* Time: 11:16
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Xsl;
using System.Xml;
using System.IO;
using System.Text;
namespace K2XML_Converter
{
public partial class MainForm : Form
{
// The name of the configuration file.
const string CONF_NAME = "K2XML_Converter.conf";
const string NAMESPACES =
"xmlns:kactus2=\"http://funbase.cs.tut.fi/\" " +
"xmlns:spirit=\"http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5\" " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xsi:schemaLocation=\"http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5 " +
"http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5/index.xsd\" ";
const string APIDEF_NO_NS = "<kactus2:apiDefinition>";
const string APIDEF_YES_NS = "<kactus2:apiDefinition " + NAMESPACES + ">";
const string COMDEF_NO_NS = "<kactus2:comDefinition>";
const string COMDEF_YES_NS = "<kactus2:comDefinition " + NAMESPACES + ">";
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
// Must reserve a link for link label before use!
this.linkLabel.Links.Add(0, 0, "joku");
}
// How many files processed so far?
int processed = 0;
void ConversionWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// Use intendation.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " ";
// Reset the counter
processed = 0;
// Log the when started
logStream.WriteLine( "Conversion started: " + started_ );
// How many files
logStream.WriteLine( "Scanned " + totalFiles + " files for processing." );
// The used XSLT files.
logStream.WriteLine( "The first used XSLT file: " + firstConPath );
logStream.WriteLine( "The second used XSLT file: " + secondConPath );
logStream.WriteLine( "The third used XSLT file: " + thirdConPath );
logStream.WriteLine();
logStream.WriteLine( "******************************" );
logStream.WriteLine();
// Convert each scanned file now, with certain exceptions of course.
// NOTICE: file specific non-error messages are ommitted from console for performance!
foreach (string file in files)
{
try
{
if (conversionWorker.CancellationPending == true)
{
// The conversion has been manually halted before its end!
e.Cancel = true;
// Log and file it.
logStream.WriteLine();
string errs = "***CONVERSION HALTED BY USER***";
Console.WriteLine( errs );
logStream.WriteLine( errs );
break;
}
// File info is needed for file details, like extension, directory, ect.
System.IO.FileInfo fi = new System.IO.FileInfo(file);
// One less file to check.
++processed;
// As long as there wont be division by zero, report the progress.
if ( totalFiles > 0 )
{
conversionWorker.ReportProgress( 100 * processed / totalFiles );
}
// Only XML files are to be converted.
if ( fi.Extension != ".xml" )
continue;
// Begins with prefix -> already converted -> do not convert!
if ( fi.Name.StartsWith( prefix, StringComparison.Ordinal ) )
{
// Log it.
logStream.WriteLine( fi.Name + " was skipped since it was already converted!" );
continue;
}
// Form the path of the new file.
string newPath = fi.Directory + System.IO.Path.DirectorySeparatorChar.ToString() + prefix + fi.Name;
// Log which original file was opened.
logStream.WriteLine( fi.Name + ":" );
// When:
logStream.WriteLine( "\tOpened: " + DateTime.Now );
// What was the output file?
logStream.WriteLine( "\tOutput: " + newPath );
// Read the original from file system.
string xml = "";
try
{
xml = File.ReadAllText(file);
}
catch ( Exception exp )
{
string errs = "Error while reading XML file: " + exp.Message;
Console.WriteLine( errs );
logStream.WriteLine( "\t" + errs );
}
// Replace API and com definition root elements with ones containing namespaces.
xml = xml.Replace( APIDEF_NO_NS, APIDEF_YES_NS );
xml = xml.Replace( COMDEF_NO_NS, COMDEF_YES_NS );
// Wrap the document to a reader.
XmlReader firstReader = new XmlTextReader(new StringReader(xml));
// Dump the intermediate conversion result to memory rather than filesystem.
MemoryStream firstStream = new MemoryStream();
// Needs a writer to write to that stream.
XmlWriter firstWriter = XmlWriter.Create( firstStream, xslt1.OutputSettings );
// Execute the transform and output the results to the intermediate stream.
try
{
xslt1.Transform( firstReader, firstWriter );
}
catch ( XsltException exp )
{
string errs = "Error in 1st conversion: " + exp.Message + " Location: " + exp.LineNumber + ", " + exp.LinePosition;
Console.WriteLine( errs );
logStream.WriteLine( "\t" + errs );
}
// Roll back the first stream after writing.
firstStream.Seek( 0, SeekOrigin.Begin );
// Needs a reader to read from that stream.
XmlReader secondReader = XmlReader.Create( firstStream );
// Dump the intermediate conversion result to memory rather than filesystem.
MemoryStream secondStream = new MemoryStream();
// Needs a writer to write to that stream.
XmlWriter secondWriter = XmlWriter.Create( secondStream, xslt2.OutputSettings );
// Execute the second transformation, which is again outputted to memory.
try
{
xslt2.Transform( secondReader, secondWriter );
}
catch ( XmlException exp )
{
string errs = "Error while reading the second XML: " + exp.Message + " Location: "
+ exp.LineNumber + ", " + exp.LinePosition;
Console.WriteLine( errs );
logStream.WriteLine( "\t" + errs );
}
catch ( XsltException exp )
{
string errs = "Error in 2nd conversion: " + exp.Message + " Location: " + exp.LineNumber + ", " + exp.LinePosition;
Console.WriteLine( errs );
logStream.WriteLine( "\t" + errs );
}
// Roll back the first stream after writing.
secondStream.Seek( 0, SeekOrigin.Begin );
// Needs a reader to read from that stream.
XmlReader thirdReader = XmlReader.Create( secondStream );
// The final output is dumped to the filesystem.
FileStream thirdStream = new FileStream( newPath, FileMode.Create );
// Needs a writer to write to that stream.
XmlWriter thirdWriter = XmlWriter.Create( thirdStream, xslt3.OutputSettings );
// Execute the third transformation, which is outputted to file system.
try
{
xslt3.Transform( thirdReader, thirdWriter );
}
catch ( XmlException exp )
{
string errs = "Error while reading the third XML: " + exp.Message + " Location: "
+ exp.LineNumber + ", " + exp.LinePosition;
Console.WriteLine( errs );
logStream.WriteLine( "\t" + errs );
}
catch ( XsltException exp )
{
string errs = "Error in 3rd conversion: " + exp.Message + " Location: " + exp.LineNumber + ", " + exp.LinePosition;
Console.WriteLine( errs );
logStream.WriteLine( "\t" + errs );
}
// Try to flush the output writer, and close every one of them.
try
{
thirdWriter.Flush();
firstReader.Close();
firstWriter.Close();
firstStream.Close();
secondReader.Close();
secondWriter.Close();
secondStream.Close();
thirdReader.Close();
thirdWriter.Close();
thirdStream.Close();
}
catch ( Exception exp )
{
string errs = "Error while closing streams: " + exp.Message;
Console.WriteLine( errs );
logStream.WriteLine( "\t" + errs );
}
}
catch ( Exception exp )
{
// Catch and report any uncatched execeptions.
string errs = "Error while processing: " + exp.Message;
Console.WriteLine(errs);
logStream.WriteLine( "\t" + errs );
}
try
{
// Log when files were closed
logStream.WriteLine( "\tClosed: " + DateTime.Now );
}
catch ( Exception exp )
{
// Any anomaly caused by this entry should be reported as well.
string errs = "Error while logging closing time of XML files: " + exp.Message;
Console.WriteLine(errs);
}
}
}
// Date when conversion work was requested.
DateTime started_;
// Configurations
String prefix; // Prefix used for converted files.
// Stream to write the log file.
StreamWriter logStream;
// The used stylesheets containing the conversion rules.
XslCompiledTransform xslt1;
XslCompiledTransform xslt2;
XslCompiledTransform xslt3;
// Locations of the stylesheets.
String firstConPath;
String secondConPath;
String thirdConPath;
// The files in current processing run.
string[] files;
int totalFiles;
// The root folder of the converted files.
string inputPath;
void ButtonConvertClick(object sender, EventArgs eargs)
{
// No action if already converting or even scanning.
if ( conversionWorker.IsBusy || fileScanner.IsBusy )
return;
// Load the configurations.
try
{
StreamReader cfgStream = new StreamReader(CONF_NAME);
prefix = cfgStream.ReadLine();
firstConPath = cfgStream.ReadLine();
secondConPath = cfgStream.ReadLine();
thirdConPath = cfgStream.ReadLine();
// Close the configuration file: No further need for it.
cfgStream.Close();
}
catch ( Exception exp )
{
// If configurations failed, no action!
string errs = "Error while loading configurations: " + exp.Message;
labelStatus.Text = errs;
Console.WriteLine(errs);
return;
}
// Must have a prefix.
if ( prefix == null || prefix.Length < 1 )
{
string errs = "The file name prefix must be at least 1 character long." +
" The prefix is defined at file " + CONF_NAME;
labelStatus.Text = errs;
Console.WriteLine(errs);
return;
}
try
{
// Load the style sheets.
xslt1 = new XslCompiledTransform();
xslt1.Load(firstConPath);
xslt2 = new XslCompiledTransform();
xslt2.Load(secondConPath);
xslt3 = new XslCompiledTransform();
xslt3.Load(thirdConPath);
}
catch ( Exception exp )
{
// If loading style sheets failed, no action!
string errs = "Error while loading style sheets: " + exp.Message +
" Style sheet paths are defined in file " + CONF_NAME;
labelStatus.Text = errs;
Console.WriteLine(errs);
return;
}
inputPath = textBoxFolder.Text;
// Check that there actually is something to process.
if (!System.IO.Directory.Exists( inputPath ) )
{
string errs = "Error: Invalid target directory specified!";
labelStatus.Text = errs;
Console.WriteLine(errs);
return;
}
try
{
// Open the log file for writing.
logStream = new StreamWriter("conversion.log", false, Encoding.UTF8);
linkLabel.Links[0].LinkData = Path.GetFullPath("conversion.log");
linkLabel.Text = "Log file written in " + Path.GetFullPath("conversion.log");
}
catch ( Exception exp )
{
// Must be able to open the log file.
string errs = "Error while opening the log file: " + exp.Message;
labelStatus.Text = errs;
Console.WriteLine(errs);
return;
}
labelStatus.Text = "Scanning the folder for input files...";
fileScanner.RunWorkerAsync();
buttonConvert.Enabled = false;
}
void ButtonSelectFolderClick(object sender, EventArgs e)
{
// Show the FolderBrowserDialog.
DialogResult result = folderBrowserDialog.ShowDialog();
if( result == DialogResult.OK )
{
textBoxFolder.Text = folderBrowserDialog.SelectedPath;
}
}
void ConversionWorkerRunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
// It is complete! Time to tell how long it lasted.
DateTime concluded = DateTime.Now;
labelStatus.Text = "Conversion started: " + started_.ToString() + "\n\rConversion concluded: " + concluded.ToString()
+ "\n\rProcessed " + processed + " files";
try
{
logStream.WriteLine();
logStream.WriteLine( "******************************" );
logStream.WriteLine();
string conclusion = "Conversion concluded: " + concluded;
logStream.WriteLine( conclusion );
Console.WriteLine( conclusion );
// Time to close the configuration stream.
logStream.Flush();
logStream.Close();
}
catch ( Exception exp )
{
// Error will be informed.
string errs = "Error while closing the log file: " + exp.Message;
labelStatus.Text = errs;
Console.WriteLine(errs);
}
// Re-enable conversion, cannot press halt any more.
buttonConvert.Enabled = true;
buttonHalt.Enabled = false;
// When complete, the bar is always at full!
if ( !e.Cancelled )
progressBar.Value = 100;
}
void ConversionWorkerProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if ( progressBar.Value < e.ProgressPercentage )
{
// Progress has been made -> Tell to user.
progressBar.PerformStep();
}
}
void FileScannerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// Scan all files in advance.
files = System.IO.Directory.GetFiles( inputPath, "*.xml", SearchOption.AllDirectories );
// Mark up how many there are in total.
totalFiles = files.Length;
}
void FileScannerRunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
if ( conversionWorker.IsBusy )
{
labelStatus.Text = "Error: A conversion was already on progress after scanning the files.";
return;
}
// Reset the bar.
progressBar.Value = 0;
// Update status, check the time, start the work.
string startS = "Processing " + totalFiles + " files...";
labelStatus.Text = startS;
Console.WriteLine( startS );
started_ = DateTime.Now;
conversionWorker.RunWorkerAsync();
// Conversion started -> may halt it
buttonHalt.Enabled = true;
}
void LinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
System.Diagnostics.Process.Start(linkLabel.Links[0].LinkData.ToString());
}
catch ( Exception exp )
{
// Error will be informed.
string errs = "Error while viewing the log file: " + exp.Message;
labelStatus.Text = errs;
Console.WriteLine(errs);
}
}
void ButtonHaltlClick(object sender, EventArgs e)
{
conversionWorker.CancelAsync();
}
void ButtonAboutClick(object sender, EventArgs e)
{
// Create and display when needed!
About af = new About();
af.Show();
}
}
}