LibreOffice Module xmloff (master) 1
propertyimport.cxx
Go to the documentation of this file.
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 <cmath>
23
24#include "propertyimport.hxx"
25
27
28#include <utility>
29#include <xmloff/xmlimp.hxx>
30#include <xmloff/xmluconv.hxx>
32#include <o3tl/temporary.hxx>
33#include <osl/diagnose.h>
34#include <sal/log.hxx>
37#include <tools/date.hxx>
38#include <tools/time.hxx>
39#include <com/sun/star/util/Date.hpp>
40#include <com/sun/star/util/Time.hpp>
41#include <com/sun/star/util/DateTime.hpp>
42#include <unotools/datetime.hxx>
43#include <rtl/strbuf.hxx>
44
45using namespace ::xmloff::token;
46
47namespace xmloff
48{
49
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::xml;
53 using ::com::sun::star::xml::sax::XAttributeList;
54 using ::com::sun::star::xml::sax::XFastAttributeList;
55
56 // NO using namespace ...util !!!
57 // need a tools Date/Time/DateTime below, which would conflict with the uno types then
58
59#define TYPE_DATE 1
60#define TYPE_TIME 2
61#define TYPE_DATETIME 3
62
63//= PropertyConversion
64namespace
65{
66 css::util::Time lcl_getTime(double _nValue)
67 {
68 css::util::Time aTime;
69 sal_uInt64 nIntValue = static_cast<sal_uInt64>(_nValue * ::tools::Time::nanoSecPerDay);
70 aTime.NanoSeconds = nIntValue % ::tools::Time::nanoSecPerSec;
72 aTime.Seconds = nIntValue % ::tools::Time::secondPerMinute;
74 aTime.Minutes = nIntValue % ::tools::Time::minutePerHour;
76 OSL_ENSURE(nIntValue < 24, "lcl_getTime: more than a day?");
77 aTime.Hours = nIntValue;
78
79 return aTime;
80 }
81
82 css::util::Date lcl_getDate( double _nValue )
83 {
84 Date aToolsDate(static_cast<sal_uInt32>(_nValue));
85 css::util::Date aDate;
86 ::utl::typeConvert(aToolsDate, aDate);
87 return aDate;
88 }
89}
90
91Any PropertyConversion::convertString( const css::uno::Type& _rExpectedType,
92 const OUString& _rReadCharacters, const SvXMLEnumMapEntry<sal_uInt16>* _pEnumMap, const bool _bInvertBoolean )
93{
94 Any aReturn;
95 bool bEnumAsInt = false;
96 switch (_rExpectedType.getTypeClass())
97 {
98 case TypeClass_BOOLEAN: // sal_Bool
99 {
100 bool bValue;
101 bool bSuccess =
102 ::sax::Converter::convertBool(bValue, _rReadCharacters);
103 OSL_ENSURE(bSuccess,
104 OStringBuffer("PropertyConversion::convertString: could not convert \"" +
105 OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) +
106 "\" into a boolean!").getStr());
107 aReturn <<= (_bInvertBoolean ? !bValue : bValue);
108 }
109 break;
110 case TypeClass_SHORT: // sal_Int16
111 case TypeClass_LONG: // sal_Int32
112 if (!_pEnumMap)
113 { // it's a real int32/16 property
114 sal_Int32 nValue(0);
115 bool bSuccess =
116 ::sax::Converter::convertNumber(nValue, _rReadCharacters);
117 OSL_ENSURE(bSuccess,
118 OStringBuffer("PropertyConversion::convertString: could not convert \"" +
119 OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) +
120 "\" into an integer!").getStr());
121 if (TypeClass_SHORT == _rExpectedType.getTypeClass())
122 aReturn <<= static_cast<sal_Int16>(nValue);
123 else
124 aReturn <<= nValue;
125 break;
126 }
127 bEnumAsInt = true;
128 [[fallthrough]];
129 case TypeClass_ENUM:
130 {
131 sal_uInt16 nEnumValue(0);
132 bool bSuccess = SvXMLUnitConverter::convertEnum(nEnumValue, _rReadCharacters, _pEnumMap);
133 OSL_ENSURE(bSuccess, "PropertyConversion::convertString: could not convert to an enum value!");
134
135 if (bEnumAsInt)
136 if (TypeClass_SHORT == _rExpectedType.getTypeClass())
137 aReturn <<= static_cast<sal_Int16>(nEnumValue);
138 else
139 aReturn <<= static_cast<sal_Int32>(nEnumValue);
140 else
141 aReturn = ::cppu::int2enum(static_cast<sal_Int32>(nEnumValue), _rExpectedType);
142 }
143 break;
144 case TypeClass_HYPER:
145 {
146 OSL_FAIL("PropertyConversion::convertString: 64-bit integers not implemented yet!");
147 }
148 break;
149 case TypeClass_DOUBLE:
150 {
151 double nValue;
152 bool bSuccess =
153 ::sax::Converter::convertDouble(nValue, _rReadCharacters);
154 OSL_ENSURE(bSuccess,
155 OStringBuffer(OString::Concat("PropertyConversion::convertString: could not convert \"") +
156 OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) +
157 "\" into a double!").getStr());
158 aReturn <<= nValue;
159 }
160 break;
161 case TypeClass_STRING:
162 aReturn <<= _rReadCharacters;
163 break;
164 case TypeClass_STRUCT:
165 {
166 sal_Int32 nType = 0;
167 if ( _rExpectedType.equals( ::cppu::UnoType< css::util::Date >::get() ) )
169 else if ( _rExpectedType.equals( ::cppu::UnoType< css::util::Time >::get() ) )
171 else if ( _rExpectedType.equals( ::cppu::UnoType< css::util::DateTime >::get() ) )
173
174 if ( nType )
175 {
176 // first extract the double
177 double nValue = 0;
178 bool bSuccess =
179 ::sax::Converter::convertDouble(nValue, _rReadCharacters);
180 OSL_ENSURE(bSuccess,
181 OStringBuffer("PropertyConversion::convertString: could not convert \"" +
182 OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) +
183 "\" into a double!").getStr());
184
185 // then convert it into the target type
186 switch (nType)
187 {
188 case TYPE_DATE:
189 {
190 OSL_ENSURE(std::modf(nValue, &o3tl::temporary(double())) == 0,
191 "PropertyConversion::convertString: a Date value with a fractional part?");
192 aReturn <<= lcl_getDate(nValue);
193 }
194 break;
195 case TYPE_TIME:
196 {
197 OSL_ENSURE((static_cast<sal_uInt32>(nValue)) == 0,
198 "PropertyConversion::convertString: a tools::Time value with more than a fractional part?");
199 aReturn <<= lcl_getTime(nValue);
200 }
201 break;
202 case TYPE_DATETIME:
203 {
204 css::util::Time aTime = lcl_getTime(nValue);
205 css::util::Date aDate = lcl_getDate(nValue);
206
207 css::util::DateTime aDateTime;
208 aDateTime.NanoSeconds = aTime.NanoSeconds;
209 aDateTime.Seconds = aTime.Seconds;
210 aDateTime.Minutes = aTime.Minutes;
211 aDateTime.Hours = aTime.Hours;
212 aDateTime.Day = aDate.Day;
213 aDateTime.Month = aDate.Month;
214 aDateTime.Year = aDate.Year;
215 aReturn <<= aDateTime;
216 }
217 break;
218 }
219 }
220 else
221 OSL_FAIL("PropertyConversion::convertString: unsupported property type!");
222 }
223 break;
224 default:
225 OSL_FAIL("PropertyConversion::convertString: invalid type class!");
226 }
227
228 return aReturn;
229}
230
232{
233 Type aUnoType( cppu::UnoType<void>::get() );
234
235 static std::map< OUString, css::uno::Type > s_aTypeNameMap
236 {
238 // Not a copy paste error, quotation from:
239 // http://nabble.documentfoundation.org/Question-unoType-for-getXmlToken-dbaccess-reportdesign-module-tp4109071p4109116.html
240 // all numeric types (including the UNO double)
241 // consistently map to XML_FLOAT, so taking the extra precision from the
242 // C++ type "float" to "double" makes absolute sense
246 };
247
248 const std::map< OUString, css::uno::Type >::iterator aTypePos = s_aTypeNameMap.find( _rType );
249 OSL_ENSURE( s_aTypeNameMap.end() != aTypePos, "PropertyConversion::xmlTypeToUnoType: invalid property name!" );
250 if ( s_aTypeNameMap.end() != aTypePos )
251 aUnoType = aTypePos->second;
252
253 return aUnoType;
254}
255
256//= OPropertyImport
258 :SvXMLImportContext(_rImport.getGlobalContext())
259 ,m_rContext(_rImport)
260 ,m_bTrackAttributes(false)
261{
262}
263
264css::uno::Reference< css::xml::sax::XFastContextHandler > OPropertyImport::createFastChildContext(
265 sal_Int32 nElement,
266 const css::uno::Reference< css::xml::sax::XFastAttributeList >& /*xAttrList*/ )
267{
268 if( (nElement & TOKEN_MASK) == token::XML_PROPERTIES )
269 {
271 }
272 else
273 SAL_WARN("xmloff", "unknown element " << SvXMLImport::getPrefixAndNameFromToken(nElement));
274 return nullptr;
275}
276
277void OPropertyImport::startFastElement(sal_Int32 /*nElement*/, const Reference< XFastAttributeList >& xAttrList)
278{
279
280 // assume the 'worst' case: all attributes describe properties. This should save our property array
281 // some reallocs
283
284 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
285 {
286 handleAttribute(aIter.getToken(), aIter.toString());
287
289 m_aEncounteredAttributes.insert(aIter.getToken() & TOKEN_MASK);
290 }
291
292 // TODO: create PropertyValues for all the attributes which were not present, because they were implied
293 // this is necessary as soon as we have properties where the XML default is different from the property
294 // default
295}
296
297bool OPropertyImport::encounteredAttribute(sal_Int32 nAttributeToken) const
298{
299 OSL_ENSURE(m_bTrackAttributes, "OPropertyImport::encounteredAttribute: attribute tracking not enabled!");
301}
302
303void OPropertyImport::characters(const OUString& _rChars )
304{
305 // ignore them (should be whitespace only)
306 OSL_ENSURE(_rChars.trim().isEmpty(), "OPropertyImport::Characters: non-whitespace characters!");
307}
308
309bool OPropertyImport::handleAttribute(sal_Int32 nAttributeToken, const OUString& _rValue)
310{
312 if (pProperty)
313 {
314 // create and store a new PropertyValue
315 PropertyValue aNewValue;
316 aNewValue.Name = pProperty->sPropertyName;
317
318 // convert the value string into the target type
319 if ((nAttributeToken & TOKEN_MASK) == token::XML_HREF)
320 {
321 aNewValue.Value <<= m_rContext.getGlobalContext().GetAbsoluteReference(_rValue);
322 }
323 else
324 {
325 aNewValue.Value = PropertyConversion::convertString(
326 pProperty->aPropertyType, _rValue, pProperty->pEnumMap,
327 pProperty->bInverseSemantics);
328 }
329 implPushBackPropertyValue( aNewValue );
330 return true;
331 }
332 if ((nAttributeToken & TOKEN_MASK) != token::XML_TYPE) // xlink:type is valid but ignored for <form:form>
333 {
334 SAL_WARN( "xmloff", "OPropertyImport::handleAttribute: Can't handle "
335 << SvXMLImport::getPrefixAndNameFromToken(nAttributeToken) << "=" << _rValue );
336 return false;
337 }
338 return true;
339}
340
341//= OPropertyElementsContext
343 OPropertyImportRef _xPropertyImporter)
344 :SvXMLImportContext(_rImport)
345 ,m_xPropertyImporter(std::move(_xPropertyImporter))
346{
347}
348
349css::uno::Reference< css::xml::sax::XFastContextHandler > OPropertyElementsContext::createFastChildContext(
350 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
351{
352 if( (nElement & TOKEN_MASK) == XML_PROPERTY )
353 {
355 }
356 else if( (nElement & TOKEN_MASK) == XML_LIST_PROPERTY )
357 {
359 }
360 return nullptr;
361}
362
363#if OSL_DEBUG_LEVEL > 0
365 sal_Int32 /*nElement*/,
366 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
367 {
368 OSL_ENSURE(0 == xAttrList->getFastAttributes().getLength(), "OPropertyElementsContext::StartElement: the form:properties element should not have attributes!");
369 }
370
371 void OPropertyElementsContext::characters(const OUString& _rChars)
372 {
373 OSL_ENSURE(_rChars.trim().isEmpty(), "OPropertyElementsContext::Characters: non-whitespace characters detected!");
374 }
375#endif
376
377//= OSinglePropertyContext
379 OPropertyImportRef _xPropertyImporter)
380 :SvXMLImportContext(_rImport)
381 ,m_xPropertyImporter(std::move(_xPropertyImporter))
382{
383}
384
386 sal_Int32 /*nElement*/,
387 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
388{
389 css::beans::PropertyValue aPropValue; // the property the instance imports currently
390 css::uno::Type aPropType; // the type of the property the instance imports currently
391
392 OUString sType, sValue;
393 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
394 {
395 switch (aIter.getToken())
396 {
398 aPropValue.Name = aIter.toString();
399 break;
401 sType = aIter.toString();
402 break;
406 sValue = aIter.toString();
407 break;
408 default:
409 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
410 }
411 }
412
413 // the name of the property
414 OSL_ENSURE(!aPropValue.Name.isEmpty(), "OSinglePropertyContext::StartElement: invalid property name!");
415
416 // needs to be translated into a css::uno::Type
418 if( TypeClass_VOID == aPropType.getTypeClass() )
419 {
420 aPropValue.Value = Any();
421 }
422 else
423 {
424 aPropValue.Value =
426 sValue);
427 }
428
429 // now that we finally have our property value, add it to our parent object
430 if( !aPropValue.Name.isEmpty() )
431 m_xPropertyImporter->implPushBackGenericPropertyValue(aPropValue);
432}
433
434//= OListPropertyContext
436 OPropertyImportRef _rPropertyImporter )
437 :SvXMLImportContext( _rImport )
438 ,m_xPropertyImporter(std::move( _rPropertyImporter ))
439{
440}
441
443 sal_Int32 /*nElement*/,
444 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
445{
446 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
447 {
448 switch (aIter.getToken())
449 {
451 m_sPropertyName = aIter.toString();
452 break;
454 m_sPropertyType = aIter.toString();
455 break;
456 default:
457 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
458 }
459 }
460}
461
463{
464 OSL_ENSURE( !m_sPropertyName.isEmpty() && !m_sPropertyType.isEmpty(),
465 "OListPropertyContext::EndElement: no property name or type!" );
466
467 if ( m_sPropertyName.isEmpty() || m_sPropertyType.isEmpty() )
468 return;
469
470 Sequence< Any > aListElements( m_aListValues.size() );
471 Any* pListElement = aListElements.getArray();
473 for ( const auto& rListValue : m_aListValues )
474 {
475 *pListElement = PropertyConversion::convertString( aType, rListValue );
476 ++pListElement;
477 }
478
479 PropertyValue aSequenceValue;
480 aSequenceValue.Name = m_sPropertyName;
481 aSequenceValue.Value <<= aListElements;
482
483 m_xPropertyImporter->implPushBackGenericPropertyValue( aSequenceValue );
484}
485
486css::uno::Reference< css::xml::sax::XFastContextHandler > OListPropertyContext::createFastChildContext(
487 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
488{
489 if ( (nElement & TOKEN_MASK) == XML_LIST_VALUE )
490 {
491 m_aListValues.emplace_back();
492 return new OListValueContext( GetImport(), *m_aListValues.rbegin() );
493 }
494 return nullptr;
495}
496
497//= OListValueContext
498OListValueContext::OListValueContext( SvXMLImport& _rImport, OUString& _rListValueHolder )
499 :SvXMLImportContext( _rImport )
500 ,m_rListValueHolder( _rListValueHolder )
501{
502}
503
505 sal_Int32 /*nElement*/,
506 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
507{
508 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
509 {
510 switch(aIter.getToken())
511 {
515 m_rListValueHolder = aIter.toString();
516 break;
517 default:
518 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
519 }
520 }
521}
522
523} // namespace xmloff
524
525/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sType
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
static bool convertEnum(EnumT &rEnum, std::u16string_view rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:145
css::uno::Type const & get()
const_iterator find(const Value &x) const
const_iterator end() const
std::pair< const_iterator, bool > insert(Value &&x)
static void convertDouble(OUStringBuffer &rBuffer, double fNumber, bool bWriteUnits, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit)
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
static bool convertBool(bool &rBool, std::u16string_view rString)
static const sal_Int64 minutePerHour
static const sal_Int64 nanoSecPerDay
static const sal_Int64 nanoSecPerSec
static const sal_Int64 secondPerMinute
const AttributeAssignment * getAttributeTranslation(sal_Int32 nAttributeToken)
return the AttributeAssignment which corresponds to the given attribute
OAttribute2Property & getAttributeMap()
Definition: layerimport.hxx:95
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
OPropertyImportRef m_xPropertyImporter
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
::std::vector< OUString > m_aListValues
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
OListPropertyContext(SvXMLImport &_rImport, OPropertyImportRef _xPropertyImporter)
OListValueContext(SvXMLImport &_rImport, OUString &_rListValueHolder)
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
helper class for importing the <form:properties> element
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
OPropertyElementsContext(SvXMLImport &_rImport, OPropertyImportRef _xPropertyImporter)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
OPropertyImportRef m_xPropertyImporter
virtual void SAL_CALL characters(const OUString &_rChars) override
This method is called for all characters that are contained in the current element.
PropertyValueArray m_aValues
void implPushBackPropertyValue(const css::beans::PropertyValue &_rProp)
virtual bool handleAttribute(sal_Int32 nElement, const OUString &_rValue)
handle one single attribute.
o3tl::sorted_vector< sal_Int32 > m_aEncounteredAttributes
OPropertyImport(OFormLayerXMLImport_Impl &_rImport)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
bool encounteredAttribute(sal_Int32 nElement) const
determine if the element imported by the object had a given attribute.
virtual void SAL_CALL characters(const OUString &_rChars) override
This method is called for all characters that are contained in the current element.
OFormLayerXMLImport_Impl & m_rContext
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &_rxAttrList) override
helper class for importing a single <form:property> element
OPropertyImportRef m_xPropertyImporter
OSinglePropertyContext(SvXMLImport &_rImport, OPropertyImportRef _xPropertyImporter)
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
static css::uno::Any convertString(const css::uno::Type &_rExpectedType, const OUString &_rReadCharacters, const SvXMLEnumMapEntry< EnumT > *_pEnumMap=nullptr)
static css::uno::Type xmlTypeToUnoType(const OUString &_rType)
sal_Int32 _nValue
sal_Int16 nValue
#define SAL_WARN(area, stream)
size
Type
FORM
constexpr T & temporary(T &&x)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3529
#define TYPE_DATETIME
#define TYPE_TIME
#define TYPE_DATE
QPRO_FUNC_TYPE nType
const SvXMLEnumMapEntry< sal_uInt16 > * pEnumMap
const Reference< XComponentContext > & m_rContext
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
constexpr sal_Int32 TOKEN_MASK
Definition: xmlimp.hxx:94