xref: /trunk/main/cppuhelper/source/macro_expander.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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     // XServiceInfo impl
111     virtual OUString SAL_CALL getImplementationName();
112     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName );
113     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames();
114 };
115 
116 //__________________________________________________________________________________________________
disposing()117 void Bootstrap_MacroExpander::disposing()
118 {}
119 //__________________________________________________________________________________________________
~Bootstrap_MacroExpander()120 Bootstrap_MacroExpander::~Bootstrap_MacroExpander() SAL_THROW( () )
121 {}
122 
123 // XServiceInfo impl
124 //__________________________________________________________________________________________________
getImplementationName()125 OUString Bootstrap_MacroExpander::getImplementationName()
126 {
127     return s_impl_name();
128 }
129 //__________________________________________________________________________________________________
supportsService(OUString const & serviceName)130 sal_Bool Bootstrap_MacroExpander::supportsService( OUString const & serviceName )
131 {
132     Sequence< OUString > const & service_names = s_get_service_names();
133     OUString const * p = service_names.getConstArray();
134     for ( sal_Int32 nPos = service_names.getLength(); nPos--; )
135     {
136         if (p[ nPos ].equals( serviceName ))
137             return sal_True;
138     }
139     return sal_False;
140 }
141 //__________________________________________________________________________________________________
getSupportedServiceNames()142 Sequence< OUString > Bootstrap_MacroExpander::getSupportedServiceNames()
143 {
144     return s_get_service_names();
145 }
146 
147 // XMacroExpander impl
148 //__________________________________________________________________________________________________
expandMacros(OUString const & exp)149 OUString Bootstrap_MacroExpander::expandMacros( OUString const & exp )
150 {
151     return cppuhelper::detail::expandMacros( exp );
152 }
153 
154 //==================================================================================================
service_create(Reference<XComponentContext> const & xComponentContext)155 Reference< XInterface > SAL_CALL service_create(
156     Reference< XComponentContext > const & xComponentContext )
157 {
158     return static_cast< ::cppu::OWeakObject * >( new Bootstrap_MacroExpander( xComponentContext ) );
159 }
160 
161 }
162 
163 namespace cppu
164 {
165 
166 //##################################################################################################
create_boostrap_macro_expander_factory()167 Reference< lang::XSingleComponentFactory > create_boostrap_macro_expander_factory() SAL_THROW( () )
168 {
169     Reference< lang::XSingleComponentFactory > free(::cppu::createSingleComponentFactory(
170                                                         service_create,
171                                                         s_impl_name(),
172                                                         s_get_service_names() ));
173 
174     uno::Environment curr_env(Environment::getCurrent());
175     uno::Environment target_env(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))));
176 
177     uno::Mapping target2curr(target_env, curr_env);
178 
179     return Reference<lang::XSingleComponentFactory>(
180         reinterpret_cast<lang::XSingleComponentFactory *>(
181             target2curr.mapInterface(free.get(), ::getCppuType(&free))),
182         SAL_NO_ACQUIRE);
183 }
184 
185 }
186