LibreOffice Module stoc (master) 1
UriSchemeParser_vndDOTsunDOTstarDOTexpand.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
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>
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
40namespace com::sun::star::uno { class XComponentContext; }
41namespace com::sun::star::uno { class XInterface; }
42namespace com::sun::star::uri { class XUriReference; }
43
44namespace {
45
46bool parseSchemeSpecificPart(OUString const & part) {
47 // Liberally accepts both an empty opaque_part and an opaque_part that
48 // starts with a non-escaped "/":
49 return part.isEmpty()
50 || (!::rtl::Uri::decode(part, ::rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8).isEmpty());
51}
52
53class UrlReference:
54 public ::cppu::WeakImplHelper<css::uri::XVndSunStarExpandUrlReference>
55{
56public:
57 UrlReference(OUString const & scheme, OUString const & path):
58 base_(
59 scheme, false, OUString(), path, false,
60 OUString())
61 {}
62
63 UrlReference(const UrlReference&) = delete;
64 UrlReference& operator=(const UrlReference&) = delete;
65
66 virtual OUString SAL_CALL getUriReference() override
67 { return base_.getUriReference(); }
68
69 virtual sal_Bool SAL_CALL isAbsolute() override
70 { return base_.isAbsolute(); }
71
72 virtual OUString SAL_CALL getScheme() override
73 { return base_.getScheme(); }
74
75 virtual OUString SAL_CALL getSchemeSpecificPart() override
76 { return base_.getSchemeSpecificPart(); }
77
78 virtual sal_Bool SAL_CALL isHierarchical() override
79 { return base_.isHierarchical(); }
80
81 virtual sal_Bool SAL_CALL hasAuthority() override
82 { return base_.hasAuthority(); }
83
84 virtual OUString SAL_CALL getAuthority() override
85 { return base_.getAuthority(); }
86
87 virtual OUString SAL_CALL getPath() override
88 { return base_.getPath(); }
89
90 virtual sal_Bool SAL_CALL hasRelativePath() override
91 { return base_.hasRelativePath(); }
92
93 virtual ::sal_Int32 SAL_CALL getPathSegmentCount() override
94 { return base_.getPathSegmentCount(); }
95
96 virtual OUString SAL_CALL getPathSegment(sal_Int32 index) override
97 { return base_.getPathSegment(index); }
98
99 virtual sal_Bool SAL_CALL hasQuery() override
100 { return base_.hasQuery(); }
101
102 virtual OUString SAL_CALL getQuery() override
103 { return base_.getQuery(); }
104
105 virtual sal_Bool SAL_CALL hasFragment() override
106 { return base_.hasFragment(); }
107
108 virtual OUString SAL_CALL getFragment() override
109 { return base_.getFragment(); }
110
111 virtual void SAL_CALL setFragment(OUString const & fragment) override
112 { base_.setFragment(fragment); }
113
114 virtual void SAL_CALL clearFragment() override
115 { base_.clearFragment(); }
116
117 virtual OUString SAL_CALL expand(
118 css::uno::Reference< css::util::XMacroExpander > const & expander) override;
119
120private:
121 virtual ~UrlReference() override {}
122
124};
125
126OUString UrlReference::expand(
127 css::uno::Reference< css::util::XMacroExpander > const & expander)
128{
129 if (!expander.is()) {
130 throw css::uno::RuntimeException("null expander passed to XVndSunStarExpandUrl.expand");
131 }
132 return expander->expandMacros(
133 ::rtl::Uri::decode(
134 getPath(), ::rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8));
135}
136
137class Parser:
138 public ::cppu::WeakImplHelper<
139 css::lang::XServiceInfo, css::uri::XUriSchemeParser>
140{
141public:
142 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
160private:
161 virtual ~Parser() override {}
162};
163
164OUString Parser::getImplementationName()
165{
166 return "com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand";
167}
168
169sal_Bool Parser::supportsService(OUString const & serviceName)
170{
171 return cppu::supportsService(this, serviceName);
172}
173
174css::uno::Sequence< OUString > Parser::getSupportedServiceNames()
175{
176 return { "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand" };
177}
178
179css::uno::Reference< css::uri::XUriReference > Parser::parse(
180 OUString const & scheme, OUString const & schemeSpecificPart)
181{
182 if (!parseSchemeSpecificPart(schemeSpecificPart)) {
183 return css::uno::Reference< css::uri::XUriReference >();
184 }
185 return new UrlReference(scheme, schemeSpecificPart);
186}
187
188}
189
190extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
192 css::uno::Sequence<css::uno::Any> const &)
193{
194 //TODO: single instance
195 return ::cppu::acquire(new Parser());
196}
197
198/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_uri_UriSchemeParser_vndDOTsunDOTstarDOTexpand_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
bool parse(OUString const &uri, SourceProviderScannerData *data)
unsigned char sal_Bool