LibreOffice Module stoc (master) 1
ExternalUriReferenceTranslator.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/Sequence.hxx>
24#include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
27#include <cppuhelper/weak.hxx>
28#include <osl/thread.h>
29#include <rtl/string.h>
30#include <rtl/textenc.h>
31#include <rtl/uri.h>
32#include <rtl/uri.hxx>
33#include <rtl/ustrbuf.hxx>
34#include <rtl/ustring.hxx>
35#include <sal/types.h>
36
37namespace com::sun::star::uno { class XInterface; }
38namespace com::sun::star::uno { class XComponentContext; }
39
40namespace {
41
42class Translator:
43 public cppu::WeakImplHelper<
44 css::lang::XServiceInfo, css::uri::XExternalUriReferenceTranslator>
45{
46public:
47 Translator() {}
48
49 Translator(const Translator&) = delete;
50 Translator& operator=(const Translator&) = delete;
51
52 virtual OUString SAL_CALL getImplementationName() override;
53
54 virtual sal_Bool SAL_CALL supportsService(OUString const & serviceName) override;
55
56 virtual css::uno::Sequence< OUString > SAL_CALL
57 getSupportedServiceNames() override;
58
59 virtual OUString SAL_CALL
60 translateToInternal(OUString const & externalUriReference) override;
61
62 virtual OUString SAL_CALL
63 translateToExternal(OUString const & internalUriReference) override;
64
65private:
66 virtual ~Translator() override {}
67};
68
69OUString Translator::getImplementationName()
70{
71 return "com.sun.star.comp.uri.ExternalUriReferenceTranslator";
72}
73
74sal_Bool Translator::supportsService(OUString const & serviceName)
75{
76 return cppu::supportsService(this, serviceName);
77}
78
79css::uno::Sequence< OUString > Translator::getSupportedServiceNames()
80{
81 css::uno::Sequence< OUString > s { "com.sun.star.uri.ExternalUriReferenceTranslator" };
82 return s;
83}
84
85OUString Translator::translateToInternal(
86 OUString const & externalUriReference)
87{
88 if (!externalUriReference.matchIgnoreAsciiCase("file:/"))
89 {
90 return externalUriReference;
91 }
92 sal_Int32 i = RTL_CONSTASCII_LENGTH("file:");
93 OUStringBuffer buf(128);
94 buf.append(externalUriReference.subView(0, 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))
98 {
99 buf.append("//");
100 }
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] != '/'))
107 {
108 ++j;
109 }
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();
120 }
121 buf.append(seg);
122 }
123 if (j == externalUriReference.getLength()) {
124 break;
125 }
126 buf.append(externalUriReference[j]);
127 path = externalUriReference[j] == '/';
128 i = j + 1;
129 }
130 return buf.makeStringAndClear();
131}
132
133OUString Translator::translateToExternal(
134 OUString const & internalUriReference)
135{
136 if (!internalUriReference.matchIgnoreAsciiCase("file://"))
137 {
138 return internalUriReference;
139 }
140 sal_Int32 i = RTL_CONSTASCII_LENGTH("file://");
141 OUStringBuffer buf(128);
142 buf.append(internalUriReference.subView(0, 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] != '/'))
149 {
150 ++j;
151 }
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();
165 }
166 buf.append(seg);
167 }
168 if (j == internalUriReference.getLength()) {
169 break;
170 }
171 buf.append(internalUriReference[j]);
172 path = internalUriReference[j] == '/';
173 i = j + 1;
174 }
175 return buf.makeStringAndClear();
176}
177
178}
179
180extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
182 css::uno::Sequence<css::uno::Any> const &)
183{
184 return ::cppu::acquire(new Translator);
185}
186
187/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_uri_ExternalUriReferenceTranslator_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Sequence< Any >(* Translator)(const Sequence< Any > &)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
unsigned char sal_Bool