1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
34 #include <rtl/uri.hxx>
35 #include <rtl/ustring.hxx>
36 #include <sal/types.h>
38 #include "UriReference.hxx"
40 namespace com::sun::star::uno
{ class XComponentContext
; }
41 namespace com::sun::star::uno
{ class XInterface
; }
42 namespace com::sun::star::uri
{ class XUriReference
; }
46 bool parseSchemeSpecificPart(OUString
const & part
) {
47 // Liberally accepts both an empty opaque_part and an opaque_part that
48 // starts with a non-escaped "/":
50 || (!::rtl::Uri::decode(part
, ::rtl_UriDecodeStrict
, RTL_TEXTENCODING_UTF8
).isEmpty());
54 public ::cppu::WeakImplHelper
<css::uri::XVndSunStarExpandUrlReference
>
57 UrlReference(OUString
const & scheme
, OUString
const & path
):
59 scheme
, false, OUString(), path
, false,
63 UrlReference(const UrlReference
&) = delete;
64 UrlReference
& operator=(const UrlReference
&) = delete;
66 virtual OUString SAL_CALL
getUriReference() override
67 { return base_
.getUriReference(); }
69 virtual sal_Bool SAL_CALL
isAbsolute() override
70 { return base_
.isAbsolute(); }
72 virtual OUString SAL_CALL
getScheme() override
73 { return base_
.getScheme(); }
75 virtual OUString SAL_CALL
getSchemeSpecificPart() override
76 { return base_
.getSchemeSpecificPart(); }
78 virtual sal_Bool SAL_CALL
isHierarchical() override
79 { return base_
.isHierarchical(); }
81 virtual sal_Bool SAL_CALL
hasAuthority() override
82 { return base_
.hasAuthority(); }
84 virtual OUString SAL_CALL
getAuthority() override
85 { return base_
.getAuthority(); }
87 virtual OUString SAL_CALL
getPath() override
88 { return base_
.getPath(); }
90 virtual sal_Bool SAL_CALL
hasRelativePath() override
91 { return base_
.hasRelativePath(); }
93 virtual ::sal_Int32 SAL_CALL
getPathSegmentCount() override
94 { return base_
.getPathSegmentCount(); }
96 virtual OUString SAL_CALL
getPathSegment(sal_Int32 index
) override
97 { return base_
.getPathSegment(index
); }
99 virtual sal_Bool SAL_CALL
hasQuery() override
100 { return base_
.hasQuery(); }
102 virtual OUString SAL_CALL
getQuery() override
103 { return base_
.getQuery(); }
105 virtual sal_Bool SAL_CALL
hasFragment() override
106 { return base_
.hasFragment(); }
108 virtual OUString SAL_CALL
getFragment() override
109 { return base_
.getFragment(); }
111 virtual void SAL_CALL
setFragment(OUString
const & fragment
) override
112 { base_
.setFragment(fragment
); }
114 virtual void SAL_CALL
clearFragment() override
115 { base_
.clearFragment(); }
117 virtual OUString SAL_CALL
expand(
118 css::uno::Reference
< css::util::XMacroExpander
> const & expander
) override
;
121 virtual ~UrlReference() override
{}
123 stoc::uriproc::UriReference base_
;
126 OUString
UrlReference::expand(
127 css::uno::Reference
< css::util::XMacroExpander
> const & expander
)
129 if (!expander
.is()) {
130 throw css::uno::RuntimeException(u
"null expander passed to XVndSunStarExpandUrl.expand"_ustr
);
132 return expander
->expandMacros(
134 getPath(), ::rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
));
138 public ::cppu::WeakImplHelper
<
139 css::lang::XServiceInfo
, css::uri::XUriSchemeParser
>
144 Parser(const Parser
&) = delete;
145 Parser
& operator=(const Parser
&) = delete;
147 virtual OUString SAL_CALL
getImplementationName() override
;
149 virtual sal_Bool SAL_CALL
supportsService(
150 OUString
const & serviceName
) override
;
152 virtual css::uno::Sequence
< OUString
> SAL_CALL
153 getSupportedServiceNames() override
;
155 virtual css::uno::Reference
< css::uri::XUriReference
> SAL_CALL
157 OUString
const & scheme
,
158 OUString
const & schemeSpecificPart
) override
;
161 virtual ~Parser() override
{}
164 OUString
Parser::getImplementationName()
166 return u
"com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"_ustr
;
169 sal_Bool
Parser::supportsService(OUString
const & serviceName
)
171 return cppu::supportsService(this, serviceName
);
174 css::uno::Sequence
< OUString
> Parser::getSupportedServiceNames()
176 return { u
"com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"_ustr
};
179 css::uno::Reference
< css::uri::XUriReference
> Parser::parse(
180 OUString
const & scheme
, OUString
const & schemeSpecificPart
)
182 if (!parseSchemeSpecificPart(schemeSpecificPart
)) {
183 return css::uno::Reference
< css::uri::XUriReference
>();
185 return new UrlReference(scheme
, schemeSpecificPart
);
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 &)
194 //TODO: single instance
195 return ::cppu::acquire(new Parser());
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */