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