LibreOffice Module dbaccess (master) 1
xmlDataSourceSetting.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#include <sal/log.hxx>
22
23#include <map>
24
27#include "xmlfilter.hxx"
28#include <xmloff/xmltoken.hxx>
30#include "xmlEnums.hxx"
31#include <osl/diagnose.h>
32
33namespace dbaxml
34{
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::xml::sax;
37
39 ,const Reference< XFastAttributeList > & _xAttrList
40 ,OXMLDataSourceSetting* _pContainer) :
41 SvXMLImportContext( rImport )
42 ,m_pContainer(_pContainer)
43 ,m_bIsList(false)
44{
45
47
48 for (auto &aIter : sax_fastparser::castToFastAttributeList( _xAttrList ))
49 {
50 switch( aIter.getToken() & TOKEN_MASK )
51 {
53 m_bIsList = aIter.toView() == "true";
54 break;
56 {
57 // needs to be translated into a css::uno::Type
58 static std::map< OUString, css::uno::Type > s_aTypeNameMap = []()
59 {
60 std::map< OUString, css::uno::Type > tmp;
62 // Not a copy paste error, see comment xmloff/source/forms/propertyimport.cxx lines 244-248
69 return tmp;
70 }();
71
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;
76 }
77 break;
79 m_aSetting.Name = aIter.toString();
80 break;
81 default:
82 XMLOFF_WARN_UNKNOWN("dbaccess", aIter);
83 }
84 }
85
86}
87
89{
90}
91
92css::uno::Reference< css::xml::sax::XFastContextHandler > OXMLDataSourceSetting::createFastChildContext(
93 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
94{
95 SvXMLImportContext *pContext = nullptr;
96
97 switch( nElement & TOKEN_MASK )
98 {
100 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
101 pContext = new OXMLDataSourceSetting( GetOwnImport(), xAttrList);
102 break;
104 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
105 pContext = new OXMLDataSourceSetting( GetOwnImport(), xAttrList,this );
106 break;
107 }
108
109 return pContext;
110}
111
113{
114 if ( !m_aSetting.Name.isEmpty() )
115 {
116 if ( m_bIsList && m_aInfoSequence.hasElements() )
117 m_aSetting.Value <<= m_aInfoSequence;
118
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();
123
125 }
126}
127
128void OXMLDataSourceSetting::characters( const OUString& rChars )
129{
130 if ( m_pContainer )
131 m_pContainer->addValue(rChars);
132}
133
134void OXMLDataSourceSetting::addValue(const OUString& _sValue)
135{
136 Any aValue;
137 if( TypeClass_VOID != m_aPropType.getTypeClass() )
138 aValue = convertString(m_aPropType, _sValue);
139
140 if ( !m_bIsList )
141 m_aSetting.Value = aValue;
142 else
143 {
144 sal_Int32 nPos = m_aInfoSequence.getLength();
145 m_aInfoSequence.realloc(nPos+1);
146 m_aInfoSequence.getArray()[nPos] = aValue;
147 }
148}
149
151{
152 return static_cast<ODBFilter&>(GetImport());
153}
154
155Any OXMLDataSourceSetting::convertString(const css::uno::Type& _rExpectedType, const OUString& _rReadCharacters)
156{
157 Any aReturn;
158 switch (_rExpectedType.getTypeClass())
159 {
160 case TypeClass_BOOLEAN: // sal_Bool
161 {
162 bool bValue(false);
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!");
168 aReturn <<= bValue;
169 }
170 break;
171 case TypeClass_SHORT: // sal_Int16
172 case TypeClass_LONG: // sal_Int32
173 { // it's a real int32/16 property
174 sal_Int32 nValue(0);
175 bool const bSuccess =
176 ::sax::Converter::convertNumber(nValue, _rReadCharacters);
177 SAL_WARN_IF(!bSuccess, "dbaccess",
178 "OXMLDataSourceSetting::convertString: could not convert \""
179 << _rReadCharacters << "\" into an integer!");
180 if (TypeClass_SHORT == _rExpectedType.getTypeClass())
181 aReturn <<= static_cast<sal_Int16>(nValue);
182 else
183 aReturn <<= nValue;
184 break;
185 }
186 case TypeClass_HYPER:
187 {
188 OSL_FAIL("OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
189 }
190 break;
191 case TypeClass_DOUBLE:
192 {
193 double nValue = 0.0;
194 bool const bSuccess =
195 ::sax::Converter::convertDouble(nValue, _rReadCharacters);
196 SAL_WARN_IF(!bSuccess, "dbaccess",
197 "OXMLDataSourceSetting::convertString: could not convert \""
198 << _rReadCharacters << "\" into a double!");
199 aReturn <<= nValue;
200 }
201 break;
202 case TypeClass_STRING:
203 aReturn <<= _rReadCharacters;
204 break;
205 default:
206 SAL_WARN("dbaccess",
207 "OXMLDataSourceSetting::convertString: invalid type class!");
208 }
209
210 return aReturn;
211}
212
213} // namespace dbaxml
214
215/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLImport & GetImport()
css::uno::Type const & get()
void addInfo(const css::beans::PropertyValue &_rInfo)
add an Info to the sequence which will be appended to the data source
Definition: xmlfilter.hxx:108
virtual void SAL_CALL characters(const OUString &rChars) override
css::beans::PropertyValue m_aSetting
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
void addValue(const OUString &_sValue)
adds value to property
static css::uno::Any convertString(const css::uno::Type &_rExpectedType, const OUString &_rReadCharacters)
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
OXMLDataSourceSetting(ODBFilter &rImport, const css::uno::Reference< css::xml::sax::XFastAttributeList > &_xAttrList, OXMLDataSourceSetting *_pContainer=nullptr)
OXMLDataSourceSetting * m_pContainer
css::uno::Sequence< css::uno::Any > m_aInfoSequence
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)
sal_Int16 nValue
sal_uInt16 nPos
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
XML_DATA_SOURCE_SETTING
XML_DATA_SOURCE_SETTING_NAME
XML_DATA_SOURCE_SETTING_TYPE
XML_FLOAT
XML_STRING
XML_DATA_SOURCE_SETTING_IS_LIST
XML_BOOLEAN
XML_VOID
XML_DOUBLE
XML_DATA_SOURCE_SETTING_VALUE
XML_SHORT
XML_INT
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
#define PROGRESS_BAR_STEP
Definition: xmlEnums.hxx:21
#define XMLOFF_WARN_UNKNOWN(area, rIter)
constexpr sal_Int32 TOKEN_MASK