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