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