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 "xmlControlProperty.hxx"
21 :
22 : #include <rtl/strbuf.hxx>
23 : #include <sax/tools/converter.hxx>
24 : #include "xmlfilter.hxx"
25 : #include <xmloff/xmltoken.hxx>
26 : #include <xmloff/xmlnmspe.hxx>
27 : #include <xmloff/nmspmap.hxx>
28 : #include "xmlEnums.hxx"
29 : #include <tools/debug.hxx>
30 : #include <tools/datetime.hxx>
31 : #include <unotools/datetime.hxx>
32 : #include <com/sun/star/util/DateTime.hpp>
33 :
34 : #define TYPE_DATE 1
35 : #define TYPE_TIME 2
36 : #define TYPE_DATETIME 3
37 :
38 : namespace rptxml
39 : {
40 : using namespace ::com::sun::star::uno;
41 : using namespace ::com::sun::star::beans;
42 : using namespace ::com::sun::star::xml::sax;
43 :
44 : DBG_NAME( rpt_OXMLControlProperty )
45 :
46 0 : OXMLControlProperty::OXMLControlProperty( ORptFilter& rImport
47 : ,sal_uInt16 nPrfx
48 : ,const ::rtl::OUString& _sLocalName
49 : ,const Reference< XAttributeList > & _xAttrList
50 : ,const Reference< XPropertySet >& _xControl
51 : ,OXMLControlProperty* _pContainer) :
52 : SvXMLImportContext( rImport, nPrfx, _sLocalName )
53 : ,m_xControl(_xControl)
54 : ,m_pContainer(_pContainer)
55 0 : ,m_bIsList(sal_False)
56 : {
57 : DBG_CTOR( rpt_OXMLControlProperty,NULL);
58 :
59 0 : m_aPropType = ::getVoidCppuType();
60 :
61 : OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
62 : OSL_ENSURE(m_xControl.is(),"Control is NULL!");
63 :
64 0 : const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
65 0 : const SvXMLTokenMap& rTokenMap = rImport.GetControlPropertyElemTokenMap();
66 :
67 0 : const sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
68 0 : for(sal_Int16 i = 0; i < nLength; ++i)
69 : {
70 0 : ::rtl::OUString sLocalName;
71 0 : const rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
72 0 : const sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
73 0 : const rtl::OUString sValue = _xAttrList->getValueByIndex( i );
74 :
75 0 : switch( rTokenMap.Get( nPrefix, sLocalName ) )
76 : {
77 : case XML_TOK_LIST_PROPERTY:
78 0 : m_bIsList = sValue == "true";
79 0 : break;
80 : case XML_TOK_VALUE_TYPE:
81 : {
82 : // needs to be translated into a ::com::sun::star::uno::Type
83 : DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Type, MapString2Type );
84 0 : static MapString2Type s_aTypeNameMap;
85 0 : if (!s_aTypeNameMap.size())
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< ::rtl::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 ConstMapString2TypeIterator 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 : DBG_DTOR( rpt_OXMLControlProperty,NULL);
118 0 : }
119 : // -----------------------------------------------------------------------------
120 0 : SvXMLImportContext* OXMLControlProperty::CreateChildContext(
121 : sal_uInt16 nPrefix,
122 : const ::rtl::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 ::rtl::OUString& rChars )
167 : {
168 0 : if ( m_pContainer )
169 0 : m_pContainer->addValue(rChars);
170 0 : }
171 : // -----------------------------------------------------------------------------
172 0 : void OXMLControlProperty::addValue(const ::rtl::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 ::rtl::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 : sal_Bool bSuccess =
203 : #endif
204 0 : ::sax::Converter::convertBool(bValue, _rReadCharacters);
205 : OSL_ENSURE(bSuccess,
206 : ::rtl::OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
207 : append(::rtl::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 : sal_Bool bSuccess =
218 : #endif
219 0 : ::sax::Converter::convertNumber(nValue, _rReadCharacters);
220 : OSL_ENSURE(bSuccess,
221 : ::rtl::OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
222 : append(rtl::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 : 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 : sal_Bool bSuccess =
240 : #endif
241 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
242 : OSL_ENSURE(bSuccess,
243 : ::rtl::OStringBuffer("OXMLControlProperty::convertString: could not convert \"").
244 : append(::rtl::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 = ::getCppuType(static_cast< ::com::sun::star::util::Date* >(NULL));
256 0 : static ::com::sun::star::uno::Type s_aTimeType = ::getCppuType(static_cast< ::com::sun::star::util::Time* >(NULL));
257 0 : static ::com::sun::star::uno::Type s_aDateTimeType = ::getCppuType(static_cast< ::com::sun::star::util::DateTime* >(NULL));
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 : sal_Bool bSuccess =
271 : #endif
272 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
273 : OSL_ENSURE(bSuccess,
274 : ::rtl::OStringBuffer("OPropertyImport::convertString: could not convert \"").
275 : append(rtl::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 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.HundredthSeconds = aTime.HundredthSeconds;
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_uInt32 nIntValue = sal_Int32(_nValue * 8640000);
330 0 : nIntValue *= 8640000;
331 0 : aTime.HundredthSeconds = (sal_uInt16)( nIntValue % 100 );
332 0 : nIntValue /= 100;
333 0 : aTime.Seconds = (sal_uInt16)( nIntValue % 60 );
334 0 : nIntValue /= 60;
335 0 : aTime.Minutes = (sal_uInt16)( nIntValue % 60 );
336 0 : nIntValue /= 60;
337 : OSL_ENSURE(nIntValue < 24, "OPropertyImport::implGetTime: more than a day?");
338 0 : aTime.Hours = static_cast< sal_uInt16 >( nIntValue );
339 :
340 0 : return aTime;
341 : }
342 :
343 : //---------------------------------------------------------------------
344 0 : ::com::sun::star::util::Date OXMLControlProperty::implGetDate(double _nValue)
345 : {
346 0 : Date aToolsDate((sal_uInt32)_nValue);
347 0 : ::com::sun::star::util::Date aDate;
348 0 : ::utl::typeConvert(aToolsDate, aDate);
349 0 : return aDate;
350 : }
351 : //----------------------------------------------------------------------------
352 : } // namespace rptxml
353 : // -----------------------------------------------------------------------------
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|