/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
Any convertAsEnum(bool bEnumAsInt, const css::uno::Type& _rExpectedType,
std::u16string_view _rReadCharacters, const SvXMLEnumMapEntry<sal_uInt16>* _pEnumMap)
{
Any aReturn;
sal_uInt16 nEnumValue(0); bool bSuccess = SvXMLUnitConverter::convertEnum(nEnumValue, _rReadCharacters, _pEnumMap);
OSL_ENSURE(bSuccess, "PropertyConversion::convertString: could not convert to an enum value!");
if (bEnumAsInt)
{ if (TypeClass_SHORT == _rExpectedType.getTypeClass())
aReturn <<= static_cast<sal_Int16>(nEnumValue); else
aReturn <<= static_cast<sal_Int32>(nEnumValue);
} else
aReturn = ::cppu::int2enum(static_cast<sal_Int32>(nEnumValue), _rExpectedType);
return aReturn;
}
}
Any PropertyConversion::convertString( const css::uno::Type& _rExpectedType, const OUString& _rReadCharacters, const SvXMLEnumMapEntry<sal_uInt16>* _pEnumMap, constbool _bInvertBoolean )
{
Any aReturn; switch (_rExpectedType.getTypeClass())
{ case TypeClass_BOOLEAN: // sal_Bool
{ bool bValue; bool bSuccess =
::sax::Converter::convertBool(bValue, _rReadCharacters);
OSL_ENSURE(bSuccess,
OStringBuffer("PropertyConversion::convertString: could not convert \"" +
OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) + "\" into a boolean!").getStr());
aReturn <<= (_bInvertBoolean ? !bValue : bValue);
} break; case TypeClass_SHORT: // sal_Int16
{ if (!_pEnumMap)
{ // it's a real int16 property
sal_Int32 nValue(0); bool bSuccess =
::sax::Converter::convertNumber(nValue, _rReadCharacters, SAL_MIN_INT16, SAL_MAX_INT16);
OSL_ENSURE(bSuccess,
OStringBuffer("PropertyConversion::convertString: could not convert \"" +
OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) + "\" into a sal_Int16!").getStr());
aReturn <<= static_cast<sal_Int16>(nValue); break;
}
aReturn = convertAsEnum(true, _rExpectedType, _rReadCharacters, _pEnumMap);
} break; case TypeClass_LONG: // sal_Int32
{ if (!_pEnumMap)
{ // it's a real int32 property
sal_Int32 nValue(0); bool bSuccess =
::sax::Converter::convertNumber(nValue, _rReadCharacters);
OSL_ENSURE(bSuccess,
OStringBuffer("PropertyConversion::convertString: could not convert \"" +
OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) + "\" into a sal_Int32!").getStr());
aReturn <<= nValue; break;
}
aReturn = convertAsEnum(true, _rExpectedType, _rReadCharacters, _pEnumMap);
} break; case TypeClass_ENUM:
{
aReturn = convertAsEnum(false, _rExpectedType, _rReadCharacters, _pEnumMap);
} break; case TypeClass_HYPER:
{
OSL_FAIL("PropertyConversion::convertString: 64-bit integers not implemented yet!");
} break; case TypeClass_DOUBLE:
{ double nValue; bool bSuccess =
::sax::Converter::convertDouble(nValue, _rReadCharacters);
OSL_ENSURE(bSuccess,
OStringBuffer(OString::Concat("PropertyConversion::convertString: could not convert \"") +
OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) + "\" into a double!").getStr());
aReturn <<= nValue;
} break; case TypeClass_STRING:
aReturn <<= _rReadCharacters; break; case TypeClass_STRUCT:
{
sal_Int32 nType = 0; if ( _rExpectedType.equals( ::cppu::UnoType< css::util::Date >::get() ) )
nType = TYPE_DATE; elseif ( _rExpectedType.equals( ::cppu::UnoType< css::util::Time >::get() ) )
nType = TYPE_TIME; elseif ( _rExpectedType.equals( ::cppu::UnoType< css::util::DateTime >::get() ) )
nType = TYPE_DATETIME;
if ( nType )
{ // first extract the double double nValue = 0; bool bSuccess =
::sax::Converter::convertDouble(nValue, _rReadCharacters);
OSL_ENSURE(bSuccess,
OStringBuffer("PropertyConversion::convertString: could not convert \"" +
OUStringToOString(_rReadCharacters, RTL_TEXTENCODING_ASCII_US) + "\" into a double!").getStr());
// then convert it into the target type switch (nType)
{ case TYPE_DATE:
{
OSL_ENSURE(std::modf(nValue, &o3tl::temporary(double())) == 0, "PropertyConversion::convertString: a Date value with a fractional part?");
aReturn <<= lcl_getDate(nValue);
} break; case TYPE_TIME:
{
OSL_ENSURE((static_cast<sal_uInt32>(nValue)) == 0, "PropertyConversion::convertString: a tools::Time value with more than a fractional part?");
aReturn <<= lcl_getTime(nValue);
} break; case TYPE_DATETIME:
{
css::util::Time aTime = lcl_getTime(nValue);
css::util::Date aDate = lcl_getDate(nValue);
Type PropertyConversion::xmlTypeToUnoType( const OUString& _rType )
{
Type aUnoType( cppu::UnoType<void>::get() );
static std::map< OUString, css::uno::Type > s_aTypeNameMap
{
{ token::GetXMLToken( token::XML_BOOLEAN ) , cppu::UnoType<bool>::get()}, // Not a copy paste error, quotation from: // http://nabble.documentfoundation.org/Question-unoType-for-getXmlToken-dbaccess-reportdesign-module-tp4109071p4109116.html // all numeric types (including the UNO double) // consistently map to XML_FLOAT, so taking the extra precision from the // C++ type "float" to "double" makes absolute sense
{ token::GetXMLToken( token::XML_FLOAT ) , ::cppu::UnoType<double>::get()},
{ token::GetXMLToken( token::XML_STRING ) , ::cppu::UnoType<OUString>::get()},
{ token::GetXMLToken( token::XML_VOID ) , cppu::UnoType<void>::get() },
};
// assume the 'worst' case: all attributes describe properties. This should save our property array // some reallocs
m_aValues.reserve(sax_fastparser::castToFastAttributeList(xAttrList).size());
if (m_bTrackAttributes)
m_aEncounteredAttributes.insert(aIter.getToken() & TOKEN_MASK);
}
// TODO: create PropertyValues for all the attributes which were not present, because they were implied // this is necessary as soon as we have properties where the XML default is different from the property // default
}
void OSinglePropertyContext::startFastElement(
sal_Int32 /*nElement*/, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
{
css::beans::PropertyValue aPropValue; // the property the instance imports currently
css::uno::Type aPropType; // the type of the property the instance imports currently
OUString sType, sValue; for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
{ switch (aIter.getToken())
{ case XML_ELEMENT(FORM, XML_PROPERTY_NAME):
aPropValue.Name = aIter.toString(); break; case XML_ELEMENT(OFFICE, XML_VALUE_TYPE):
sType = aIter.toString(); break; case XML_ELEMENT(OFFICE, XML_VALUE): case XML_ELEMENT(OFFICE, XML_BOOLEAN_VALUE): case XML_ELEMENT(OFFICE, XML_STRING_VALUE):
sValue = aIter.toString(); break; default:
XMLOFF_WARN_UNKNOWN("xmloff", aIter);
}
}
// the name of the property
OSL_ENSURE(!aPropValue.Name.isEmpty(), "OSinglePropertyContext::StartElement: invalid property name!");
// needs to be translated into a css::uno::Type
aPropType = PropertyConversion::xmlTypeToUnoType( sType ); if( TypeClass_VOID == aPropType.getTypeClass() )
{
aPropValue.Value = Any();
} else
{
aPropValue.Value =
PropertyConversion::convertString(aPropType,
sValue);
}
// now that we finally have our property value, add it to our parent object if( !aPropValue.Name.isEmpty() )
m_xPropertyImporter->implPushBackGenericPropertyValue(aPropValue);
}
void OListPropertyContext::endFastElement(sal_Int32 )
{
OSL_ENSURE( !m_sPropertyName.isEmpty() && !m_sPropertyType.isEmpty(), "OListPropertyContext::EndElement: no property name or type!" );
if ( m_sPropertyName.isEmpty() || m_sPropertyType.isEmpty() ) return;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.