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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 #include "formulaparserpool.hxx"
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
32 #include <com/sun/star/sheet/XFilterFormulaParser.hpp>
33 #include <rtl/instance.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <sfx2/objsh.hxx>
36 #include "document.hxx"
37 
38 using ::rtl::OUString;
39 using ::rtl::OUStringHash;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::sheet;
44 using namespace ::com::sun::star::uno;
45 
46 // ============================================================================
47 
48 namespace {
49 
50 class ScParserFactoryMap
51 {
52 public:
53     explicit            ScParserFactoryMap();
54 
55     Reference< XFormulaParser > createFormulaParser(
56                             const Reference< XComponent >& rxComponent,
57                             const OUString& rNamespace );
58 
59 private:
60     typedef ::std::hash_map< OUString, Reference< XSingleComponentFactory >, OUStringHash > FactoryMap;
61 
62     Reference< XComponentContext > mxContext;   /// Global component context.
63     FactoryMap          maFactories;            /// All parser factories, mapped by formula namespace.
64 };
65 
ScParserFactoryMap()66 ScParserFactoryMap::ScParserFactoryMap() :
67     mxContext( ::comphelper::getProcessComponentContext() )
68 {
69     if( mxContext.is() ) try
70     {
71         // enumerate all implementations of the FormulaParser service
72         Reference< XContentEnumerationAccess > xFactoryEA( mxContext->getServiceManager(), UNO_QUERY_THROW );
73         Reference< XEnumeration > xEnum( xFactoryEA->createContentEnumeration( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sheet.FilterFormulaParser" ) ) ), UNO_SET_THROW );
74         while( xEnum->hasMoreElements() ) try // single try/catch for every element
75         {
76             // create an instance of the formula parser implementation
77             Reference< XSingleComponentFactory > xCompFactory( xEnum->nextElement(), UNO_QUERY_THROW );
78             Reference< XFilterFormulaParser > xParser( xCompFactory->createInstanceWithContext( mxContext ), UNO_QUERY_THROW );
79 
80             // store factory in the map
81             OUString aNamespace = xParser->getSupportedNamespace();
82             if( aNamespace.getLength() > 0 )
83                 maFactories[ aNamespace ] = xCompFactory;
84         }
85         catch( Exception& )
86         {
87         }
88     }
89     catch( Exception& )
90     {
91     }
92 }
93 
createFormulaParser(const Reference<XComponent> & rxComponent,const OUString & rNamespace)94 Reference< XFormulaParser > ScParserFactoryMap::createFormulaParser(
95         const Reference< XComponent >& rxComponent, const OUString& rNamespace )
96 {
97     Reference< XFormulaParser > xParser;
98     FactoryMap::const_iterator aIt = maFactories.find( rNamespace );
99     if( aIt != maFactories.end() ) try
100     {
101         Sequence< Any > aArgs( 1 );
102         aArgs[ 0 ] <<= rxComponent;
103         xParser.set( aIt->second->createInstanceWithArgumentsAndContext( aArgs, mxContext ), UNO_QUERY_THROW );
104     }
105     catch( Exception& )
106     {
107     }
108     return xParser;
109 }
110 
111 struct ScParserFactorySingleton : public ::rtl::Static< ScParserFactoryMap, ScParserFactorySingleton > {};
112 
113 } // namespace
114 
115 // ============================================================================
116 
ScFormulaParserPool(const ScDocument & rDoc)117 ScFormulaParserPool::ScFormulaParserPool( const ScDocument& rDoc ) :
118     mrDoc( rDoc )
119 {
120 }
121 
~ScFormulaParserPool()122 ScFormulaParserPool::~ScFormulaParserPool()
123 {
124 }
125 
hasFormulaParser(const OUString & rNamespace)126 bool ScFormulaParserPool::hasFormulaParser( const OUString& rNamespace )
127 {
128     return getFormulaParser( rNamespace ).is();
129 }
130 
getFormulaParser(const OUString & rNamespace)131 Reference< XFormulaParser > ScFormulaParserPool::getFormulaParser( const OUString& rNamespace )
132 {
133     // try to find an existing parser entry
134     ParserMap::iterator aIt = maParsers.find( rNamespace );
135     if( aIt != maParsers.end() )
136         return aIt->second;
137 
138     // always create a new entry in the map (even if the following initialization fails)
139     Reference< XFormulaParser >& rxParser = maParsers[ rNamespace ];
140 
141     // try to create a new parser object
142     if( SfxObjectShell* pDocShell = mrDoc.GetDocumentShell() ) try
143     {
144         Reference< XComponent > xComponent( pDocShell->GetModel(), UNO_QUERY_THROW );
145         ScParserFactoryMap& rFactoryMap = ScParserFactorySingleton::get();
146         rxParser = rFactoryMap.createFormulaParser( xComponent, rNamespace );
147     }
148     catch( Exception& )
149     {
150     }
151     return rxParser;
152 }
153 
154 // ============================================================================
155 
156