Patterns: WIP Patterns version 2
This commit is contained in:
parent
6b8d376656
commit
1c3e0b175b
101
Modules.Patterns/BaseFunctions.vb
Normal file
101
Modules.Patterns/BaseFunctions.vb
Normal file
@ -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
|
||||
3
Modules.Patterns/Constants.vb
Normal file
3
Modules.Patterns/Constants.vb
Normal file
@ -0,0 +1,3 @@
|
||||
Public Class Constants
|
||||
|
||||
End Class
|
||||
13
Modules.Patterns/Modules/BaseModule.vb
Normal file
13
Modules.Patterns/Modules/BaseModule.vb
Normal file
@ -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
|
||||
51
Modules.Patterns/Modules/Clipboard.vb
Normal file
51
Modules.Patterns/Modules/Clipboard.vb
Normal file
@ -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
|
||||
61
Modules.Patterns/Modules/Controls.vb
Normal file
61
Modules.Patterns/Modules/Controls.vb
Normal file
@ -0,0 +1,61 @@
|
||||
Imports System.Windows.Forms
|
||||
Imports DigitalData.Controls.LookupGrid
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace [PatternModule]
|
||||
''' <summary>
|
||||
''' Simple patterns that only rely on .NET functions
|
||||
''' </summary>
|
||||
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
|
||||
19
Modules.Patterns/Modules/IModule.vb
Normal file
19
Modules.Patterns/Modules/IModule.vb
Normal file
@ -0,0 +1,19 @@
|
||||
Public Interface IModule
|
||||
''' <summary>
|
||||
''' The short identifier which identifies all placeholders of this module
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Property PatternIdentifier As String
|
||||
|
||||
''' <summary>
|
||||
''' Does this Module have outside dependencies like a database or a library like windream
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Property IsComplex As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' Main Replace Function
|
||||
''' </summary>
|
||||
''' <returns>The replaced string</returns>
|
||||
Function Replace(pInput As String, pReplaceMap As Dictionary(Of String, Object)) As String
|
||||
End Interface
|
||||
57
Modules.Patterns/Modules/Internal.vb
Normal file
57
Modules.Patterns/Modules/Internal.vb
Normal file
@ -0,0 +1,57 @@
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace [PatternModule]
|
||||
''' <summary>
|
||||
''' Simple patterns that only rely on .NET functions
|
||||
''' </summary>
|
||||
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
|
||||
13
Modules.Patterns/Pattern.vb
Normal file
13
Modules.Patterns/Pattern.vb
Normal file
@ -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
|
||||
@ -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
|
||||
@ -43,6 +43,13 @@
|
||||
<OptionInfer>On</OptionInfer>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="DevExpress.Utils.v19.2, Version=19.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
|
||||
<Reference Include="DevExpress.XtraEditors.v19.2, Version=19.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
|
||||
<Reference Include="DevExpress.XtraGrid.v19.2, Version=19.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
|
||||
<Reference Include="Interop.WINDREAMLib">
|
||||
<HintPath>P:\Visual Studio Projekte\Bibliotheken\windream\Interop.WINDREAMLib.dll</HintPath>
|
||||
<EmbedInteropTypes>True</EmbedInteropTypes>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.7.10\lib\net45\NLog.dll</HintPath>
|
||||
@ -73,6 +80,14 @@
|
||||
<Import Include="System.Threading.Tasks" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BaseFunctions.vb" />
|
||||
<Compile Include="Constants.vb" />
|
||||
<Compile Include="Modules\IModule.vb" />
|
||||
<Compile Include="Modules\Clipboard.vb" />
|
||||
<Compile Include="Modules\Controls.vb" />
|
||||
<Compile Include="Modules\Internal.vb" />
|
||||
<Compile Include="Modules\BaseModule.vb" />
|
||||
<Compile Include="Pattern.vb" />
|
||||
<Compile Include="Patterns.vb" />
|
||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||
<Compile Include="My Project\Application.Designer.vb">
|
||||
@ -89,6 +104,7 @@
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<Compile Include="Patterns2.vb" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="My Project\Resources.resx">
|
||||
@ -112,6 +128,10 @@
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Controls.LookupGrid\LookupControl.vbproj">
|
||||
<Project>{3dcd6d1a-c830-4241-b7e4-27430e7ea483}</Project>
|
||||
<Name>LookupControl</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Modules.Logging\Logging.vbproj">
|
||||
<Project>{903B2D7D-3B80-4BE9-8713-7447B704E1B0}</Project>
|
||||
<Name>Logging</Name>
|
||||
@ -121,5 +141,6 @@
|
||||
<Name>ZooFlow</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
|
||||
</Project>
|
||||
134
Modules.Patterns/Patterns2.vb
Normal file
134
Modules.Patterns/Patterns2.vb
Normal file
@ -0,0 +1,134 @@
|
||||
Imports System.Text.RegularExpressions
|
||||
Imports System.Windows.Forms
|
||||
Imports DigitalData.Controls.LookupGrid
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports WINDREAMLib
|
||||
|
||||
''' <summary>
|
||||
''' 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}
|
||||
''' </summary>
|
||||
Public Class clsPatterns
|
||||
''' <summary>
|
||||
''' Complex patterns that rely on Windream
|
||||
''' </summary>
|
||||
Public Const PATTERN_WMI = "WMI"
|
||||
|
||||
''' <summary>
|
||||
''' Complex patterns that rely on IDB Attribute values
|
||||
''' </summary>
|
||||
Public Const PATTERN_IDBA = "IDBA"
|
||||
|
||||
''' <summary>
|
||||
''' Complex patterns that rely on Control Values
|
||||
''' </summary>
|
||||
Public Const PATTERN_CTRL = "CTRL"
|
||||
|
||||
''' <summary>
|
||||
''' Simple patterns that rely on Data from the TBDD_USER table
|
||||
''' </summary>
|
||||
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"
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' Wraps a pattern-type and -value in the common format: {#type#value}
|
||||
''' </summary>
|
||||
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
|
||||
Loading…
x
Reference in New Issue
Block a user