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_cppuhelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <uno/mapping.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
36*cdf0e10cSrcweir #include <cppuhelper/compbase2.hxx>
37*cdf0e10cSrcweir #include <cppuhelper/component_context.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
41*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "macro_expander.hxx"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
46*cdf0e10cSrcweir #define SERVICE_NAME_A "com.sun.star.lang.MacroExpander"
47*cdf0e10cSrcweir #define SERVICE_NAME_B "com.sun.star.lang.BootstrapMacroExpander"
48*cdf0e10cSrcweir #define IMPL_NAME "com.sun.star.lang.comp.cppuhelper.BootstrapMacroExpander"
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir using namespace ::rtl;
52*cdf0e10cSrcweir using namespace ::osl;
53*cdf0e10cSrcweir using namespace ::com::sun::star;
54*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir namespace cppu
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir //---- private forward -----------------------------------------------------------------------------
59*cdf0e10cSrcweir Bootstrap const & get_unorc() SAL_THROW( () );
60*cdf0e10cSrcweir }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir namespace cppuhelper { namespace detail {
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir rtl::OUString expandMacros(rtl::OUString const & text) {
65*cdf0e10cSrcweir     rtl::OUString t(text);
66*cdf0e10cSrcweir     rtl_bootstrap_expandMacros_from_handle(
67*cdf0e10cSrcweir         cppu::get_unorc().getHandle(), &t.pData);
68*cdf0e10cSrcweir     return t;
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir } }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir namespace
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir inline OUString s_impl_name() { return OUSTR(IMPL_NAME); }
76*cdf0e10cSrcweir static Sequence< OUString > const & s_get_service_names()
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     static Sequence< OUString > const * s_pnames = 0;
79*cdf0e10cSrcweir     if (! s_pnames)
80*cdf0e10cSrcweir     {
81*cdf0e10cSrcweir         MutexGuard guard( Mutex::getGlobalMutex() );
82*cdf0e10cSrcweir         if (! s_pnames)
83*cdf0e10cSrcweir         {
84*cdf0e10cSrcweir             static Sequence< OUString > s_names( 2 );
85*cdf0e10cSrcweir             s_names[ 0 ] = OUSTR(SERVICE_NAME_A);
86*cdf0e10cSrcweir             s_names[ 1 ] = OUSTR(SERVICE_NAME_B);
87*cdf0e10cSrcweir             s_pnames = &s_names;
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir     return *s_pnames;
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper2<
94*cdf0e10cSrcweir     util::XMacroExpander, lang::XServiceInfo > t_uno_impl;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir struct mutex_holder
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     Mutex m_mutex;
99*cdf0e10cSrcweir };
100*cdf0e10cSrcweir class Bootstrap_MacroExpander : public mutex_holder, public t_uno_impl
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir protected:
103*cdf0e10cSrcweir     virtual void SAL_CALL disposing();
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir public:
106*cdf0e10cSrcweir     inline Bootstrap_MacroExpander( Reference< XComponentContext > const & ) SAL_THROW( () )
107*cdf0e10cSrcweir         : t_uno_impl( m_mutex )
108*cdf0e10cSrcweir         {}
109*cdf0e10cSrcweir     virtual ~Bootstrap_MacroExpander()
110*cdf0e10cSrcweir         SAL_THROW( () );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     // XMacroExpander impl
113*cdf0e10cSrcweir     virtual OUString SAL_CALL expandMacros( OUString const & exp )
114*cdf0e10cSrcweir         throw (lang::IllegalArgumentException);
115*cdf0e10cSrcweir     // XServiceInfo impl
116*cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName()
117*cdf0e10cSrcweir         throw (RuntimeException);
118*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
119*cdf0e10cSrcweir         throw (RuntimeException);
120*cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
121*cdf0e10cSrcweir         throw (RuntimeException);
122*cdf0e10cSrcweir };
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir //__________________________________________________________________________________________________
125*cdf0e10cSrcweir void Bootstrap_MacroExpander::disposing()
126*cdf0e10cSrcweir {}
127*cdf0e10cSrcweir //__________________________________________________________________________________________________
128*cdf0e10cSrcweir Bootstrap_MacroExpander::~Bootstrap_MacroExpander() SAL_THROW( () )
129*cdf0e10cSrcweir {}
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir // XServiceInfo impl
132*cdf0e10cSrcweir //__________________________________________________________________________________________________
133*cdf0e10cSrcweir OUString Bootstrap_MacroExpander::getImplementationName()
134*cdf0e10cSrcweir     throw (RuntimeException)
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir     return s_impl_name();
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir //__________________________________________________________________________________________________
139*cdf0e10cSrcweir sal_Bool Bootstrap_MacroExpander::supportsService( OUString const & serviceName )
140*cdf0e10cSrcweir     throw (RuntimeException)
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir     Sequence< OUString > const & service_names = s_get_service_names();
143*cdf0e10cSrcweir     OUString const * p = service_names.getConstArray();
144*cdf0e10cSrcweir     for ( sal_Int32 nPos = service_names.getLength(); nPos--; )
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir         if (p[ nPos ].equals( serviceName ))
147*cdf0e10cSrcweir             return sal_True;
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir     return sal_False;
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir //__________________________________________________________________________________________________
152*cdf0e10cSrcweir Sequence< OUString > Bootstrap_MacroExpander::getSupportedServiceNames()
153*cdf0e10cSrcweir     throw (RuntimeException)
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     return s_get_service_names();
156*cdf0e10cSrcweir }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir // XMacroExpander impl
159*cdf0e10cSrcweir //__________________________________________________________________________________________________
160*cdf0e10cSrcweir OUString Bootstrap_MacroExpander::expandMacros( OUString const & exp )
161*cdf0e10cSrcweir     throw (lang::IllegalArgumentException)
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir     return cppuhelper::detail::expandMacros( exp );
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir //==================================================================================================
167*cdf0e10cSrcweir Reference< XInterface > SAL_CALL service_create(
168*cdf0e10cSrcweir     Reference< XComponentContext > const & xComponentContext )
169*cdf0e10cSrcweir     SAL_THROW( (RuntimeException) )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >( new Bootstrap_MacroExpander( xComponentContext ) );
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir namespace cppu
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir //##################################################################################################
180*cdf0e10cSrcweir Reference< lang::XSingleComponentFactory > create_boostrap_macro_expander_factory() SAL_THROW( () )
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	Reference< lang::XSingleComponentFactory > free(::cppu::createSingleComponentFactory(
183*cdf0e10cSrcweir 														service_create,
184*cdf0e10cSrcweir 														s_impl_name(),
185*cdf0e10cSrcweir 														s_get_service_names() ));
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	uno::Environment curr_env(Environment::getCurrent());
188*cdf0e10cSrcweir 	uno::Environment target_env(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))));
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	uno::Mapping target2curr(target_env, curr_env);
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	return Reference<lang::XSingleComponentFactory>(
193*cdf0e10cSrcweir 		reinterpret_cast<lang::XSingleComponentFactory *>(
194*cdf0e10cSrcweir 			target2curr.mapInterface(free.get(), ::getCppuType(&free))),
195*cdf0e10cSrcweir 		SAL_NO_ACQUIRE);
196*cdf0e10cSrcweir }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir }
199