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: ucpexpand.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_ucb.hxx"
34 #include "rtl/uri.hxx"
35 #include "osl/mutex.hxx"
36 #include "cppuhelper/compbase2.hxx"
37 #include "cppuhelper/factory.hxx"
38 #include "cppuhelper/implementationentry.hxx"
39 #include "ucbhelper/content.hxx"
40 #include "com/sun/star/uno/XComponentContext.hpp"
41 #include "com/sun/star/lang/DisposedException.hpp"
42 #include "com/sun/star/lang/XServiceInfo.hpp"
43 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
44 #include "com/sun/star/registry/XRegistryKey.hpp"
45 #include "com/sun/star/util/XMacroExpander.hpp"
46 #include "com/sun/star/ucb/XContentProvider.hpp"
48 #define EXPAND_PROTOCOL "vnd.sun.star.expand"
49 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
50 #define ARLEN(x) sizeof (x) / sizeof *(x)
53 using namespace ::com::sun::star
;
54 using ::rtl::OUString
;
61 mutable ::osl::Mutex m_mutex
;
64 typedef ::cppu::WeakComponentImplHelper2
<
65 lang::XServiceInfo
, ucb::XContentProvider
> t_impl_helper
;
67 //==============================================================================
68 class ExpandContentProviderImpl
: protected MutexHolder
, public t_impl_helper
70 uno::Reference
< util::XMacroExpander
> m_xMacroExpander
;
72 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const;
75 inline void check() const;
76 virtual void SAL_CALL
disposing();
79 inline ExpandContentProviderImpl(
80 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
81 : t_impl_helper( m_mutex
),
83 xComponentContext
->getValueByName(
84 OUSTR("/singletons/com.sun.star.util.theMacroExpander") ),
85 uno::UNO_QUERY_THROW
)
87 virtual ~ExpandContentProviderImpl() throw ();
90 virtual OUString SAL_CALL
getImplementationName()
91 throw (uno::RuntimeException
);
92 virtual sal_Bool SAL_CALL
supportsService( OUString
const & serviceName
)
93 throw (uno::RuntimeException
);
94 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
95 throw (uno::RuntimeException
);
98 virtual uno::Reference
< ucb::XContent
> SAL_CALL
queryContent(
99 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
)
100 throw (ucb::IllegalIdentifierException
, uno::RuntimeException
);
101 virtual sal_Int32 SAL_CALL
compareContentIds(
102 uno::Reference
< ucb::XContentIdentifier
> const & xId1
,
103 uno::Reference
< ucb::XContentIdentifier
> const & xId2
)
104 throw (uno::RuntimeException
);
107 //______________________________________________________________________________
108 inline void ExpandContentProviderImpl::check() const
111 // MutexGuard guard( m_mutex );
112 if (rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
114 throw lang::DisposedException(
115 OUSTR("expand content provider instance has "
116 "already been disposed!"),
117 static_cast< OWeakObject
* >(
118 const_cast< ExpandContentProviderImpl
* >(this) ) );
122 //______________________________________________________________________________
123 ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
127 //______________________________________________________________________________
128 void ExpandContentProviderImpl::disposing()
132 //==============================================================================
133 static uno::Reference
< uno::XInterface
> SAL_CALL
create(
134 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
135 SAL_THROW( (uno::Exception
) )
137 return static_cast< ::cppu::OWeakObject
* >(
138 new ExpandContentProviderImpl( xComponentContext
) );
141 //==============================================================================
142 static OUString SAL_CALL
implName()
144 return OUSTR("com.sun.star.comp.ucb.ExpandContentProvider");
147 //==============================================================================
148 static uno::Sequence
< OUString
> SAL_CALL
supportedServices()
150 OUString names
[] = {
151 OUSTR("com.sun.star.ucb.ExpandContentProvider"),
152 OUSTR("com.sun.star.ucb.ContentProvider")
154 return uno::Sequence
< OUString
>( names
, ARLEN(names
) );
158 //______________________________________________________________________________
159 OUString
ExpandContentProviderImpl::getImplementationName()
160 throw (uno::RuntimeException
)
166 //______________________________________________________________________________
167 uno::Sequence
< OUString
> ExpandContentProviderImpl::getSupportedServiceNames()
168 throw (uno::RuntimeException
)
171 return supportedServices();
174 //______________________________________________________________________________
175 sal_Bool
ExpandContentProviderImpl::supportsService(
176 OUString
const & serviceName
)
177 throw (uno::RuntimeException
)
180 uno::Sequence
< OUString
> supported_services( getSupportedServiceNames() );
181 OUString
const * ar
= supported_services
.getConstArray();
182 for ( sal_Int32 pos
= supported_services
.getLength(); pos
--; )
184 if (ar
[ pos
].equals( serviceName
))
190 //______________________________________________________________________________
191 OUString
ExpandContentProviderImpl::expandUri(
192 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const
194 OUString
uri( xIdentifier
->getContentIdentifier() );
195 if (uri
.compareToAscii(
196 RTL_CONSTASCII_STRINGPARAM(EXPAND_PROTOCOL
":") ) != 0)
198 throw ucb::IllegalIdentifierException(
199 OUSTR("expected protocol " EXPAND_PROTOCOL
"!"),
200 static_cast< OWeakObject
* >(
201 const_cast< ExpandContentProviderImpl
* >(this) ) );
204 OUString
str( uri
.copy( sizeof (EXPAND_PROTOCOL
":") -1 ) );
205 // decode uric class chars
206 str
= ::rtl::Uri::decode(
207 str
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
208 // expand macro string
209 return m_xMacroExpander
->expandMacros( str
);
213 //______________________________________________________________________________
214 uno::Reference
< ucb::XContent
> ExpandContentProviderImpl::queryContent(
215 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
)
216 throw (ucb::IllegalIdentifierException
, uno::RuntimeException
)
219 OUString
uri( expandUri( xIdentifier
) );
221 ::ucbhelper::Content ucb_content
;
222 if (::ucbhelper::Content::create(
223 uri
, uno::Reference
< ucb::XCommandEnvironment
>(), ucb_content
))
225 return ucb_content
.get();
229 return uno::Reference
< ucb::XContent
>();
233 //______________________________________________________________________________
234 sal_Int32
ExpandContentProviderImpl::compareContentIds(
235 uno::Reference
< ucb::XContentIdentifier
> const & xId1
,
236 uno::Reference
< ucb::XContentIdentifier
> const & xId2
)
237 throw (uno::RuntimeException
)
242 OUString
uri1( expandUri( xId1
) );
243 OUString
uri2( expandUri( xId2
) );
244 return uri1
.compareTo( uri2
);
246 catch (ucb::IllegalIdentifierException
& exc
)
248 (void) exc
; // unused
250 0, ::rtl::OUStringToOString(
251 exc
.Message
, RTL_TEXTENCODING_UTF8
).getStr() );
256 static const ::cppu::ImplementationEntry s_entries
[] =
262 ::cppu::createSingleComponentFactory
,
273 void SAL_CALL
component_getImplementationEnvironment(
274 const sal_Char
** ppEnvTypeName
, uno_Environment
** )
276 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
279 sal_Bool SAL_CALL
component_writeInfo(
280 lang::XMultiServiceFactory
* pServiceManager
,
281 registry::XRegistryKey
* pRegistryKey
)
283 return ::cppu::component_writeInfoHelper(
284 pServiceManager
, pRegistryKey
, s_entries
);
287 void * SAL_CALL
component_getFactory(
288 const sal_Char
* pImplName
,
289 lang::XMultiServiceFactory
* pServiceManager
,
290 registry::XRegistryKey
* pRegistryKey
)
292 return ::cppu::component_getFactoryHelper(
293 pImplName
, pServiceManager
, pRegistryKey
, s_entries
);