nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / filter / xml / xmlDataSourceSetting.cxx
blobf709757a5f7d9ad8f6ade71a92d7118eb250381a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 #include <map>
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>
33 namespace dbaxml
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)
43 ,m_bIsList(false)
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";
56 break;
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();
71 return tmp;
72 }();
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;
79 break;
80 case XML_DATA_SOURCE_SETTING_NAME:
81 m_aSetting.Name = sValue;
82 break;
83 default:
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);
104 break;
105 case XML_DATA_SOURCE_SETTING_VALUE:
106 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
107 pContext = new OXMLDataSourceSetting( GetOwnImport(), xAttrList,this );
108 break;
111 return pContext;
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 )
132 if ( m_pContainer )
133 m_pContainer->addValue(rChars);
136 void OXMLDataSourceSetting::addValue(const OUString& _sValue)
138 Any aValue;
139 if( TypeClass_VOID != m_aPropType.getTypeClass() )
140 aValue = convertString(m_aPropType, _sValue);
142 if ( !m_bIsList )
143 m_aSetting.Value = aValue;
144 else
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)
159 Any aReturn;
160 switch (_rExpectedType.getTypeClass())
162 case TypeClass_BOOLEAN: // sal_Bool
164 bool bValue(false);
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!");
170 aReturn <<= bValue;
172 break;
173 case TypeClass_SHORT: // sal_Int16
174 case TypeClass_LONG: // sal_Int32
175 { // it's a real int32/16 property
176 sal_Int32 nValue(0);
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);
184 else
185 aReturn <<= nValue;
186 break;
188 case TypeClass_HYPER:
190 OSL_FAIL("OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
192 break;
193 case TypeClass_DOUBLE:
195 double nValue = 0.0;
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!");
201 aReturn <<= nValue;
203 break;
204 case TypeClass_STRING:
205 aReturn <<= _rReadCharacters;
206 break;
207 default:
208 SAL_WARN("dbaccess",
209 "OXMLDataSourceSetting::convertString: invalid type class!");
212 return aReturn;
215 } // namespace dbaxml
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */