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 #ifndef _FRAMEWORK_SCRIPT_PROVIDER_XFUNCTIONPROVIDER_HXX_
25 #define _FRAMEWORK_SCRIPT_PROVIDER_XFUNCTIONPROVIDER_HXX_
26 
27 #include <rtl/ustring.hxx>
28 
29 #include <cppuhelper/implbase5.hxx>
30 
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/uno/RuntimeException.hpp>
33 #include <com/sun/star/container/XNameContainer.hpp>
34 #include <com/sun/star/document/XScriptInvocationContext.hpp>
35 
36 #include <com/sun/star/lang/XInitialization.hpp>
37 
38 #include <com/sun/star/script/provider/XScriptProvider.hpp>
39 #include <com/sun/star/script/browse/XBrowseNode.hpp>
40 
41 #include "ProviderCache.hxx"
42 
43 namespace func_provider
44 {
45 // for simplification
46 #define css ::com::sun::star
47 
48  typedef ::cppu::WeakImplHelper5<
49      css::script::provider::XScriptProvider,
50      css::script::browse::XBrowseNode, css::lang::XServiceInfo,
51      css::lang::XInitialization,
52      css::container::XNameContainer > t_helper;
53 
54 class MasterScriptProvider :
55             public t_helper
56 {
57 public:
58     MasterScriptProvider(
59         const css::uno::Reference< css::uno::XComponentContext >
60         & xContext ) throw( css::uno::RuntimeException );
61     ~MasterScriptProvider();
62 
63     // XServiceInfo implementation
64     virtual ::rtl::OUString SAL_CALL getImplementationName( )
65         throw( css::uno::RuntimeException );
66 
67     // XBrowseNode implementation
68     virtual ::rtl::OUString SAL_CALL getName()
69         throw ( css::uno::RuntimeException );
70     virtual css::uno::Sequence< css::uno::Reference< css::script::browse::XBrowseNode > > SAL_CALL getChildNodes()
71         throw ( css::uno::RuntimeException );
72     virtual sal_Bool SAL_CALL hasChildNodes()
73         throw ( css::uno::RuntimeException );
74     virtual sal_Int16 SAL_CALL getType()
75         throw ( css::uno::RuntimeException );
76     // XNameContainer
77     virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const css::uno::Any& aElement ) throw ( css::lang::IllegalArgumentException, css::container::ElementExistException, css::lang::WrappedTargetException, css::uno::RuntimeException);
78     virtual void SAL_CALL removeByName( const ::rtl::OUString& Name ) throw ( css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException);
79 
80     // XNameReplace
81     virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const css::uno::Any& aElement ) throw ( css::lang::IllegalArgumentException, css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException);
82     // XNameAccess
83     virtual css::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw ( css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException);
84     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw ( css::uno::RuntimeException);
85     virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException);
86 
87     // XElementAccess
88     virtual css::uno::Type SAL_CALL getElementType(  ) throw ( css::uno::RuntimeException);
89     virtual sal_Bool SAL_CALL hasElements(  ) throw ( css::uno::RuntimeException);
90     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
91         throw( css::uno::RuntimeException );
92     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( )
93         throw( css::uno::RuntimeException );
94 
95     // XScriptProvider implementation
96     virtual css::uno::Reference < css::script::provider::XScript > SAL_CALL
97         getScript( const ::rtl::OUString& scriptURI )
98         throw( css::script::provider::ScriptFrameworkErrorException,
99                css::uno::RuntimeException );
100 
101     /**
102      *  XInitialise implementation
103      *
104      * @param args expected to contain a single ::rtl::OUString
105      * containing the URI
106      */
107     virtual void SAL_CALL initialize( const css::uno::Sequence < css::uno::Any > & args )
108         throw ( css::uno::Exception, css::uno::RuntimeException);
109 
110     // Public method to return all Language Providers in this MasterScriptProviders
111     // context.
112     css::uno::Sequence< css::uno::Reference< css::script::provider::XScriptProvider > > SAL_CALL
113         getAllProviders() throw ( css::uno::RuntimeException );
114 
isPkgProvider()115     bool isPkgProvider() { return m_bIsPkgMSP; }
getPkgProvider()116     css::uno::Reference< css::script::provider::XScriptProvider > getPkgProvider() { return m_xMSPPkg; }
117     // returns context string for this provider, eg
getContextString()118     ::rtl::OUString getContextString() { return m_sCtxString; }
119 
120 private:
121     ::rtl::OUString parseLocationName( const ::rtl::OUString& location );
122     void  createPkgProvider();
123     bool  isValid();
124     ::rtl::OUString getURLForModel();
125     const css::uno::Sequence< ::rtl::OUString >& getProviderNames();
126 
127     ProviderCache* providerCache();
128     /* to obtain other services if needed */
129     css::uno::Reference< css::uno::XComponentContext >              m_xContext;
130     css::uno::Reference< css::lang::XMultiComponentFactory >        m_xMgr;
131     css::uno::Reference< css::frame::XModel >                       m_xModel;
132     css::uno::Reference< css::document::XScriptInvocationContext >  m_xInvocationContext;
133     css::uno::Sequence< css::uno::Any >                             m_sAargs;
134     ::rtl::OUString                                                 m_sNodeName;
135 
136     // This component supports XInitialization, it can be created
137     // using createInstanceXXX() or createInstanceWithArgumentsXXX using
138     // the service Mangager.
139     // Need to detect proper initialisation and validity
140     // for the object, so m_bIsValid indicates that the object is valid is set in ctor
141     // in case of createInstanceWithArgumentsXXX() called m_bIsValid is set to reset
142     // and then set to true when initialisation is complete
143     bool m_bIsValid;
144     // m_bInitialised ensure initialisation only takes place once.
145     bool m_bInitialised;
146     bool m_bIsPkgMSP;
147     css::uno::Reference< css::script::provider::XScriptProvider > m_xMSPPkg;
148     ProviderCache* m_pPCache;
149     osl::Mutex m_mutex;
150     ::rtl::OUString m_sCtxString;
151 };
152 } // namespace func_provider
153 #endif //_FRAMEWORK_SCRIPT_PROVIDER_XFUNCTIONPROVIDER_HXX_
154