Coverage Report

Created: 2026-05-16 09:25

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