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