1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "rtl/uri.hxx"
31 #include "osl/mutex.hxx"
32 #include "cppuhelper/compbase2.hxx"
33 #include "cppuhelper/factory.hxx"
34 #include "cppuhelper/implementationentry.hxx"
35 #include "ucbhelper/content.hxx"
36 #include "com/sun/star/uno/XComponentContext.hpp"
37 #include "com/sun/star/lang/DisposedException.hpp"
38 #include "com/sun/star/lang/XServiceInfo.hpp"
39 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
40 #include "com/sun/star/registry/XRegistryKey.hpp"
41 #include "com/sun/star/util/XMacroExpander.hpp"
42 #include "com/sun/star/ucb/XContentProvider.hpp"
44 #define EXPAND_PROTOCOL "vnd.sun.star.expand"
45 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
46 #define ARLEN(x) sizeof (x) / sizeof *(x)
49 using namespace ::com::sun::star
;
50 using ::rtl::OUString
;
57 mutable ::osl::Mutex m_mutex
;
60 typedef ::cppu::WeakComponentImplHelper2
<
61 lang::XServiceInfo
, ucb::XContentProvider
> t_impl_helper
;
63 //==============================================================================
64 class ExpandContentProviderImpl
: protected MutexHolder
, public t_impl_helper
66 uno::Reference
< util::XMacroExpander
> m_xMacroExpander
;
68 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const;
71 inline void check() const;
72 virtual void SAL_CALL
disposing();
75 inline ExpandContentProviderImpl(
76 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
77 : t_impl_helper( m_mutex
),
79 xComponentContext
->getValueByName(
80 OUSTR("/singletons/com.sun.star.util.theMacroExpander") ),
81 uno::UNO_QUERY_THROW
)
83 virtual ~ExpandContentProviderImpl() throw ();
86 virtual OUString SAL_CALL
getImplementationName()
87 throw (uno::RuntimeException
);
88 virtual sal_Bool SAL_CALL
supportsService( OUString
const & serviceName
)
89 throw (uno::RuntimeException
);
90 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
91 throw (uno::RuntimeException
);
94 virtual uno::Reference
< ucb::XContent
> SAL_CALL
queryContent(
95 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
)
96 throw (ucb::IllegalIdentifierException
, uno::RuntimeException
);
97 virtual sal_Int32 SAL_CALL
compareContentIds(
98 uno::Reference
< ucb::XContentIdentifier
> const & xId1
,
99 uno::Reference
< ucb::XContentIdentifier
> const & xId2
)
100 throw (uno::RuntimeException
);
103 //______________________________________________________________________________
104 inline void ExpandContentProviderImpl::check() const
107 // MutexGuard guard( m_mutex );
108 if (rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
110 throw lang::DisposedException(
111 OUSTR("expand content provider instance has "
112 "already been disposed!"),
113 static_cast< OWeakObject
* >(
114 const_cast< ExpandContentProviderImpl
* >(this) ) );
118 //______________________________________________________________________________
119 ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
123 //______________________________________________________________________________
124 void ExpandContentProviderImpl::disposing()
128 //==============================================================================
129 static uno::Reference
< uno::XInterface
> SAL_CALL
create(
130 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
131 SAL_THROW( (uno::Exception
) )
133 return static_cast< ::cppu::OWeakObject
* >(
134 new ExpandContentProviderImpl( xComponentContext
) );
137 //==============================================================================
138 static OUString SAL_CALL
implName()
140 return OUSTR("com.sun.star.comp.ucb.ExpandContentProvider");
143 //==============================================================================
144 static uno::Sequence
< OUString
> SAL_CALL
supportedServices()
146 OUString names
[] = {
147 OUSTR("com.sun.star.ucb.ExpandContentProvider"),
148 OUSTR("com.sun.star.ucb.ContentProvider")
150 return uno::Sequence
< OUString
>( names
, ARLEN(names
) );
154 //______________________________________________________________________________
155 OUString
ExpandContentProviderImpl::getImplementationName()
156 throw (uno::RuntimeException
)
162 //______________________________________________________________________________
163 uno::Sequence
< OUString
> ExpandContentProviderImpl::getSupportedServiceNames()
164 throw (uno::RuntimeException
)
167 return supportedServices();
170 //______________________________________________________________________________
171 sal_Bool
ExpandContentProviderImpl::supportsService(
172 OUString
const & serviceName
)
173 throw (uno::RuntimeException
)
176 uno::Sequence
< OUString
> supported_services( getSupportedServiceNames() );
177 OUString
const * ar
= supported_services
.getConstArray();
178 for ( sal_Int32 pos
= supported_services
.getLength(); pos
--; )
180 if (ar
[ pos
].equals( serviceName
))
186 //______________________________________________________________________________
187 OUString
ExpandContentProviderImpl::expandUri(
188 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const
190 OUString
uri( xIdentifier
->getContentIdentifier() );
191 if (uri
.compareToAscii(
192 RTL_CONSTASCII_STRINGPARAM(EXPAND_PROTOCOL
":") ) != 0)
194 throw ucb::IllegalIdentifierException(
195 OUSTR("expected protocol " EXPAND_PROTOCOL
"!"),
196 static_cast< OWeakObject
* >(
197 const_cast< ExpandContentProviderImpl
* >(this) ) );
200 OUString
str( uri
.copy( sizeof (EXPAND_PROTOCOL
":") -1 ) );
201 // decode uric class chars
202 str
= ::rtl::Uri::decode(
203 str
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
204 // expand macro string
205 return m_xMacroExpander
->expandMacros( str
);
209 //______________________________________________________________________________
210 uno::Reference
< ucb::XContent
> ExpandContentProviderImpl::queryContent(
211 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
)
212 throw (ucb::IllegalIdentifierException
, uno::RuntimeException
)
215 OUString
uri( expandUri( xIdentifier
) );
217 ::ucbhelper::Content ucb_content
;
218 if (::ucbhelper::Content::create(
219 uri
, uno::Reference
< ucb::XCommandEnvironment
>(), ucb_content
))
221 return ucb_content
.get();
225 return uno::Reference
< ucb::XContent
>();
229 //______________________________________________________________________________
230 sal_Int32
ExpandContentProviderImpl::compareContentIds(
231 uno::Reference
< ucb::XContentIdentifier
> const & xId1
,
232 uno::Reference
< ucb::XContentIdentifier
> const & xId2
)
233 throw (uno::RuntimeException
)
238 OUString
uri1( expandUri( xId1
) );
239 OUString
uri2( expandUri( xId2
) );
240 return uri1
.compareTo( uri2
);
242 catch (const ucb::IllegalIdentifierException
& exc
)
244 (void) exc
; // unused
246 ::rtl::OUStringToOString(
247 exc
.Message
, RTL_TEXTENCODING_UTF8
).getStr() );
252 static const ::cppu::ImplementationEntry s_entries
[] =
258 ::cppu::createSingleComponentFactory
,
269 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(
270 const sal_Char
* pImplName
,
271 lang::XMultiServiceFactory
* pServiceManager
,
272 registry::XRegistryKey
* pRegistryKey
)
274 return ::cppu::component_getFactoryHelper(
275 pImplName
, pServiceManager
, pRegistryKey
, s_entries
);
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */