1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
25 #include "xmlDataSourceSetting.hxx"
26 #include <sax/tools/converter.hxx>
27 #include "xmlfilter.hxx"
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/ProgressBarHelper.hxx>
30 #include "xmlEnums.hxx"
31 #include <osl/diagnose.h>
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::xml::sax
;
38 OXMLDataSourceSetting::OXMLDataSourceSetting( ODBFilter
& rImport
39 ,const Reference
< XFastAttributeList
> & _xAttrList
40 ,OXMLDataSourceSetting
* _pContainer
) :
41 SvXMLImportContext( rImport
)
42 ,m_pContainer(_pContainer
)
46 m_aPropType
= cppu::UnoType
<void>::get();
48 for (auto &aIter
: sax_fastparser::castToFastAttributeList( _xAttrList
))
50 switch( aIter
.getToken() & TOKEN_MASK
)
52 case XML_DATA_SOURCE_SETTING_IS_LIST
:
53 m_bIsList
= aIter
.toView() == "true";
55 case XML_DATA_SOURCE_SETTING_TYPE
:
57 // needs to be translated into a css::uno::Type
58 static std::map
< OUString
, css::uno::Type
> s_aTypeNameMap
= []()
60 std::map
< OUString
, css::uno::Type
> tmp
;
61 tmp
[GetXMLToken( XML_BOOLEAN
)] = cppu::UnoType
<bool>::get();
62 // Not a copy paste error, see comment xmloff/source/forms/propertyimport.cxx lines 244-248
63 tmp
[GetXMLToken( XML_FLOAT
)] = ::cppu::UnoType
<double>::get();
64 tmp
[GetXMLToken( XML_DOUBLE
)] = ::cppu::UnoType
<double>::get();
65 tmp
[GetXMLToken( XML_STRING
)] = ::cppu::UnoType
<OUString
>::get();
66 tmp
[GetXMLToken( XML_INT
)] = ::cppu::UnoType
<sal_Int32
>::get();
67 tmp
[GetXMLToken( XML_SHORT
)] = ::cppu::UnoType
<sal_Int16
>::get();
68 tmp
[GetXMLToken( XML_VOID
)] = cppu::UnoType
<void>::get();
72 const std::map
< OUString
, css::uno::Type
>::const_iterator aTypePos
= s_aTypeNameMap
.find(aIter
.toString());
73 OSL_ENSURE(s_aTypeNameMap
.end() != aTypePos
, "OXMLDataSourceSetting::OXMLDataSourceSetting: invalid type!");
74 if (s_aTypeNameMap
.end() != aTypePos
)
75 m_aPropType
= aTypePos
->second
;
78 case XML_DATA_SOURCE_SETTING_NAME
:
79 m_aSetting
.Name
= aIter
.toString();
82 XMLOFF_WARN_UNKNOWN("dbaccess", aIter
);
88 OXMLDataSourceSetting::~OXMLDataSourceSetting()
92 css::uno::Reference
< css::xml::sax::XFastContextHandler
> OXMLDataSourceSetting::createFastChildContext(
93 sal_Int32 nElement
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
95 SvXMLImportContext
*pContext
= nullptr;
97 switch( nElement
& TOKEN_MASK
)
99 case XML_DATA_SOURCE_SETTING
:
100 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
101 pContext
= new OXMLDataSourceSetting( GetOwnImport(), xAttrList
);
103 case XML_DATA_SOURCE_SETTING_VALUE
:
104 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
105 pContext
= new OXMLDataSourceSetting( GetOwnImport(), xAttrList
,this );
112 void OXMLDataSourceSetting::endFastElement(sal_Int32
)
114 if ( !m_aSetting
.Name
.isEmpty() )
116 if ( m_bIsList
&& m_aInfoSequence
.hasElements() )
117 m_aSetting
.Value
<<= m_aInfoSequence
;
119 // if our property is of type string, but was empty, ensure that
120 // we don't add a VOID value
121 if ( !m_bIsList
&& ( m_aPropType
.getTypeClass() == TypeClass_STRING
) && !m_aSetting
.Value
.hasValue() )
122 m_aSetting
.Value
<<= OUString();
124 GetOwnImport().addInfo(m_aSetting
);
128 void OXMLDataSourceSetting::characters( const OUString
& rChars
)
131 m_pContainer
->addValue(rChars
);
134 void OXMLDataSourceSetting::addValue(const OUString
& _sValue
)
137 if( TypeClass_VOID
!= m_aPropType
.getTypeClass() )
138 aValue
= convertString(m_aPropType
, _sValue
);
141 m_aSetting
.Value
= std::move(aValue
);
144 sal_Int32 nPos
= m_aInfoSequence
.getLength();
145 m_aInfoSequence
.realloc(nPos
+1);
146 m_aInfoSequence
.getArray()[nPos
] = std::move(aValue
);
150 ODBFilter
& OXMLDataSourceSetting::GetOwnImport()
152 return static_cast<ODBFilter
&>(GetImport());
155 Any
OXMLDataSourceSetting::convertString(const css::uno::Type
& _rExpectedType
, const OUString
& _rReadCharacters
)
158 switch (_rExpectedType
.getTypeClass())
160 case TypeClass_BOOLEAN
: // sal_Bool
163 bool const bSuccess
=
164 ::sax::Converter::convertBool(bValue
, _rReadCharacters
);
165 SAL_WARN_IF(!bSuccess
, "dbaccess",
166 "OXMLDataSourceSetting::convertString: could not convert \""
167 << _rReadCharacters
<< "\" into a boolean!");
171 case TypeClass_SHORT
: // sal_Int16
172 { // it's a real int16 property
174 bool const bSuccess
=
175 ::sax::Converter::convertNumber(nValue
, _rReadCharacters
,
176 SAL_MIN_INT16
, SAL_MAX_INT16
);
177 SAL_WARN_IF(!bSuccess
, "dbaccess",
178 "OXMLDataSourceSetting::convertString: could not convert \""
179 << _rReadCharacters
<< "\" into a sal_Int16!");
180 aReturn
<<= static_cast<sal_Int16
>(nValue
);
183 case TypeClass_LONG
: // sal_Int32
184 { // it's a real int32 property
186 bool const bSuccess
=
187 ::sax::Converter::convertNumber(nValue
, _rReadCharacters
);
188 SAL_WARN_IF(!bSuccess
, "dbaccess",
189 "OXMLDataSourceSetting::convertString: could not convert \""
190 << _rReadCharacters
<< "\" into a sal_Int32!");
194 case TypeClass_HYPER
:
196 OSL_FAIL("OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
199 case TypeClass_DOUBLE
:
202 bool const bSuccess
=
203 ::sax::Converter::convertDouble(nValue
, _rReadCharacters
);
204 SAL_WARN_IF(!bSuccess
, "dbaccess",
205 "OXMLDataSourceSetting::convertString: could not convert \""
206 << _rReadCharacters
<< "\" into a double!");
210 case TypeClass_STRING
:
211 aReturn
<<= _rReadCharacters
;
215 "OXMLDataSourceSetting::convertString: invalid type class!");
221 } // namespace dbaxml
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */