-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cs
892 lines (799 loc) · 28.9 KB
/
Application.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
using IS4.SFI.Application;
using IS4.SFI.Application.Tools;
using IS4.SFI.Services;
using Microsoft.Extensions.Logging;
using MorseCode.ITask;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace IS4.SFI
{
/// <summary>
/// The main application for analyzing input files and producing output.
/// </summary>
/// <typeparam name="TInspector">The type of <see cref="Inspector"/> to use.</typeparam>
public class Application<TInspector> : ConfigurableApplication, IResultFactory<ValueTuple, string>, IResultFactory<bool, string> where TInspector : ExtensibleInspector, new()
{
readonly IApplicationEnvironment environment;
TextWriter writer;
/// <inheritdoc/>
protected override TextWriter LogWriter => writer;
/// <inheritdoc/>
protected override int WindowWidth => environment.WindowWidth;
/// <inheritdoc/>
protected override string ExecutableName => environment.ExecutableName ?? base.ExecutableName;
readonly InspectorOptions options;
static readonly IEnumerable<string> modeNames = Enum.GetNames(typeof(Mode)).Select(n => n.ToLowerInvariant());
static readonly IEnumerable<string> bufferingNames = Enum.GetValues(typeof(BufferingLevel)).Cast<Enum>().Select(v => $"{v.ToString().ToLowerInvariant()} ({Convert.ToInt32(v)})");
/// <summary>
/// Creates a new instance of the application from the supplied environment.
/// </summary>
/// <param name="environment">
/// An instance of <see cref="IApplicationEnvironment"/>
/// providing manipulation with the environment.
/// </param>
public Application(IApplicationEnvironment environment) : base("https://sfi.is4.site/config")
{
this.environment = environment;
writer = environment.LogWriter;
options = new InspectorOptions
{
PrettyPrint = true,
Buffering = BufferingLevel.Temporary,
NewLine = environment.NewLine,
HideMetadata = true
};
}
Mode? mode;
readonly List<string> inputs = new();
readonly List<string> queries = new();
readonly List<string> plugins = new();
readonly List<Matcher> componentMatchers = new();
Regex? mainHash;
string? mainHashName;
string? output;
readonly Dictionary<string, Dictionary<string, List<string>>> componentProperties = new(StringComparer.OrdinalIgnoreCase);
readonly List<KeyValuePair<Regex, List<KeyValuePair<Regex, string>>>> regexComponentProperties = new();
bool quiet;
bool dataOnly;
bool onlyOnce;
ILogger? logger;
/// <summary>
/// Runs the application with the supplied arguments.
/// </summary>
/// <param name="args">The arguments to the application.</param>
public async ValueTask Run(params string[] args)
{
try{
LoadConfig();
Parse(args);
if(!quiet) Banner();
if(mode == null)
{
throw new ApplicationException($"Mode must be specified (one of {String.Join(", ", modeNames)})! Use -? for help.");
}
var inspector = new TInspector
{
CacheResults = onlyOnce
};
if(!quiet)
{
inspector.OutputLog = new ComponentLogger(writer);
}
foreach(var plugin in plugins)
{
inspector.AdditionalPluginIdentifiers.Add(plugin);
}
logger = inspector.OutputLog;
await inspector.AddDefault();
ConfigurationTools.RegisterCustomDescriptors();
int componentCount = 0;
async Task ConfigureComponents()
{
// Filter out the components based on arguments
foreach(var collection in inspector.ComponentCollections)
{
componentCount += await collection.Filter(this);
foreach(var component in collection.Collection)
{
// Subscribe to the OutputFile event
if(component is IHasFileOutput fileOutput)
{
fileOutput.OutputFile += OnOutputFile;
}
if(componentProperties.Count > 0 || regexComponentProperties.Count > 0)
{
// Get properties for this component
var id = collection.GetIdentifier(component);
ExtractComponentProperties(id, out var properties, out var regexProperties);
SetProperties(component, id, properties, regexProperties);
}
}
}
}
switch(mode)
{
// Print the available components
case Mode.List:
await ConfigureComponents();
if(!quiet) LogWriter.WriteLine("Included components:");
foreach(var collection in inspector.ComponentCollections)
{
await collection.ForEach(this);
}
return;
// Print the options XML
case Mode.Options:
PrintOptionsXml();
return;
case Mode.About:
if(output != null)
{
inputs.Add(output);
}
if(inputs.Count == 0)
{
throw new ApplicationException("'about' requires the name of a component.");
}
await ConfigureComponents();
foreach(var collection in inspector.ComponentCollections)
{
aboutBrowsedCollection = collection;
if(inputs.Contains(collection.Attribute.Prefix))
{
AboutCollection(collection);
}
await collection.ForEach(this);
}
return;
}
if(inputs.Count == 0 || output == null)
{
throw new ApplicationException("At least one input and an output must be specified!");
}
if(quiet)
{
writer = TextWriter.Null;
}
await ConfigureComponents();
foreach(var matcher in componentMatchers)
{
if(!matcher.HadEffect && matcher.Pattern != null)
{
logger?.LogWarning($"Warning: Pattern '{matcher.Pattern}' did not {(matcher.Result ? "include" : "exclude")} any components!");
}
}
foreach(var properties in componentProperties)
{
logger?.LogWarning($"Warning: Properties for component {properties.Key} could not be found!");
}
logger?.LogInformation($"Included {componentCount} components in total.");
if(mainHash != null)
{
// Set the primary ni: hash
var hash = inspector.DataAnalyzer.HashAlgorithms.FirstOrDefault(h => mainHash.IsMatch(TextTools.GetUserFriendlyName(h)));
if(hash == null)
{
throw new ApplicationException($"Main hash '{mainHashName}' cannot be found!");
}
inspector.DataAnalyzer.ContentUriFormatter = new NiHashedContentUriFormatter(hash);
}
if(options.Format == null)
{
options.Format = VDS.RDF.MimeTypesHelper.GetTrueFileExtension(output);
if(String.IsNullOrEmpty(options.Format))
{
options.Format = null;
}
}
// Load the input files from the environment
var inputFiles = inputs.SelectMany(input => environment.GetFiles(input)).ToList();
if(inputFiles.Count == 0)
{
throw new ApplicationException("No specified input files were found!");
}
var queryFiles = queries.SelectMany(query => environment.GetFiles(query).SelectMany(f => f.EnumerateFiles())).ToList();
if(queryFiles.Count == 0 && queries.Count > 0)
{
throw new ApplicationException("No specified SPARQL queries were found!");
}
if(mode == Mode.Search)
{
options.OutputIsSparqlResults = true;
if(queryFiles.Count == 0)
{
throw new ApplicationException("A SPARQL query must be provided via -s for search!");
}
}
options.Queries = queryFiles;
var update = environment.Update();
if(!update.Equals(default(ValueTask)))
{
// Only subscribe when the operation is asynchronous
await update;
inspector.Updated += environment.Update;
}
// Open the output RDF file
var streams = new List<Stream>();
Stream Factory(string mime)
{
var stream = environment.CreateFile(output, mime);
streams.Add(stream);
return stream;
}
try{
if(dataOnly)
{
// The input should be treated as a byte sequence without file metadata
await inspector.Inspect<IStreamFactory>(inputFiles.SelectMany(f => f.EnumerateFiles()), Factory, options);
}else{
await inspector.Inspect(inputFiles, Factory, options);
}
}finally{
foreach(var stream in streams)
{
stream.Dispose();
}
}
}catch(ApplicationExitException)
{
}catch(InternalApplicationException e) when(GlobalOptions.SuppressNonCriticalExceptions)
{
environment.LogWriter.WriteLine(e.InnerException.Message);
}catch(Exception e) when(GlobalOptions.SuppressNonCriticalExceptions)
{
environment.LogWriter.WriteLine(e.Message);
}
}
/// <summary>
/// Called from an analyzer when an output file should be created.
/// </summary>
private async ValueTask OnOutputFile(bool isBinary, INodeMatchProperties properties, Func<Stream, ValueTask> writer)
{
properties.Name ??= Guid.NewGuid().ToString("N");
var mediaType = properties.MediaType;
var path = properties.PathFormat ?? "${name}${extension}";
properties.PathFormat = null;
var name = TextTools.SubstituteVariables(path, properties.GetPropertyValues());
mediaType ??= isBinary ? "application/octet-stream" : "text/plain";
LogWriter?.WriteLine($"Extracting {mediaType} to '{name}'...");
using var stream = environment.CreateFile(name, mediaType);
await writer(stream);
}
private void ExtractComponentProperties(string id, out Dictionary<string, List<string>>? properties, out List<KeyValuePair<Regex, string>>? regexProperties)
{
if(componentProperties.TryGetValue(id, out properties))
{
componentProperties.Remove(id);
}else{
properties = null;
}
regexProperties = null;
foreach(var pair in regexComponentProperties)
{
if(pair.Key.IsMatch(id))
{
(regexProperties ??= new()).AddRange(pair.Value);
}
}
}
#region Components
private void SetProperties(object component, string componentName, IDictionary<string, List<string>>? properties, IList<KeyValuePair<Regex, string>>? regexProperties)
{
if(properties == null && regexProperties == null)
{
return;
}
ConfigurationTools.SetProperties(component, componentName, name => {
if(properties != null && properties.TryGetValue(name, out var list))
{
properties.Remove(name);
}else{
list = null;
}
if(regexProperties != null)
{
foreach(var pair in regexProperties)
{
if(pair.Key.IsMatch(name))
{
(list ??= new()).Add(pair.Value);
}
}
}
return list ?? (IEnumerable<string>)Array.Empty<string>();
}, logger);
if(properties != null)
{
foreach(var pair in properties)
{
LogWriter?.WriteLine($"Warning: Property {componentName}:{pair.Key} was not found!");
}
}
}
/// <summary>
/// Checks whether a component is matched by the inner list of matchers.
/// </summary>
/// <typeparam name="T">The type of the component.</typeparam>
/// <param name="component">The component object.</param>
/// <param name="name">The name of the component.</param>
/// <returns>Whether the component should be included.</returns>
bool IsIncluded<T>(T component, string name)
{
var included = true;
for(int i = 0; i < componentMatchers.Count; i++)
{
var matcher = componentMatchers[i];
if(included != matcher.Result && matcher.Predicate(name))
{
included = matcher.Result;
matcher.HadEffect = true;
}
}
return included;
}
async ITask<bool> IResultFactory<bool, string>.Invoke<T>(T value, string name)
{
return IsIncluded(value, name);
}
#endregion
#region Listing
void ListComponent<T>(T component, string id) where T : notnull
{
LogWriter?.WriteLine($" - {id} ({TextTools.GetUserFriendlyName(TypeDescriptor.GetReflectionType(component))})");
ExtractComponentProperties(id, out var properties, out var regexProperties);
SetProperties(component, id, properties, regexProperties);
foreach(var prop in ConfigurationTools.GetConfigurableProperties(component))
{
if(prop.IsObsolete(out _))
{
continue;
}
var value = prop.GetValue(component);
var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
var converter = prop.Converter;
string typeDesc;
if(ConfigurationTools.GetStandardValues(type, converter, out var values))
{
const int maxShown = 10;
typeDesc = String.Join("|", values.Cast<object>().Take(maxShown).Select(converter.ConvertToInvariantString));
if(!converter.GetStandardValuesExclusive() || values.Count > maxShown)
{
typeDesc = $"{TextTools.GetIdentifierFromType(type)}: {typeDesc}...";
}
}else{
typeDesc = TextTools.GetIdentifierFromType(type);
}
var line = $"{id}:{TextTools.FormatComponentName(prop.Name)} ({typeDesc})";
if(value != null)
{
string text;
if(GetPropertySequenceValue(prop, type, value, ref converter) is { } sequence)
{
// Format as sequence
text = String.Join(", ", sequence.Cast<object>().Select(converter.ConvertToInvariantString));
}else{
text = converter.ConvertToInvariantString(value);
}
LogWriter?.WriteLine($" - {line} = {text}");
}else{
LogWriter?.WriteLine($" - {line}");
}
}
}
static IEnumerable? GetPropertySequenceValue(PropertyDescriptor property, Type type, object value, ref TypeConverter typeConverter)
{
if(property.IsReadOnly && value is IEnumerable sequence && value is not string && ConfigurationTools.GetCollectionElementType(type) is { } elementType)
{
typeConverter = TypeDescriptor.GetConverter(elementType);
return sequence;
}
return null;
}
SFI.Application.ComponentCollection? aboutBrowsedCollection;
void AboutComponent<T>(T component, string id) where T : notnull
{
if(inputs.Contains(id))
{
LogWriter?.WriteLine();
LogWriter?.WriteLine($"# Component `{id}`");
var type = TypeDescriptor.GetReflectionType(component);
bool realType = type.Equals(component.GetType());
if(realType)
{
var elementType = aboutBrowsedCollection!.Attribute.CommonType ?? typeof(T);
LogWriter?.WriteLine($"Element type: {elementType.FullName}");
}
var assembly = type.Assembly;
LogWriter?.WriteLine($"Component type: {type.FullName}");
LogWriter?.WriteLine($"Assembly: {assembly.GetName().Name}");
PrintAttributes("Assembly ", assembly);
if(realType)
{
// Not a proxy configuration for another type
var baseType = type;
do{
// Look for the nearest base type
if(!baseType.IsGenericType)
{
// Must be generic
continue;
}
var genArgs = baseType.GetGenericArguments();
if(genArgs.Length != 1)
{
// Must have single type argument
continue;
}
var objType = genArgs[0];
LogWriter?.WriteLine($"Argument type: {objType.FullName}");
LogWriter?.WriteLine($"Argument assembly: {objType.Assembly.GetName().Name}");
PrintAttributes("Argument assembly ", objType.Assembly);
break;
}while((baseType = baseType.BaseType) != null);
}
PrintAttributes("", TypeDescriptor.GetAttributes(component));
var properties = ConfigurationTools.GetConfigurableProperties(component);
foreach(var prop in properties)
{
LogWriter?.WriteLine();
LogWriter?.WriteLine($"## Property `{TextTools.FormatComponentName(prop.Name)}`");
LogWriter?.WriteLine($"Name: {prop.Name}");
var propType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
LogWriter?.WriteLine($"Type: {propType.FullName}");
var converter = prop.Converter;
if(ConfigurationTools.GetStandardValues(propType, converter, out var values))
{
var valuesText = String.Join("|", values.Cast<object>().Select(converter.ConvertToInvariantString));
var exclusive = converter.GetStandardValuesExclusive();
LogWriter?.WriteLine($"Standard values: {valuesText}{(exclusive ? "" : "...")}");
}
PrintAttributes("", prop.Attributes);
var value = prop.GetValue(component);
if(value != null)
{
if(GetPropertySequenceValue(prop, propType, value, ref converter) is { } sequence)
{
// Format as sequence
var text = String.Join(", ", sequence.Cast<object>().Select(converter.ConvertToInvariantString));
LogWriter?.WriteLine($"Values: {text}");
}else{
LogWriter?.WriteLine($"Value: {converter.ConvertToInvariantString(value)}");
}
}
}
}
}
void AboutCollection(Application.ComponentCollection collection)
{
LogWriter?.WriteLine();
LogWriter?.WriteLine($"# Collection `{collection.Attribute.Prefix}`");
Type? componentType = null;
if(collection.Property is PropertyDescriptor prop)
{
PrintAttributes("", prop.Attributes);
componentType = prop.ComponentType;
}
var elementType = collection.Attribute.CommonType ?? collection.ElementType;
LogWriter?.WriteLine($"Element type: {elementType.FullName}");
LogWriter?.WriteLine($"Element assembly: {elementType.Assembly.GetName().Name}");
if(collection.Component is object component)
{
LogWriter?.WriteLine();
LogWriter?.WriteLine($"Component: {TextTools.GetUserFriendlyName(component)}");
PrintAttributes("Component ", TypeDescriptor.GetAttributes(component));
componentType ??= TypeDescriptor.GetReflectionType(component);
}
if(componentType != null)
{
var assembly = componentType.Assembly;
LogWriter?.WriteLine($"Component type: {componentType.FullName}");
LogWriter?.WriteLine($"Assembly: {assembly.GetName().Name}");
PrintAttributes("Assembly ", assembly);
}
}
void PrintAttributes(string prefix, ICustomAttributeProvider provider)
{
foreach(var pair in ConfigurationTools.GetTextAttributes(provider.GetCustomAttributes(false).OfType<Attribute>()))
{
LogWriter?.WriteLine($"{prefix}{pair.Key}: {pair.Value}");
}
}
void PrintAttributes(string prefix, AttributeCollection attributes)
{
foreach(var pair in ConfigurationTools.GetTextAttributes(attributes.OfType<Attribute>()))
{
LogWriter?.WriteLine($"{prefix}{pair.Key}: {pair.Value}");
}
}
async ITask<ValueTuple> IResultFactory<ValueTuple, string>.Invoke<T>(T component, string id)
{
if(IsIncluded(component, id))
{
switch(mode)
{
case Mode.List:
ListComponent(component, id);
break;
case Mode.About:
AboutComponent(component, id);
break;
}
}
return default;
}
#endregion
#region Parameters
/// <inheritdoc/>
protected override string Usage => $"({String.Join("|", modeNames)}) [options] input... output";
/// <inheritdoc/>
public override void Description()
{
LogWriter.WriteLine();
LogWriter.Write(" ");
OutputWrapPad("This software analyzes the formats of given files and outputs RDF description of their contents.", 1);
}
/// <summary>
/// The operating mode of the application.
/// </summary>
enum Mode
{
/// <summary>
/// The application should describe the input files in RDF.
/// </summary>
Describe,
/// <summary>
/// The application should search input files using SPARQL.
/// </summary>
Search,
/// <summary>
/// The application should list all available components.
/// </summary>
List,
/// <summary>
/// The application should display information about a particular component.
/// </summary>
About,
/// <summary>
/// The application should output the provided options in the XML format.
/// </summary>
Options,
}
/// <inheritdoc/>
public override IList<OptionInfo> GetOptions()
{
return new OptionInfoCollection{
{"q", "quiet", null, "do not print any additional messages"},
{"i", "include", "pattern", "include given components"},
{"x", "exclude", "pattern", "exclude given components"},
{"f", "format", "extension|mime", "the RDF serialization format of the output"},
{"c", "compress", null, "perform gzip compression on the output"},
{"m", "metadata", null, "add annotation metadata to output"},
{"d", "data-only", null, "do not store input file information"},
{"u", "ugly", null, "do not use pretty print"},
{"o", "only-once", null, "skip processing duplicate entities"},
{"b", "buffered", String.Join("|", bufferingNames), "set the level of graph buffering of data"},
{"h", "hash", "pattern", "set the main binary hash"},
{"r", "root", "uri", "set the hierarchy root URI prefix"},
{"s", "sparql-query", "file", "perform a SPARQL query during processing"},
{"p", "plugin", "id", "loads a plugin with a particular identifier"},
{"C", "config", "file", "loads additional configuration"},
{"?", "help", null, "displays this help message"},
{null, "[component]:[property]", "value", "sets a specific component's property (see list)"}
};
}
/// <inheritdoc/>
protected override OperandState OnOperandFound(string operand)
{
if(mode == null)
{
if(!Enum.TryParse<Mode>(operand, true, out var mode))
{
throw new ApplicationException($"Invalid mode (must be one of {String.Join(", ", modeNames)}).");
}
this.mode = mode;
if(mode == Mode.Options)
{
CaptureOptions();
}
return OperandState.ContinueOptions;
}
if(output == null)
{
output = operand;
return OperandState.OnlyOperands;
}
inputs.Add(output);
output = operand;
return OperandState.OnlyOperands;
}
static readonly Regex componentPropertyRegex = new(@"^([^:]+:[^:]+):(.*)$", RegexOptions.Compiled);
/// <inheritdoc/>
protected override string GetCanonicalOption(string option)
{
return option switch
{
"q" => "quiet",
"c" => "compress",
"m" => "metadata",
"d" => "data-only",
"o" => "only-once",
"b" => "buffered",
"u" => "ugly",
"r" => "root",
"i" => "include",
"x" => "exclude",
"h" => "hash",
"s" => "sparql-query",
"f" => "format",
"p" => "plugin",
"C" => "config",
"?" => "help",
_ => base.GetCanonicalOption(option),
};
}
/// <inheritdoc/>
protected override OptionArgumentFlags OnOptionFound(string option)
{
switch(GetCanonicalOption(option))
{
case "quiet":
quiet = true;
return OptionArgumentFlags.None;
case "compress":
options.CompressedOutput = true;
return OptionArgumentFlags.None;
case "metadata":
options.HideMetadata = false;
return OptionArgumentFlags.None;
case "data-only":
dataOnly = true;
return OptionArgumentFlags.None;
case "only-once":
onlyOnce = true;
return OptionArgumentFlags.None;
case "buffered":
return OptionArgumentFlags.OptionalArgument;
case "ugly":
options.PrettyPrint = false;
return OptionArgumentFlags.None;
case "root":
return OptionArgumentFlags.RequiredArgument;
case "include":
return OptionArgumentFlags.RequiredArgument | OptionArgumentFlags.AllowMultiple;
case "exclude":
return OptionArgumentFlags.RequiredArgument | OptionArgumentFlags.AllowMultiple;
case "hash":
return OptionArgumentFlags.RequiredArgument;
case "sparql-query":
return OptionArgumentFlags.RequiredArgument | OptionArgumentFlags.AllowMultiple;
case "format":
return OptionArgumentFlags.RequiredArgument;
case "plugin":
return OptionArgumentFlags.RequiredArgument | OptionArgumentFlags.AllowMultiple;
case "config":
return OptionArgumentFlags.RequiredArgument | OptionArgumentFlags.AllowMultiple;
case "help":
Help();
return OptionArgumentFlags.None;
default:
if(componentPropertyRegex.IsMatch(option))
{
return OptionArgumentFlags.RequiredArgument | OptionArgumentFlags.AllowMultiple;
}
throw UnrecognizedOption(option);
}
}
/// <inheritdoc/>
protected override void OnOptionArgumentFound(string option, string? argument, OptionArgumentFlags flags)
{
switch(GetCanonicalOption(option))
{
case "buffered":
if(String.IsNullOrEmpty(argument))
{
options.Buffering = BufferingLevel.Full;
}else if(Enum.TryParse(argument, true, out BufferingLevel level) && Enum.IsDefined(typeof(BufferingLevel), level))
{
options.Buffering = level;
}else{
throw ArgumentInvalid(option, $"one of {String.Join(", ", bufferingNames)}");
}
break;
case "root":
if(!Uri.TryCreate(argument, UriKind.Absolute, out _))
{
throw new ApplicationException($"The argument to option '{option}' must be a well-formed absolute URI.");
}
options.Root = argument!;
break;
case "include":
componentMatchers.Add(new Matcher(true, argument!));
break;
case "exclude":
componentMatchers.Add(new Matcher(false, argument!));
break;
case "hash":
mainHash = TextTools.ConvertWildcardToRegex(argument!);
mainHashName = argument;
componentMatchers.Add(new Matcher(true, "data-hash:" + argument!, true));
break;
case "sparql-query":
queries.Add(argument!);
break;
case "format":
options.Format = argument!;
break;
case "plugin":
plugins.Add(argument!);
break;
case "config":
LoadConfig(argument!);
break;
default:
if(componentPropertyRegex.Match(option) is { Success: true } propMatch)
{
var componentId = propMatch.Groups[1].Value.ToLowerInvariant();
var propertyId = propMatch.Groups[2].Value.ToLowerInvariant();
if(TextTools.ContainsWildcardCharacters(componentId) || TextTools.ContainsWildcardCharacters(propertyId))
{
regexComponentProperties.Add(new(
TextTools.ConvertWildcardToRegex(componentId),
new()
{
new(TextTools.ConvertWildcardToRegex(propertyId), argument!)
}
));
}else{
if(!componentProperties.TryGetValue(componentId, out var dict))
{
componentProperties[componentId] = dict = new(StringComparer.OrdinalIgnoreCase);
}
if(!dict.TryGetValue(propertyId, out var list))
{
dict[propertyId] = list = new();
}
list.Add(argument!);
}
}
break;
}
}
class Matcher
{
public bool Result { get; }
public string? Pattern { get; }
public Predicate<string> Predicate { get; }
public bool HadEffect { get; set; }
public Matcher(bool result, string pattern, bool forgetPattern = false)
{
Result = result;
Pattern = forgetPattern ? null : pattern;
Predicate = TextTools.ConvertWildcardToRegex(pattern).IsMatch;
}
}
#endregion
void LoadConfig(string name = "sfi-config.xml")
{
foreach(var config in environment.GetFiles(name).OfType<IFileInfo>())
{
LoadConfigXml(name, config);
}
}
void PrintOptionsXml()
{
if(output == null)
{
throw new ApplicationException("An output must be specified to save the options!");
}
using var file = environment.CreateFile(output, "application/xml");
SaveConfigXml(file);
}
}
}