Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <map>
23 :
24 : #include "xmlControlProperty.hxx"
25 :
26 : #include <rtl/strbuf.hxx>
27 : #include <sax/tools/converter.hxx>
28 : #include "xmlfilter.hxx"
29 : #include <xmloff/xmltoken.hxx>
30 : #include <xmloff/xmlnmspe.hxx>
31 : #include <xmloff/nmspmap.hxx>
32 : #include "xmlEnums.hxx"
33 : #include <tools/debug.hxx>
34 : #include <tools/datetime.hxx>
35 : #include <unotools/datetime.hxx>
36 : #include <com/sun/star/util/DateTime.hpp>
37 : #include <rtl/math.hxx>
38 :
39 : #define TYPE_DATE 1
40 : #define TYPE_TIME 2
41 : #define TYPE_DATETIME 3
42 :
43 : namespace rptxml
44 : {
45 : using namespace ::com::sun::star::uno;
46 : using namespace ::com::sun::star::beans;
47 : using namespace ::com::sun::star::xml::sax;
48 :
49 0 : OXMLControlProperty::OXMLControlProperty( ORptFilter& rImport
50 : ,sal_uInt16 nPrfx
51 : ,const OUString& _sLocalName
52 : ,const Reference< XAttributeList > & _xAttrList
53 : ,const Reference< XPropertySet >& _xControl
54 : ,OXMLControlProperty* _pContainer) :
55 : SvXMLImportContext( rImport, nPrfx, _sLocalName )
56 : ,m_xControl(_xControl)
57 : ,m_pContainer(_pContainer)
58 0 : ,m_bIsList(false)
59 : {
60 0 : m_aPropType = ::getVoidCppuType();
61 :
62 : OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
63 : OSL_ENSURE(m_xControl.is(),"Control is NULL!");
64 :
65 0 : const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
66 0 : const SvXMLTokenMap& rTokenMap = rImport.GetControlPropertyElemTokenMap();
67 :
68 0 : const sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
69 0 : for(sal_Int16 i = 0; i < nLength; ++i)
70 : {
71 0 : OUString sLocalName;
72 0 : const OUString sAttrName = _xAttrList->getNameByIndex( i );
73 0 : const sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
74 0 : const OUString sValue = _xAttrList->getValueByIndex( i );
75 :
76 0 : switch( rTokenMap.Get( nPrefix, sLocalName ) )
77 : {
78 : case XML_TOK_LIST_PROPERTY:
79 0 : m_bIsList = sValue == "true";
80 0 : break;
81 : case XML_TOK_VALUE_TYPE:
82 : {
83 : // needs to be translated into a ::com::sun::star::uno::Type
84 0 : static std::map< OUString, css::uno::Type > s_aTypeNameMap;
85 0 : if (s_aTypeNameMap.empty())
86 : {
87 0 : s_aTypeNameMap[GetXMLToken( XML_BOOLEAN)] = ::getBooleanCppuType();
88 : // Not a copy paste error, see comment xmloff/source/forms/propertyimport.cxx lines 244-248
89 0 : s_aTypeNameMap[GetXMLToken( XML_FLOAT)] = cppu::UnoType<double>::get();
90 0 : s_aTypeNameMap[GetXMLToken( XML_DOUBLE)] = cppu::UnoType<double>::get();
91 0 : s_aTypeNameMap[GetXMLToken( XML_STRING)] = cppu::UnoType<OUString>::get();
92 0 : s_aTypeNameMap[GetXMLToken( XML_INT)] = cppu::UnoType<sal_Int32>::get();
93 0 : s_aTypeNameMap[GetXMLToken( XML_SHORT)] = cppu::UnoType<sal_Int16>::get();
94 0 : s_aTypeNameMap[GetXMLToken( XML_DATE)] = cppu::UnoType<com::sun::star::util::Date>::get();
95 0 : s_aTypeNameMap[GetXMLToken( XML_TIME)] = cppu::UnoType<com::sun::star::util::Time>::get();
96 0 : s_aTypeNameMap[GetXMLToken( XML_VOID)] = ::getVoidCppuType();
97 : }
98 :
99 0 : const std::map< OUString, css::uno::Type >::const_iterator aTypePos = s_aTypeNameMap.find(sValue);
100 : OSL_ENSURE(s_aTypeNameMap.end() != aTypePos, "OXMLControlProperty::OXMLControlProperty: invalid type!");
101 0 : if (s_aTypeNameMap.end() != aTypePos)
102 0 : m_aPropType = aTypePos->second;
103 : }
104 0 : break;
105 : case XML_TOK_PROPERTY_NAME:
106 0 : m_aSetting.Name = sValue;
107 0 : break;
108 : default:
109 0 : break;
110 : }
111 0 : }
112 :
113 0 : }
114 :
115 :
116 0 : OXMLControlProperty::~OXMLControlProperty()
117 : {
118 0 : }
119 :
120 0 : SvXMLImportContext* OXMLControlProperty::CreateChildContext(
121 : sal_uInt16 nPrefix,
122 : const OUString& rLocalName,
123 : const Reference< XAttributeList > & xAttrList )
124 : {
125 0 : SvXMLImportContext *pContext = 0;
126 0 : ORptFilter& rImport = GetOwnImport();
127 0 : const SvXMLTokenMap& rTokenMap = rImport.GetControlPropertyElemTokenMap();
128 :
129 0 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
130 : {
131 : case XML_TOK_LIST_PROPERTY:
132 0 : rImport.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
133 0 : pContext = new OXMLControlProperty( rImport, nPrefix, rLocalName,xAttrList,m_xControl);
134 0 : break;
135 : case XML_TOK_VALUE:
136 0 : rImport.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
137 0 : pContext = new OXMLControlProperty( rImport, nPrefix, rLocalName,xAttrList,m_xControl,this );
138 0 : break;
139 : default:
140 0 : break;
141 : }
142 :
143 0 : if( !pContext )
144 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
145 :
146 0 : return pContext;
147 : }
148 :
149 0 : void OXMLControlProperty::EndElement()
150 : {
151 0 : if ( !m_aSetting.Name.isEmpty() && m_xControl.is() )
152 : {
153 0 : if ( m_bIsList && !m_aSequence.getLength() )
154 0 : m_aSetting.Value <<= m_aSequence;
155 : try
156 : {
157 0 : m_xControl->setPropertyValue(m_aSetting.Name,m_aSetting.Value);
158 : }
159 0 : catch(const Exception&)
160 : {
161 : OSL_FAIL("Unknown property found!");
162 : }
163 : }
164 0 : }
165 :
166 0 : void OXMLControlProperty::Characters( const OUString& rChars )
167 : {
168 0 : if ( m_pContainer )
169 0 : m_pContainer->addValue(rChars);
170 0 : }
171 :
172 0 : void OXMLControlProperty::addValue(const OUString& _sValue)
173 : {
174 0 : Any aValue;
175 0 : if( TypeClass_VOID != m_aPropType.getTypeClass() )
176 0 : aValue = convertString(m_aPropType, _sValue);
177 :
178 0 : if ( !m_bIsList )
179 0 : m_aSetting.Value = aValue;
180 : else
181 : {
182 0 : sal_Int32 nPos = m_aSequence.getLength();
183 0 : m_aSequence.realloc(nPos+1);
184 0 : m_aSequence[nPos] = aValue;
185 0 : }
186 0 : }
187 :
188 0 : ORptFilter& OXMLControlProperty::GetOwnImport()
189 : {
190 0 : return static_cast<ORptFilter&>(GetImport());
191 : }
192 :
193 0 : Any OXMLControlProperty::convertString(const ::com::sun::star::uno::Type& _rExpectedType, const OUString& _rReadCharacters)
194 : {
195 0 : Any aReturn;
196 0 : switch (_rExpectedType.getTypeClass())
197 : {
198 : case TypeClass_BOOLEAN: // sal_Bool
199 : {
200 0 : bool bValue(false);
201 : #if OSL_DEBUG_LEVEL > 0
202 : bool bSuccess =
203 : #endif
204 0 : ::sax::Converter::convertBool(bValue, _rReadCharacters);
205 : OSL_ENSURE(bSuccess,
206 : OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
207 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
208 : append("\" into a boolean!").getStr());
209 0 : aReturn <<= bValue;
210 : }
211 0 : break;
212 : case TypeClass_SHORT: // sal_Int16
213 : case TypeClass_LONG: // sal_Int32
214 : { // it's a real int32/16 property
215 0 : sal_Int32 nValue(0);
216 : #if OSL_DEBUG_LEVEL > 0
217 : bool bSuccess =
218 : #endif
219 0 : ::sax::Converter::convertNumber(nValue, _rReadCharacters);
220 : OSL_ENSURE(bSuccess,
221 : OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
222 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
223 : append("\" into an integer!").getStr());
224 0 : if (TypeClass_SHORT == _rExpectedType.getTypeClass())
225 0 : aReturn <<= (sal_Int16)nValue;
226 : else
227 0 : aReturn <<= (sal_Int32)nValue;
228 0 : break;
229 : }
230 : case TypeClass_HYPER:
231 : {
232 : OSL_FAIL("OXMLControlProperty::convertString: 64-bit integers not implemented yet!");
233 : }
234 0 : break;
235 : case TypeClass_DOUBLE:
236 : {
237 0 : double nValue = 0.0;
238 : #if OSL_DEBUG_LEVEL > 0
239 : bool bSuccess =
240 : #endif
241 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
242 : OSL_ENSURE(bSuccess,
243 : OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
244 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
245 : append("\" into a double!").getStr());
246 0 : aReturn <<= (double)nValue;
247 : }
248 0 : break;
249 : case TypeClass_STRING:
250 0 : aReturn <<= _rReadCharacters;
251 0 : break;
252 : case TypeClass_STRUCT:
253 : {
254 : // recognized structs:
255 0 : static ::com::sun::star::uno::Type s_aDateType = ::cppu::UnoType<com::sun::star::util::Date>::get();
256 0 : static ::com::sun::star::uno::Type s_aTimeType = ::cppu::UnoType<com::sun::star::util::Time>::get();
257 0 : static ::com::sun::star::uno::Type s_aDateTimeType = ::cppu::UnoType<com::sun::star::util::DateTime>::get();
258 0 : sal_Int32 nType = 0;
259 0 : if ( _rExpectedType.equals(s_aDateType) )
260 0 : nType = TYPE_DATE;
261 0 : else if ( _rExpectedType.equals(s_aTimeType) )
262 0 : nType = TYPE_TIME;
263 0 : else if ( _rExpectedType.equals(s_aDateTimeType) )
264 0 : nType = TYPE_DATETIME;
265 0 : if ( nType )
266 : {
267 : // first extract the double
268 0 : double nValue = 0;
269 : #if OSL_DEBUG_LEVEL > 0
270 : bool bSuccess =
271 : #endif
272 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
273 : OSL_ENSURE(bSuccess,
274 : OStringBuffer("OPropertyImport::convertString: could not convert \"").
275 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
276 : append("\" into a double!").getStr());
277 :
278 : // then convert it into the target type
279 0 : switch (nType)
280 : {
281 : case TYPE_DATE:
282 : {
283 : OSL_ENSURE(((sal_uInt32)nValue) - nValue == 0,
284 : "OPropertyImport::convertString: a Date value with a fractional part?");
285 0 : aReturn <<= implGetDate(nValue);
286 : }
287 0 : break;
288 : case TYPE_TIME:
289 : {
290 : OSL_ENSURE(((sal_uInt32)nValue) == 0,
291 : "OPropertyImport::convertString: a tools::Time value with more than a fractional part?");
292 0 : aReturn <<= implGetTime(nValue);
293 : }
294 0 : break;
295 : case TYPE_DATETIME:
296 : {
297 0 : ::com::sun::star::util::Time aTime = implGetTime(nValue);
298 0 : ::com::sun::star::util::Date aDate = implGetDate(nValue);
299 :
300 0 : ::com::sun::star::util::DateTime aDateTime;
301 0 : aDateTime.NanoSeconds = aTime.NanoSeconds;
302 0 : aDateTime.Seconds = aTime.Seconds;
303 0 : aDateTime.Minutes = aTime.Minutes;
304 0 : aDateTime.Hours = aTime.Hours;
305 0 : aDateTime.Day = aDate.Day;
306 0 : aDateTime.Month = aDate.Month;
307 0 : aDateTime.Year = aDate.Year;
308 0 : aReturn <<= aDateTime;
309 : }
310 0 : break;
311 : default:
312 0 : break;
313 : }
314 : }
315 : else
316 : OSL_FAIL("OPropertyImport::convertString: unsupported property type!");
317 : }
318 0 : break;
319 : default:
320 : OSL_FAIL("OXMLControlProperty::convertString: invalid type class!");
321 : }
322 :
323 0 : return aReturn;
324 : }
325 :
326 0 : ::com::sun::star::util::Time OXMLControlProperty::implGetTime(double _nValue)
327 : {
328 0 : ::com::sun::star::util::Time aTime;
329 0 : sal_uInt64 nIntValue = ::rtl::math::round(_nValue * 86400000000000.0);
330 0 : aTime.NanoSeconds = (sal_uInt16)( nIntValue % 1000000000 );
331 0 : nIntValue /= 1000000000;
332 0 : aTime.Seconds = (sal_uInt16)( nIntValue % 60 );
333 0 : nIntValue /= 60;
334 0 : aTime.Minutes = (sal_uInt16)( nIntValue % 60 );
335 0 : nIntValue /= 60;
336 : OSL_ENSURE(nIntValue < 24, "OPropertyImport::implGetTime: more than a day?");
337 0 : aTime.Hours = static_cast< sal_uInt16 >( nIntValue );
338 :
339 0 : return aTime;
340 : }
341 :
342 :
343 0 : ::com::sun::star::util::Date OXMLControlProperty::implGetDate(double _nValue)
344 : {
345 0 : Date aToolsDate((sal_uInt32)_nValue);
346 0 : ::com::sun::star::util::Date aDate;
347 0 : ::utl::typeConvert(aToolsDate, aDate);
348 0 : return aDate;
349 : }
350 :
351 : } // namespace rptxml
352 :
353 :
354 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|