xref: /aoo42x/main/ucb/source/ucp/expand/ucpexpand.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucb.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "rtl/uri.hxx"
32*cdf0e10cSrcweir #include "osl/mutex.hxx"
33*cdf0e10cSrcweir #include "cppuhelper/compbase2.hxx"
34*cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
35*cdf0e10cSrcweir #include "cppuhelper/implementationentry.hxx"
36*cdf0e10cSrcweir #include "ucbhelper/content.hxx"
37*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
38*cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
39*cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
40*cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
41*cdf0e10cSrcweir #include "com/sun/star/registry/XRegistryKey.hpp"
42*cdf0e10cSrcweir #include "com/sun/star/util/XMacroExpander.hpp"
43*cdf0e10cSrcweir #include "com/sun/star/ucb/XContentProvider.hpp"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #define EXPAND_PROTOCOL "vnd.sun.star.expand"
46*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
47*cdf0e10cSrcweir #define ARLEN(x) sizeof (x) / sizeof *(x)
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace ::com::sun::star;
51*cdf0e10cSrcweir using ::rtl::OUString;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir namespace
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir struct MutexHolder
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     mutable ::osl::Mutex m_mutex;
59*cdf0e10cSrcweir };
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper2<
62*cdf0e10cSrcweir     lang::XServiceInfo, ucb::XContentProvider > t_impl_helper;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir //==============================================================================
65*cdf0e10cSrcweir class ExpandContentProviderImpl : protected MutexHolder, public t_impl_helper
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     uno::Reference< util::XMacroExpander > m_xMacroExpander;
68*cdf0e10cSrcweir     OUString expandUri(
69*cdf0e10cSrcweir         uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir protected:
72*cdf0e10cSrcweir     inline void check() const;
73*cdf0e10cSrcweir     virtual void SAL_CALL disposing();
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir public:
76*cdf0e10cSrcweir     inline ExpandContentProviderImpl(
77*cdf0e10cSrcweir         uno::Reference< uno::XComponentContext > const & xComponentContext )
78*cdf0e10cSrcweir         : t_impl_helper( m_mutex ),
79*cdf0e10cSrcweir           m_xMacroExpander(
80*cdf0e10cSrcweir               xComponentContext->getValueByName(
81*cdf0e10cSrcweir                   OUSTR("/singletons/com.sun.star.util.theMacroExpander") ),
82*cdf0e10cSrcweir               uno::UNO_QUERY_THROW )
83*cdf0e10cSrcweir         {}
84*cdf0e10cSrcweir     virtual ~ExpandContentProviderImpl() throw ();
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     // XServiceInfo
87*cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName()
88*cdf0e10cSrcweir         throw (uno::RuntimeException);
89*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
90*cdf0e10cSrcweir         throw (uno::RuntimeException);
91*cdf0e10cSrcweir     virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
92*cdf0e10cSrcweir         throw (uno::RuntimeException);
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     // XContentProvider
95*cdf0e10cSrcweir     virtual uno::Reference< ucb::XContent > SAL_CALL queryContent(
96*cdf0e10cSrcweir         uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
97*cdf0e10cSrcweir         throw (ucb::IllegalIdentifierException, uno::RuntimeException);
98*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL compareContentIds(
99*cdf0e10cSrcweir         uno::Reference< ucb::XContentIdentifier > const & xId1,
100*cdf0e10cSrcweir         uno::Reference< ucb::XContentIdentifier > const & xId2 )
101*cdf0e10cSrcweir         throw (uno::RuntimeException);
102*cdf0e10cSrcweir };
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir //______________________________________________________________________________
105*cdf0e10cSrcweir inline void ExpandContentProviderImpl::check() const
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     // xxx todo guard?
108*cdf0e10cSrcweir //     MutexGuard guard( m_mutex );
109*cdf0e10cSrcweir     if (rBHelper.bInDispose || rBHelper.bDisposed)
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         throw lang::DisposedException(
112*cdf0e10cSrcweir             OUSTR("expand content provider instance has "
113*cdf0e10cSrcweir                   "already been disposed!"),
114*cdf0e10cSrcweir             static_cast< OWeakObject * >(
115*cdf0e10cSrcweir                 const_cast< ExpandContentProviderImpl * >(this) ) );
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir //______________________________________________________________________________
120*cdf0e10cSrcweir ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir //______________________________________________________________________________
125*cdf0e10cSrcweir void ExpandContentProviderImpl::disposing()
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir //==============================================================================
130*cdf0e10cSrcweir static uno::Reference< uno::XInterface > SAL_CALL create(
131*cdf0e10cSrcweir     uno::Reference< uno::XComponentContext > const & xComponentContext )
132*cdf0e10cSrcweir     SAL_THROW( (uno::Exception) )
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >(
135*cdf0e10cSrcweir         new ExpandContentProviderImpl( xComponentContext ) );
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir //==============================================================================
139*cdf0e10cSrcweir static OUString SAL_CALL implName()
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     return OUSTR("com.sun.star.comp.ucb.ExpandContentProvider");
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir //==============================================================================
145*cdf0e10cSrcweir static uno::Sequence< OUString > SAL_CALL supportedServices()
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir     OUString names [] = {
148*cdf0e10cSrcweir         OUSTR("com.sun.star.ucb.ExpandContentProvider"),
149*cdf0e10cSrcweir         OUSTR("com.sun.star.ucb.ContentProvider")
150*cdf0e10cSrcweir     };
151*cdf0e10cSrcweir     return uno::Sequence< OUString >( names, ARLEN(names) );
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir // XServiceInfo
155*cdf0e10cSrcweir //______________________________________________________________________________
156*cdf0e10cSrcweir OUString ExpandContentProviderImpl::getImplementationName()
157*cdf0e10cSrcweir     throw (uno::RuntimeException)
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir     check();
160*cdf0e10cSrcweir     return implName();
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir //______________________________________________________________________________
164*cdf0e10cSrcweir uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
165*cdf0e10cSrcweir     throw (uno::RuntimeException)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir     check();
168*cdf0e10cSrcweir     return supportedServices();
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir //______________________________________________________________________________
172*cdf0e10cSrcweir sal_Bool ExpandContentProviderImpl::supportsService(
173*cdf0e10cSrcweir     OUString const & serviceName )
174*cdf0e10cSrcweir     throw (uno::RuntimeException)
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir //     check();
177*cdf0e10cSrcweir     uno::Sequence< OUString > supported_services( getSupportedServiceNames() );
178*cdf0e10cSrcweir     OUString const * ar = supported_services.getConstArray();
179*cdf0e10cSrcweir     for ( sal_Int32 pos = supported_services.getLength(); pos--; )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir         if (ar[ pos ].equals( serviceName ))
182*cdf0e10cSrcweir             return true;
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir     return false;
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir //______________________________________________________________________________
188*cdf0e10cSrcweir OUString ExpandContentProviderImpl::expandUri(
189*cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
190*cdf0e10cSrcweir {
191*cdf0e10cSrcweir     OUString uri( xIdentifier->getContentIdentifier() );
192*cdf0e10cSrcweir     if (uri.compareToAscii(
193*cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM(EXPAND_PROTOCOL ":") ) != 0)
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         throw ucb::IllegalIdentifierException(
196*cdf0e10cSrcweir             OUSTR("expected protocol " EXPAND_PROTOCOL "!"),
197*cdf0e10cSrcweir             static_cast< OWeakObject * >(
198*cdf0e10cSrcweir                 const_cast< ExpandContentProviderImpl * >(this) ) );
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir     // cut protocol
201*cdf0e10cSrcweir     OUString str( uri.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
202*cdf0e10cSrcweir     // decode uric class chars
203*cdf0e10cSrcweir     str = ::rtl::Uri::decode(
204*cdf0e10cSrcweir         str, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
205*cdf0e10cSrcweir     // expand macro string
206*cdf0e10cSrcweir     return m_xMacroExpander->expandMacros( str );
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir // XContentProvider
210*cdf0e10cSrcweir //______________________________________________________________________________
211*cdf0e10cSrcweir uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
212*cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
213*cdf0e10cSrcweir     throw (ucb::IllegalIdentifierException, uno::RuntimeException)
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir     check();
216*cdf0e10cSrcweir     OUString uri( expandUri( xIdentifier ) );
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     ::ucbhelper::Content ucb_content;
219*cdf0e10cSrcweir     if (::ucbhelper::Content::create(
220*cdf0e10cSrcweir             uri, uno::Reference< ucb::XCommandEnvironment >(), ucb_content ))
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir         return ucb_content.get();
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir     else
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         return uno::Reference< ucb::XContent >();
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir //______________________________________________________________________________
231*cdf0e10cSrcweir sal_Int32 ExpandContentProviderImpl::compareContentIds(
232*cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > const & xId1,
233*cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > const & xId2 )
234*cdf0e10cSrcweir     throw (uno::RuntimeException)
235*cdf0e10cSrcweir {
236*cdf0e10cSrcweir     check();
237*cdf0e10cSrcweir     try
238*cdf0e10cSrcweir     {
239*cdf0e10cSrcweir         OUString uri1( expandUri( xId1 ) );
240*cdf0e10cSrcweir         OUString uri2( expandUri( xId2 ) );
241*cdf0e10cSrcweir         return uri1.compareTo( uri2 );
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir     catch (ucb::IllegalIdentifierException & exc)
244*cdf0e10cSrcweir     {
245*cdf0e10cSrcweir         (void) exc; // unused
246*cdf0e10cSrcweir         OSL_ENSURE(
247*cdf0e10cSrcweir             0, ::rtl::OUStringToOString(
248*cdf0e10cSrcweir                 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
249*cdf0e10cSrcweir         return -1;
250*cdf0e10cSrcweir     }
251*cdf0e10cSrcweir }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir static const ::cppu::ImplementationEntry s_entries [] =
254*cdf0e10cSrcweir {
255*cdf0e10cSrcweir 	{
256*cdf0e10cSrcweir 		create,
257*cdf0e10cSrcweir         implName,
258*cdf0e10cSrcweir 		supportedServices,
259*cdf0e10cSrcweir         ::cppu::createSingleComponentFactory,
260*cdf0e10cSrcweir 		0, 0
261*cdf0e10cSrcweir 	},
262*cdf0e10cSrcweir 	{ 0, 0, 0, 0, 0, 0 }
263*cdf0e10cSrcweir };
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir extern "C"
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
271*cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
272*cdf0e10cSrcweir {
273*cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir void * SAL_CALL component_getFactory(
277*cdf0e10cSrcweir 	const sal_Char * pImplName,
278*cdf0e10cSrcweir     lang::XMultiServiceFactory * pServiceManager,
279*cdf0e10cSrcweir     registry::XRegistryKey * pRegistryKey )
280*cdf0e10cSrcweir {
281*cdf0e10cSrcweir 	return ::cppu::component_getFactoryHelper(
282*cdf0e10cSrcweir         pImplName, pServiceManager, pRegistryKey, s_entries );
283*cdf0e10cSrcweir }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir }
286