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