Bump version to 21.06.18.1
[LibreOffice.git] / stoc / source / uriproc / ExternalUriReferenceTranslator.cxx
blob9e21c5aef79fb575ced7cff1b1bc940a7f85fcbd
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 <string_view>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
27 #include <cppuhelper/implbase.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <cppuhelper/weak.hxx>
30 #include <osl/thread.h>
31 #include <rtl/string.h>
32 #include <rtl/textenc.h>
33 #include <rtl/uri.h>
34 #include <rtl/uri.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <rtl/ustring.hxx>
37 #include <sal/types.h>
39 namespace com::sun::star::uno { class XInterface; }
40 namespace com::sun::star::uno { class XComponentContext; }
42 namespace {
44 class Translator:
45 public cppu::WeakImplHelper<
46 css::lang::XServiceInfo, css::uri::XExternalUriReferenceTranslator>
48 public:
49 Translator() {}
51 Translator(const Translator&) = delete;
52 Translator& operator=(const Translator&) = delete;
54 virtual OUString SAL_CALL getImplementationName() override;
56 virtual sal_Bool SAL_CALL supportsService(OUString const & serviceName) override;
58 virtual css::uno::Sequence< OUString > SAL_CALL
59 getSupportedServiceNames() override;
61 virtual OUString SAL_CALL
62 translateToInternal(OUString const & externalUriReference) override;
64 virtual OUString SAL_CALL
65 translateToExternal(OUString const & internalUriReference) override;
67 private:
68 virtual ~Translator() override {}
71 OUString Translator::getImplementationName()
73 return "com.sun.star.comp.uri.ExternalUriReferenceTranslator";
76 sal_Bool Translator::supportsService(OUString const & serviceName)
78 return cppu::supportsService(this, serviceName);
81 css::uno::Sequence< OUString > Translator::getSupportedServiceNames()
83 css::uno::Sequence< OUString > s { "com.sun.star.uri.ExternalUriReferenceTranslator" };
84 return s;
87 OUString Translator::translateToInternal(
88 OUString const & externalUriReference)
90 if (!externalUriReference.matchIgnoreAsciiCase("file:/"))
92 return externalUriReference;
94 sal_Int32 i = RTL_CONSTASCII_LENGTH("file:");
95 OUStringBuffer buf(128);
96 buf.append(std::u16string_view(externalUriReference).substr(0, i));
97 // Some environments (e.g., Java) produce illegal file URLs without an
98 // authority part; treat them as having an empty authority part:
99 if (!externalUriReference.match("//", i))
101 buf.append("//");
103 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
104 for (bool path = true;;) {
105 sal_Int32 j = i;
106 while (j != externalUriReference.getLength()
107 && externalUriReference[j] != '#'
108 && (!path || externalUriReference[j] != '/'))
110 ++j;
112 if (j != i) {
113 OUString seg(
114 rtl::Uri::encode(
115 rtl::Uri::decode(
116 externalUriReference.copy(i, j - i),
117 rtl_UriDecodeStrict, encoding),
118 rtl_UriCharClassPchar, rtl_UriEncodeStrict,
119 RTL_TEXTENCODING_UTF8));
120 if (seg.isEmpty()) {
121 return OUString();
123 buf.append(seg);
125 if (j == externalUriReference.getLength()) {
126 break;
128 buf.append(externalUriReference[j]);
129 path = externalUriReference[j] == '/';
130 i = j + 1;
132 return buf.makeStringAndClear();
135 OUString Translator::translateToExternal(
136 OUString const & internalUriReference)
138 if (!internalUriReference.matchIgnoreAsciiCase("file://"))
140 return internalUriReference;
142 sal_Int32 i = RTL_CONSTASCII_LENGTH("file://");
143 OUStringBuffer buf(128);
144 buf.append(std::u16string_view(internalUriReference).substr(0, i));
145 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
146 for (bool path = true;;) {
147 sal_Int32 j = i;
148 while (j != internalUriReference.getLength()
149 && internalUriReference[j] != '#'
150 && (!path || internalUriReference[j] != '/'))
152 ++j;
154 if (j != i) {
155 // Use rtl_UriDecodeToIuri -> rtl_UriEncodeStrictKeepEscapes instead
156 // of rtl_UriDecodeStrict -> rtl_UriEncodeStrict, so that spurious
157 // non--UTF-8 octets like "%FE" are copied verbatim:
158 OUString seg(
159 rtl::Uri::encode(
160 rtl::Uri::decode(
161 internalUriReference.copy(i, j - i),
162 rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8),
163 rtl_UriCharClassPchar, rtl_UriEncodeStrictKeepEscapes,
164 encoding));
165 if (seg.isEmpty()) {
166 return OUString();
168 buf.append(seg);
170 if (j == internalUriReference.getLength()) {
171 break;
173 buf.append(internalUriReference[j]);
174 path = internalUriReference[j] == '/';
175 i = j + 1;
177 return buf.makeStringAndClear();
182 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
183 com_sun_star_comp_uri_ExternalUriReferenceTranslator_get_implementation(css::uno::XComponentContext* ,
184 css::uno::Sequence<css::uno::Any> const &)
186 return ::cppu::acquire(new Translator);
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */