xref: /AOO41X/main/sc/source/core/tool/formulaparserpool.cxx (revision b3f79822e811ac3493b185030a72c3c5a51f32d8)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "formulaparserpool.hxx"
28cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
29cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
31cdf0e10cSrcweir #include <com/sun/star/lang/XSingleComponentFactory.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sheet/XFilterFormulaParser.hpp>
33cdf0e10cSrcweir #include <rtl/instance.hxx>
34cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
35cdf0e10cSrcweir #include <sfx2/objsh.hxx>
36cdf0e10cSrcweir #include "document.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using ::rtl::OUString;
39cdf0e10cSrcweir using ::rtl::OUStringHash;
40cdf0e10cSrcweir using namespace ::com::sun::star::beans;
41cdf0e10cSrcweir using namespace ::com::sun::star::container;
42cdf0e10cSrcweir using namespace ::com::sun::star::lang;
43cdf0e10cSrcweir using namespace ::com::sun::star::sheet;
44cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir // ============================================================================
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace {
49cdf0e10cSrcweir 
50cdf0e10cSrcweir class ScParserFactoryMap
51cdf0e10cSrcweir {
52cdf0e10cSrcweir public:
53cdf0e10cSrcweir     explicit            ScParserFactoryMap();
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     Reference< XFormulaParser > createFormulaParser(
56cdf0e10cSrcweir                             const Reference< XComponent >& rxComponent,
57cdf0e10cSrcweir                             const OUString& rNamespace );
58cdf0e10cSrcweir 
59cdf0e10cSrcweir private:
60cdf0e10cSrcweir     typedef ::std::hash_map< OUString, Reference< XSingleComponentFactory >, OUStringHash > FactoryMap;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     Reference< XComponentContext > mxContext;   /// Global component context.
63cdf0e10cSrcweir     FactoryMap          maFactories;            /// All parser factories, mapped by formula namespace.
64cdf0e10cSrcweir };
65cdf0e10cSrcweir 
ScParserFactoryMap()66cdf0e10cSrcweir ScParserFactoryMap::ScParserFactoryMap() :
67cdf0e10cSrcweir     mxContext( ::comphelper::getProcessComponentContext() )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     if( mxContext.is() ) try
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         // enumerate all implementations of the FormulaParser service
72cdf0e10cSrcweir         Reference< XContentEnumerationAccess > xFactoryEA( mxContext->getServiceManager(), UNO_QUERY_THROW );
73cdf0e10cSrcweir         Reference< XEnumeration > xEnum( xFactoryEA->createContentEnumeration( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sheet.FilterFormulaParser" ) ) ), UNO_SET_THROW );
74cdf0e10cSrcweir         while( xEnum->hasMoreElements() ) try // single try/catch for every element
75cdf0e10cSrcweir         {
76cdf0e10cSrcweir             // create an instance of the formula parser implementation
77cdf0e10cSrcweir             Reference< XSingleComponentFactory > xCompFactory( xEnum->nextElement(), UNO_QUERY_THROW );
78cdf0e10cSrcweir             Reference< XFilterFormulaParser > xParser( xCompFactory->createInstanceWithContext( mxContext ), UNO_QUERY_THROW );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir             // store factory in the map
81cdf0e10cSrcweir             OUString aNamespace = xParser->getSupportedNamespace();
82cdf0e10cSrcweir             if( aNamespace.getLength() > 0 )
83cdf0e10cSrcweir                 maFactories[ aNamespace ] = xCompFactory;
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir         catch( Exception& )
86cdf0e10cSrcweir         {
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir     catch( Exception& )
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
createFormulaParser(const Reference<XComponent> & rxComponent,const OUString & rNamespace)94cdf0e10cSrcweir Reference< XFormulaParser > ScParserFactoryMap::createFormulaParser(
95cdf0e10cSrcweir         const Reference< XComponent >& rxComponent, const OUString& rNamespace )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     Reference< XFormulaParser > xParser;
98cdf0e10cSrcweir     FactoryMap::const_iterator aIt = maFactories.find( rNamespace );
99cdf0e10cSrcweir     if( aIt != maFactories.end() ) try
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         Sequence< Any > aArgs( 1 );
102cdf0e10cSrcweir         aArgs[ 0 ] <<= rxComponent;
103cdf0e10cSrcweir         xParser.set( aIt->second->createInstanceWithArgumentsAndContext( aArgs, mxContext ), UNO_QUERY_THROW );
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir     catch( Exception& )
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir     return xParser;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir struct ScParserFactorySingleton : public ::rtl::Static< ScParserFactoryMap, ScParserFactorySingleton > {};
112cdf0e10cSrcweir 
113cdf0e10cSrcweir } // namespace
114cdf0e10cSrcweir 
115cdf0e10cSrcweir // ============================================================================
116cdf0e10cSrcweir 
ScFormulaParserPool(const ScDocument & rDoc)117cdf0e10cSrcweir ScFormulaParserPool::ScFormulaParserPool( const ScDocument& rDoc ) :
118cdf0e10cSrcweir     mrDoc( rDoc )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
~ScFormulaParserPool()122cdf0e10cSrcweir ScFormulaParserPool::~ScFormulaParserPool()
123cdf0e10cSrcweir {
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
hasFormulaParser(const OUString & rNamespace)126cdf0e10cSrcweir bool ScFormulaParserPool::hasFormulaParser( const OUString& rNamespace )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     return getFormulaParser( rNamespace ).is();
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
getFormulaParser(const OUString & rNamespace)131cdf0e10cSrcweir Reference< XFormulaParser > ScFormulaParserPool::getFormulaParser( const OUString& rNamespace )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     // try to find an existing parser entry
134cdf0e10cSrcweir     ParserMap::iterator aIt = maParsers.find( rNamespace );
135cdf0e10cSrcweir     if( aIt != maParsers.end() )
136cdf0e10cSrcweir         return aIt->second;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     // always create a new entry in the map (even if the following initialization fails)
139cdf0e10cSrcweir     Reference< XFormulaParser >& rxParser = maParsers[ rNamespace ];
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     // try to create a new parser object
142cdf0e10cSrcweir     if( SfxObjectShell* pDocShell = mrDoc.GetDocumentShell() ) try
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         Reference< XComponent > xComponent( pDocShell->GetModel(), UNO_QUERY_THROW );
145cdf0e10cSrcweir         ScParserFactoryMap& rFactoryMap = ScParserFactorySingleton::get();
146cdf0e10cSrcweir         rxParser = rFactoryMap.createFormulaParser( xComponent, rNamespace );
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir     catch( Exception& )
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir     return rxParser;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir // ============================================================================
155cdf0e10cSrcweir 
156