Patterns: WIP Patterns version 2
This commit is contained in:
@@ -47,58 +47,58 @@ Public Class ClassPatterns
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Private _Logger As Logger
|
||||
Private _LogConfig As LogConfig
|
||||
Private ReadOnly _Logger As Logger
|
||||
Private ReadOnly _LogConfig As LogConfig
|
||||
|
||||
Private _Regex As Regex = New Regex("{#(\w+)#([\w\s_-]+)}+")
|
||||
Private _AllPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL, PATTERN_USER, PATTERN_INT}
|
||||
Private _ComplexPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL}
|
||||
Private _SimplePatterns As New List(Of String) From {PATTERN_USER, PATTERN_INT}
|
||||
Private ReadOnly _Regex As Regex = New Regex("{#(\w+)#([\w\s_-]+)}+")
|
||||
Private ReadOnly _AllPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL, PATTERN_USER, PATTERN_INT}
|
||||
Private ReadOnly _ComplexPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL}
|
||||
Private ReadOnly _SimplePatterns As New List(Of String) From {PATTERN_USER, PATTERN_INT}
|
||||
|
||||
''' <summary>
|
||||
''' Wraps a pattern-type and -value in the common format: {#type#value}
|
||||
''' </summary>
|
||||
Public Function WrapPatternValue(type As String, value As String) As String
|
||||
Return New Pattern(type, value).ToString
|
||||
Public Function WrapPatternValue(pType As String, pValue As String) As String
|
||||
Return New Pattern(pType, pValue).ToString
|
||||
End Function
|
||||
|
||||
Public Sub New(LogConfig As LogConfig)
|
||||
_LogConfig = LogConfig
|
||||
_Logger = LogConfig.GetLogger
|
||||
Public Sub New(pLogConfig As LogConfig)
|
||||
_LogConfig = pLogConfig
|
||||
_Logger = pLogConfig.GetLogger
|
||||
End Sub
|
||||
|
||||
Public Function ReplaceAllValues(input As String, User As State.UserState, ClipboardContents As String) As String
|
||||
Public Function ReplaceAllValues(pInput As String, pUser As State.UserState, pClipboardContents As String) As String
|
||||
Try
|
||||
Dim result = input
|
||||
Dim result = pInput
|
||||
|
||||
result = ReplaceClipboardContents(result, ClipboardContents)
|
||||
result = ReplaceClipboardContents(result, pClipboardContents)
|
||||
result = ReplaceInternalValues(result)
|
||||
result = ReplaceUserValues(result, User)
|
||||
result = ReplaceUserValues(result, pUser)
|
||||
|
||||
Return result
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("Error in ReplaceAllValues:" & ex.Message)
|
||||
Return input
|
||||
Return pInput
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function ReplaceClipboardContents(Input As String, ClipboardContents As String) As String
|
||||
Dim oResult = Input
|
||||
Public Function ReplaceClipboardContents(pInput As String, pClipboardContents As String) As String
|
||||
Dim oResult = pInput
|
||||
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_DE.ToLower, ClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_DE.ToUpper, ClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_EN.ToLower, ClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_EN.ToUpper, ClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_DE, ClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_EN, ClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_DE.ToLower, pClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_DE.ToUpper, pClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_EN.ToLower, pClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_EN.ToUpper, pClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_DE, pClipboardContents)
|
||||
oResult = oResult.Replace(CLIPBOARD_VALUE_EN, pClipboardContents)
|
||||
|
||||
Return oResult
|
||||
End Function
|
||||
|
||||
Public Function ReplaceInternalValues(Input As String) As String
|
||||
Public Function ReplaceInternalValues(pInput As String) As String
|
||||
Try
|
||||
Dim oResult = Input
|
||||
Dim oResult = pInput
|
||||
|
||||
' Replace Username(s)
|
||||
While ContainsPatternAndValue(oResult, PATTERN_INT, INT_VALUE_USERNAME)
|
||||
@@ -124,45 +124,45 @@ Public Class ClassPatterns
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("Error in ReplaceInternalValues:" & ex.Message)
|
||||
Return Input
|
||||
Return pInput
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function ReplaceUserValues(Input As String, User As State.UserState) As String
|
||||
Public Function ReplaceUserValues(pInput As String, pUser As State.UserState) As String
|
||||
Try
|
||||
Dim oResult = Input
|
||||
Dim oResult = pInput
|
||||
|
||||
While ContainsPatternAndValue(oResult, PATTERN_USER, USER_VALUE_PRENAME)
|
||||
oResult = ReplacePattern(Input, PATTERN_USER, User.GivenName)
|
||||
oResult = ReplacePattern(pInput, PATTERN_USER, pUser.GivenName)
|
||||
End While
|
||||
|
||||
While ContainsPatternAndValue(oResult, PATTERN_USER, USER_VALUE_USER_ID)
|
||||
oResult = ReplacePattern(Input, PATTERN_USER, User.UserId.ToString)
|
||||
oResult = ReplacePattern(pInput, PATTERN_USER, pUser.UserId.ToString)
|
||||
End While
|
||||
|
||||
While ContainsPatternAndValue(oResult, PATTERN_USER, USER_VALUE_SURNAME)
|
||||
oResult = ReplacePattern(Input, PATTERN_USER, User.Surname)
|
||||
oResult = ReplacePattern(pInput, PATTERN_USER, pUser.Surname)
|
||||
End While
|
||||
|
||||
While ContainsPatternAndValue(oResult, PATTERN_USER, USER_VALUE_SHORTNAME)
|
||||
oResult = ReplacePattern(Input, PATTERN_USER, User.ShortName)
|
||||
oResult = ReplacePattern(pInput, PATTERN_USER, pUser.ShortName)
|
||||
End While
|
||||
|
||||
While ContainsPatternAndValue(oResult, PATTERN_USER, USER_VALUE_EMAIL)
|
||||
oResult = ReplacePattern(Input, PATTERN_USER, User.Email)
|
||||
oResult = ReplacePattern(pInput, PATTERN_USER, pUser.Email)
|
||||
End While
|
||||
|
||||
Return oResult
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("Error in ReplaceUserValues:" & ex.Message)
|
||||
Return Input
|
||||
Return pInput
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function ReplaceControlValues(Input As String, Panel As Panel) As String
|
||||
Public Function ReplaceControlValues(pInput As String, pPanel As Panel) As String
|
||||
Try
|
||||
Dim oResult = Input
|
||||
Dim oResult = pInput
|
||||
Dim oTryCounter = 0
|
||||
|
||||
While ContainsPattern(oResult, PATTERN_CTRL)
|
||||
@@ -171,7 +171,7 @@ Public Class ClassPatterns
|
||||
End If
|
||||
|
||||
Dim controlName As String = GetNextPattern(oResult, PATTERN_CTRL).Value
|
||||
Dim control As Control = Panel.Controls.Find(controlName, False).FirstOrDefault()
|
||||
Dim control As Control = pPanel.Controls.Find(controlName, False).FirstOrDefault()
|
||||
|
||||
If control IsNot Nothing Then
|
||||
Dim value As String = control.Text
|
||||
@@ -185,17 +185,17 @@ Public Class ClassPatterns
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("Error in ReplaceControlValues:" & ex.Message)
|
||||
Return Input
|
||||
Return pInput
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function ContainsPattern(input As String, type As String) As Boolean
|
||||
Dim elements As MatchCollection = _Regex.Matches(input)
|
||||
Private Function ContainsPattern(pInput As String, pType As String) As Boolean
|
||||
Dim elements As MatchCollection = _Regex.Matches(pInput)
|
||||
|
||||
For Each element As Match In elements
|
||||
Dim t As String = element.Groups(1).Value
|
||||
|
||||
If t = type Then
|
||||
If t = pType Then
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
@@ -203,23 +203,23 @@ Public Class ClassPatterns
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Public Function GetNextPattern(Input As String, Type As String) As Pattern
|
||||
Dim oElements As MatchCollection = _Regex.Matches(Input)
|
||||
Public Function GetNextPattern(pInput As String, pType As String) As Pattern
|
||||
Dim oElements As MatchCollection = _Regex.Matches(pInput)
|
||||
|
||||
For Each oElement As Match In oElements
|
||||
' Pattern in input
|
||||
Dim oType As String = oElement.Groups(1).Value
|
||||
Dim oValue As String = oElement.Groups(2).Value
|
||||
|
||||
If oType = Type Then
|
||||
If oType = pType Then
|
||||
Return New Pattern(oType, oValue)
|
||||
End If
|
||||
Next
|
||||
|
||||
Return Nothing
|
||||
End Function
|
||||
Public Function GetAllPatterns(Input As String) As List(Of Pattern)
|
||||
Dim elements As MatchCollection = _Regex.Matches(Input)
|
||||
Public Function GetAllPatterns(pInput As String) As List(Of Pattern)
|
||||
Dim elements As MatchCollection = _Regex.Matches(pInput)
|
||||
Dim results As New List(Of Pattern)
|
||||
|
||||
For Each element As Match In elements
|
||||
@@ -232,33 +232,33 @@ Public Class ClassPatterns
|
||||
|
||||
Return results
|
||||
End Function
|
||||
Public Function ReplacePattern(Input As String, Type As String, Replacement As String) As String
|
||||
Dim oElements As MatchCollection = _Regex.Matches(Input)
|
||||
Public Function ReplacePattern(pInput As String, pType As String, pReplacement As String) As String
|
||||
Dim oElements As MatchCollection = _Regex.Matches(pInput)
|
||||
|
||||
If IsNothing(Replacement) Or Replacement = String.Empty Then
|
||||
Return Input
|
||||
If IsNothing(pReplacement) Or pReplacement = String.Empty Then
|
||||
Return pInput
|
||||
End If
|
||||
|
||||
For Each element As Match In oElements
|
||||
' if group 1 contains the 'pattern' the replace whole group with 'replacement'
|
||||
' and return it
|
||||
If element.Groups(1).Value = Type Then
|
||||
Return Regex.Replace(Input, element.Groups(0).Value, Replacement)
|
||||
If element.Groups(1).Value = pType Then
|
||||
Return Regex.Replace(pInput, element.Groups(0).Value, pReplacement)
|
||||
End If
|
||||
Next
|
||||
|
||||
' no replacement made
|
||||
Return Input
|
||||
Return pInput
|
||||
End Function
|
||||
Private Function ContainsPatternAndValue(Input As String, Type As String, Value As String) As Boolean
|
||||
Dim oElements As MatchCollection = _Regex.Matches(Input)
|
||||
Private Function ContainsPatternAndValue(pInput As String, pType As String, pValue As String) As Boolean
|
||||
Dim oElements As MatchCollection = _Regex.Matches(pInput)
|
||||
|
||||
For Each oElement As Match In oElements
|
||||
' Pattern in input
|
||||
Dim oType As String = oElement.Groups(1).Value
|
||||
Dim oValue As String = oElement.Groups(2).Value
|
||||
|
||||
If oType = Type And oValue = Value Then
|
||||
If oType = pType And oValue = pValue Then
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
@@ -266,28 +266,24 @@ Public Class ClassPatterns
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Public Function HasAnyPatterns(Input As String) As Boolean
|
||||
Return _AllPatterns.Any(Function(p)
|
||||
Return HasPattern(Input, p)
|
||||
End Function)
|
||||
Public Function HasAnyPatterns(pInput As String) As Boolean
|
||||
Return _AllPatterns.Any(Function(pPattern) HasPattern(pInput, pPattern))
|
||||
End Function
|
||||
|
||||
Public Function HasOnlySimplePatterns(Input As String) As Boolean
|
||||
Return Not HasComplexPatterns(Input)
|
||||
Public Function HasOnlySimplePatterns(pInput As String) As Boolean
|
||||
Return Not HasComplexPatterns(pInput)
|
||||
End Function
|
||||
|
||||
Public Function HasComplexPatterns(Input As String) As Boolean
|
||||
Return _ComplexPatterns.Any(Function(oPattern)
|
||||
Return HasPattern(Input, oPattern)
|
||||
End Function)
|
||||
Public Function HasComplexPatterns(pInput As String) As Boolean
|
||||
Return _ComplexPatterns.Any(Function(oPattern) HasPattern(pInput, oPattern))
|
||||
End Function
|
||||
|
||||
Public Function HasPattern(Input As String, Type As String) As Boolean
|
||||
Dim oMatches = _Regex.Matches(Input)
|
||||
Public Function HasPattern(pInput As String, pType As String) As Boolean
|
||||
Dim oMatches = _Regex.Matches(pInput)
|
||||
|
||||
For Each oMatch As Match In oMatches
|
||||
For Each oGroup As Group In oMatch.Groups
|
||||
If oGroup.Value = Type Then
|
||||
If oGroup.Value = pType Then
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
@@ -295,18 +291,4 @@ Public Class ClassPatterns
|
||||
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Public Class Pattern
|
||||
Public ReadOnly Property Type As String
|
||||
Public ReadOnly Property Value As String
|
||||
|
||||
Public Sub New(Type As String, Value As String)
|
||||
Me.Type = Type
|
||||
Me.Value = Value
|
||||
End Sub
|
||||
|
||||
Public Overrides Function ToString() As String
|
||||
Return $"{{#{Type}#{Value}}}"
|
||||
End Function
|
||||
End Class
|
||||
End Class
|
||||
Reference in New Issue
Block a user