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