Patterns: WIP Patterns2

This commit is contained in:
Jonathan Jenne
2021-11-01 13:28:56 +01:00
parent d83c0125a4
commit dcf5bbe21c
8 changed files with 205 additions and 139 deletions

View File

@@ -1,13 +1,103 @@
Imports DigitalData.Modules.Logging
Imports System.Text.RegularExpressions
Namespace [PatternModule]
Public Class BaseModule
Friend ReadOnly BaseFunctions As BaseFunctions
Friend ReadOnly Logger As Logger
Private ReadOnly MyRegex As Regex = New Regex("{#(\w+)#([\:\.\w\s_-]+)}+")
Private Const MAX_TRY_COUNT = 500
Public Sub New(pLogConfig As LogConfig)
Logger = pLogConfig.GetLogger()
BaseFunctions = New BaseFunctions(pLogConfig)
End Sub
Public Sub IncrementCounterOrThrow(ByRef pCounter As Integer)
If pCounter >= MAX_TRY_COUNT Then
Throw New OverflowException("Max tries exceeded while replacing placeholders!")
End If
pCounter += 1
End Sub
Public Function ReplacePattern(input As String, type As String, replacement As String) As String
Dim elements As MatchCollection = MyRegex.Matches(input)
If IsNothing(replacement) Then
Return input
End If
For Each element As Match In elements
' 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)
End If
Next
' no replacement made
Return input
End Function
Public Function ContainsPatternAndValue(pInput As String, pType As String, pValue As String) As Boolean
Dim oElements As MatchCollection = MyRegex.Matches(pInput)
For Each oElement As Match In oElements
' Pattern in pInput
Dim t As String = oElement.Groups(1).Value
Dim v As String = oElement.Groups(2).Value
If t = pType And v = pValue Then
Return True
End If
Next
Return False
End Function
Public Function ContainsPattern(pInput As String, pType As String) As String
Dim oElements As MatchCollection = MyRegex.Matches(pInput)
For Each oElement As Match In oElements
Dim t As String = oElement.Groups(1).Value
If t = pType Then
Return True
End If
Next
Return False
End Function
Public Function HasPattern(pInput As String, pType As String) As Boolean
Dim oMatches = MyRegex.Matches(pInput)
For Each oMatch As Match In oMatches
For Each oGroup As Group In oMatch.Groups
If oGroup.Value = pType Then
Return True
End If
Next
Next
Return False
End Function
Public Function GetNextPattern(input As String, type As String) As Pattern
Dim elements As MatchCollection = MyRegex.Matches(input)
For Each element As Match In elements
' Pattern in pInput
Dim t As String = element.Groups(1).Value
Dim v As String = element.Groups(2).Value
If t = type Then
Return New Pattern(t, v)
End If
Next
Return Nothing
End Function
End Class
End Namespace

View File

@@ -34,9 +34,9 @@ Namespace [PatternModule]
oResult = oResult.Replace(CLIPBOARD_VALUE_EN, oClipboardContents)
' Replace Clipboard Contents
While BaseFunctions.ContainsPatternAndValue(oResult, PatternIdentifier, CLIP_VALUE_BOARD)
oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, oClipboardContents)
BaseFunctions.IncrementCounterOrThrow(oCounter)
While ContainsPatternAndValue(oResult, PatternIdentifier, CLIP_VALUE_BOARD)
oResult = ReplacePattern(oResult, PatternIdentifier, oClipboardContents)
IncrementCounterOrThrow(oCounter)
End While
Logger.Debug("Input after Clipboard.Replace: [{0}]", pInput)

View File

