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 OUString sValue
= aIter
.toString();
52 switch( aIter
.getToken() & TOKEN_MASK
)
54 case XML_DATA_SOURCE_SETTING_IS_LIST
:
55 m_bIsList
= sValue
== "true";
57 case XML_DATA_SOURCE_SETTING_TYPE
:
59 // needs to be translated into a css::uno::Type
60 static std::map
< OUString
, css::uno::Type
> s_aTypeNameMap
= [&]()
62 std::map
< OUString
, css::uno::Type
> tmp
;
63 tmp
[GetXMLToken( XML_BOOLEAN
)] = cppu::UnoType
<bool>::get();
64 // Not a copy paste error, see comment xmloff/source/forms/propertyimport.cxx lines 244-248
65 tmp
[GetXMLToken( XML_FLOAT
)] = ::cppu::UnoType
<double>::get();
66 tmp
[GetXMLToken( XML_DOUBLE
)] = ::cppu::UnoType
<double>::get();
67 tmp
[GetXMLToken( XML_STRING
)] = ::cppu::UnoType
<OUString
>::get();
68 tmp
[GetXMLToken( XML_INT
)] = ::cppu::UnoType
<sal_Int32
>::get();
69 tmp
[GetXMLToken( XML_SHORT
)] = ::cppu::UnoType
<sal_Int16
>::get();
70 tmp
[GetXMLToken( XML_VOID
)] = cppu::UnoType
<void>::get();
74 const std::map
< OUString
, css::uno::Type
>::const_iterator aTypePos
= s_aTypeNameMap
.find(sValue
);
75 OSL_ENSURE(s_aTypeNameMap
.end() != aTypePos
, "OXMLDataSourceSetting::OXMLDataSourceSetting: invalid type!");
76 if (s_aTypeNameMap
.end() != aTypePos
)
77 m_aPropType
= aTypePos
->second
;
80 case XML_DATA_SOURCE_SETTING_NAME
:
81 m_aSetting
.Name
= sValue
;
84 XMLOFF_WARN_UNKNOWN_ATTR("dbaccess", aIter
.getToken(), aIter
.toString());
90 OXMLDataSourceSetting::~OXMLDataSourceSetting()
94 css::uno::Reference
< css::xml::sax::XFastContextHandler
> OXMLDataSourceSetting::createFastChildContext(
95 sal_Int32 nElement
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
97 SvXMLImportContext
*pContext
= nullptr;
99 switch( nElement
& TOKEN_MASK
)
101 case XML_DATA_SOURCE_SETTING
:
102 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
103 pContext
= new OXMLDataSourceSetting( GetOwnImport(), xAttrList
);
105 case XML_DATA_SOURCE_SETTING_VALUE
:
106 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
107 pContext
= new OXMLDataSourceSetting( GetOwnImport(), xAttrList
,this );
114 void OXMLDataSourceSetting::endFastElement(sal_Int32
)
116 if ( !m_aSetting
.Name
.isEmpty() )
118 if ( m_bIsList
&& m_aInfoSequence
.hasElements() )
119 m_aSetting
.Value
<<= m_aInfoSequence
;
121 // if our property is of type string, but was empty, ensure that
122 // we don't add a VOID value
123 if ( !m_bIsList
&& ( m_aPropType
.getTypeClass() == TypeClass_STRING
) && !m_aSetting
.Value
.hasValue() )
124 m_aSetting
.Value
<<= OUString();
126 GetOwnImport().addInfo(m_aSetting
);
130 void OXMLDataSourceSetting::characters( const OUString
& rChars
)
133 m_pContainer
->addValue(rChars
);
136 void OXMLDataSourceSetting::addValue(const OUString
& _sValue
)
139 if( TypeClass_VOID
!= m_aPropType
.getTypeClass() )
140 aValue
= convertString(m_aPropType
, _sValue
);
143 m_aSetting
.Value
= aValue
;
146 sal_Int32 nPos
= m_aInfoSequence
.getLength();
147 m_aInfoSequence
.realloc(nPos
+1);
148 m_aInfoSequence
[nPos
] = aValue
;
152 ODBFilter
& OXMLDataSourceSetting::GetOwnImport()
154 return static_cast<ODBFilter
&>(GetImport());
157 Any
OXMLDataSourceSetting::convertString(const css::uno::Type
& _rExpectedType
, const OUString
& _rReadCharacters
)
160 switch (_rExpectedType
.getTypeClass())
162 case TypeClass_BOOLEAN
: // sal_Bool
165 bool const bSuccess
=
166 ::sax::Converter::convertBool(bValue
, _rReadCharacters
);
167 SAL_WARN_IF(!bSuccess
, "dbaccess",
168 "OXMLDataSourceSetting::convertString: could not convert \""
169 << _rReadCharacters
<< "\" into a boolean!");
173 case TypeClass_SHORT
: // sal_Int16
174 case TypeClass_LONG
: // sal_Int32
175 { // it's a real int32/16 property
177 bool const bSuccess
=
178 ::sax::Converter::convertNumber(nValue
, _rReadCharacters
);
179 SAL_WARN_IF(!bSuccess
, "dbaccess",
180 "OXMLDataSourceSetting::convertString: could not convert \""
181 << _rReadCharacters
<< "\" into an integer!");
182 if (TypeClass_SHORT
== _rExpectedType
.getTypeClass())
183 aReturn
<<= static_cast<sal_Int16
>(nValue
);
188 case TypeClass_HYPER
:
190 OSL_FAIL("OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
193 case TypeClass_DOUBLE
:
196 bool const bSuccess
=
197 ::sax::Converter::convertDouble(nValue
, _rReadCharacters
);
198 SAL_WARN_IF(!bSuccess
, "dbaccess",
199 "OXMLDataSourceSetting::convertString: could not convert \""
200 << _rReadCharacters
<< "\" into a double!");
204 case TypeClass_STRING
:
205 aReturn
<<= _rReadCharacters
;
209 "OXMLDataSourceSetting::convertString: invalid type class!");
215 } // namespace dbaxml
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */