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,101 +0,0 @@
Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Logging
Public Class BaseFunctions
Private 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
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

View File

@ -1,13 +1,103 @@
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports System.Text.RegularExpressions
Namespace [PatternModule] Namespace [PatternModule]
Public Class BaseModule Public Class BaseModule
Friend ReadOnly BaseFunctions As BaseFunctions
Friend ReadOnly Logger As Logger 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) Public Sub New(pLogConfig As LogConfig)
Logger = pLogConfig.GetLogger() Logger = pLogConfig.GetLogger()
BaseFunctions = New BaseFunctions(pLogConfig)
End Sub 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 Class
End Namespace End Namespace

View File

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

View File

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

View File

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

View File

@ -80,8 +80,8 @@
<Import Include="System.Threading.Tasks" /> <Import Include="System.Threading.Tasks" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="BaseFunctions.vb" />
<Compile Include="Constants.vb" /> <Compile Include="Constants.vb" />
<Compile Include="Modules\User.vb" />
<Compile Include="Modules\IModule.vb" /> <Compile Include="Modules\IModule.vb" />
<Compile Include="Modules\Clipboard.vb" /> <Compile Include="Modules\Clipboard.vb" />
<Compile Include="Modules\Controls.vb" /> <Compile Include="Modules\Controls.vb" />

View File

@ -2,6 +2,7 @@
Imports System.Windows.Forms Imports System.Windows.Forms
Imports DigitalData.Controls.LookupGrid Imports DigitalData.Controls.LookupGrid
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow
Imports WINDREAMLib Imports WINDREAMLib
''' <summary> ''' <summary>
@ -16,7 +17,7 @@ Imports WINDREAMLib
''' {#CTRL#CMB_2} ''' {#CTRL#CMB_2}
''' {#WMI#String 39} ''' {#WMI#String 39}
''' </summary> ''' </summary>
Public Class clsPatterns Public Class Patterns2
''' <summary> ''' <summary>
''' Complex patterns that rely on Windream ''' Complex patterns that rely on Windream
''' </summary> ''' </summary>
@ -48,19 +49,24 @@ Public Class clsPatterns
Private ReadOnly LogConfig As LogConfig Private ReadOnly LogConfig As LogConfig
Private ReadOnly Logger As Logger Private ReadOnly Logger As Logger
Private ReadOnly Base As PatternModule.BaseModule
Private ReadOnly ControlPanel As Panel Private ReadOnly ControlPanel As Panel
Private ReadOnly IDBActive As Boolean Private ReadOnly IDBActive As Boolean
Private ReadOnly Base As BaseFunctions
Private ReadOnly Modules As New List(Of IModule) Private ReadOnly Modules As New List(Of IModule)
Public Sub New(pLogConfig As LogConfig) Public Sub New(pLogConfig As LogConfig)
LogConfig = pLogConfig LogConfig = pLogConfig
Logger = pLogConfig.GetLogger() Logger = pLogConfig.GetLogger()
Base = New BaseFunctions(LogConfig) Base = New PatternModule.BaseModule(LogConfig)
Modules.Add(New PatternModule.Internal(LogConfig, Base)) Modules.AddRange({
New PatternModule.Internal(LogConfig),
New PatternModule.Clipboard(LogConfig),
New PatternModule.Controls(LogConfig),
New PatternModule.User(LogConfig)
})
End Sub End Sub
Public Sub New(pLogConfig As LogConfig, pControlPanel As Panel, pIDBActive As Boolean) Public Sub New(pLogConfig As LogConfig, pControlPanel As Panel, pIDBActive As Boolean)
@ -69,12 +75,12 @@ Public Class clsPatterns
IDBActive = pIDBActive IDBActive = pIDBActive
End Sub End Sub
Public Function ReplaceAllValues(pInput As String, pPanel As Panel) As String Public Function ReplaceAllValues(pInput As String, pPanel As Panel, pUser As State.UserState) As String
Dim oResult = pInput Dim oResult = pInput
For Each oModule In Modules For Each oModule In Modules
Try Try
Dim oArgs = GetReplaceMapForModule(oModule, pPanel) Dim oArgs = GetReplaceMapForModule(oModule, pPanel, pUser)
oResult = oModule.Replace(oResult, oArgs) oResult = oModule.Replace(oResult, oArgs)
Catch ex As Exception Catch ex As Exception
Logger.Warn("Placeholders for String [{0}] could not be replaced completely in Module [{1}]. Skipping.", pInput, oModule.GetType.Name) Logger.Warn("Placeholders for String [{0}] could not be replaced completely in Module [{1}]. Skipping.", pInput, oModule.GetType.Name)
@ -85,30 +91,40 @@ Public Class clsPatterns
Return oResult Return oResult
End Function End Function
Private Function GetReplaceMapForModule(pModule As IModule, pPanel As Panel) As Dictionary(Of String, Object) Private Function GetReplaceMapForModule(pModule As IModule, pPanel As Panel, pUser As State.UserState) As Dictionary(Of String, Object)
Dim oArgs As New Dictionary(Of String, Object) Dim oArgs As New Dictionary(Of String, Object)
Select Case pModule.GetType()
Case GetType(PatternModule.Clipboard)
End Select
If TypeOf pModule Is PatternModule.Clipboard Then If TypeOf pModule Is PatternModule.Clipboard Then
oArgs.Add(PatternModule.Clipboard.CLIP_VALUE_BOARD, My.Computer.Clipboard.GetText()) oArgs.Add(PatternModule.Clipboard.CLIP_VALUE_BOARD, My.Computer.Clipboard.GetText())
ElseIf TypeOf pModule Is PatternModule.Internal Then ElseIf TypeOf pModule Is PatternModule.Internal Then
oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, Environment.UserName) oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, System.Environment.UserName)
oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, Environment.MachineName) oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, System.Environment.MachineName)
oArgs.Add(PatternModule.Internal.INT_VALUE_DOMAIN, Environment.UserDomainName) oArgs.Add(PatternModule.Internal.INT_VALUE_DOMAIN, System.Environment.UserDomainName)
oArgs.Add(PatternModule.Internal.INT_VALUE_DATE, Now.ToShortDateString) oArgs.Add(PatternModule.Internal.INT_VALUE_DATE, Now.ToShortDateString)
ElseIf TypeOf pModule Is PatternModule.User Then
oArgs.Add(PatternModule.User.USER_VALUE_EMAIL, pUser.Email)
oArgs.Add(PatternModule.User.USER_VALUE_LANGUAGE, pUser.Language)
oArgs.Add(PatternModule.User.USER_VALUE_PRENAME, pUser.GivenName)
oArgs.Add(PatternModule.User.USER_VALUE_SHORTNAME, pUser.ShortName)
oArgs.Add(PatternModule.User.USER_VALUE_SURNAME, pUser.Surname)
oArgs.Add(PatternModule.User.USER_VALUE_USER_ID, pUser.UserId)
oArgs.Add(PatternModule.User.USER_VALUE_USER_NAME, pUser.UserName)
ElseIf TypeOf pModule Is PatternModule.Controls Then ElseIf TypeOf pModule Is PatternModule.Controls Then
oArgs.Add(PatternModule.Controls.CTRL_VALUE_PANEL, pPanel) oArgs.Add(PatternModule.Controls.CTRL_VALUE_PANEL, pPanel)
End If End If
Return oArgs Return oArgs
End Function End Function
#Region "Helper Functions" #Region "Helper Functions"
''' <summary> ''' <summary>
''' Wraps a pattern-type and -value in the common format: {#type#value} ''' Wraps a pattern-type and -value in the common format: {#type#value}
''' </summary> ''' </summary>