1*9d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9d7e27acSAndrew Rist  * distributed with this work for additional information
6*9d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
9*9d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9d7e27acSAndrew Rist  *
11*9d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9d7e27acSAndrew Rist  *
13*9d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9d7e27acSAndrew Rist  * software distributed under the License is distributed on an
15*9d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
17*9d7e27acSAndrew Rist  * specific language governing permissions and limitations
18*9d7e27acSAndrew Rist  * under the License.
19*9d7e27acSAndrew Rist  *
20*9d7e27acSAndrew Rist  *************************************************************/
21*9d7e27acSAndrew Rist 
22*9d7e27acSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <uno/mapping.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
32cdf0e10cSrcweir #include <cppuhelper/compbase2.hxx>
33cdf0e10cSrcweir #include <cppuhelper/component_context.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
36cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
37cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "macro_expander.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
42cdf0e10cSrcweir #define SERVICE_NAME_A "com.sun.star.lang.MacroExpander"
43cdf0e10cSrcweir #define SERVICE_NAME_B "com.sun.star.lang.BootstrapMacroExpander"
44cdf0e10cSrcweir #define IMPL_NAME "com.sun.star.lang.comp.cppuhelper.BootstrapMacroExpander"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir using namespace ::rtl;
48cdf0e10cSrcweir using namespace ::osl;
49cdf0e10cSrcweir using namespace ::com::sun::star;
50cdf0e10cSrcweir using namespace ::com::sun::star::uno;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace cppu
53cdf0e10cSrcweir {
54cdf0e10cSrcweir //---- private forward -----------------------------------------------------------------------------
55cdf0e10cSrcweir Bootstrap const & get_unorc() SAL_THROW( () );
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir namespace cppuhelper { namespace detail {
59cdf0e10cSrcweir 
expandMacros(rtl::OUString const & text)60cdf0e10cSrcweir rtl::OUString expandMacros(rtl::OUString const & text) {
61cdf0e10cSrcweir     rtl::OUString t(text);
62cdf0e10cSrcweir     rtl_bootstrap_expandMacros_from_handle(
63cdf0e10cSrcweir         cppu::get_unorc().getHandle(), &t.pData);
64cdf0e10cSrcweir     return t;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir } }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir namespace
70cdf0e10cSrcweir {
s_impl_name()71cdf0e10cSrcweir inline OUString s_impl_name() { return OUSTR(IMPL_NAME); }
s_get_service_names()72cdf0e10cSrcweir static Sequence< OUString > const & s_get_service_names()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     static Sequence< OUString > const * s_pnames = 0;
75cdf0e10cSrcweir     if (! s_pnames)
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         MutexGuard guard( Mutex::getGlobalMutex() );
78cdf0e10cSrcweir         if (! s_pnames)
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             static Sequence< OUString > s_names( 2 );
81cdf0e10cSrcweir             s_names[ 0 ] = OUSTR(SERVICE_NAME_A);
82cdf0e10cSrcweir             s_names[ 1 ] = OUSTR(SERVICE_NAME_B);
83cdf0e10cSrcweir             s_pnames = &s_names;
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir     return *s_pnames;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper2<
90cdf0e10cSrcweir     util::XMacroExpander, lang::XServiceInfo > t_uno_impl;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir struct mutex_holder
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     Mutex m_mutex;
95cdf0e10cSrcweir };
96cdf0e10cSrcweir class Bootstrap_MacroExpander : public mutex_holder, public t_uno_impl
97cdf0e10cSrcweir {
98cdf0e10cSrcweir protected:
99cdf0e10cSrcweir     virtual void SAL_CALL disposing();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir public:
102cdf0e10cSrcweir     inline Bootstrap_MacroExpander( Reference< XComponentContext > const & ) SAL_THROW( () )
103cdf0e10cSrcweir         : t_uno_impl( m_mutex )
104cdf0e10cSrcweir         {}
105cdf0e10cSrcweir     virtual ~Bootstrap_MacroExpander()
106cdf0e10cSrcweir         SAL_THROW( () );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     // XMacroExpander impl
109cdf0e10cSrcweir     virtual OUString SAL_CALL expandMacros( OUString const & exp )
110cdf0e10cSrcweir         throw (lang::IllegalArgumentException);
111cdf0e10cSrcweir     // XServiceInfo impl
112cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName()
113cdf0e10cSrcweir         throw (RuntimeException);
114cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
115cdf0e10cSrcweir         throw (RuntimeException);
116cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
117cdf0e10cSrcweir         throw (RuntimeException);
118cdf0e10cSrcweir };
119cdf0e10cSrcweir 
120cdf0e10cSrcweir //__________________________________________________________________________________________________
disposing()121cdf0e10cSrcweir void Bootstrap_MacroExpander::disposing()
122cdf0e10cSrcweir {}
123cdf0e10cSrcweir //__________________________________________________________________________________________________
~Bootstrap_MacroExpander()124cdf0e10cSrcweir Bootstrap_MacroExpander::~Bootstrap_MacroExpander() SAL_THROW( () )
125cdf0e10cSrcweir {}
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // XServiceInfo impl
128cdf0e10cSrcweir //__________________________________________________________________________________________________
getImplementationName()129cdf0e10cSrcweir OUString Bootstrap_MacroExpander::getImplementationName()
130cdf0e10cSrcweir     throw (RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     return s_impl_name();
133cdf0e10cSrcweir }
134cdf0e10cSrcweir //__________________________________________________________________________________________________
supportsService(OUString const & serviceName)135cdf0e10cSrcweir sal_Bool Bootstrap_MacroExpander::supportsService( OUString const & serviceName )
136cdf0e10cSrcweir     throw (RuntimeException)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     Sequence< OUString > const & service_names = s_get_service_names();
139cdf0e10cSrcweir     OUString const * p = service_names.getConstArray();
140cdf0e10cSrcweir     for ( sal_Int32 nPos = service_names.getLength(); nPos--; )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         if (p[ nPos ].equals( serviceName ))
143cdf0e10cSrcweir             return sal_True;
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir     return sal_False;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir //__________________________________________________________________________________________________
getSupportedServiceNames()148cdf0e10cSrcweir Sequence< OUString > Bootstrap_MacroExpander::getSupportedServiceNames()
149cdf0e10cSrcweir     throw (RuntimeException)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     return s_get_service_names();
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir // XMacroExpander impl
155cdf0e10cSrcweir //__________________________________________________________________________________________________
expandMacros(OUString const & exp)156cdf0e10cSrcweir OUString Bootstrap_MacroExpander::expandMacros( OUString const & exp )
157cdf0e10cSrcweir     throw (lang::IllegalArgumentException)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     return cppuhelper::detail::expandMacros( exp );
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir //==================================================================================================
service_create(Reference<XComponentContext> const & xComponentContext)163cdf0e10cSrcweir Reference< XInterface > SAL_CALL service_create(
164cdf0e10cSrcweir     Reference< XComponentContext > const & xComponentContext )
165cdf0e10cSrcweir     SAL_THROW( (RuntimeException) )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >( new Bootstrap_MacroExpander( xComponentContext ) );
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir namespace cppu
173cdf0e10cSrcweir {
174cdf0e10cSrcweir 
175cdf0e10cSrcweir //##################################################################################################
create_boostrap_macro_expander_factory()176cdf0e10cSrcweir Reference< lang::XSingleComponentFactory > create_boostrap_macro_expander_factory() SAL_THROW( () )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	Reference< lang::XSingleComponentFactory > free(::cppu::createSingleComponentFactory(
179cdf0e10cSrcweir 														service_create,
180cdf0e10cSrcweir 														s_impl_name(),
181cdf0e10cSrcweir 														s_get_service_names() ));
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	uno::Environment curr_env(Environment::getCurrent());
184cdf0e10cSrcweir 	uno::Environment target_env(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))));
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	uno::Mapping target2curr(target_env, curr_env);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	return Reference<lang::XSingleComponentFactory>(
189cdf0e10cSrcweir 		reinterpret_cast<lang::XSingleComponentFactory *>(
190cdf0e10cSrcweir 			target2curr.mapInterface(free.get(), ::getCppuType(&free))),
191cdf0e10cSrcweir 		SAL_NO_ACQUIRE);
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir }
195