From dcf5bbe21c5e13d99e216724b41d302815685d81 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Mon, 1 Nov 2021 13:28:56 +0100 Subject: [PATCH] Patterns: WIP Patterns2 --- Modules.Patterns/BaseFunctions.vb | 101 ------------------------- Modules.Patterns/Modules/BaseModule.vb | 94 ++++++++++++++++++++++- Modules.Patterns/Modules/Clipboard.vb | 6 +- Modules.Patterns/Modules/Controls.vb | 8 +- Modules.Patterns/Modules/Internal.vb | 26 +++---- Modules.Patterns/Modules/User.vb | 61 +++++++++++++++ Modules.Patterns/Patterns.vbproj | 2 +- Modules.Patterns/Patterns2.vb | 48 ++++++++---- 8 files changed, 206 insertions(+), 140 deletions(-) delete mode 100644 Modules.Patterns/BaseFunctions.vb create mode 100644 Modules.Patterns/Modules/User.vb diff --git a/Modules.Patterns/BaseFunctions.vb b/Modules.Patterns/BaseFunctions.vb deleted file mode 100644 index 2a0035fa..00000000 --- a/Modules.Patterns/BaseFunctions.vb +++ /dev/null @@ -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 diff --git a/Modules.Patterns/Modules/BaseModule.vb b/Modules.Patterns/Modules/BaseModule.vb index 37aa00f8..f6829e7e 100644 --- a/Modules.Patterns/Modules/BaseModule.vb +++ b/Modules.Patterns/Modules/BaseModule.vb @@ -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 diff --git a/Modules.Patterns/Modules/Clipboard.vb b/Modules.Patterns/Modules/Clipboard.vb index a6ad465e..b175dc04 100644 --- a/Modules.Patterns/Modules/Clipboard.vb +++ b/Modules.Patterns/Modules/Clipboard.vb @@ -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) diff --git a/Modules.Patterns/Modules/Controls.vb b/Modules.Patterns/Modules/Controls.vb index 69dd218d..738b78f8 100644 --- a/Modules.Patterns/Modules/Controls.vb +++ b/Modules.Patterns/Modules/Controls.vb @@ -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 diff --git a/Modules.Patterns/Modules/Internal.vb b/Modules.Patterns/Modules/Internal.vb index a1ad4df2..f3807049 100644 --- a/Modules.Patterns/Modules/Internal.vb +++ b/Modules.Patterns/Modules/Internal.vb @@ -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 diff --git a/Modules.Patterns/Modules/User.vb b/Modules.Patterns/Modules/User.vb new file mode 100644 index 00000000..18ea6e15 --- /dev/null +++ b/Modules.Patterns/Modules/User.vb @@ -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 diff --git a/Modules.Patterns/Patterns.vbproj b/Modules.Patterns/Patterns.vbproj index aaab2540..1e8c7443 100644 --- a/Modules.Patterns/Patterns.vbproj +++ b/Modules.Patterns/Patterns.vbproj @@ -80,8 +80,8 @@ - + diff --git a/Modules.Patterns/Patterns2.vb b/Modules.Patterns/Patterns2.vb index 3a8aae7f..1c7c1d87 100644 --- a/Modules.Patterns/Patterns2.vb +++ b/Modules.Patterns/Patterns2.vb @@ -2,6 +2,7 @@ Imports System.Windows.Forms Imports DigitalData.Controls.LookupGrid Imports DigitalData.Modules.Logging +Imports DigitalData.Modules.ZooFlow Imports WINDREAMLib ''' @@ -16,7 +17,7 @@ Imports WINDREAMLib ''' {#CTRL#CMB_2} ''' {#WMI#String 39} ''' -Public Class clsPatterns +Public Class Patterns2 ''' ''' Complex patterns that rely on Windream ''' @@ -48,19 +49,24 @@ Public Class clsPatterns Private ReadOnly LogConfig As LogConfig Private ReadOnly Logger As Logger + Private ReadOnly Base As PatternModule.BaseModule Private ReadOnly ControlPanel As Panel Private ReadOnly IDBActive As Boolean - Private ReadOnly Base As BaseFunctions Private ReadOnly Modules As New List(Of IModule) Public Sub New(pLogConfig As LogConfig) LogConfig = pLogConfig Logger = pLogConfig.GetLogger() - Base = New BaseFunctions(LogConfig) - - Modules.Add(New PatternModule.Internal(LogConfig, Base)) + Base = New PatternModule.BaseModule(LogConfig) + + Modules.AddRange({ + New PatternModule.Internal(LogConfig), + New PatternModule.Clipboard(LogConfig), + New PatternModule.Controls(LogConfig), + New PatternModule.User(LogConfig) + }) End Sub Public Sub New(pLogConfig As LogConfig, pControlPanel As Panel, pIDBActive As Boolean) @@ -69,12 +75,12 @@ Public Class clsPatterns IDBActive = pIDBActive 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 For Each oModule In Modules Try - Dim oArgs = GetReplaceMapForModule(oModule, pPanel) + Dim oArgs = GetReplaceMapForModule(oModule, pPanel, pUser) oResult = oModule.Replace(oResult, oArgs) Catch ex As Exception 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 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) + Select Case pModule.GetType() + Case GetType(PatternModule.Clipboard) + + + End Select + + If TypeOf pModule Is PatternModule.Clipboard Then oArgs.Add(PatternModule.Clipboard.CLIP_VALUE_BOARD, My.Computer.Clipboard.GetText()) ElseIf TypeOf pModule Is PatternModule.Internal Then - oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, Environment.UserName) - oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, Environment.MachineName) - oArgs.Add(PatternModule.Internal.INT_VALUE_DOMAIN, Environment.UserDomainName) + oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, System.Environment.UserName) + oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, System.Environment.MachineName) + oArgs.Add(PatternModule.Internal.INT_VALUE_DOMAIN, System.Environment.UserDomainName) 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 oArgs.Add(PatternModule.Controls.CTRL_VALUE_PANEL, pPanel) - End If Return oArgs End Function #Region "Helper Functions" - - - ''' ''' Wraps a pattern-type and -value in the common format: {#type#value} '''