-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgramUsingEngineType.cs
More file actions
343 lines (305 loc) · 13.7 KB
/
ProgramUsingEngineType.cs
File metadata and controls
343 lines (305 loc) · 13.7 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using XsltDebugger.DebugAdapter;
namespace XsltDebugger.ConsoleTest;
class ProgramUsingEngineType
{
private const string TestDataFolder = "TestData";
private const string IntegrationFolderName = "Integration";
private static readonly string DefaultTestDataFolder = Path.Combine(TestDataFolder, IntegrationFolderName);
static async Task Main(string[] args)
{
// Parse engine type from command line (--engine compiled or --engine saxon)
string engineType = "saxon"; // default
var filteredArgs = new List<string>();
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--engine" && i + 1 < args.Length)
{
engineType = args[i + 1].ToLower();
i++; // skip next arg
}
else
{
filteredArgs.Add(args[i]);
}
}
Console.WriteLine($"=== XSLT Debugger Console Test (Using {(engineType == "compiled" ? "CompiledEngine" : "SaxonEngine")}) ===\n");
// Use different defaults based on engine type
var defaultXslt = engineType == "compiled"
? Path.Combine(DefaultTestDataFolder, "xslt/compiled/sample-inline-cs-with-usings.xslt")
: Path.Combine(DefaultTestDataFolder, "xslt/saxon/ShipmentConf3.xslt");
var defaultXml = Path.Combine(DefaultTestDataFolder, engineType == "compiled" ? "xml/sample-inline-cs-with-usings.xml" : "xml/ShipmentConf-proper.xml");
var stylesheetPath = ResolveInput(filteredArgs.ToArray(), 0, defaultXslt);
var xmlPath = ResolveInput(filteredArgs.ToArray(), 1, defaultXml);
if (!File.Exists(stylesheetPath))
{
Console.WriteLine($"ERROR: Stylesheet not found: {stylesheetPath}");
return;
}
if (!File.Exists(xmlPath))
{
Console.WriteLine($"ERROR: XML not found: {xmlPath}");
return;
}
try
{
// Create engine based on type
IXsltEngine engine;
if (engineType == "compiled")
{
Console.WriteLine("1. Initializing XsltCompiledEngine from DebugAdapter...");
engine = new XsltCompiledEngine();
}
else
{
Console.WriteLine("1. Initializing SaxonEngine from DebugAdapter...");
engine = new SaxonEngine();
}
// Enable debugging with log level
XsltEngineManager.SetDebugFlags(debug: true, LogLevel.Log);
Console.WriteLine(" >> Debugging ENABLED (Log)\n");
// Set breakpoint - detect which file and set appropriate line
var fullStylesheetPath = Path.GetFullPath(stylesheetPath);
int breakpointLine = stylesheetPath.Contains("message-test") ? 9 :
stylesheetPath.Contains("ShipmentConf3") ? 55 :
stylesheetPath.Contains("VariableLoggingSampleV1") ? 8 :
stylesheetPath.Contains("step-into-test") ? 12 : 26;
engine.SetBreakpoints(new[] { (fullStylesheetPath, breakpointLine) });
Console.WriteLine($" >> Breakpoint set at line {breakpointLine}\n");
// Set up event handlers to capture output
XsltEngineManager.EngineOutput += (output) =>
{
// Highlight important messages with colors
if (output.Contains("Instrumenting", StringComparison.OrdinalIgnoreCase))
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($">> {output}");
Console.ResetColor();
}
else if (output.Contains("[xsl:message]", StringComparison.OrdinalIgnoreCase))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($">> {output}");
Console.ResetColor();
}
else if (output.Contains("Captured variable", StringComparison.OrdinalIgnoreCase))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($">> {output}");
Console.ResetColor();
}
else if (output.StartsWith("[trace]") || output.StartsWith("[traceall]"))
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(output);
Console.ResetColor();
}
else if (output.StartsWith("[debug]"))
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(output);
Console.ResetColor();
}
else
{
Console.WriteLine(output);
}
};
XsltEngineManager.EngineStopped += (file, line, reason) =>
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine($"╔═══════════════════════════════════════════════════════════════╗");
Console.WriteLine($"║ BREAKPOINT HIT - Line {line,-45}║");
Console.WriteLine($"╚═══════════════════════════════════════════════════════════════╝");
Console.ResetColor();
// Display context information
var context = XsltEngineManager.LastContext;
if (context != null)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n 📍 CONTEXT:");
Console.ResetColor();
Console.WriteLine($" Node Type: {context.NodeType}");
Console.WriteLine($" Name: {context.Name}");
var value = context.Value?.Length > 100 ? context.Value.Substring(0, 100) + "..." : context.Value ?? "";
if (!string.IsNullOrWhiteSpace(value))
{
Console.WriteLine($" Value: {value.Replace("\n", "\\n").Replace("\r", "")}");
}
if (context.HasAttributes)
{
Console.WriteLine(" Attributes:");
var attrNav = context.Clone();
if (attrNav.MoveToFirstAttribute())
{
do
{
Console.WriteLine($" @{attrNav.Name} = \"{attrNav.Value}\"");
} while (attrNav.MoveToNextAttribute());
}
}
}
// Display registered stylesheet namespaces
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n 🌐 REGISTERED NAMESPACES:");
Console.ResetColor();
if (XsltEngineManager.StylesheetNamespaces.Count > 0)
{
foreach (var kvp in XsltEngineManager.StylesheetNamespaces)
{
var prefix = string.IsNullOrEmpty(kvp.Key) ? "(no prefix)" : kvp.Key;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write($" {prefix}");
Console.ResetColor();
Console.Write(" → ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(kvp.Value);
Console.ResetColor();
}
}
else
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(" (no custom namespaces)");
Console.ResetColor();
}
// Display XSLT variables
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n 📊 XSLT VARIABLES:");
Console.ResetColor();
if (XsltEngineManager.Variables.Count > 0)
{
foreach (var kvp in XsltEngineManager.Variables)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write($" ${kvp.Key}");
Console.ResetColor();
Console.WriteLine($" = {kvp.Value}");
}
}
else
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(" (no variables captured yet)");
Console.ResetColor();
}
Console.WriteLine();
// Auto-continue for console test
Task.Run(async () =>
{
await Task.Delay(100);
await engine.ContinueAsync();
});
};
XsltEngineManager.EngineTerminated += (exitCode) =>
{
Console.WriteLine();
Console.ForegroundColor = exitCode == 0 ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine($"╔═══════════════════════════════════════════════════════════════╗");
Console.WriteLine($"║ TRANSFORMATION {(exitCode == 0 ? "COMPLETED" : "FAILED"),-47}║");
Console.WriteLine($"║ Exit Code: {exitCode,-51}║");
Console.WriteLine($"╚═══════════════════════════════════════════════════════════════╝");
Console.ResetColor();
};
Console.WriteLine($"2. Starting transformation...");
Console.WriteLine($" Stylesheet: {stylesheetPath}");
Console.WriteLine($" XML: {xmlPath}");
Console.WriteLine($" Stop on entry: false\n");
// Run the transformation (this is async and will call our event handlers)
await engine.StartAsync(
Path.GetFullPath(stylesheetPath),
Path.GetFullPath(xmlPath),
stopOnEntry: false
);
// Give it a moment to finish
await Task.Delay(1000);
Console.WriteLine("\n=== TEST COMPLETE ===");
}
catch (Exception ex)
{
Console.WriteLine($"\n=== ERROR ===");
Console.WriteLine($"{ex.GetType().Name}: {ex.Message}");
Console.WriteLine($"\nStack trace:\n{ex.StackTrace}");
}
}
private static string ResolveInput(string[] args, int index, string fallback)
{
var candidate = index < args.Length ? args[index] : null;
if (!string.IsNullOrWhiteSpace(candidate))
{
var resolved = TryResolve(candidate!);
if (resolved != null)
{
return resolved;
}
return candidate!;
}
var fallbackResolved = TryResolve(fallback);
return fallbackResolved ?? fallback;
}
private static string? TryResolve(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
if (Path.IsPathRooted(path) && File.Exists(path))
{
return path;
}
if (File.Exists(path))
{
return Path.GetFullPath(path);
}
var dir = new DirectoryInfo(Environment.CurrentDirectory);
for (var i = 0; i < 8 && dir != null; i++)
{
var direct = Path.Combine(dir.FullName, path);
if (File.Exists(direct))
{
return direct;
}
if (!StartsWithSegment(path, TestDataFolder))
{
var testDataPath = Path.Combine(dir.FullName, TestDataFolder, path);
if (File.Exists(testDataPath))
{
return testDataPath;
}
}
if (!StartsWithSegment(path, IntegrationFolderName) &&
!path.Contains($"{TestDataFolder}{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase) &&
!path.Contains($"{TestDataFolder}/", StringComparison.OrdinalIgnoreCase))
{
var integrationPath = Path.Combine(dir.FullName, TestDataFolder, IntegrationFolderName, path);
if (File.Exists(integrationPath))
{
return integrationPath;
}
}
dir = dir.Parent;
}
return null;
}
private static bool StartsWithSegment(string path, string segment)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
if (path.StartsWith(segment + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (Path.DirectorySeparatorChar != '/' &&
path.StartsWith(segment + "/", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return path.Equals(segment, StringComparison.OrdinalIgnoreCase);
}
}