1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"UtilProperty" script:
language=
"StarBasic">
4 REM =======================================================================================================================
5 REM === The Access2Base library is a part of the LibreOffice project. ===
6 REM === Full documentation is available on http://www.access2base.com ===
7 REM =======================================================================================================================
9 '**********************************************************************
10 ' UtilProperty module
12 ' Module of utilities to manipulate arrays of PropertyValue
's.
13 '**********************************************************************
15 '**********************************************************************
16 ' Copyright (c)
2003-
2004 Danny Brewer
17 ' d29583@groovegarden.com
18 '**********************************************************************
20 '**********************************************************************
21 ' If you make changes, please append to the change log below.
24 ' Danny Brewer Revised
2004-
02-
25-
01
25 ' Jean-Pierre Ledure Adapted to Access2Base coding conventions
26 ' PropValuesToStr rewritten and addition of StrToPropValues
27 ' Bug corrected on date values
28 ' Addition of support of
2-dimensional arrays
29 ' Support of empty arrays to allow JSON conversions
30 '**********************************************************************
34 Private Const cstHEADER =
"### PROPERTYVALUES ###
"
35 Private Const cstEMPTYARRAY =
"### EMPTY ARRAY ###
"
37 REM =======================================================================================================================
38 Public Function _MakePropertyValue(ByVal Optional psName As String, Optional pvValue As Variant) As com.sun.star.beans.PropertyValue
39 ' Create and return a new com.sun.star.beans.PropertyValue.
41 Dim oPropertyValue As New com.sun.star.beans.PropertyValue
43 If Not IsMissing(psName) Then oPropertyValue.Name = psName
44 If Not IsMissing(pvValue) Then oPropertyValue.Value = _CheckPropertyValue(pvValue)
45 _MakePropertyValue() = oPropertyValue
47 End Function
' _MakePropertyValue V1.3
.0
49 REM =======================================================================================================================
50 Public Function _CheckPropertyValue(ByRef pvValue As Variant) As Variant
51 ' Date BASIC variables give error. Change them to strings
52 ' Empty arrays should be replaced by cstEMPTYARRAY
54 If VarType(pvValue) = vbDate Then
55 _CheckPropertyValue = Utils._CStr(pvValue, False)
56 ElseIf IsArray(pvValue) Then
57 If UBound(pvValue,
1)
< LBound(pvValue,
1) Then _CheckPropertyValue = cstEMPTYARRAY Else _CheckPropertyValue = pvValue
59 _CheckPropertyValue = pvValue
62 End Function
' _CheckPropertyValue
64 REM =======================================================================================================================
65 Public Function _NumPropertyValues(ByRef pvPropertyValuesArray As Variant) As Integer
66 ' Return the number of PropertyValue
's in an array.
68 ' pvPropertyValuesArray - an array of PropertyValue
's, that is an array of com.sun.star.beans.PropertyValue.
69 ' Returns zero if the array contains no elements.
71 Dim iNumProperties As Integer
72 If Not IsArray(pvPropertyValuesArray) Then iNumProperties =
0 Else iNumProperties = UBound(pvPropertyValuesArray) +
1
73 _NumPropertyValues() = iNumProperties
75 End Function
' _NumPropertyValues V1.3
.0
77 REM =======================================================================================================================
78 Public Function _FindPropertyIndex(ByRef pvPropertyValuesArray As Variant, ByVal psPropName As String ) As Integer
79 ' Find a particular named property from an array of PropertyValue
's.
80 ' Finds the index in the array of PropertyValue
's and returns it, or returns -
1 if it was not found.
82 Dim iNumProperties As Integer, i As Integer, vProp As Variant
83 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
84 For i =
0 To iNumProperties -
1
85 vProp = pvPropertyValuesArray(i)
86 If UCase(vProp.Name) = UCase(psPropName) Then
87 _FindPropertyIndex() = i
91 _FindPropertyIndex() = -
1
93 End Function
' _FindPropertyIndex V1.3
.0
95 REM =======================================================================================================================
96 Public Function _FindProperty(ByRef pvPropertyValuesArray As Variant, ByVal psPropName As String) As com.sun.star.beans.PropertyValue
97 ' Find a particular named property from an array of PropertyValue
's.
98 ' Finds the PropertyValue and returns it, or returns Null if not found.
100 Dim iPropIndex As Integer, vProp As Variant
101 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
102 If iPropIndex
>=
0 Then
103 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
104 _FindProperty() = vProp
107 End Function
' _FindProperty V1.3
.0
109 REM =======================================================================================================================
110 Public Function _GetPropertyValue(ByRef pvPropertyValuesArray As Variant, ByVal psPropName As String, Optional pvDefaultValue) As Variant
111 ' Get the value of a particular named property from an array of PropertyValue
's.
112 ' vDefaultValue - This value is returned if the property is not found in the array.
114 Dim iPropIndex As Integer, vProp As Variant, vValue As Variant, vMatrix As Variant, i As Integer, j As Integer
115 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
116 If iPropIndex
>=
0 Then
117 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
118 vValue = vProp.Value
' get the value from the PropertyValue
119 If VarType(vValue) = vbString Then
120 If vValue = cstEMPTYARRAY Then _GetPropertyValue() = Array() Else _GetPropertyValue() = vValue
121 ElseIf IsArray(vValue) Then
122 If IsArray(vValue(
0)) Then
' Array of arrays
124 ReDim vMatrix(
0 To UBound(vValue),
0 To UBound(vValue(
0)))
125 For i =
0 To UBound(vValue)
126 For j =
0 To UBound(vValue(
0))
127 vMatrix(i, j) = vValue(i)(j)
130 _GetPropertyValue() = vMatrix
132 _GetPropertyValue() = vValue
' Simple vector OK
135 _GetPropertyValue() = vValue
138 If IsMissing(pvDefaultValue) Then pvDefaultValue = Null
139 _GetPropertyValue() = pvDefaultValue
142 End Function
' _GetPropertyValue V1.3
.0
144 REM =======================================================================================================================
145 Public Sub _SetPropertyValue(ByRef pvPropertyValuesArray As Variant, ByVal psPropName As String, ByVal pvValue As Variant)
146 ' Set the value of a particular named property from an array of PropertyValue
's.
148 Dim iPropIndex As Integer, vProp As Variant, iNumProperties As Integer
150 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
151 If iPropIndex
>=
0 Then
152 ' Found, the PropertyValue is already in the array. Just modify its value.
153 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
154 vProp.Value = _CheckPropertyValue(pvValue)
' set the property value.
155 pvPropertyValuesArray(iPropIndex) = vProp
' put it back into array
157 ' Not found, the array contains no PropertyValue with this name. Append new element to array.
158 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
159 If iNumProperties =
0 Then
160 pvPropertyValuesArray = Array(_MakePropertyValue(psPropName, pvValue))
162 ' Make array larger.
163 Redim Preserve pvPropertyValuesArray(iNumProperties)
164 ' Assign new PropertyValue
165 pvPropertyValuesArray(iNumProperties) = _MakePropertyValue(psPropName, pvValue)
169 End Sub
' _SetPropertyValue V1.3
.0
171 REM =======================================================================================================================
172 Public Sub _DeleteProperty(ByRef pvPropertyValuesArray As Variant, ByVal psPropName As String)
173 ' Delete a particular named property from an array of PropertyValue
's.
175 Dim iPropIndex As Integer
176 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
177 If iPropIndex
>=
0 Then _DeleteIndexedProperty(pvPropertyValuesArray, iPropIndex)
179 End Sub
' _DeletePropertyValue V1.3
.0
181 REM =======================================================================================================================
182 Public Sub _DeleteIndexedProperty(ByRef pvPropertyValuesArray As Variant, ByVal piPropIndex As Integer)
183 ' Delete a particular indexed property from an array of PropertyValue
's.
185 Dim iNumProperties As Integer, i As Integer
186 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
188 ' Did we find it?
189 If piPropIndex
< 0 Then
191 ElseIf iNumProperties =
1 Then
192 ' Just return a new empty array
193 pvPropertyValuesArray = Array()
195 ' If it is NOT the last item in the array, then shift other elements down into it
's position.
196 If piPropIndex
< iNumProperties -
1 Then
197 ' Bump items down lower in the array.
198 For i = piPropIndex To iNumProperties -
2
199 pvPropertyValuesArray(i) = pvPropertyValuesArray(i +
1)
202 ' Redimension the array to have one fewer element.
203 Redim Preserve pvPropertyValuesArray(iNumProperties -
2)
206 End Sub
' _DeleteIndexedProperty V1.3
.0
208 REM =======================================================================================================================
209 Public Function _PropValuesToStr(ByRef pvPropertyValuesArray As Variant) As String
210 ' Return a string with dumped content of the array of PropertyValue
's.
212 ' NameOfProperty = This is a string (or
12 or
2016-
12-
31 12:
05 or
123.45 or -
0.12E-05 ...)
213 ' NameOfArray = (
10)
214 ' 1;
2;
3;
4;
5;
6;
7;
8;
9;
10
215 ' NameOfMatrix = (
2,
10)
216 ' 1;
2;
3;
4;
5;
6;
7;
8;
9;
10
217 ' A;B;C;D;E;F;G;H;I;J
218 ' Semicolons and backslashes are escaped with a backslash (see _CStr and _CVar functions)
220 Dim iNumProperties As Integer, sResult As String, i As Integer, j As Integer, vProp As Variant
221 Dim sName As String, vValue As Variant, iType As Integer
225 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
227 sResult = cstHEADER
& cstLF
228 For i =
0 To iNumProperties -
1
229 vProp = pvPropertyValuesArray(i)
232 iType = VarType(vValue)
234 Case
< vbArray
' Scalar
235 sResult = sResult
& sName
& " =
" & Utils._CStr(vValue, False)
& cstLF
236 Case Else
' Vector or matrix
237 If uBound(vValue,
1)
< 0 Then
238 sResult = sResult
& sName
& " = (
0)
" & cstLF
239 ' 1-dimension but vector of vectors must also be considered
240 ElseIf VarType(vValue(
0))
>= vbArray Then
241 sResult = sResult
& sName
& " = (
" & UBound(vValue) +
1 & ",
" & UBound(vValue(
0)) +
1 & ")
" & cstLF
242 For j =
0 To UBound(vValue)
243 sResult = sResult
& Utils._CStr(vValue(j), False)
& cstLF
246 sResult = sResult
& sName
& " = (
" & UBound(vValue,
1) +
1 & ")
" & cstLF
247 sResult = sResult
& Utils._CStr(vValue, False)
& cstLF
252 _PropValuesToStr() = Left(sResult, Len(sResult) -
1)
' Remove last LF
254 End Function
' _PropValuesToStr V1.3
.0
256 REM =======================================================================================================================
257 Public Function _StrToPropValues(psString) As Variant
258 ' Return an array of PropertyValue
's rebuilt from the string parameter
260 Dim vString() As Variant, i As Integer,iArray As Integer, iRows As Integer, iCols As Integer
261 Dim lPosition As Long, sName As String, vValue As Variant, vResult As Variant, sDim As String
264 Const cstEqualArray =
" = (
", cstEqual =
" =
"
267 _StrToPropValues = Array()
270 If psString =
"" Then Exit Function
271 vString = Split(psString, cstLF)
272 If UBound(vString)
<=
0 Then Exit Function
' There must be at least one name-value pair
273 If vString(
0)
<> cstHEADER Then Exit Function
' Check origin
276 For i =
1 To UBound(vString)
277 If vString(i)
<> "" Then
' Skip empty lines
278 If iArray
< 0 Then
' Not busy with array row
280 sName = Utils._RegexSearch(vString(i),
"^\b\w+\b
", lPosition)
' Identifier
281 If sName =
"" Then Exit Function
282 If InStr(vString(i), cstEqualArray) = lPosition + Len(sName) Then
' Start array processing
283 lSearch = lPosition + Len(sName) + Len(cstEqualArray) -
1
284 sDim = Utils._RegexSearch(vString(i),
"\([
0-
9]+\)
", lSearch)
' e.g. (
10)
285 If sDim =
"(
0)
" Then
' Empty array
288 _SetPropertyValue(vResult, sName, vValue)
289 ElseIf sDim
<> "" Then
' Vector with content
290 iCols = CInt(Mid(sDim,
2, Len(sDim) -
2))
292 ReDim vValue(
0 To iCols -
1)
294 Else
' Matrix with content
295 lSearch = lPosition + Len(sName) + Len(cstEqualArray) -
1
296 sDim = Utils._RegexSearch(vString(i),
"\([
0-
9]+,
", lSearch)
' e.g. (
10,
297 iRows = CInt(Mid(sDim,
2, Len(sDim) -
2))
298 sDim = Utils._RegexSearch(vString(i),
",[
0-
9]+\)
", lSearch)
' e.g. ,
20)
299 iCols = CInt(Mid(sDim,
2, Len(sDim) -
2))
300 ReDim vValue(
0 To iRows -
1)
303 ElseIf InStr(vString(i), cstEqual) = lPosition + Len(sName) Then
304 vValue = Utils._CVar(Mid(vString(i), Len(sName) + Len(cstEqual) +
1))
305 _SetPropertyValue(vResult, sName, vValue)
309 Else
' Line is an array row
311 vValue = Utils._CVar(vString(i), True)
' Keep dates as strings
313 _SetPropertyValue(vResult, sName, vValue)
315 vValue(iArray) = Utils._CVar(vString(i), True)
316 If iArray
< iRows -
1 Then
320 _SetPropertyValue(vResult, sName, vValue)
327 _StrToPropValues = vResult