merged tag LIBREOFFICE_3_2_99_3
[LibreOffice.git] / cppuhelper / source / macro_expander.cxx
blob4bbebbd2110549e1df9e2fd1062b8ded6eb8851d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_cppuhelper.hxx"
32 #include <rtl/bootstrap.hxx>
34 #include <uno/mapping.hxx>
36 #include <cppuhelper/factory.hxx>
37 #include <cppuhelper/compbase2.hxx>
38 #include <cppuhelper/component_context.hxx>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/util/XMacroExpander.hpp>
42 #include "com/sun/star/uno/RuntimeException.hpp"
44 #include "macro_expander.hxx"
46 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
47 #define SERVICE_NAME_A "com.sun.star.lang.MacroExpander"
48 #define SERVICE_NAME_B "com.sun.star.lang.BootstrapMacroExpander"
49 #define IMPL_NAME "com.sun.star.lang.comp.cppuhelper.BootstrapMacroExpander"
52 using namespace ::rtl;
53 using namespace ::osl;
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
57 namespace cppu
59 //---- private forward -----------------------------------------------------------------------------
60 Bootstrap const & get_unorc() SAL_THROW( () );
63 namespace cppuhelper { namespace detail {
65 rtl::OUString expandMacros(rtl::OUString const & text) {
66 rtl::OUString t(text);
67 rtl_bootstrap_expandMacros_from_handle(
68 cppu::get_unorc().getHandle(), &t.pData);
69 return t;
72 } }
74 namespace
76 inline OUString s_impl_name() { return OUSTR(IMPL_NAME); }
77 static Sequence< OUString > const & s_get_service_names()
79 static Sequence< OUString > const * s_pnames = 0;
80 if (! s_pnames)
82 MutexGuard guard( Mutex::getGlobalMutex() );
83 if (! s_pnames)
85 static Sequence< OUString > s_names( 2 );
86 s_names[ 0 ] = OUSTR(SERVICE_NAME_A);
87 s_names[ 1 ] = OUSTR(SERVICE_NAME_B);
88 s_pnames = &s_names;
91 return *s_pnames;
94 typedef ::cppu::WeakComponentImplHelper2<
95 util::XMacroExpander, lang::XServiceInfo > t_uno_impl;
97 struct mutex_holder
99 Mutex m_mutex;
101 class Bootstrap_MacroExpander : public mutex_holder, public t_uno_impl
103 protected:
104 virtual void SAL_CALL disposing();
106 public:
107 inline Bootstrap_MacroExpander( Reference< XComponentContext > const & ) SAL_THROW( () )
108 : t_uno_impl( m_mutex )
110 virtual ~Bootstrap_MacroExpander()
111 SAL_THROW( () );
113 // XMacroExpander impl
114 virtual OUString SAL_CALL expandMacros( OUString const & exp )
115 throw (lang::IllegalArgumentException);
116 // XServiceInfo impl
117 virtual OUString SAL_CALL getImplementationName()
118 throw (RuntimeException);
119 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
120 throw (RuntimeException);
121 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
122 throw (RuntimeException);
125 //__________________________________________________________________________________________________
126 void Bootstrap_MacroExpander::disposing()
128 //__________________________________________________________________________________________________
129 Bootstrap_MacroExpander::~Bootstrap_MacroExpander() SAL_THROW( () )
132 // XServiceInfo impl
133 //__________________________________________________________________________________________________
134 OUString Bootstrap_MacroExpander::getImplementationName()
135 throw (RuntimeException)
137 return s_impl_name();
139 //__________________________________________________________________________________________________
140 sal_Bool Bootstrap_MacroExpander::supportsService( OUString const & serviceName )
141 throw (RuntimeException)
143 Sequence< OUString > const & service_names = s_get_service_names();
144 OUString const * p = service_names.getConstArray();
145 for ( sal_Int32 nPos = service_names.getLength(); nPos--; )
147 if (p[ nPos ].equals( serviceName ))
148 return sal_True;
150 return sal_False;
152 //__________________________________________________________________________________________________
153 Sequence< OUString > Bootstrap_MacroExpander::getSupportedServiceNames()
154 throw (RuntimeException)
156 return s_get_service_names();
159 // XMacroExpander impl
160 //__________________________________________________________________________________________________
161 OUString Bootstrap_MacroExpander::expandMacros( OUString const & exp )
162 throw (lang::IllegalArgumentException)
164 return cppuhelper::detail::expandMacros( exp );
167 //==================================================================================================
168 Reference< XInterface > SAL_CALL service_create(
169 Reference< XComponentContext > const & xComponentContext )
170 SAL_THROW( (RuntimeException) )
172 return static_cast< ::cppu::OWeakObject * >( new Bootstrap_MacroExpander( xComponentContext ) );
177 namespace cppu
180 //##################################################################################################
181 Reference< lang::XSingleComponentFactory > create_boostrap_macro_expander_factory() SAL_THROW( () )
183 Reference< lang::XSingleComponentFactory > free(::cppu::createSingleComponentFactory(
184 service_create,
185 s_impl_name(),
186 s_get_service_names() ));
188 uno::Environment curr_env(Environment::getCurrent());
189 uno::Environment target_env(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))));
191 uno::Mapping target2curr(target_env, curr_env);
193 return Reference<lang::XSingleComponentFactory>(
194 reinterpret_cast<lang::XSingleComponentFactory *>(
195 target2curr.mapInterface(free.get(), ::getCppuType(&free))),
196 SAL_NO_ACQUIRE);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */