1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
import uno
import locale
import traceback
from com.sun.star.uno import Exception as UnoException
from com.sun.star.uno import RuntimeException
class Helper(object):
DAY_IN_MILLIS = (24 * 60 * 60 * 1000)
def convertUnoDatetoInteger(self, DateValue):
oCal = java.util.Calendar.getInstance()
oCal.set(DateValue.Year, DateValue.Month, DateValue.Day)
dTime = oCal.getTime()
lTime = dTime.getTime()
lDate = lTime / (3600 * 24000)
return lDate
@classmethod
def setUnoPropertyValue(self, xPSet, PropertyName, PropertyValue):
try:
if xPSet.getPropertySetInfo().hasPropertyByName(PropertyName):
uno.invoke(xPSet,"setPropertyValue", (PropertyName,PropertyValue))
else:
selementnames = xPSet.getPropertySetInfo().getProperties()
raise ValueError("No Such Property: '" + PropertyName + "'");
except UnoException, exception:
print PropertyName
traceback.print_exc()
@classmethod
def getUnoObjectbyName(self, xName, ElementName):
try:
if xName.hasByName(ElementName) == True:
return xName.getByName(ElementName)
else:
raise RuntimeException();
except UnoException, exception:
traceback.print_exc()
return None
@classmethod
def getPropertyValue(self, CurPropertyValue, PropertyName):
MaxCount = len(CurPropertyValue)
i = 0
while i < MaxCount:
if CurPropertyValue[i] is not None:
if CurPropertyValue[i].Name.equals(PropertyName):
return CurPropertyValue[i].Value
i += 1
raise RuntimeException()
@classmethod
def getPropertyValuefromAny(self, CurPropertyValue, PropertyName):
try:
if CurPropertyValue is not None:
MaxCount = len(CurPropertyValue)
i = 0
while i < MaxCount:
if CurPropertyValue[i] is not None:
aValue = CurPropertyValue[i]
if aValue is not None and aValue.Name.equals(PropertyName):
return aValue.Value
i += 1
return None
except UnoException, exception:
traceback.print_exc()
return None
@classmethod
def getUnoPropertyValue(self, xPSet, PropertyName):
try:
if xPSet is not None:
oObject = xPSet.getPropertyValue(PropertyName)
if oObject is not None:
return oObject
return None
except UnoException, exception:
traceback.print_exc()
return None
@classmethod
def getUnoArrayPropertyValue(self, xPSet, PropertyName):
try:
if xPSet is not None:
oObject = xPSet.getPropertyValue(PropertyName)
if isinstance(oObject,list):
return getArrayValue(oObject)
except UnoException, exception:
traceback.print_exc()
return None
@classmethod
def getUnoStructValue(self, xPSet, PropertyName):
try:
if xPSet is not None:
if xPSet.getPropertySetInfo().hasPropertyByName(PropertyName) == True:
oObject = xPSet.getPropertyValue(PropertyName)
return oObject
return None
except UnoException, exception:
traceback.print_exc()
return None
@classmethod
def setUnoPropertyValues(self, xMultiPSetLst, PropertyNames, PropertyValues):
try:
if xMultiPSetLst is not None:
uno.invoke(xMultiPSetLst, "setPropertyValues", (PropertyNames, PropertyValues))
else:
i = 0
while i < len(PropertyNames):
self.setUnoPropertyValue(xMultiPSetLst, PropertyNames[i], PropertyValues[i])
i += 1
except Exception, e:
traceback.print_exc()
'''
checks if the value of an object that represents an array is null.
check beforehand if the Object is really an array with "AnyConverter.IsArray(oObject)
@param oValue the paramter that has to represent an object
@return a null reference if the array is empty
'''
@classmethod
def getArrayValue(self, oValue):
try:
#VetoableChangeSupport Object
oPropList = list(oValue)
nlen = len(oPropList)
if nlen == 0:
return None
else:
return oPropList
except UnoException, exception:
traceback.print_exc()
return None
def getComponentContext(_xMSF):
# Get the path to the extension and try to add the path to the class loader
aHelper = PropertySetHelper(_xMSF);
aDefaultContext = aHelper.getPropertyValueAsObject("DefaultContext");
return aDefaultContext;
def getMacroExpander(_xMSF):
xComponentContext = self.getComponentContext(_xMSF);
aSingleton = xComponentContext.getValueByName("/singletons/com.sun.star.util.theMacroExpander");
return aSingleton;
class DateUtils(object):
def __init__(self, xmsf, docMSF):
defaults = docMSF.createInstance("com.sun.star.text.Defaults")
l = Helper.getUnoStructValue(defaults, "CharLocale")
jl = locale.setlocale(l.Language, l.Country, l.Variant)
self.calendar = Calendar.getInstance(jl)
self.formatSupplier = document
formatSettings = self.formatSupplier.getNumberFormatSettings()
date = Helper.getUnoPropertyValue(formatSettings, "NullDate")
self.calendar.set(date.Year, date.Month - 1, date.Day)
self.docNullTime = getTimeInMillis()
self.formatter = NumberFormatter.createNumberFormatter(xmsf, self.formatSupplier)
'''
@param format a constant of the enumeration NumberFormatIndex
@return
'''
def getFormat(self, format):
return NumberFormatter.getNumberFormatterKey(self.formatSupplier, format)
def getFormatter(self):
return self.formatter
def getTimeInMillis(self):
dDate = self.calendar.getTime()
return dDate.getTime()
'''
@param date a VCL date in form of 20041231
@return a document relative date
'''
def getDocumentDateAsDouble(self, date):
self.calendar.clear()
self.calendar.set(date / 10000, (date % 10000) / 100 - 1, date % 100)
date1 = getTimeInMillis()
'''
docNullTime and date1 are in millis, but
I need a day...
'''
daysDiff = (date1 - self.docNullTime) / DAY_IN_MILLIS + 1
return daysDiff
def getDocumentDateAsDouble(self, date):
return getDocumentDateAsDouble(date.Year * 10000 + date.Month * 100 + date.Day)
def getDocumentDateAsDouble(self, javaTimeInMillis):
self.calendar.clear()
JavaTools.setTimeInMillis(self.calendar, javaTimeInMillis)
date1 = getTimeInMillis()
'''
docNullTime and date1 are in millis, but
I need a day...
'''
daysDiff = (date1 - self.docNullTime) / DAY_IN_MILLIS + 1
return daysDiff
def format(self, formatIndex, date):
return self.formatter.convertNumberToString(formatIndex, getDocumentDateAsDouble(date))
def format(self, formatIndex, date):
return self.formatter.convertNumberToString(formatIndex, getDocumentDateAsDouble(date))
def format(self, formatIndex, javaTimeInMillis):
return self.formatter.convertNumberToString(formatIndex, getDocumentDateAsDouble(javaTimeInMillis))
|