Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/stoc/source/uriproc/UriSchemeParser_vndDOTsunDOTstarDOTexpand.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
20
#include <sal/config.h>
21
22
#include <com/sun/star/lang/XServiceInfo.hpp>
23
#include <com/sun/star/uno/Reference.hxx>
24
#include <com/sun/star/uno/RuntimeException.hpp>
25
#include <com/sun/star/uno/Sequence.hxx>
26
#include <com/sun/star/uri/XUriSchemeParser.hpp>
27
#include <com/sun/star/uri/XVndSunStarExpandUrlReference.hpp>
28
#include <com/sun/star/util/XMacroExpander.hpp>
29
#include <cppuhelper/implbase.hxx>
30
#include <cppuhelper/supportsservice.hxx>
31
#include <cppuhelper/weak.hxx>
32
#include <rtl/textenc.h>
33
#include <rtl/uri.h>
34
#include <rtl/uri.hxx>
35
#include <rtl/ustring.hxx>
36
#include <sal/types.h>
37
38
#include "UriReference.hxx"
39
40
namespace com::sun::star::uno { class XComponentContext; }
41
namespace com::sun::star::uno { class XInterface; }
42
namespace com::sun::star::uri { class XUriReference; }
43
44
namespace {
45
46
0
bool parseSchemeSpecificPart(OUString const & part) {
47
    // Liberally accepts both an empty opaque_part and an opaque_part that
48
    // starts with a non-escaped "/":
49
0
    return part.isEmpty()
50
0
        || (!::rtl::Uri::decode(part, ::rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8).isEmpty());
51
0
}
52
53
class UrlReference:
54
    public ::cppu::WeakImplHelper<css::uri::XVndSunStarExpandUrlReference>
55
{
56
public:
57
    UrlReference(OUString const & scheme, OUString const & path):
58
0
        base_(
59
0
            scheme, false, OUString(), path, false,
60
0
            OUString())
61
0
    {}
62
63
    UrlReference(const UrlReference&) = delete;
64
    UrlReference& operator=(const UrlReference&) = delete;
65
66
    virtual OUString SAL_CALL getUriReference() override
67
0
    { return base_.getUriReference(); }
68
69
    virtual sal_Bool SAL_CALL isAbsolute() override
70
0
    { return base_.isAbsolute(); }
71
72
    virtual OUString SAL_CALL getScheme() override
73
0
    { return base_.getScheme(); }
74
75
    virtual OUString SAL_CALL getSchemeSpecificPart() override
76
0
    { return base_.getSchemeSpecificPart(); }
77
78
    virtual sal_Bool SAL_CALL isHierarchical() override
79
0
    { return base_.isHierarchical(); }
80
81
    virtual sal_Bool SAL_CALL hasAuthority() override
82
0
    { return base_.hasAuthority(); }
83
84
    virtual OUString SAL_CALL getAuthority() override
85
0
    { return base_.getAuthority(); }
86
87
    virtual OUString SAL_CALL getPath() override
88
0
    { return base_.getPath(); }
89
90
    virtual sal_Bool SAL_CALL hasRelativePath() override
91
0
    { return base_.hasRelativePath(); }
92
93
    virtual ::sal_Int32 SAL_CALL getPathSegmentCount() override
94
0
    { return base_.getPathSegmentCount(); }
95
96
    virtual OUString SAL_CALL getPathSegment(sal_Int32 index) override
97
0
    { return base_.getPathSegment(index); }
98
99
    virtual sal_Bool SAL_CALL hasQuery() override
100
0
    { return base_.hasQuery(); }
101
102
    virtual OUString SAL_CALL getQuery() override
103
0
    { return base_.getQuery(); }
104
105
    virtual sal_Bool SAL_CALL hasFragment() override
106
0
    { return base_.hasFragment(); }
107
108
    virtual OUString SAL_CALL getFragment() override
109
0
    { return base_.getFragment(); }
110
111
    virtual void SAL_CALL setFragment(OUString const & fragment) override
112
0
    { base_.setFragment(fragment); }
113
114
    virtual void SAL_CALL clearFragment() override
115
0
    { base_.clearFragment(); }
116
117
    virtual OUString SAL_CALL expand(
118
        css::uno::Reference< css::util::XMacroExpander > const & expander) override;
119
120
private:
121
0
    virtual ~UrlReference() override {}
122
123
    stoc::uriproc::UriReference base_;
124
};
125
126
OUString UrlReference::expand(
127
    css::uno::Reference< css::util::XMacroExpander > const & expander)
128
0
{
129
0
    if (!expander.is()) {
130
0
        throw css::uno::RuntimeException(u"null expander passed to XVndSunStarExpandUrl.expand"_ustr);
131
0
    }
132
0
    return expander->expandMacros(
133
0
        ::rtl::Uri::decode(
134
0
            getPath(), ::rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8));
135
0
}
136
137
class Parser:
138
    public ::cppu::WeakImplHelper<
139
        css::lang::XServiceInfo, css::uri::XUriSchemeParser>
140
{
141
public:
142
0
    Parser() {}
143
144
    Parser(const Parser&) = delete;
145
    Parser& operator=(const Parser&) = delete;
146
147
    virtual OUString SAL_CALL getImplementationName() override;
148
149
    virtual sal_Bool SAL_CALL supportsService(
150
        OUString const & serviceName) override;
151
152
    virtual css::uno::Sequence< OUString > SAL_CALL
153
    getSupportedServiceNames() override;
154
155
    virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
156
    parse(
157
        OUString const & scheme,
158
        OUString const & schemeSpecificPart) override;
159
160
private:
161
0
    virtual ~Parser() override {}
162
};
163
164
OUString Parser::getImplementationName()
165
0
{
166
0
    return u"com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"_ustr;
167
0
}
168
169
sal_Bool Parser::supportsService(OUString const & serviceName)
170
0
{
171
0
    return cppu::supportsService(this, serviceName);
172
0
}
173
174
css::uno::Sequence< OUString > Parser::getSupportedServiceNames()
175
0
{
176
0
    return { u"com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"_ustr };
177
0
}
178
179
css::uno::Reference< css::uri::XUriReference > Parser::parse(
180
    OUString const & scheme, OUString const & schemeSpecificPart)
181
0
{
182
0
    if (!parseSchemeSpecificPart(schemeSpecificPart)) {
183
0
        return css::uno::Reference< css::uri::XUriReference >();
184
0
    }
185
0
    return new UrlReference(scheme, schemeSpecificPart);
186
0
}
187
188
}
189
190
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
191
com_sun_star_comp_uri_UriSchemeParser_vndDOTsunDOTstarDOTexpand_get_implementation(css::uno::XComponentContext*,
192
        css::uno::Sequence<css::uno::Any> const &)
193
0
{
194
    //TODO: single instance
195
0
    return ::cppu::acquire(new Parser());
196
0
}
197
198
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */