1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ExternalUriReferenceTranslator.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
34 #include "stocservices.hxx"
36 #include "supportsService.hxx"
38 #include "com/sun/star/lang/XServiceInfo.hpp"
39 #include "com/sun/star/uno/Exception.hpp"
40 #include "com/sun/star/uno/Reference.hxx"
41 #include "com/sun/star/uno/RuntimeException.hpp"
42 #include "com/sun/star/uno/Sequence.hxx"
43 #include "com/sun/star/uno/XComponentContext.hpp"
44 #include "com/sun/star/uno/XInterface.hpp"
45 #include "com/sun/star/uri/XExternalUriReferenceTranslator.hpp"
46 #include "cppuhelper/implbase2.hxx"
47 #include "cppuhelper/weak.hxx"
48 #include "osl/thread.h"
49 #include "rtl/string.h"
50 #include "rtl/textenc.h"
52 #include "rtl/uri.hxx"
53 #include "rtl/ustrbuf.hxx"
54 #include "rtl/ustring.hxx"
55 #include "sal/types.h"
59 namespace css
= com::sun::star
;
63 class Translator
: public cppu::WeakImplHelper2
<
64 css::lang::XServiceInfo
, css::uri::XExternalUriReferenceTranslator
>
68 css::uno::Reference
< css::uno::XComponentContext
> const & context
):
71 virtual rtl::OUString SAL_CALL
getImplementationName()
72 throw (css::uno::RuntimeException
);
74 virtual sal_Bool SAL_CALL
supportsService(rtl::OUString
const & serviceName
)
75 throw (css::uno::RuntimeException
);
77 virtual css::uno::Sequence
< rtl::OUString
> SAL_CALL
78 getSupportedServiceNames() throw (css::uno::RuntimeException
);
80 virtual rtl::OUString SAL_CALL
81 translateToInternal(rtl::OUString
const & externalUriReference
)
82 throw (css::uno::RuntimeException
);
84 virtual rtl::OUString SAL_CALL
85 translateToExternal(rtl::OUString
const & internalUriReference
)
86 throw (css::uno::RuntimeException
);
89 Translator(Translator
&); // not implemented
90 void operator =(Translator
); // not implemented
92 virtual ~Translator() {}
94 css::uno::Reference
< css::uno::XComponentContext
> m_context
;
97 rtl::OUString
Translator::getImplementationName()
98 throw (css::uno::RuntimeException
)
101 stoc_services::ExternalUriReferenceTranslator::getImplementationName();
104 sal_Bool
Translator::supportsService(rtl::OUString
const & serviceName
)
105 throw (css::uno::RuntimeException
)
107 return stoc::uriproc::supportsService(
108 getSupportedServiceNames(), serviceName
);
111 css::uno::Sequence
< rtl::OUString
> Translator::getSupportedServiceNames()
112 throw (css::uno::RuntimeException
)
114 return stoc_services::ExternalUriReferenceTranslator::
115 getSupportedServiceNames();
118 rtl::OUString
Translator::translateToInternal(
119 rtl::OUString
const & externalUriReference
)
120 throw (css::uno::RuntimeException
)
122 if (!externalUriReference
.matchIgnoreAsciiCaseAsciiL(
123 RTL_CONSTASCII_STRINGPARAM("file:/")))
125 return externalUriReference
;
127 sal_Int32 i
= RTL_CONSTASCII_LENGTH("file:");
128 rtl::OUStringBuffer buf
;
129 buf
.append(externalUriReference
.getStr(), i
);
130 // Some environments (e.g., Java) produce illegal file URLs without an
131 // authority part; treat them as having an empty authority part:
132 if (!externalUriReference
.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("//"), i
))
134 buf
.appendAscii(RTL_CONSTASCII_STRINGPARAM("//"));
136 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
137 for (bool path
= true;;) {
139 while (j
!= externalUriReference
.getLength()
140 && externalUriReference
[j
] != '#'
141 && (!path
|| externalUriReference
[j
] != '/'))
149 externalUriReference
.copy(i
, j
- i
),
150 rtl_UriDecodeStrict
, encoding
),
151 rtl_UriCharClassPchar
, rtl_UriEncodeStrict
,
152 RTL_TEXTENCODING_UTF8
));
153 if (seg
.getLength() == 0) {
154 return rtl::OUString();
158 if (j
== externalUriReference
.getLength()) {
161 buf
.append(externalUriReference
[j
]);
162 path
= externalUriReference
[j
] == '/';
165 return buf
.makeStringAndClear();
168 rtl::OUString
Translator::translateToExternal(
169 rtl::OUString
const & internalUriReference
)
170 throw (css::uno::RuntimeException
)
172 if (!internalUriReference
.matchIgnoreAsciiCaseAsciiL(
173 RTL_CONSTASCII_STRINGPARAM("file://")))
175 return internalUriReference
;
177 sal_Int32 i
= RTL_CONSTASCII_LENGTH("file://");
178 rtl::OUStringBuffer buf
;
179 buf
.append(internalUriReference
.getStr(), i
);
180 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
181 for (bool path
= true;;) {
183 while (j
!= internalUriReference
.getLength()
184 && internalUriReference
[j
] != '#'
185 && (!path
|| internalUriReference
[j
] != '/'))
190 // Use rtl_UriDecodeToIuri -> rtl_UriEncodeStrictKeepEscapes instead
191 // of rtl_UriDecodeStrict -> rtl_UriEncodeStrict, so that spurious
192 // non--UTF-8 octets like "%FE" are copied verbatim:
196 internalUriReference
.copy(i
, j
- i
),
197 rtl_UriDecodeToIuri
, RTL_TEXTENCODING_UTF8
),
198 rtl_UriCharClassPchar
, rtl_UriEncodeStrictKeepEscapes
,
200 if (seg
.getLength() == 0) {
201 return rtl::OUString();
205 if (j
== internalUriReference
.getLength()) {
208 buf
.append(internalUriReference
[j
]);
209 path
= internalUriReference
[j
] == '/';
212 return buf
.makeStringAndClear();
217 namespace stoc_services
{ namespace ExternalUriReferenceTranslator
{
219 css::uno::Reference
< css::uno::XInterface
> create(
220 css::uno::Reference
< css::uno::XComponentContext
> const & context
)
221 SAL_THROW((css::uno::Exception
))
224 return static_cast< cppu::OWeakObject
* >(new Translator(context
));
225 } catch (std::bad_alloc
&) {
226 throw css::uno::RuntimeException(
227 rtl::OUString::createFromAscii("std::bad_alloc"), 0);
231 rtl::OUString
getImplementationName() {
232 return rtl::OUString::createFromAscii(
233 "com.sun.star.comp.uri.ExternalUriReferenceTranslator");
236 css::uno::Sequence
< rtl::OUString
> getSupportedServiceNames() {
237 css::uno::Sequence
< rtl::OUString
> s(1);
238 s
[0] = rtl::OUString::createFromAscii(
239 "com.sun.star.uri.ExternalUriReferenceTranslator");