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