Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / stoc / source / uriproc / UriSchemeParser_vndDOTsunDOTstarDOTexpand.cxx
blobd246f07c9221b6a2043bce7fb63e5ca475a74d2c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <exception>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/uno/XComponentContext.hpp>
30 #include <com/sun/star/uno/XInterface.hpp>
31 #include <com/sun/star/uri/XUriReference.hpp>
32 #include <com/sun/star/uri/XUriSchemeParser.hpp>
33 #include <com/sun/star/uri/XVndSunStarExpandUrlReference.hpp>
34 #include <com/sun/star/util/XMacroExpander.hpp>
35 #include <cppuhelper/implbase.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <cppuhelper/weak.hxx>
38 #include <osl/diagnose.h>
39 #include <rtl/textenc.h>
40 #include <rtl/uri.h>
41 #include <rtl/uri.hxx>
42 #include <rtl/ustring.h>
43 #include <rtl/ustring.hxx>
44 #include <sal/types.h>
46 #include "UriReference.hxx"
48 namespace {
50 bool parseSchemeSpecificPart(OUString const & part) {
51 // Liberally accepts both an empty opaque_part and an opaque_part that
52 // starts with a non-escaped "/":
53 return part.isEmpty()
54 || (!::rtl::Uri::decode(part, ::rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8).isEmpty());
57 class UrlReference:
58 public ::cppu::WeakImplHelper<css::uri::XVndSunStarExpandUrlReference>
60 public:
61 UrlReference(OUString const & scheme, OUString const & path):
62 base_(
63 scheme, false, false, OUString(), path, false,
64 OUString())
67 UrlReference(const UrlReference&) = delete;
68 UrlReference& operator=(const UrlReference&) = delete;
70 virtual OUString SAL_CALL getUriReference() override
71 { return base_.getUriReference(); }
73 virtual sal_Bool SAL_CALL isAbsolute() override
74 { return base_.isAbsolute(); }
76 virtual OUString SAL_CALL getScheme() override
77 { return base_.getScheme(); }
79 virtual OUString SAL_CALL getSchemeSpecificPart() override
80 { return base_.getSchemeSpecificPart(); }
82 virtual sal_Bool SAL_CALL isHierarchical() override
83 { return base_.isHierarchical(); }
85 virtual sal_Bool SAL_CALL hasAuthority() override
86 { return base_.hasAuthority(); }
88 virtual OUString SAL_CALL getAuthority() override
89 { return base_.getAuthority(); }
91 virtual OUString SAL_CALL getPath() override
92 { return base_.getPath(); }
94 virtual sal_Bool SAL_CALL hasRelativePath() override
95 { return base_.hasRelativePath(); }
97 virtual ::sal_Int32 SAL_CALL getPathSegmentCount() override
98 { return base_.getPathSegmentCount(); }
100 virtual OUString SAL_CALL getPathSegment(sal_Int32 index) override
101 { return base_.getPathSegment(index); }
103 virtual sal_Bool SAL_CALL hasQuery() override
104 { return base_.hasQuery(); }
106 virtual OUString SAL_CALL getQuery() override
107 { return base_.getQuery(); }
109 virtual sal_Bool SAL_CALL hasFragment() override
110 { return base_.hasFragment(); }
112 virtual OUString SAL_CALL getFragment() override
113 { return base_.getFragment(); }
115 virtual void SAL_CALL setFragment(OUString const & fragment) override
116 { base_.setFragment(fragment); }
118 virtual void SAL_CALL clearFragment() override
119 { base_.clearFragment(); }
121 virtual OUString SAL_CALL expand(
122 css::uno::Reference< css::util::XMacroExpander > const & expander) override;
124 private:
125 virtual ~UrlReference() override {}
127 stoc::uriproc::UriReference base_;
130 OUString UrlReference::expand(
131 css::uno::Reference< css::util::XMacroExpander > const & expander)
133 OSL_ASSERT(expander.is());
134 return expander->expandMacros(
135 ::rtl::Uri::decode(
136 getPath(), ::rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8));
139 class Parser:
140 public ::cppu::WeakImplHelper<
141 css::lang::XServiceInfo, css::uri::XUriSchemeParser>
143 public:
144 Parser() {}
146 Parser(const Parser&) = delete;
147 Parser& operator=(const Parser&) = delete;
149 virtual OUString SAL_CALL getImplementationName() override;
151 virtual sal_Bool SAL_CALL supportsService(
152 OUString const & serviceName) override;
154 virtual css::uno::Sequence< OUString > SAL_CALL
155 getSupportedServiceNames() override;
157 virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
158 parse(
159 OUString const & scheme,
160 OUString const & schemeSpecificPart) override;
162 private:
163 virtual ~Parser() override {}
166 OUString Parser::getImplementationName()
168 return OUString("com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand");
171 sal_Bool Parser::supportsService(OUString const & serviceName)
173 return cppu::supportsService(this, serviceName);
176 css::uno::Sequence< OUString > Parser::getSupportedServiceNames()
178 css::uno::Sequence< OUString > s { "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand" };
179 return s;
182 css::uno::Reference< css::uri::XUriReference > Parser::parse(
183 OUString const & scheme, OUString const & schemeSpecificPart)
185 if (!parseSchemeSpecificPart(schemeSpecificPart)) {
186 return css::uno::Reference< css::uri::XUriReference >();
188 return new UrlReference(scheme, schemeSpecificPart);
193 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* SAL_CALL
194 com_sun_star_comp_uri_UriSchemeParser_vndDOTsunDOTstarDOTexpand_get_implementation(css::uno::XComponentContext*,
195 css::uno::Sequence<css::uno::Any> const &)
197 //TODO: single instance
198 return ::cppu::acquire(new Parser());
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */