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 : DBG_NAME( rpt_OXMLControlProperty )
50 :
51 0 : OXMLControlProperty::OXMLControlProperty( ORptFilter& rImport
52 : ,sal_uInt16 nPrfx
53 : ,const OUString& _sLocalName
54 : ,const Reference< XAttributeList > & _xAttrList
55 : ,const Reference< XPropertySet >& _xControl
56 : ,OXMLControlProperty* _pContainer) :
57 : SvXMLImportContext( rImport, nPrfx, _sLocalName )
58 : ,m_xControl(_xControl)
59 : ,m_pContainer(_pContainer)
60 0 : ,m_bIsList(sal_False)
61 : {
62 : DBG_CTOR( rpt_OXMLControlProperty,NULL);
63 :
64 0 : m_aPropType = ::getVoidCppuType();
65 :
66 : OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
67 : OSL_ENSURE(m_xControl.is(),"Control is NULL!");
68 :
69 0 : const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
70 0 : const SvXMLTokenMap& rTokenMap = rImport.GetControlPropertyElemTokenMap();
71 :
72 0 : const sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
73 0 : for(sal_Int16 i = 0; i < nLength; ++i)
74 : {
75 0 : OUString sLocalName;
76 0 : const OUString sAttrName = _xAttrList->getNameByIndex( i );
77 0 : const sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
78 0 : const OUString sValue = _xAttrList->getValueByIndex( i );
79 :
80 0 : switch( rTokenMap.Get( nPrefix, sLocalName ) )
81 : {
82 : case XML_TOK_LIST_PROPERTY:
83 0 : m_bIsList = sValue == "true";
84 0 : break;
85 : case XML_TOK_VALUE_TYPE:
86 : {
87 : // needs to be translated into a ::com::sun::star::uno::Type
88 0 : static std::map< OUString, css::uno::Type > s_aTypeNameMap;
89 0 : if (s_aTypeNameMap.empty())
90 : {
91 0 : s_aTypeNameMap[GetXMLToken( XML_BOOLEAN)] = ::getBooleanCppuType();
92 0 : s_aTypeNameMap[GetXMLToken( XML_FLOAT)] = ::getCppuType( static_cast< double* >(NULL) );
93 0 : s_aTypeNameMap[GetXMLToken( XML_DOUBLE)] = ::getCppuType( static_cast< double* >(NULL) );
94 0 : s_aTypeNameMap[GetXMLToken( XML_STRING)] = ::getCppuType( static_cast< OUString* >(NULL) );
95 0 : s_aTypeNameMap[GetXMLToken( XML_INT)] = ::getCppuType( static_cast< sal_Int32* >(NULL) );
96 0 : s_aTypeNameMap[GetXMLToken( XML_SHORT)] = ::getCppuType( static_cast< sal_Int16* >(NULL) );
97 0 : s_aTypeNameMap[GetXMLToken( XML_DATE)] = ::getCppuType( static_cast< com::sun::star::util::Date* >(NULL) );
98 0 : s_aTypeNameMap[GetXMLToken( XML_TIME)] = ::getCppuType( static_cast< com::sun::star::util::Time* >(NULL) );
99 0 : s_aTypeNameMap[GetXMLToken( XML_VOID)] = ::getVoidCppuType();
100 : }
101 :
102 0 : const std::map< OUString, css::uno::Type >::const_iterator aTypePos = s_aTypeNameMap.find(sValue);
103 : OSL_ENSURE(s_aTypeNameMap.end() != aTypePos, "OXMLControlProperty::OXMLControlProperty: invalid type!");
104 0 : if (s_aTypeNameMap.end() != aTypePos)
105 0 : m_aPropType = aTypePos->second;
106 : }
107 0 : break;
108 : case XML_TOK_PROPERTY_NAME:
109 0 : m_aSetting.Name = sValue;
110 0 : break;
111 : default:
112 0 : break;
113 : }
114 0 : }
115 :
116 0 : }
117 : // -----------------------------------------------------------------------------
118 :
119 0 : OXMLControlProperty::~OXMLControlProperty()
120 : {
121 : DBG_DTOR( rpt_OXMLControlProperty,NULL);
122 0 : }
123 : // -----------------------------------------------------------------------------
124 0 : SvXMLImportContext* OXMLControlProperty::CreateChildContext(
125 : sal_uInt16 nPrefix,
126 : const OUString& rLocalName,
127 : const Reference< XAttributeList > & xAttrList )
128 : {
129 0 : SvXMLImportContext *pContext = 0;
130 0 : ORptFilter& rImport = GetOwnImport();
131 0 : const SvXMLTokenMap& rTokenMap = rImport.GetControlPropertyElemTokenMap();
132 :
133 0 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
134 : {
135 : case XML_TOK_LIST_PROPERTY:
136 0 : rImport.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
137 0 : pContext = new OXMLControlProperty( rImport, nPrefix, rLocalName,xAttrList,m_xControl);
138 0 : break;
139 : case XML_TOK_VALUE:
140 0 : rImport.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
141 0 : pContext = new OXMLControlProperty( rImport, nPrefix, rLocalName,xAttrList,m_xControl,this );
142 0 : break;
143 : default:
144 0 : break;
145 : }
146 :
147 0 : if( !pContext )
148 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
149 :
150 0 : return pContext;
151 : }
152 : // -----------------------------------------------------------------------------
153 0 : void OXMLControlProperty::EndElement()
154 : {
155 0 : if ( !m_aSetting.Name.isEmpty() && m_xControl.is() )
156 : {
157 0 : if ( m_bIsList && !m_aSequence.getLength() )
158 0 : m_aSetting.Value <<= m_aSequence;
159 : try
160 : {
161 0 : m_xControl->setPropertyValue(m_aSetting.Name,m_aSetting.Value);
162 : }
163 0 : catch(const Exception&)
164 : {
165 : OSL_FAIL("Unknown property found!");
166 : }
167 : }
168 0 : }
169 : // -----------------------------------------------------------------------------
170 0 : void OXMLControlProperty::Characters( const OUString& rChars )
171 : {
172 0 : if ( m_pContainer )
173 0 : m_pContainer->addValue(rChars);
174 0 : }
175 : // -----------------------------------------------------------------------------
176 0 : void OXMLControlProperty::addValue(const OUString& _sValue)
177 : {
178 0 : Any aValue;
179 0 : if( TypeClass_VOID != m_aPropType.getTypeClass() )
180 0 : aValue = convertString(m_aPropType, _sValue);
181 :
182 0 : if ( !m_bIsList )
183 0 : m_aSetting.Value = aValue;
184 : else
185 : {
186 0 : sal_Int32 nPos = m_aSequence.getLength();
187 0 : m_aSequence.realloc(nPos+1);
188 0 : m_aSequence[nPos] = aValue;
189 0 : }
190 0 : }
191 : // -----------------------------------------------------------------------------
192 0 : ORptFilter& OXMLControlProperty::GetOwnImport()
193 : {
194 0 : return static_cast<ORptFilter&>(GetImport());
195 : }
196 : // -----------------------------------------------------------------------------
197 0 : Any OXMLControlProperty::convertString(const ::com::sun::star::uno::Type& _rExpectedType, const OUString& _rReadCharacters)
198 : {
199 0 : Any aReturn;
200 0 : switch (_rExpectedType.getTypeClass())
201 : {
202 : case TypeClass_BOOLEAN: // sal_Bool
203 : {
204 0 : bool bValue(false);
205 : #if OSL_DEBUG_LEVEL > 0
206 : sal_Bool bSuccess =
207 : #endif
208 0 : ::sax::Converter::convertBool(bValue, _rReadCharacters);
209 : OSL_ENSURE(bSuccess,
210 : OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
211 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
212 : append("\" into a boolean!").getStr());
213 0 : aReturn <<= bValue;
214 : }
215 0 : break;
216 : case TypeClass_SHORT: // sal_Int16
217 : case TypeClass_LONG: // sal_Int32
218 : { // it's a real int32/16 property
219 0 : sal_Int32 nValue(0);
220 : #if OSL_DEBUG_LEVEL > 0
221 : sal_Bool bSuccess =
222 : #endif
223 0 : ::sax::Converter::convertNumber(nValue, _rReadCharacters);
224 : OSL_ENSURE(bSuccess,
225 : OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
226 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
227 : append("\" into an integer!").getStr());
228 0 : if (TypeClass_SHORT == _rExpectedType.getTypeClass())
229 0 : aReturn <<= (sal_Int16)nValue;
230 : else
231 0 : aReturn <<= (sal_Int32)nValue;
232 0 : break;
233 : }
234 : case TypeClass_HYPER:
235 : {
236 : OSL_FAIL("OXMLControlProperty::convertString: 64-bit integers not implemented yet!");
237 : }
238 0 : break;
239 : case TypeClass_DOUBLE:
240 : {
241 0 : double nValue = 0.0;
242 : #if OSL_DEBUG_LEVEL > 0
243 : sal_Bool bSuccess =
244 : #endif
245 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
246 : OSL_ENSURE(bSuccess,
247 : OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
248 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
249 : append("\" into a double!").getStr());
250 0 : aReturn <<= (double)nValue;
251 : }
252 0 : break;
253 : case TypeClass_STRING:
254 0 : aReturn <<= _rReadCharacters;
255 0 : break;
256 : case TypeClass_STRUCT:
257 : {
258 : // recognized structs:
259 0 : static ::com::sun::star::uno::Type s_aDateType = ::getCppuType(static_cast< ::com::sun::star::util::Date* >(NULL));
260 0 : static ::com::sun::star::uno::Type s_aTimeType = ::getCppuType(static_cast< ::com::sun::star::util::Time* >(NULL));
261 0 : static ::com::sun::star::uno::Type s_aDateTimeType = ::getCppuType(static_cast< ::com::sun::star::util::DateTime* >(NULL));
262 0 : sal_Int32 nType = 0;
263 0 : if ( _rExpectedType.equals(s_aDateType) )
264 0 : nType = TYPE_DATE;
265 0 : else if ( _rExpectedType.equals(s_aTimeType) )
266 0 : nType = TYPE_TIME;
267 0 : else if ( _rExpectedType.equals(s_aDateTimeType) )
268 0 : nType = TYPE_DATETIME;
269 0 : if ( nType )
270 : {
271 : // first extract the double
272 0 : double nValue = 0;
273 : #if OSL_DEBUG_LEVEL > 0
274 : sal_Bool bSuccess =
275 : #endif
276 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
277 : OSL_ENSURE(bSuccess,
278 : OStringBuffer("OPropertyImport::convertString: could not convert \"").
279 : append(OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US)).
280 : append("\" into a double!").getStr());
281 :
282 : // then convert it into the target type
283 0 : switch (nType)
284 : {
285 : case TYPE_DATE:
286 : {
287 : OSL_ENSURE(((sal_uInt32)nValue) - nValue == 0,
288 : "OPropertyImport::convertString: a Date value with a fractional part?");
289 0 : aReturn <<= implGetDate(nValue);
290 : }
291 0 : break;
292 : case TYPE_TIME:
293 : {
294 : OSL_ENSURE(((sal_uInt32)nValue) == 0,
295 : "OPropertyImport::convertString: a Time value with more than a fractional part?");
296 0 : aReturn <<= implGetTime(nValue);
297 : }
298 0 : break;
299 : case TYPE_DATETIME:
300 : {
301 0 : ::com::sun::star::util::Time aTime = implGetTime(nValue);
302 0 : ::com::sun::star::util::Date aDate = implGetDate(nValue);
303 :
304 0 : ::com::sun::star::util::DateTime aDateTime;
305 0 : aDateTime.NanoSeconds = aTime.NanoSeconds;
306 0 : aDateTime.Seconds = aTime.Seconds;
307 0 : aDateTime.Minutes = aTime.Minutes;
308 0 : aDateTime.Hours = aTime.Hours;
309 0 : aDateTime.Day = aDate.Day;
310 0 : aDateTime.Month = aDate.Month;
311 0 : aDateTime.Year = aDate.Year;
312 0 : aReturn <<= aDateTime;
313 : }
314 0 : break;
315 : default:
316 0 : break;
317 : }
318 : }
319 : else
320 : OSL_FAIL("OPropertyImport::convertString: unsupported property type!");
321 : }
322 0 : break;
323 : default:
324 : OSL_FAIL("OXMLControlProperty::convertString: invalid type class!");
325 : }
326 :
327 0 : return aReturn;
328 : }
329 : //---------------------------------------------------------------------
330 0 : ::com::sun::star::util::Time OXMLControlProperty::implGetTime(double _nValue)
331 : {
332 0 : ::com::sun::star::util::Time aTime;
333 0 : sal_uInt64 nIntValue = ::rtl::math::round(_nValue * 86400000000000.0);
334 0 : aTime.NanoSeconds = (sal_uInt16)( nIntValue % 1000000000 );
335 0 : nIntValue /= 1000000000;
336 0 : aTime.Seconds = (sal_uInt16)( nIntValue % 60 );
337 0 : nIntValue /= 60;
338 0 : aTime.Minutes = (sal_uInt16)( nIntValue % 60 );
339 0 : nIntValue /= 60;
340 : OSL_ENSURE(nIntValue < 24, "OPropertyImport::implGetTime: more than a day?");
341 0 : aTime.Hours = static_cast< sal_uInt16 >( nIntValue );
342 :
343 0 : return aTime;
344 : }
345 :
346 : //---------------------------------------------------------------------
347 0 : ::com::sun::star::util::Date OXMLControlProperty::implGetDate(double _nValue)
348 : {
349 0 : Date aToolsDate((sal_uInt32)_nValue);
350 0 : ::com::sun::star::util::Date aDate;
351 0 : ::utl::typeConvert(aToolsDate, aDate);
352 0 : return aDate;
353 : }
354 : //----------------------------------------------------------------------------
355 : } // namespace rptxml
356 : // -----------------------------------------------------------------------------
357 :
358 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|