From 1c3e0b175b08c45f70c6f9ed57354680b84933b3 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 13 Oct 2021 10:36:10 +0200 Subject: [PATCH] Patterns: WIP Patterns version 2 --- Modules.Patterns/BaseFunctions.vb | 101 +++++++++++++++++ Modules.Patterns/Constants.vb | 3 + Modules.Patterns/Modules/BaseModule.vb | 13 +++ Modules.Patterns/Modules/Clipboard.vb | 51 +++++++++ Modules.Patterns/Modules/Controls.vb | 61 ++++++++++ Modules.Patterns/Modules/IModule.vb | 19 ++++ Modules.Patterns/Modules/Internal.vb | 57 ++++++++++ Modules.Patterns/Pattern.vb | 13 +++ Modules.Patterns/Patterns.vb | 150 +++++++++++-------------- Modules.Patterns/Patterns.vbproj | 21 ++++ Modules.Patterns/Patterns2.vb | 134 ++++++++++++++++++++++ 11 files changed, 539 insertions(+), 84 deletions(-) create mode 100644 Modules.Patterns/BaseFunctions.vb create mode 100644 Modules.Patterns/Constants.vb create mode 100644 Modules.Patterns/Modules/BaseModule.vb create mode 100644 Modules.Patterns/Modules/Clipboard.vb create mode 100644 Modules.Patterns/Modules/Controls.vb create mode 100644 Modules.Patterns/Modules/IModule.vb create mode 100644 Modules.Patterns/Modules/Internal.vb create mode 100644 Modules.Patterns/Pattern.vb create mode 100644 Modules.Patterns/Patterns2.vb diff --git a/Modules.Patterns/BaseFunctions.vb b/Modules.Patterns/BaseFunctions.vb new file mode 100644 index 00000000..2a0035fa --- /dev/null +++ b/Modules.Patterns/BaseFunctions.vb @@ -0,0 +1,101 @@ +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/Constants.vb b/Modules.Patterns/Constants.vb new file mode 100644 index 00000000..2dfcf510 --- /dev/null +++ b/Modules.Patterns/Constants.vb @@ -0,0 +1,3 @@ +Public Class Constants + +End Class diff --git a/Modules.Patterns/Modules/BaseModule.vb b/Modules.Patterns/Modules/BaseModule.vb new file mode 100644 index 00000000..37aa00f8 --- /dev/null +++ b/Modules.Patterns/Modules/BaseModule.vb @@ -0,0 +1,13 @@ +Imports DigitalData.Modules.Logging + +Namespace [PatternModule] + Public Class BaseModule + Friend ReadOnly BaseFunctions As BaseFunctions + Friend ReadOnly Logger As Logger + + Public Sub New(pLogConfig As LogConfig) + Logger = pLogConfig.GetLogger() + BaseFunctions = New BaseFunctions(pLogConfig) + End Sub + End Class +End Namespace diff --git a/Modules.Patterns/Modules/Clipboard.vb b/Modules.Patterns/Modules/Clipboard.vb new file mode 100644 index 00000000..a6ad465e --- /dev/null +++ b/Modules.Patterns/Modules/Clipboard.vb @@ -0,0 +1,51 @@ +Imports DigitalData.Modules.Logging + +Namespace [PatternModule] + Public Class Clipboard + Inherits BaseModule + Implements IModule + + Public Const CLIP_VALUE_BOARD As String = "BOARD" + + Public Const CLIPBOARD_VALUE_DE = "@Zwischenablage" + Public Const CLIPBOARD_VALUE_EN = "@Clipboard" + + Public Property PatternIdentifier As String = "CLIP" Implements IModule.PatternIdentifier + + Public Property IsComplex As Boolean = False 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 + Dim oClipboardContents = pReplaceMap.Item(CLIP_VALUE_BOARD) + + Try + ' LEGACY: Replace Clipboard Contents + oResult = oResult.Replace(CLIPBOARD_VALUE_DE.ToLower, oClipboardContents) + oResult = oResult.Replace(CLIPBOARD_VALUE_DE.ToUpper, oClipboardContents) + oResult = oResult.Replace(CLIPBOARD_VALUE_DE, oClipboardContents) + + oResult = oResult.Replace(CLIPBOARD_VALUE_EN.ToLower, oClipboardContents) + oResult = oResult.Replace(CLIPBOARD_VALUE_EN.ToUpper, oClipboardContents) + 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) + End While + + Logger.Debug("Input after Clipboard.Replace: [{0}]", pInput) + Catch ex As Exception + Logger.Error(ex) + + End Try + + Return oResult + End Function + End Class +End Namespace diff --git a/Modules.Patterns/Modules/Controls.vb b/Modules.Patterns/Modules/Controls.vb new file mode 100644 index 00000000..69dd218d --- /dev/null +++ b/Modules.Patterns/Modules/Controls.vb @@ -0,0 +1,61 @@ +Imports System.Windows.Forms +Imports DigitalData.Controls.LookupGrid +Imports DigitalData.Modules.Logging + +Namespace [PatternModule] + ''' + ''' Simple patterns that only rely on .NET functions + ''' + Public Class Controls + Inherits BaseModule + Implements IModule + + Public Const CTRL_VALUE_PANEL = "PANEL" + + Public Property PatternIdentifier As String = "CTRL" 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 + Dim oPanel As Panel = pReplaceMap.Item("CTRL_VALUE_PANEL") + + While BaseFunctions.ContainsPattern(oResult, PatternIdentifier) + Dim oControlName As String = BaseFunctions.GetNextPattern(oResult, PatternIdentifier).Value + Dim oControl As Control = oPanel.Controls.Find(oControlName, False).FirstOrDefault() + + If oControl IsNot Nothing Then + Dim oReplaceValue As String + Select Case oControl.GetType.ToString + Case GetType(TextBox).ToString + oReplaceValue = oControl.Text + Case GetType(LookupControl3).ToString + Dim oLookupControl3 As LookupControl3 = oControl + If oLookupControl3.Properties.SelectedValues.Count = 1 Then + oReplaceValue = oLookupControl3.Properties.SelectedValues.Item(0) + Else + oReplaceValue = "0" + End If + Case GetType(ComboBox).ToString + oReplaceValue = oControl.Text + Case GetType(CheckBox).ToString + Dim oCheckBox As CheckBox = oControl + oReplaceValue = oCheckBox.Checked + Case Else + oReplaceValue = "0" + End Select + + oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, oReplaceValue) + End If + + BaseFunctions.IncrementCounterOrThrow(oCounter) + End While + + Return oResult + End Function + End Class +End Namespace diff --git a/Modules.Patterns/Modules/IModule.vb b/Modules.Patterns/Modules/IModule.vb new file mode 100644 index 00000000..91bcf20e --- /dev/null +++ b/Modules.Patterns/Modules/IModule.vb @@ -0,0 +1,19 @@ +Public Interface IModule + ''' + ''' The short identifier which identifies all placeholders of this module + ''' + ''' + Property PatternIdentifier As String + + ''' + ''' Does this Module have outside dependencies like a database or a library like windream + ''' + ''' + Property IsComplex As Boolean + + ''' + ''' Main Replace Function + ''' + ''' The replaced string + Function Replace(pInput As String, pReplaceMap As Dictionary(Of String, Object)) As String +End Interface diff --git a/Modules.Patterns/Modules/Internal.vb b/Modules.Patterns/Modules/Internal.vb new file mode 100644 index 00000000..a1ad4df2 --- /dev/null +++ b/Modules.Patterns/Modules/Internal.vb @@ -0,0 +1,57 @@ +Imports DigitalData.Modules.Logging + +Namespace [PatternModule] + ''' + ''' Simple patterns that only rely on .NET functions + ''' + Public Class Internal + Inherits BaseModule + Implements IModule + + Public Const INT_VALUE_USERNAME = "USERNAME" + Public Const INT_VALUE_MACHINE = "MACHINE" + Public Const INT_VALUE_DOMAIN = "DOMAIN" + Public Const INT_VALUE_DATE = "DATE" + + Public Property PatternIdentifier As String = "INT" Implements IModule.PatternIdentifier + + Public Property IsComplex As Boolean = False Implements IModule.IsComplex + + Public Sub New(pLogConfig As LogConfig, pBaseFunctions As BaseFunctions) + 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 + + ' Replace Username(s) + While BaseFunctions.ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_USERNAME) + oResult = BaseFunctions.ReplacePattern(oResult, PatternIdentifier, pReplaceMap.Item(INT_VALUE_USERNAME)) + BaseFunctions.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) + 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) + 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) + End While + + Return oResult + End Function + End Class +End Namespace diff --git a/Modules.Patterns/Pattern.vb b/Modules.Patterns/Pattern.vb new file mode 100644 index 00000000..16c6bbfb --- /dev/null +++ b/Modules.Patterns/Pattern.vb @@ -0,0 +1,13 @@ +Public Class Pattern + Public ReadOnly Property Type As String + Public ReadOnly Property Value As String + + Public Sub New(pType As String, pValue As String) + Me.Type = pType + Me.Value = pValue + End Sub + + Public Overrides Function ToString() As String + Return $"{{#{Type}#{Value}}}" + End Function +End Class diff --git a/Modules.Patterns/Patterns.vb b/Modules.Patterns/Patterns.vb index ac3f7aca..fc23fe65 100644 --- a/Modules.Patterns/Patterns.vb +++ b/Modules.Patterns/Patterns.vb @@ -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} ''' ''' Wraps a pattern-type and -value in the common format: {#type#value} ''' - 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 \ No newline at end of file diff --git a/Modules.Patterns/Patterns.vbproj b/Modules.Patterns/Patterns.vbproj index ca03b414..aaab2540 100644 --- a/Modules.Patterns/Patterns.vbproj +++ b/Modules.Patterns/Patterns.vbproj @@ -43,6 +43,13 @@ On + + + + + P:\Visual Studio Projekte\Bibliotheken\windream\Interop.WINDREAMLib.dll + True + ..\packages\NLog.4.7.10\lib\net45\NLog.dll @@ -73,6 +80,14 @@ + + + + + + + + @@ -89,6 +104,7 @@ Settings.settings True + @@ -112,6 +128,10 @@ + + {3dcd6d1a-c830-4241-b7e4-27430e7ea483} + LookupControl + {903B2D7D-3B80-4BE9-8713-7447B704E1B0} Logging @@ -121,5 +141,6 @@ ZooFlow + \ No newline at end of file diff --git a/Modules.Patterns/Patterns2.vb b/Modules.Patterns/Patterns2.vb new file mode 100644 index 00000000..3a8aae7f --- /dev/null +++ b/Modules.Patterns/Patterns2.vb @@ -0,0 +1,134 @@ +Imports System.Text.RegularExpressions +Imports System.Windows.Forms +Imports DigitalData.Controls.LookupGrid +Imports DigitalData.Modules.Logging +Imports WINDREAMLib + +''' +''' Defines common Functions for Checking for and replacing placeholders. +''' This Class also includes a child class `Pattern` for passing around Patterns. +''' +''' The format of all placeholders is: +''' {#TYPE#VALUE} +''' +''' Some Examples: +''' {#INT#USERNAME} +''' {#CTRL#CMB_2} +''' {#WMI#String 39} +''' +Public Class clsPatterns + ''' + ''' Complex patterns that rely on Windream + ''' + Public Const PATTERN_WMI = "WMI" + + ''' + ''' Complex patterns that rely on IDB Attribute values + ''' + Public Const PATTERN_IDBA = "IDBA" + + ''' + ''' Complex patterns that rely on Control Values + ''' + Public Const PATTERN_CTRL = "CTRL" + + ''' + ''' Simple patterns that rely on Data from the TBDD_USER table + ''' + Public Const PATTERN_USER = "USER" + + 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 VALUE_PROFILE_ID = "PROFILE_ID" + Public Const VALUE_PROFILE_TITLE = "PROFILE_TITLE" + + Private ReadOnly LogConfig As LogConfig + Private ReadOnly Logger As Logger + + 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)) + End Sub + + Public Sub New(pLogConfig As LogConfig, pControlPanel As Panel, pIDBActive As Boolean) + MyClass.New(pLogConfig) + ControlPanel = pControlPanel + IDBActive = pIDBActive + End Sub + + Public Function ReplaceAllValues(pInput As String, pPanel As Panel) As String + Dim oResult = pInput + + For Each oModule In Modules + Try + Dim oArgs = GetReplaceMapForModule(oModule, pPanel) + 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) + Logger.Error(ex) + End Try + Next + + Return oResult + End Function + + Private Function GetReplaceMapForModule(pModule As IModule, pPanel As Panel) As Dictionary(Of String, Object) + Dim oArgs As New Dictionary(Of String, Object) + + 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_DATE, Now.ToShortDateString) + + 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} + ''' + Public Function WrapPatternValue(pType As String, pValue As String) As String + Return New Pattern(pType, pValue).ToString + End Function + + Public Function HasAnyPatterns(input) As Boolean + Return Modules.Any(Function(m) Base.HasPattern(input, m.PatternIdentifier)) + End Function + + Public Function HasComplexPatterns(input As String) As Boolean + Return Modules. + Where(Function(m) m.IsComplex = True). + Any(Function(m) Base.HasPattern(input, m.PatternIdentifier)) + End Function + + Public Function HasOnlySimplePatterns(input As String) As Boolean + Return Not HasComplexPatterns(input) + End Function +#End Region + +End Class \ No newline at end of file