BadScript 2
Loading...
Searching...
No Matches
BadLinqQuery.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Globalization;
3using System.Text;
4
6
11
15public static class BadLinqQuery
16{
20 private static readonly List<BadLinqQueryCommand> s_Commands = new List<BadLinqQueryCommand>();
21
39
44 public static void RegisterCommand(BadLinqQueryCommand command)
45 {
46 s_Commands.Add(command);
47 }
48
56 public static IEnumerable Parse(string linqQuery, IEnumerable input)
57 {
58 IEnumerable current = input;
59
60 while (!string.IsNullOrEmpty(linqQuery))
61 {
62 linqQuery = linqQuery.Trim();
63 string command = linqQuery.Split(' ').First();
64 linqQuery = linqQuery.Remove(0, command.Length);
66 x => x.Names.Any(
67 y =>
68 y.ToLower(CultureInfo.InvariantCulture) == command.ToLower(CultureInfo.InvariantCulture)
69 )
70 );
71
72 if (cmd.HasArgument)
73 {
74 StringBuilder sb = new StringBuilder();
75
76 while (!string.IsNullOrEmpty(linqQuery) &&
77 !s_Commands.Any(x => x.Names.Any(y => linqQuery.StartsWith(y))))
78 {
79 sb.Append(linqQuery[0]);
80 linqQuery = linqQuery.Substring(1);
81 }
82
83 string arg = sb.ToString().Trim();
84
85 if (string.IsNullOrWhiteSpace(arg) && !cmd.IsArgumentOptional)
86 {
87 throw new Exception("Missing Argument for command " + cmd);
88 }
89
90 current = cmd.Run(new BadLinqQueryCommandData(current, arg));
91 }
92 else
93 {
94 current = cmd.Run(new BadLinqQueryCommandData(current));
95 }
96 }
97
98 return current;
99 }
100}
Implements the Command Data for the BadLinqQuery.
Implements an Abstract Command for the BadLinqQuery.
bool HasArgument
Does the Command have an Argument?
IEnumerable Run(BadLinqQueryCommandData data)
Runs the Command.
Implements the BadLinqQuery funcitonality.
static void RegisterCommand(BadLinqQueryCommand command)
Registers a new command.
static readonly List< BadLinqQueryCommand > s_Commands
The registered commands.
static IEnumerable Parse(string linqQuery, IEnumerable input)
Parses the given linq query and applies it to the given input.
static BadLinqQuery()
Static Constructor.
Implements the Select Command for the BadLinqQuery.
Contains the Linq Queries.