@@ -24,8 +24,8 @@ Namespace [PatternModule]
Dim oCounter = 0
Dim oPanel As Panel = pReplaceMap.Item("CTRL_VALUE_PANEL")
While BaseFunctions.ContainsPattern(oResult, PatternIdentifier)
Dim oControlName As String = BaseFunctions.GetNextPattern(oResult, PatternIdentifier).Value
While ContainsPattern(oResult, PatternIdentifier)
Dim oControlName As String = GetNextPattern(oResult, PatternIdentifier).Value
Dim oControl As Control = oPanel.Controls.Find(oControlName, False).FirstOrDefault()
If oControl IsNot Nothing Then
@@ -49,10 +49,10 @@ Namespace [PatternModule]
oReplaceValue = "0"
End Select
oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, oReplaceValue)
oResult = ReplacePattern(oResult, PatternIdentifier, oReplaceValue)
End If
BaseFunctions.IncrementCounterOrThrow(oCounter)
IncrementCounterOrThrow(oCounter)
End While
Return oResult

View File

@@ -17,7 +17,7 @@ Namespace [PatternModule]
Public Property IsComplex As Boolean = False Implements IModule.IsComplex
Public Sub New(pLogConfig As LogConfig, pBaseFunctions As BaseFunctions)
Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig)
End Sub
@@ -26,29 +26,29 @@ Namespace [PatternModule]
Dim oCounter = 0
' Replace Username(s)
While BaseFunctions.ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_USERNAME)
oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_USERNAME))
BaseFunctions.IncrementCounterOrThrow(oCounter)
While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_USERNAME)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_USERNAME))
IncrementCounterOrThrow(oCounter)
End While
' Replace Machinename(s)
While BaseFunctions.ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_MACHINE)
oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_MACHINE))
BaseFunctions.IncrementCounterOrThrow(oCounter)
While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_MACHINE)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_MACHINE))
IncrementCounterOrThrow(oCounter)
End While
' Replace Domainname(s)
While BaseFunctions.ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DOMAIN)
oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_DOMAIN))
BaseFunctions.IncrementCounterOrThrow(oCounter)
While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DOMAIN)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_DOMAIN))
IncrementCounterOrThrow(oCounter)
End While
' Replace CurrentDate(s)
While BaseFunctions.ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DATE)
oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_DATE))
BaseFunctions.IncrementCounterOrThrow(oCounter)
While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DATE)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_DATE))
IncrementCounterOrThrow(oCounter)
End While
Return oResult

View File

@@ -0,0 +1,61 @@
Imports DigitalData.Modules.Logging
Namespace [PatternModule]
Public Class User
Inherits BaseModule
Implements IModule
Public Const USER_VALUE_PRENAME = "PRENAME"
Public Const USER_VALUE_SURNAME = "SURNAME"
Public Const USER_VALUE_EMAIL = "EMAIL"
Public Const USER_VALUE_SHORTNAME = "SHORTNAME"
Public Const USER_VALUE_LANGUAGE = "LANGUAGE"
Public Const USER_VALUE_USER_ID = "USER_ID"
Public Const USER_VALUE_USER_NAME = "USER_NAME"
Public Property PatternIdentifier As String = "USER" Implements IModule.PatternIdentifier
Public Property IsComplex As Boolean = True Implements IModule.IsComplex
Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig)
End Sub
Public Function Replace(pInput As String, pReplaceMap As Dictionary(Of String, Object)) As String Implements IModule.Replace
Dim oResult = pInput
Dim oCounter = 0
While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_PRENAME)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(USER_VALUE_PRENAME))
IncrementCounterOrThrow(oCounter)
End While
While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_SURNAME)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(USER_VALUE_SURNAME))
IncrementCounterOrThrow(oCounter)
End While
While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_EMAIL)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(USER_VALUE_EMAIL))
IncrementCounterOrThrow(oCounter)
End While
While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_SHORTNAME)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(USER_VALUE_SHORTNAME))
IncrementCounterOrThrow(oCounter)
End While
While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_LANGUAGE)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(USER_VALUE_LANGUAGE))
IncrementCounterOrThrow(oCounter)
End While
While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_USER_ID)
oResult = ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(USER_VALUE_USER_ID))
IncrementCounterOrThrow(oCounter)
End While
Return oResult
End Function
End Class
End Namespace