52 lines
2.0 KiB
VB.net
52 lines
2.0 KiB
VB.net
Imports System.Collections.Generic
|
|
Imports System.Text.RegularExpressions
|
|
|
|
Public Class JobConfigParser
|
|
Private Shared JobOptionsRegex As New Regex("(?<enabled>True|False)\|(?<cron>[\w\d\s,\?*-/]*)(?:\|(?<args>(?:\w*::\w*,?)*))?")
|
|
Private Shared JobArgumentsRegex As New Regex("(?:(?:\w+::\w+,?)?)+")
|
|
Private Const ARGS_ITEM_DELIMITER As String = ","
|
|
Private Const ARGS_KEYVALUE_DELIMITER As String = "::"
|
|
|
|
Public Shared Function ParseConfig(ConfigString As String) As JobConfig
|
|
If JobOptionsRegex.IsMatch(ConfigString) Then
|
|
Dim oMatches = JobOptionsRegex.Matches(ConfigString)
|
|
Dim oOptions As New JobConfig
|
|
|
|
If oMatches.Count = 1 Then
|
|
Dim oMatch = oMatches.Item(0)
|
|
|
|
oOptions.Enabled = CBool(oMatch.Groups("enabled").Value)
|
|
oOptions.CronExpression = oMatch.Groups("cron").Value
|
|
oOptions.Arguments = ParseOptionalArguments(oMatch.Groups("args").Value)
|
|
Else
|
|
Throw New ArgumentException("Config Malformed")
|
|
End If
|
|
|
|
Return oOptions
|
|
Else
|
|
Throw New ArgumentException("Config Malformed")
|
|
End If
|
|
End Function
|
|
|
|
Private Shared Function ParseOptionalArguments(ArgsString As String) As Dictionary(Of String, String)
|
|
Dim oArgsDictionary As New Dictionary(Of String, String)
|
|
|
|
If JobArgumentsRegex.IsMatch(ArgsString) Then
|
|
Dim oArgs As String() = ArgsString.Split(ARGS_ITEM_DELIMITER)
|
|
|
|
For Each oArg In oArgs
|
|
Dim oDelimiter As String() = New String() {ARGS_KEYVALUE_DELIMITER}
|
|
Dim oArgSplit = oArg.Split(oDelimiter, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
If oArgSplit.Length = 2 Then
|
|
oArgsDictionary.Add(oArgSplit(0), oArgSplit(1))
|
|
Else
|
|
Throw New ArgumentException("Config Malformed")
|
|
End If
|
|
Next
|
|
End If
|
|
|
|
Return oArgsDictionary
|
|
End Function
|
|
End Class
|