WIP: cleanup, work on doc result form
This commit is contained in:
138
Modules.Jobs/EDMI/ZUGFeRD/PropertyValues.vb
Normal file
138
Modules.Jobs/EDMI/ZUGFeRD/PropertyValues.vb
Normal file
@@ -0,0 +1,138 @@
|
||||
Imports System.Collections.Generic
|
||||
Imports System.Linq
|
||||
Imports System.Reflection
|
||||
Imports System.Text.RegularExpressions
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Public Class PropertyValues
|
||||
Private _indexPattern = "\((\d+)\)"
|
||||
Private _indexRegex As New Regex(_indexPattern)
|
||||
Private _Logger As Logger
|
||||
|
||||
Public Sub New(LogConfig As LogConfig)
|
||||
_Logger = LogConfig.GetLogger()
|
||||
End Sub
|
||||
|
||||
Public Function GetPropValue(Obj As Object, PropertyName As String) As List(Of Object)
|
||||
Dim oNameParts As String() = PropertyName.Split("."c)
|
||||
|
||||
If IsNothing(Obj) Then
|
||||
_Logger.Debug("`Obj` is Nothing. Exiting.")
|
||||
Return New List(Of Object)
|
||||
End If
|
||||
|
||||
|
||||
If oNameParts.Length = 1 Then
|
||||
Dim oPropInfo As PropertyInfo = Obj.GetType().GetProperty(PropertyName)
|
||||
|
||||
If IsNothing(oPropInfo) Then
|
||||
_Logger.Debug("Property {0} does not exist.", PropertyName)
|
||||
Return New List(Of Object)
|
||||
Else
|
||||
Dim oPropValue = oPropInfo.GetValue(Obj, Nothing)
|
||||
Return New List(Of Object) From {oPropValue}
|
||||
End If
|
||||
End If
|
||||
|
||||
For Each oPart As String In oNameParts
|
||||
Dim oType As Type = Obj.GetType()
|
||||
Dim oPartName = oPart
|
||||
Dim oIndex As Integer = Nothing
|
||||
Dim oHasIndex As Boolean = HasIndex(oPartName)
|
||||
|
||||
If oHasIndex Then
|
||||
oPartName = StripIndex(oPart)
|
||||
oIndex = GetIndex(oPart)
|
||||
End If
|
||||
|
||||
Dim oInfo As PropertyInfo = oType.GetProperty(oPartName)
|
||||
|
||||
If IsNothing(oInfo) OrElse IsNothing(oInfo.GetValue(Obj, Nothing)) Then
|
||||
_Logger.Debug("Property {0} does not exist.", oPartName)
|
||||
Return New List(Of Object)
|
||||
End If
|
||||
|
||||
Obj = oInfo.GetValue(Obj, Nothing)
|
||||
|
||||
If oHasIndex Then
|
||||
Obj = Obj(0)
|
||||
End If
|
||||
|
||||
If IsArray(Obj) And Not oHasIndex Then
|
||||
Dim oCurrentPart As String = oPart
|
||||
Dim oSplitString As String() = New String() {oCurrentPart & "."}
|
||||
Dim oPathFragments = PropertyName.Split(oSplitString, StringSplitOptions.None)
|
||||
Dim oResults As New List(Of Object)
|
||||
|
||||
' if path has no more subitems, return an empty list
|
||||
If oPathFragments.Length = 1 Then
|
||||
Return oResults
|
||||
End If
|
||||
|
||||
For Each oArrayItem In Obj
|
||||
Dim oResult As List(Of Object) = GetPropValue(oArrayItem, oPathFragments(1))
|
||||
|
||||
If Not IsNothing(oResult) Then
|
||||
oResults.Add(oResult)
|
||||
End If
|
||||
Next
|
||||
|
||||
Return oResults
|
||||
End If
|
||||
Next
|
||||
|
||||
Return New List(Of Object) From {Obj}
|
||||
End Function
|
||||
|
||||
Public Function GetFinalPropValue(List As List(Of Object)) As List(Of Object)
|
||||
Dim oResult As New List(Of Object)
|
||||
|
||||
For Each Item In List
|
||||
Dim oItemValue = DoGetFinalPropValue(Item)
|
||||
|
||||
If Not IsNothing(oItemValue) Then
|
||||
oResult.Add(oItemValue)
|
||||
End If
|
||||
Next
|
||||
|
||||
Return oResult
|
||||
End Function
|
||||
|
||||
Private Function DoGetFinalPropValue(Value As Object) As String
|
||||
If TypeOf Value Is List(Of Object) Then
|
||||
Dim oList = DirectCast(Value, List(Of Object))
|
||||
Dim oCount = oList.Count
|
||||
|
||||
Select Case oCount
|
||||
Case 0
|
||||
Return Nothing
|
||||
Case Else
|
||||
Return DoGetFinalPropValue(oList.First())
|
||||
End Select
|
||||
|
||||
Return DoGetFinalPropValue(Value)
|
||||
Else
|
||||
Return Value.ToString
|
||||
End If
|
||||
End Function
|
||||
|
||||
Private Function GetIndex(Prop As String) As Integer
|
||||
If Regex.IsMatch(Prop, _indexPattern) Then
|
||||
Dim oMatch = _indexRegex.Match(Prop)
|
||||
Dim oGroup = oMatch.Groups.Item(1)
|
||||
Dim oValue = oGroup.Value
|
||||
|
||||
Return Integer.Parse(oValue)
|
||||
End If
|
||||
|
||||
Return Nothing
|
||||
End Function
|
||||
|
||||
Private Function StripIndex(Prop As String) As String
|
||||
Return Regex.Replace(Prop, _indexPattern, "")
|
||||
End Function
|
||||
|
||||
Private Function HasIndex(Prop As String) As Boolean
|
||||
Return Regex.IsMatch(Prop, _indexPattern)
|
||||
End Function
|
||||
End Class
|
||||
Reference in New Issue
Block a user