xref: /AOO41X/main/comphelper/source/misc/servicedecl.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 #include "comphelper/servicedecl.hxx"
32 #include "osl/diagnose.h"
33 #include "rtl/string.hxx"
34 #include "rtl/ustrbuf.hxx"
35 #include "cppuhelper/implbase2.hxx"
36 #include "comphelper/sequence.hxx"
37 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
38 #include <vector>
39 
40 using namespace com::sun::star;
41 
42 namespace comphelper {
43 namespace service_decl {
44 
45 class ServiceDecl::Factory :
46         public cppu::WeakImplHelper2<lang::XSingleComponentFactory,
47                                      lang::XServiceInfo>,
48         private boost::noncopyable
49 {
50 public:
51     explicit Factory( ServiceDecl const& rServiceDecl )
52         : m_rServiceDecl(rServiceDecl) {}
53 
54     // XServiceInfo:
55     virtual rtl::OUString SAL_CALL getImplementationName()
56         throw (uno::RuntimeException);
57     virtual sal_Bool SAL_CALL supportsService( rtl::OUString const& name )
58         throw (uno::RuntimeException);
59     virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames()
60         throw (uno::RuntimeException);
61     // XSingleComponentFactory:
62     virtual uno::Reference<uno::XInterface> SAL_CALL createInstanceWithContext(
63         uno::Reference<uno::XComponentContext> const& xContext )
64         throw (uno::Exception);
65     virtual uno::Reference<uno::XInterface> SAL_CALL
66     createInstanceWithArgumentsAndContext(
67     uno::Sequence<uno::Any> const& args,
68     uno::Reference<uno::XComponentContext> const& xContext )
69         throw (uno::Exception);
70 
71 private:
72     virtual ~Factory();
73 
74     ServiceDecl const& m_rServiceDecl;
75 };
76 
77 ServiceDecl::Factory::~Factory()
78 {
79 }
80 
81 // XServiceInfo:
82 rtl::OUString ServiceDecl::Factory::getImplementationName()
83     throw (uno::RuntimeException)
84 {
85     return m_rServiceDecl.getImplementationName();
86 }
87 
88 sal_Bool ServiceDecl::Factory::supportsService( rtl::OUString const& name )
89     throw (uno::RuntimeException)
90 {
91     return m_rServiceDecl.supportsService(name);
92 }
93 
94 uno::Sequence<rtl::OUString> ServiceDecl::Factory::getSupportedServiceNames()
95     throw (uno::RuntimeException)
96 {
97     return m_rServiceDecl.getSupportedServiceNames();
98 }
99 
100 // XSingleComponentFactory:
101 uno::Reference<uno::XInterface> ServiceDecl::Factory::createInstanceWithContext(
102     uno::Reference<uno::XComponentContext> const& xContext )
103     throw (uno::Exception)
104 {
105     return m_rServiceDecl.m_createFunc(
106         m_rServiceDecl, uno::Sequence<uno::Any>(), xContext );
107 }
108 
109 uno::Reference<uno::XInterface>
110 ServiceDecl::Factory::createInstanceWithArgumentsAndContext(
111     uno::Sequence<uno::Any > const& args,
112     uno::Reference<uno::XComponentContext> const& xContext )
113     throw (uno::Exception)
114 {
115     return m_rServiceDecl.m_createFunc(
116         m_rServiceDecl, args, xContext );
117 }
118 
119 void * ServiceDecl::getFactory( sal_Char const* pImplName ) const
120 {
121     if (rtl_str_compare(m_pImplName, pImplName) == 0) {
122         lang::XSingleComponentFactory * const pFac( new Factory(*this) );
123         pFac->acquire();
124         return pFac;
125     }
126     return 0;
127 }
128 
129 uno::Sequence<rtl::OUString> ServiceDecl::getSupportedServiceNames() const
130 {
131     std::vector<rtl::OUString> vec;
132 
133     rtl::OString const str(m_pServiceNames);
134     sal_Int32 nIndex = 0;
135     do {
136         rtl::OString const token( str.getToken( 0, m_cDelim, nIndex ) );
137         vec.push_back( rtl::OUString( token.getStr(), token.getLength(),
138                                       RTL_TEXTENCODING_ASCII_US ) );
139     }
140     while (nIndex >= 0);
141 
142     return comphelper::containerToSequence(vec);
143 }
144 
145 bool ServiceDecl::supportsService( ::rtl::OUString const& name ) const
146 {
147     rtl::OString const str(m_pServiceNames);
148     sal_Int32 nIndex = 0;
149     do {
150         rtl::OString const token( str.getToken( 0, m_cDelim, nIndex ) );
151         if (name.equalsAsciiL( token.getStr(), token.getLength() ))
152             return true;
153     }
154     while (nIndex >= 0);
155     return false;
156 }
157 
158 rtl::OUString ServiceDecl::getImplementationName() const
159 {
160     return rtl::OUString::createFromAscii(m_pImplName);
161 }
162 
163 } // namespace service_decl
164 } // namespace comphelper
165 
166