1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
35 #include <rtl/uri.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <rtl/ustring.hxx>
38 #include <sal/types.h>
43 public cppu::WeakImplHelper
<
44 css::lang::XServiceInfo
, css::uri::XExternalUriReferenceTranslator
>
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
;
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" };
85 OUString
Translator::translateToInternal(
86 OUString
const & externalUriReference
)
88 if (!externalUriReference
.matchIgnoreAsciiCase("file:/"))
90 return externalUriReference
;
92 sal_Int32 i
= RTL_CONSTASCII_LENGTH("file:");
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
))
101 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
102 for (bool path
= true;;) {
104 while (j
!= externalUriReference
.getLength()
105 && externalUriReference
[j
] != '#'
106 && (!path
|| externalUriReference
[j
] != '/'))
114 externalUriReference
.copy(i
, j
- i
),
115 rtl_UriDecodeStrict
, encoding
),
116 rtl_UriCharClassPchar
, rtl_UriEncodeStrict
,
117 RTL_TEXTENCODING_UTF8
));
123 if (j
== externalUriReference
.getLength()) {
126 buf
.append(externalUriReference
[j
]);
127 path
= externalUriReference
[j
] == '/';
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://");
142 buf
.append(internalUriReference
.getStr(), i
);
143 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
144 for (bool path
= true;;) {
146 while (j
!= internalUriReference
.getLength()
147 && internalUriReference
[j
] != '#'
148 && (!path
|| internalUriReference
[j
] != '/'))
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:
159 internalUriReference
.copy(i
, j
- i
),
160 rtl_UriDecodeToIuri
, RTL_TEXTENCODING_UTF8
),
161 rtl_UriCharClassPchar
, rtl_UriEncodeStrictKeepEscapes
,
168 if (j
== internalUriReference
.getLength()) {
171 buf
.append(internalUriReference
[j
]);
172 path
= internalUriReference
[j
] == '/';
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: */