LibreOffice Module connectivity (master) 1
DriversConfig.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#include <config_fuzzers.h>
20
22#include <o3tl/string_view.hxx>
23#include <tools/wldcrd.hxx>
25#include <utility>
26
27using namespace connectivity;
28using namespace utl;
29using namespace ::com::sun::star;
30
31namespace
32{
33 void lcl_convert(const uno::Sequence< OUString >& _aSource,uno::Any& _rDest)
34 {
35 uno::Sequence<uno::Any> aRet(_aSource.getLength());
36 uno::Any* pAny = aRet.getArray();
37 const OUString* pIter = _aSource.getConstArray();
38 const OUString* pEnd = pIter + _aSource.getLength();
39 for (;pIter != pEnd ; ++pIter,++pAny)
40 {
41 *pAny <<= *pIter;
42 }
43 _rDest <<= aRet;
44 }
45 void lcl_fillValues(const ::utl::OConfigurationNode& _aURLPatternNode,const OUString& _sNode,::comphelper::NamedValueCollection& _rValues)
46 {
47 const ::utl::OConfigurationNode aPropertiesNode = _aURLPatternNode.openNode(_sNode);
48 if ( !aPropertiesNode.isValid() )
49 return;
50
51 uno::Sequence< OUString > aStringSeq;
52 const uno::Sequence< OUString > aProperties = aPropertiesNode.getNodeNames();
53 const OUString* pPropertiesIter = aProperties.getConstArray();
54 const OUString* pPropertiesEnd = pPropertiesIter + aProperties.getLength();
55 for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter)
56 {
57 uno::Any aValue = aPropertiesNode.getNodeValue(*pPropertiesIter + "/Value");
58 if ( aValue >>= aStringSeq )
59 {
60 lcl_convert(aStringSeq,aValue);
61 }
62 _rValues.put(*pPropertiesIter,aValue);
63 } // for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter,++pNamedIter)
64 }
65 void lcl_readURLPatternNode(const ::utl::OConfigurationTreeRoot& _aInstalled,const OUString& _sEntry,TInstalledDriver& _rInstalledDriver)
66 {
67 const ::utl::OConfigurationNode aURLPatternNode = _aInstalled.openNode(_sEntry);
68 if ( !aURLPatternNode.isValid() )
69 return;
70
71 OUString sParentURLPattern;
72 aURLPatternNode.getNodeValue("ParentURLPattern") >>= sParentURLPattern;
73 if ( !sParentURLPattern.isEmpty() )
74 lcl_readURLPatternNode(_aInstalled,sParentURLPattern,_rInstalledDriver);
75
76 OUString sDriverFactory;
77 aURLPatternNode.getNodeValue("Driver") >>= sDriverFactory;
78 if ( !sDriverFactory.isEmpty() )
79 _rInstalledDriver.sDriverFactory = sDriverFactory;
80
81 OUString sDriverTypeDisplayName;
82 aURLPatternNode.getNodeValue("DriverTypeDisplayName") >>= sDriverTypeDisplayName;
83 OSL_ENSURE(!sDriverTypeDisplayName.isEmpty(),"No valid DriverTypeDisplayName property!");
84 if ( !sDriverTypeDisplayName.isEmpty() )
85 _rInstalledDriver.sDriverTypeDisplayName = sDriverTypeDisplayName;
86
87 lcl_fillValues(aURLPatternNode,"Properties",_rInstalledDriver.aProperties);
88 lcl_fillValues(aURLPatternNode,"Features",_rInstalledDriver.aFeatures);
89 lcl_fillValues(aURLPatternNode,"MetaData",_rInstalledDriver.aMetaData);
90 }
91}
92
93DriversConfigImpl::DriversConfigImpl()
94{
95}
96
97const TInstalledDrivers& DriversConfigImpl::getInstalledDrivers(const uno::Reference< uno::XComponentContext >& _rxORB) const
98{
99 if ( m_aDrivers.empty() )
100 {
101 if ( !m_aInstalled.isValid() )
102 {
104 "org.openoffice.Office.DataAccess.Drivers/Installed", -1, ::utl::OConfigurationTreeRoot::CM_READONLY);
105 }
106
107 if ( m_aInstalled.isValid() )
108 {
109 const uno::Sequence< OUString > aURLPatterns = m_aInstalled.getNodeNames();
110 const OUString* pPatternIter = aURLPatterns.getConstArray();
111 const OUString* pPatternEnd = pPatternIter + aURLPatterns.getLength();
112 for (;pPatternIter != pPatternEnd ; ++pPatternIter)
113 {
114 TInstalledDriver aInstalledDriver;
115 lcl_readURLPatternNode(m_aInstalled,*pPatternIter,aInstalledDriver);
116 if ( !aInstalledDriver.sDriverFactory.isEmpty() )
117 m_aDrivers.emplace(*pPatternIter,aInstalledDriver);
118 }
119 } // if ( m_aInstalled.isValid() )
120 }
121 return m_aDrivers;
122}
123
124DriversConfig::DriversConfig(uno::Reference< uno::XComponentContext > _xORB)
125:m_xORB(std::move(_xORB))
126{
127}
128
129
131{
132}
133
134
136{
137 *this = _rhs;
138}
139
140
142{
143 if ( this != &_rhs )
144 {
145 m_aNode = _rhs.m_aNode;
146 }
147 return *this;
148}
149
150
151OUString DriversConfig::getDriverFactoryName(std::u16string_view _sURL) const
152{
153#if ENABLE_FUZZERS
154 if (o3tl::starts_with(_sURL, u"sdbc:dbase:"))
155 return "com.sun.star.comp.sdbc.dbase.ODriver";
156#endif
157
158 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
159 OUString sRet;
160 OUString sOldPattern;
161 for(const auto& [rPattern, rDriver] : rDrivers)
162 {
163 WildCard aWildCard(rPattern);
164 if ( sOldPattern.getLength() < rPattern.getLength() && aWildCard.Matches(_sURL) )
165 {
166 sRet = rDriver.sDriverFactory;
167 sOldPattern = rPattern;
168 }
169 }
170
171 return sRet;
172}
173
174OUString DriversConfig::getDriverTypeDisplayName(std::u16string_view _sURL) const
175{
176 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
177 OUString sRet;
178 OUString sOldPattern;
179 for(const auto& [rPattern, rDriver] : rDrivers)
180 {
181 WildCard aWildCard(rPattern);
182 if ( sOldPattern.getLength() < rPattern.getLength() && aWildCard.Matches(_sURL) )
183 {
184 sRet = rDriver.sDriverTypeDisplayName;
185 sOldPattern = rPattern;
186 }
187 }
188
189 return sRet;
190}
191
192const ::comphelper::NamedValueCollection& DriversConfig::getProperties(std::u16string_view _sURL)
193 const
194{
195 return impl_get(_sURL,1);
196}
197
198const ::comphelper::NamedValueCollection& DriversConfig::getFeatures(std::u16string_view _sURL)
199 const
200{
201 return impl_get(_sURL,0);
202}
203
204const ::comphelper::NamedValueCollection& DriversConfig::getMetaData(std::u16string_view _sURL)
205 const
206{
207 return impl_get(_sURL,2);
208}
209
210const ::comphelper::NamedValueCollection& DriversConfig::impl_get(std::u16string_view _sURL,sal_Int32 _nProps) const
211{
212 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
213 const ::comphelper::NamedValueCollection* pRet = nullptr;
214 OUString sOldPattern;
215 for(const auto& [rPattern, rDriver] : rDrivers)
216 {
217 WildCard aWildCard(rPattern);
218 if ( sOldPattern.getLength() < rPattern.getLength() && aWildCard.Matches(_sURL) )
219 {
220 switch(_nProps)
221 {
222 case 0:
223 pRet = &rDriver.aFeatures;
224 break;
225 case 1:
226 pRet = &rDriver.aProperties;
227 break;
228 case 2:
229 pRet = &rDriver.aMetaData;
230 break;
231 }
232 sOldPattern = rPattern;
233 }
234 } // for(;aIter != aEnd;++aIter)
235 if ( pRet == nullptr )
236 {
237 static const ::comphelper::NamedValueCollection s_sEmpty;
238 pRet = &s_sEmpty;
239 }
240 return *pRet;
241}
242
243uno::Sequence< OUString > DriversConfig::getURLs() const
244{
245 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
246 return comphelper::mapKeysToSequence(rDrivers);
247}
248
249/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
bool Matches(std::u16string_view rStr) const
bool put(const OUString &_rValueName, const VALUE_TYPE &_rValue)
const TInstalledDrivers & getInstalledDrivers(const css::uno::Reference< css::uno::XComponentContext > &_rxORB) const
mutable::utl::OConfigurationTreeRoot m_aInstalled
OUString getDriverTypeDisplayName(std::u16string_view _sUrl) const
DriversConfig(css::uno::Reference< css::uno::XComponentContext > _xORB)
const ::comphelper::NamedValueCollection & getFeatures(std::u16string_view _sURL) const
css::uno::Sequence< OUString > getURLs() const
DriversConfig & operator=(const DriversConfig &)
OUString getDriverFactoryName(std::u16string_view _sUrl) const
const ::comphelper::NamedValueCollection & impl_get(std::u16string_view _sURL, sal_Int32 _nProps) const
const ::comphelper::NamedValueCollection & getProperties(std::u16string_view _sURL) const
const ::comphelper::NamedValueCollection & getMetaData(std::u16string_view _sURL) const
css::uno::Reference< css::uno::XComponentContext > m_xORB
static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
float u
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
std::map< OUString, TInstalledDriver > TInstalledDrivers
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
::comphelper::NamedValueCollection aMetaData
::comphelper::NamedValueCollection aProperties
::comphelper::NamedValueCollection aFeatures