1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_stoc.hxx"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski #include "stocservices.hxx"
28*b1cdbd2cSJim Jagielski
29*b1cdbd2cSJim Jagielski #include "supportsService.hxx"
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielski #include "com/sun/star/lang/XServiceInfo.hpp"
32*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/Exception.hpp"
33*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/Reference.hxx"
34*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/RuntimeException.hpp"
35*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/Sequence.hxx"
36*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/XComponentContext.hpp"
37*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/XInterface.hpp"
38*b1cdbd2cSJim Jagielski #include "com/sun/star/uri/XExternalUriReferenceTranslator.hpp"
39*b1cdbd2cSJim Jagielski #include "cppuhelper/implbase2.hxx"
40*b1cdbd2cSJim Jagielski #include "cppuhelper/weak.hxx"
41*b1cdbd2cSJim Jagielski #include "osl/thread.h"
42*b1cdbd2cSJim Jagielski #include "rtl/string.h"
43*b1cdbd2cSJim Jagielski #include "rtl/textenc.h"
44*b1cdbd2cSJim Jagielski #include "rtl/uri.h"
45*b1cdbd2cSJim Jagielski #include "rtl/uri.hxx"
46*b1cdbd2cSJim Jagielski #include "rtl/ustrbuf.hxx"
47*b1cdbd2cSJim Jagielski #include "rtl/ustring.hxx"
48*b1cdbd2cSJim Jagielski #include "sal/types.h"
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski #include <new>
51*b1cdbd2cSJim Jagielski
52*b1cdbd2cSJim Jagielski namespace css = com::sun::star;
53*b1cdbd2cSJim Jagielski
54*b1cdbd2cSJim Jagielski namespace {
55*b1cdbd2cSJim Jagielski
56*b1cdbd2cSJim Jagielski class Translator: public cppu::WeakImplHelper2<
57*b1cdbd2cSJim Jagielski css::lang::XServiceInfo, css::uri::XExternalUriReferenceTranslator >
58*b1cdbd2cSJim Jagielski {
59*b1cdbd2cSJim Jagielski public:
Translator(css::uno::Reference<css::uno::XComponentContext> const & context)60*b1cdbd2cSJim Jagielski explicit Translator(
61*b1cdbd2cSJim Jagielski css::uno::Reference< css::uno::XComponentContext > const & context):
62*b1cdbd2cSJim Jagielski m_context(context) {}
63*b1cdbd2cSJim Jagielski
64*b1cdbd2cSJim Jagielski virtual rtl::OUString SAL_CALL getImplementationName()
65*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException);
66*b1cdbd2cSJim Jagielski
67*b1cdbd2cSJim Jagielski virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName)
68*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException);
69*b1cdbd2cSJim Jagielski
70*b1cdbd2cSJim Jagielski virtual css::uno::Sequence< rtl::OUString > SAL_CALL
71*b1cdbd2cSJim Jagielski getSupportedServiceNames() throw (css::uno::RuntimeException);
72*b1cdbd2cSJim Jagielski
73*b1cdbd2cSJim Jagielski virtual rtl::OUString SAL_CALL
74*b1cdbd2cSJim Jagielski translateToInternal(rtl::OUString const & externalUriReference)
75*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException);
76*b1cdbd2cSJim Jagielski
77*b1cdbd2cSJim Jagielski virtual rtl::OUString SAL_CALL
78*b1cdbd2cSJim Jagielski translateToExternal(rtl::OUString const & internalUriReference)
79*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException);
80*b1cdbd2cSJim Jagielski
81*b1cdbd2cSJim Jagielski private:
82*b1cdbd2cSJim Jagielski Translator(Translator &); // not implemented
83*b1cdbd2cSJim Jagielski void operator =(Translator); // not implemented
84*b1cdbd2cSJim Jagielski
~Translator()85*b1cdbd2cSJim Jagielski virtual ~Translator() {}
86*b1cdbd2cSJim Jagielski
87*b1cdbd2cSJim Jagielski css::uno::Reference< css::uno::XComponentContext > m_context;
88*b1cdbd2cSJim Jagielski };
89*b1cdbd2cSJim Jagielski
getImplementationName()90*b1cdbd2cSJim Jagielski rtl::OUString Translator::getImplementationName()
91*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException)
92*b1cdbd2cSJim Jagielski {
93*b1cdbd2cSJim Jagielski return
94*b1cdbd2cSJim Jagielski stoc_services::ExternalUriReferenceTranslator::getImplementationName();
95*b1cdbd2cSJim Jagielski }
96*b1cdbd2cSJim Jagielski
supportsService(rtl::OUString const & serviceName)97*b1cdbd2cSJim Jagielski sal_Bool Translator::supportsService(rtl::OUString const & serviceName)
98*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException)
99*b1cdbd2cSJim Jagielski {
100*b1cdbd2cSJim Jagielski return stoc::uriproc::supportsService(
101*b1cdbd2cSJim Jagielski getSupportedServiceNames(), serviceName);
102*b1cdbd2cSJim Jagielski }
103*b1cdbd2cSJim Jagielski
getSupportedServiceNames()104*b1cdbd2cSJim Jagielski css::uno::Sequence< rtl::OUString > Translator::getSupportedServiceNames()
105*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException)
106*b1cdbd2cSJim Jagielski {
107*b1cdbd2cSJim Jagielski return stoc_services::ExternalUriReferenceTranslator::
108*b1cdbd2cSJim Jagielski getSupportedServiceNames();
109*b1cdbd2cSJim Jagielski }
110*b1cdbd2cSJim Jagielski
translateToInternal(rtl::OUString const & externalUriReference)111*b1cdbd2cSJim Jagielski rtl::OUString Translator::translateToInternal(
112*b1cdbd2cSJim Jagielski rtl::OUString const & externalUriReference)
113*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException)
114*b1cdbd2cSJim Jagielski {
115*b1cdbd2cSJim Jagielski if (!externalUriReference.matchIgnoreAsciiCaseAsciiL(
116*b1cdbd2cSJim Jagielski RTL_CONSTASCII_STRINGPARAM("file:/")))
117*b1cdbd2cSJim Jagielski {
118*b1cdbd2cSJim Jagielski return externalUriReference;
119*b1cdbd2cSJim Jagielski }
120*b1cdbd2cSJim Jagielski sal_Int32 i = RTL_CONSTASCII_LENGTH("file:");
121*b1cdbd2cSJim Jagielski rtl::OUStringBuffer buf;
122*b1cdbd2cSJim Jagielski buf.append(externalUriReference.getStr(), i);
123*b1cdbd2cSJim Jagielski // Some environments (e.g., Java) produce illegal file URLs without an
124*b1cdbd2cSJim Jagielski // authority part; treat them as having an empty authority part:
125*b1cdbd2cSJim Jagielski if (!externalUriReference.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("//"), i))
126*b1cdbd2cSJim Jagielski {
127*b1cdbd2cSJim Jagielski buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("//"));
128*b1cdbd2cSJim Jagielski }
129*b1cdbd2cSJim Jagielski rtl_TextEncoding encoding = osl_getThreadTextEncoding();
130*b1cdbd2cSJim Jagielski for (bool path = true;;) {
131*b1cdbd2cSJim Jagielski sal_Int32 j = i;
132*b1cdbd2cSJim Jagielski while (j != externalUriReference.getLength()
133*b1cdbd2cSJim Jagielski && externalUriReference[j] != '#'
134*b1cdbd2cSJim Jagielski && (!path || externalUriReference[j] != '/'))
135*b1cdbd2cSJim Jagielski {
136*b1cdbd2cSJim Jagielski ++j;
137*b1cdbd2cSJim Jagielski }
138*b1cdbd2cSJim Jagielski if (j != i) {
139*b1cdbd2cSJim Jagielski rtl::OUString seg(
140*b1cdbd2cSJim Jagielski rtl::Uri::encode(
141*b1cdbd2cSJim Jagielski rtl::Uri::decode(
142*b1cdbd2cSJim Jagielski externalUriReference.copy(i, j - i),
143*b1cdbd2cSJim Jagielski rtl_UriDecodeStrict, encoding),
144*b1cdbd2cSJim Jagielski rtl_UriCharClassPchar, rtl_UriEncodeStrict,
145*b1cdbd2cSJim Jagielski RTL_TEXTENCODING_UTF8));
146*b1cdbd2cSJim Jagielski if (seg.getLength() == 0) {
147*b1cdbd2cSJim Jagielski return rtl::OUString();
148*b1cdbd2cSJim Jagielski }
149*b1cdbd2cSJim Jagielski buf.append(seg);
150*b1cdbd2cSJim Jagielski }
151*b1cdbd2cSJim Jagielski if (j == externalUriReference.getLength()) {
152*b1cdbd2cSJim Jagielski break;
153*b1cdbd2cSJim Jagielski }
154*b1cdbd2cSJim Jagielski buf.append(externalUriReference[j]);
155*b1cdbd2cSJim Jagielski path = externalUriReference[j] == '/';
156*b1cdbd2cSJim Jagielski i = j + 1;
157*b1cdbd2cSJim Jagielski }
158*b1cdbd2cSJim Jagielski return buf.makeStringAndClear();
159*b1cdbd2cSJim Jagielski }
160*b1cdbd2cSJim Jagielski
translateToExternal(rtl::OUString const & internalUriReference)161*b1cdbd2cSJim Jagielski rtl::OUString Translator::translateToExternal(
162*b1cdbd2cSJim Jagielski rtl::OUString const & internalUriReference)
163*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException)
164*b1cdbd2cSJim Jagielski {
165*b1cdbd2cSJim Jagielski if (!internalUriReference.matchIgnoreAsciiCaseAsciiL(
166*b1cdbd2cSJim Jagielski RTL_CONSTASCII_STRINGPARAM("file://")))
167*b1cdbd2cSJim Jagielski {
168*b1cdbd2cSJim Jagielski return internalUriReference;
169*b1cdbd2cSJim Jagielski }
170*b1cdbd2cSJim Jagielski sal_Int32 i = RTL_CONSTASCII_LENGTH("file://");
171*b1cdbd2cSJim Jagielski rtl::OUStringBuffer buf;
172*b1cdbd2cSJim Jagielski buf.append(internalUriReference.getStr(), i);
173*b1cdbd2cSJim Jagielski rtl_TextEncoding encoding = osl_getThreadTextEncoding();
174*b1cdbd2cSJim Jagielski for (bool path = true;;) {
175*b1cdbd2cSJim Jagielski sal_Int32 j = i;
176*b1cdbd2cSJim Jagielski while (j != internalUriReference.getLength()
177*b1cdbd2cSJim Jagielski && internalUriReference[j] != '#'
178*b1cdbd2cSJim Jagielski && (!path || internalUriReference[j] != '/'))
179*b1cdbd2cSJim Jagielski {
180*b1cdbd2cSJim Jagielski ++j;
181*b1cdbd2cSJim Jagielski }
182*b1cdbd2cSJim Jagielski if (j != i) {
183*b1cdbd2cSJim Jagielski // Use rtl_UriDecodeToIuri -> rtl_UriEncodeStrictKeepEscapes instead
184*b1cdbd2cSJim Jagielski // of rtl_UriDecodeStrict -> rtl_UriEncodeStrict, so that spurious
185*b1cdbd2cSJim Jagielski // non--UTF-8 octets like "%FE" are copied verbatim:
186*b1cdbd2cSJim Jagielski rtl::OUString seg(
187*b1cdbd2cSJim Jagielski rtl::Uri::encode(
188*b1cdbd2cSJim Jagielski rtl::Uri::decode(
189*b1cdbd2cSJim Jagielski internalUriReference.copy(i, j - i),
190*b1cdbd2cSJim Jagielski rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8),
191*b1cdbd2cSJim Jagielski rtl_UriCharClassPchar, rtl_UriEncodeStrictKeepEscapes,
192*b1cdbd2cSJim Jagielski encoding));
193*b1cdbd2cSJim Jagielski if (seg.getLength() == 0) {
194*b1cdbd2cSJim Jagielski return rtl::OUString();
195*b1cdbd2cSJim Jagielski }
196*b1cdbd2cSJim Jagielski buf.append(seg);
197*b1cdbd2cSJim Jagielski }
198*b1cdbd2cSJim Jagielski if (j == internalUriReference.getLength()) {
199*b1cdbd2cSJim Jagielski break;
200*b1cdbd2cSJim Jagielski }
201*b1cdbd2cSJim Jagielski buf.append(internalUriReference[j]);
202*b1cdbd2cSJim Jagielski path = internalUriReference[j] == '/';
203*b1cdbd2cSJim Jagielski i = j + 1;
204*b1cdbd2cSJim Jagielski }
205*b1cdbd2cSJim Jagielski return buf.makeStringAndClear();
206*b1cdbd2cSJim Jagielski }
207*b1cdbd2cSJim Jagielski
208*b1cdbd2cSJim Jagielski }
209*b1cdbd2cSJim Jagielski
210*b1cdbd2cSJim Jagielski namespace stoc_services { namespace ExternalUriReferenceTranslator {
211*b1cdbd2cSJim Jagielski
create(css::uno::Reference<css::uno::XComponentContext> const & context)212*b1cdbd2cSJim Jagielski css::uno::Reference< css::uno::XInterface > create(
213*b1cdbd2cSJim Jagielski css::uno::Reference< css::uno::XComponentContext > const & context)
214*b1cdbd2cSJim Jagielski SAL_THROW((css::uno::Exception))
215*b1cdbd2cSJim Jagielski {
216*b1cdbd2cSJim Jagielski try {
217*b1cdbd2cSJim Jagielski return static_cast< cppu::OWeakObject * >(new Translator(context));
218*b1cdbd2cSJim Jagielski } catch (std::bad_alloc &) {
219*b1cdbd2cSJim Jagielski throw css::uno::RuntimeException(
220*b1cdbd2cSJim Jagielski rtl::OUString::createFromAscii("std::bad_alloc"), 0);
221*b1cdbd2cSJim Jagielski }
222*b1cdbd2cSJim Jagielski }
223*b1cdbd2cSJim Jagielski
getImplementationName()224*b1cdbd2cSJim Jagielski rtl::OUString getImplementationName() {
225*b1cdbd2cSJim Jagielski return rtl::OUString::createFromAscii(
226*b1cdbd2cSJim Jagielski "com.sun.star.comp.uri.ExternalUriReferenceTranslator");
227*b1cdbd2cSJim Jagielski }
228*b1cdbd2cSJim Jagielski
getSupportedServiceNames()229*b1cdbd2cSJim Jagielski css::uno::Sequence< rtl::OUString > getSupportedServiceNames() {
230*b1cdbd2cSJim Jagielski css::uno::Sequence< rtl::OUString > s(1);
231*b1cdbd2cSJim Jagielski s[0] = rtl::OUString::createFromAscii(
232*b1cdbd2cSJim Jagielski "com.sun.star.uri.ExternalUriReferenceTranslator");
233*b1cdbd2cSJim Jagielski return s;
234*b1cdbd2cSJim Jagielski }
235*b1cdbd2cSJim Jagielski
236*b1cdbd2cSJim Jagielski } }
237