Line data Source code
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 <boost/noncopyable.hpp>
21 : #include <com/sun/star/lang/XServiceInfo.hpp>
22 : #include <com/sun/star/uno/Exception.hpp>
23 : #include <com/sun/star/uno/Reference.hxx>
24 : #include <com/sun/star/uno/RuntimeException.hpp>
25 : #include <com/sun/star/uno/Sequence.hxx>
26 : #include <com/sun/star/uno/XComponentContext.hpp>
27 : #include <com/sun/star/uno/XInterface.hpp>
28 : #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
29 : #include <cppuhelper/implbase2.hxx>
30 : #include <cppuhelper/supportsservice.hxx>
31 : #include <cppuhelper/weak.hxx>
32 : #include <osl/thread.h>
33 : #include <rtl/string.h>
34 : #include <rtl/textenc.h>
35 : #include <rtl/uri.h>
36 : #include <rtl/uri.hxx>
37 : #include <rtl/ustrbuf.hxx>
38 : #include <rtl/ustring.hxx>
39 : #include <sal/types.h>
40 :
41 : namespace {
42 :
43 : class Translator:
44 : public cppu::WeakImplHelper2<
45 : css::lang::XServiceInfo, css::uri::XExternalUriReferenceTranslator>,
46 : private boost::noncopyable
47 : {
48 : public:
49 66 : explicit Translator(
50 : css::uno::Reference< css::uno::XComponentContext > const & context):
51 66 : m_context(context) {}
52 :
53 : virtual OUString SAL_CALL getImplementationName()
54 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
55 :
56 : virtual sal_Bool SAL_CALL supportsService(OUString const & serviceName)
57 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 :
59 : virtual css::uno::Sequence< OUString > SAL_CALL
60 : getSupportedServiceNames() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
61 :
62 : virtual OUString SAL_CALL
63 : translateToInternal(OUString const & externalUriReference)
64 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 :
66 : virtual OUString SAL_CALL
67 : translateToExternal(OUString const & internalUriReference)
68 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
69 :
70 : private:
71 132 : virtual ~Translator() {}
72 :
73 : css::uno::Reference< css::uno::XComponentContext > m_context;
74 : };
75 :
76 1 : OUString Translator::getImplementationName()
77 : throw (css::uno::RuntimeException, std::exception)
78 : {
79 1 : return OUString("com.sun.star.comp.uri.ExternalUriReferenceTranslator");
80 : }
81 :
82 0 : sal_Bool Translator::supportsService(OUString const & serviceName)
83 : throw (css::uno::RuntimeException, std::exception)
84 : {
85 0 : return cppu::supportsService(this, serviceName);
86 : }
87 :
88 1 : css::uno::Sequence< OUString > Translator::getSupportedServiceNames()
89 : throw (css::uno::RuntimeException, std::exception)
90 : {
91 1 : css::uno::Sequence< OUString > s(1);
92 1 : s[0] = "com.sun.star.uri.ExternalUriReferenceTranslator";
93 1 : return s;
94 : }
95 :
96 65 : OUString Translator::translateToInternal(
97 : OUString const & externalUriReference)
98 : throw (css::uno::RuntimeException, std::exception)
99 : {
100 65 : if (!externalUriReference.matchIgnoreAsciiCase("file:/"))
101 : {
102 65 : return externalUriReference;
103 : }
104 0 : sal_Int32 i = RTL_CONSTASCII_LENGTH("file:");
105 0 : OUStringBuffer buf;
106 0 : buf.append(externalUriReference.getStr(), i);
107 : // Some environments (e.g., Java) produce illegal file URLs without an
108 : // authority part; treat them as having an empty authority part:
109 0 : if (!externalUriReference.match("//", i))
110 : {
111 0 : buf.append("//");
112 : }
113 0 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
114 0 : for (bool path = true;;) {
115 0 : sal_Int32 j = i;
116 0 : while (j != externalUriReference.getLength()
117 0 : && externalUriReference[j] != '#'
118 0 : && (!path || externalUriReference[j] != '/'))
119 : {
120 0 : ++j;
121 : }
122 0 : if (j != i) {
123 : OUString seg(
124 : rtl::Uri::encode(
125 : rtl::Uri::decode(
126 : externalUriReference.copy(i, j - i),
127 : rtl_UriDecodeStrict, encoding),
128 : rtl_UriCharClassPchar, rtl_UriEncodeStrict,
129 0 : RTL_TEXTENCODING_UTF8));
130 0 : if (seg.isEmpty()) {
131 0 : return OUString();
132 : }
133 0 : buf.append(seg);
134 : }
135 0 : if (j == externalUriReference.getLength()) {
136 0 : break;
137 : }
138 0 : buf.append(externalUriReference[j]);
139 0 : path = externalUriReference[j] == '/';
140 0 : i = j + 1;
141 0 : }
142 0 : return buf.makeStringAndClear();
143 : }
144 :
145 0 : OUString Translator::translateToExternal(
146 : OUString const & internalUriReference)
147 : throw (css::uno::RuntimeException, std::exception)
148 : {
149 0 : if (!internalUriReference.matchIgnoreAsciiCase("file://"))
150 : {
151 0 : return internalUriReference;
152 : }
153 0 : sal_Int32 i = RTL_CONSTASCII_LENGTH("file://");
154 0 : OUStringBuffer buf;
155 0 : buf.append(internalUriReference.getStr(), i);
156 0 : rtl_TextEncoding encoding = osl_getThreadTextEncoding();
157 0 : for (bool path = true;;) {
158 0 : sal_Int32 j = i;
159 0 : while (j != internalUriReference.getLength()
160 0 : && internalUriReference[j] != '#'
161 0 : && (!path || internalUriReference[j] != '/'))
162 : {
163 0 : ++j;
164 : }
165 0 : if (j != i) {
166 : // Use rtl_UriDecodeToIuri -> rtl_UriEncodeStrictKeepEscapes instead
167 : // of rtl_UriDecodeStrict -> rtl_UriEncodeStrict, so that spurious
168 : // non--UTF-8 octets like "%FE" are copied verbatim:
169 : OUString seg(
170 : rtl::Uri::encode(
171 : rtl::Uri::decode(
172 : internalUriReference.copy(i, j - i),
173 : rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8),
174 : rtl_UriCharClassPchar, rtl_UriEncodeStrictKeepEscapes,
175 0 : encoding));
176 0 : if (seg.isEmpty()) {
177 0 : return OUString();
178 : }
179 0 : buf.append(seg);
180 : }
181 0 : if (j == internalUriReference.getLength()) {
182 0 : break;
183 : }
184 0 : buf.append(internalUriReference[j]);
185 0 : path = internalUriReference[j] == '/';
186 0 : i = j + 1;
187 0 : }
188 0 : return buf.makeStringAndClear();
189 : }
190 :
191 : }
192 :
193 : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
194 66 : com_sun_star_comp_uri_ExternalUriReferenceTranslator_get_implementation(::com::sun::star::uno::XComponentContext* rxContext,
195 : ::com::sun::star::uno::Sequence<css::uno::Any> const &)
196 : {
197 66 : return ::cppu::acquire(new Translator(rxContext));
198 : }
199 :
200 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|