1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macro_expander.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppuhelper.hxx"
34 #include <rtl/bootstrap.hxx>
36 #include <uno/mapping.hxx>
38 #include <cppuhelper/factory.hxx>
39 #include <cppuhelper/compbase2.hxx>
40 #include <cppuhelper/component_context.hxx>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/util/XMacroExpander.hpp>
44 #include "com/sun/star/uno/RuntimeException.hpp"
46 #include "macro_expander.hxx"
48 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
49 #define SERVICE_NAME_A "com.sun.star.lang.MacroExpander"
50 #define SERVICE_NAME_B "com.sun.star.lang.BootstrapMacroExpander"
51 #define IMPL_NAME "com.sun.star.lang.comp.cppuhelper.BootstrapMacroExpander"
54 using namespace ::rtl
;
55 using namespace ::osl
;
56 using namespace ::com::sun::star
;
57 using namespace ::com::sun::star::uno
;
61 //---- private forward -----------------------------------------------------------------------------
62 Bootstrap
const & get_unorc() SAL_THROW( () );
65 namespace cppuhelper
{ namespace detail
{
67 rtl::OUString
expandMacros(rtl::OUString
const & text
) {
68 rtl::OUString
t(text
);
69 rtl_bootstrap_expandMacros_from_handle(
70 cppu::get_unorc().getHandle(), &t
.pData
);
78 inline OUString
s_impl_name() { return OUSTR(IMPL_NAME
); }
79 static Sequence
< OUString
> const & s_get_service_names()
81 static Sequence
< OUString
> const * s_pnames
= 0;
84 MutexGuard
guard( Mutex::getGlobalMutex() );
87 static Sequence
< OUString
> s_names( 2 );
88 s_names
[ 0 ] = OUSTR(SERVICE_NAME_A
);
89 s_names
[ 1 ] = OUSTR(SERVICE_NAME_B
);
96 typedef ::cppu::WeakComponentImplHelper2
<
97 util::XMacroExpander
, lang::XServiceInfo
> t_uno_impl
;
103 class Bootstrap_MacroExpander
: public mutex_holder
, public t_uno_impl
106 virtual void SAL_CALL
disposing();
109 inline Bootstrap_MacroExpander( Reference
< XComponentContext
> const & ) SAL_THROW( () )
110 : t_uno_impl( m_mutex
)
112 virtual ~Bootstrap_MacroExpander()
115 // XMacroExpander impl
116 virtual OUString SAL_CALL
expandMacros( OUString
const & exp
)
117 throw (lang::IllegalArgumentException
);
119 virtual OUString SAL_CALL
getImplementationName()
120 throw (RuntimeException
);
121 virtual sal_Bool SAL_CALL
supportsService( OUString
const & serviceName
)
122 throw (RuntimeException
);
123 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
124 throw (RuntimeException
);
127 //__________________________________________________________________________________________________
128 void Bootstrap_MacroExpander::disposing()
130 //__________________________________________________________________________________________________
131 Bootstrap_MacroExpander::~Bootstrap_MacroExpander() SAL_THROW( () )
135 //__________________________________________________________________________________________________
136 OUString
Bootstrap_MacroExpander::getImplementationName()
137 throw (RuntimeException
)
139 return s_impl_name();
141 //__________________________________________________________________________________________________
142 sal_Bool
Bootstrap_MacroExpander::supportsService( OUString
const & serviceName
)
143 throw (RuntimeException
)
145 Sequence
< OUString
> const & service_names
= s_get_service_names();
146 OUString
const * p
= service_names
.getConstArray();
147 for ( sal_Int32 nPos
= service_names
.getLength(); nPos
--; )
149 if (p
[ nPos
].equals( serviceName
))
154 //__________________________________________________________________________________________________
155 Sequence
< OUString
> Bootstrap_MacroExpander::getSupportedServiceNames()
156 throw (RuntimeException
)
158 return s_get_service_names();
161 // XMacroExpander impl
162 //__________________________________________________________________________________________________
163 OUString
Bootstrap_MacroExpander::expandMacros( OUString
const & exp
)
164 throw (lang::IllegalArgumentException
)
166 return cppuhelper::detail::expandMacros( exp
);
169 //==================================================================================================
170 Reference
< XInterface
> SAL_CALL
service_create(
171 Reference
< XComponentContext
> const & xComponentContext
)
172 SAL_THROW( (RuntimeException
) )
174 return static_cast< ::cppu::OWeakObject
* >( new Bootstrap_MacroExpander( xComponentContext
) );
182 //##################################################################################################
183 Reference
< lang::XSingleComponentFactory
> create_boostrap_macro_expander_factory() SAL_THROW( () )
185 Reference
< lang::XSingleComponentFactory
> free(::cppu::createSingleComponentFactory(
188 s_get_service_names() ));
190 uno::Environment
curr_env(Environment::getCurrent());
191 uno::Environment
target_env(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))));
193 uno::Mapping
target2curr(target_env
, curr_env
);
195 return Reference
<lang::XSingleComponentFactory
>(
196 reinterpret_cast<lang::XSingleComponentFactory
*>(
197 target2curr
.mapInterface(free
.get(), ::getCppuType(&free
))),