Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / stoc / source / uriproc / ExternalUriReferenceTranslator.cxx
blobf618b4a7ae1c873354ceb95eb47ce0e00753e0a4
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 <com/sun/star/lang/XServiceInfo.hpp>
21 #include <com/sun/star/uno/Exception.hpp>
22 #include <com/sun/star/uno/Reference.hxx>
23 #include <com/sun/star/uno/RuntimeException.hpp>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/uno/XInterface.hpp>
27 #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
28 #include <cppuhelper/implbase.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <cppuhelper/weak.hxx>
31 #include <osl/thread.h>
32 #include <rtl/string.h>
33 #include <rtl/textenc.h>
34 #include <rtl/uri.h>
35 #include <rtl/uri.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <rtl/ustring.hxx>
38 #include <sal/types.h>
40 namespace {
42 class Translator:
43 public cppu::WeakImplHelper<
44 css::lang::XServiceInfo, css::uri::XExternalUriReferenceTranslator>
46 public:
47 Translator() {}
49 Translator(const Translator&) = delete;
50 Translator& operator=(const Translator&) = delete;
52 virtual OUString SAL_CALL getImplementationName() override;
54 virtual sal_Bool SAL_CALL supportsService(OUString const & serviceName) override;
56 virtual css::uno::Sequence< OUString > SAL_CALL
57 getSupportedServiceNames() override;
59 virtual OUString SAL_CALL
60 translateToInternal(OUString const & externalUriReference) override;
62 virtual OUString SAL_CALL
63 translateToExternal(OUString const & internalUriReference) override;
65 private:
66 virtual ~Translator() override {}
69 OUString Translator::getImplementationName()
71 return OUString("com.sun.star.comp.uri.ExternalUriReferenceTranslator");
74 sal_Bool Translator::supportsService(OUString const & serviceName)
76 return cppu::supportsService(this, serviceName);
79 css::uno::Sequence< OUString > Translator::getSupportedServiceNames()
81 css::uno::Sequence< OUString > s { "com.sun.star.uri.ExternalUriReferenceTranslator" };
82 return s;
85 OUString Translator::translateToInternal(
86 OUString const & externalUriReference)
88 if (!externalUriReference.matchIgnoreAsciiCase("file:/"))
90 return externalUriReference;
92 sal_Int32 i = RTL_CONSTASCII_LENGTH("file:");
93 OUStringBuffer buf;
94 buf.append(externalUriReference.getStr(), i);
95 // Some environments (e.g., Java) produce illegal file URLs without an
96 // authority part; treat them as having an empty authority part:
97 if (!externalUriReference.match("//", i))
99 buf.append("//");
101 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
102 for (bool path = true;;) {
103 sal_Int32 j = i;
104 while (j != externalUriReference.getLength()
105 && externalUriReference[j] != '#'
106 && (!path || externalUriReference[j] != '/'))
108 ++j;
110 if (j != i) {
111 OUString seg(
112 rtl::Uri::encode(
113 rtl::Uri::decode(
114 externalUriReference.copy(i, j - i),
115 rtl_UriDecodeStrict, encoding),
116 rtl_UriCharClassPchar, rtl_UriEncodeStrict,
117 RTL_TEXTENCODING_UTF8));
118 if (seg.isEmpty()) {
119 return OUString();
121 buf.append(seg);
123 if (j == externalUriReference.getLength()) {
124 break;
126 buf.append(externalUriReference[j]);
127 path = externalUriReference[j] == '/';
128 i = j + 1;
130 return buf.makeStringAndClear();
133 OUString Translator::translateToExternal(
134 OUString const & internalUriReference)
136 if (!internalUriReference.matchIgnoreAsciiCase("file://"))
138 return internalUriReference;
140 sal_Int32 i = RTL_CONSTASCII_LENGTH("file://");
141 OUStringBuffer buf;
142 buf.append(internalUriReference.getStr(), i);
143 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
144 for (bool path = true;;) {
145 sal_Int32 j = i;
146 while (j != internalUriReference.getLength()
147 && internalUriReference[j] != '#'
148 && (!path || internalUriReference[j] != '/'))
150 ++j;
152 if (j != i) {
153 // Use rtl_UriDecodeToIuri -> rtl_UriEncodeStrictKeepEscapes instead
154 // of rtl_UriDecodeStrict -> rtl_UriEncodeStrict, so that spurious
155 // non--UTF-8 octets like "%FE" are copied verbatim:
156 OUString seg(
157 rtl::Uri::encode(
158 rtl::Uri::decode(
159 internalUriReference.copy(i, j - i),
160 rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8),
161 rtl_UriCharClassPchar, rtl_UriEncodeStrictKeepEscapes,
162 encoding));
163 if (seg.isEmpty()) {
164 return OUString();
166 buf.append(seg);
168 if (j == internalUriReference.getLength()) {
169 break;
171 buf.append(internalUriReference[j]);
172 path = internalUriReference[j] == '/';
173 i = j + 1;
175 return buf.makeStringAndClear();
180 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* SAL_CALL
181 com_sun_star_comp_uri_ExternalUriReferenceTranslator_get_implementation(css::uno::XComponentContext* ,
182 css::uno::Sequence<css::uno::Any> const &)
184 return ::cppu::acquire(new Translator);
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */