Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "formulaparserpool.hxx"
21 : #include <com/sun/star/beans/XPropertySet.hpp>
22 : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
23 : #include <com/sun/star/lang/XComponent.hpp>
24 : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
25 : #include <com/sun/star/sheet/XFilterFormulaParser.hpp>
26 : #include <rtl/instance.hxx>
27 : #include <comphelper/processfactory.hxx>
28 : #include <sfx2/objsh.hxx>
29 : #include "document.hxx"
30 :
31 : using namespace ::com::sun::star::beans;
32 : using namespace ::com::sun::star::container;
33 : using namespace ::com::sun::star::lang;
34 : using namespace ::com::sun::star::sheet;
35 : using namespace ::com::sun::star::uno;
36 :
37 : namespace {
38 :
39 0 : class ScParserFactoryMap
40 : {
41 : public:
42 : explicit ScParserFactoryMap();
43 :
44 : Reference< XFormulaParser > createFormulaParser(
45 : const Reference< XComponent >& rxComponent,
46 : const OUString& rNamespace );
47 :
48 : private:
49 : typedef ::boost::unordered_map<
50 : OUString, Reference< XSingleComponentFactory >,
51 : OUStringHash, ::std::equal_to< OUString > > FactoryMap;
52 :
53 : Reference< XComponentContext > mxContext; /// Global component context.
54 : FactoryMap maFactories; /// All parser factories, mapped by formula namespace.
55 : };
56 :
57 0 : ScParserFactoryMap::ScParserFactoryMap() :
58 0 : mxContext( ::comphelper::getProcessComponentContext() )
59 : {
60 0 : if( mxContext.is() ) try
61 : {
62 : // enumerate all implementations of the FormulaParser service
63 0 : Reference< XContentEnumerationAccess > xFactoryEA( mxContext->getServiceManager(), UNO_QUERY_THROW );
64 0 : Reference< XEnumeration > xEnum( xFactoryEA->createContentEnumeration( OUString( "com.sun.star.sheet.FilterFormulaParser" ) ), UNO_SET_THROW );
65 0 : while( xEnum->hasMoreElements() ) try // single try/catch for every element
66 : {
67 : // create an instance of the formula parser implementation
68 0 : Reference< XSingleComponentFactory > xCompFactory( xEnum->nextElement(), UNO_QUERY_THROW );
69 0 : Reference< XFilterFormulaParser > xParser( xCompFactory->createInstanceWithContext( mxContext ), UNO_QUERY_THROW );
70 :
71 : // store factory in the map
72 0 : OUString aNamespace = xParser->getSupportedNamespace();
73 0 : if( !aNamespace.isEmpty() )
74 0 : maFactories[ aNamespace ] = xCompFactory;
75 : }
76 0 : catch( Exception& )
77 : {
78 0 : }
79 : }
80 0 : catch( Exception& )
81 : {
82 : }
83 0 : }
84 :
85 0 : Reference< XFormulaParser > ScParserFactoryMap::createFormulaParser(
86 : const Reference< XComponent >& rxComponent, const OUString& rNamespace )
87 : {
88 0 : Reference< XFormulaParser > xParser;
89 0 : FactoryMap::const_iterator aIt = maFactories.find( rNamespace );
90 0 : if( aIt != maFactories.end() ) try
91 : {
92 0 : Sequence< Any > aArgs( 1 );
93 0 : aArgs[ 0 ] <<= rxComponent;
94 0 : xParser.set( aIt->second->createInstanceWithArgumentsAndContext( aArgs, mxContext ), UNO_QUERY_THROW );
95 : }
96 0 : catch( Exception& )
97 : {
98 : }
99 0 : return xParser;
100 : }
101 :
102 : struct ScParserFactorySingleton : public ::rtl::Static< ScParserFactoryMap, ScParserFactorySingleton > {};
103 :
104 : } // namespace
105 :
106 0 : ScFormulaParserPool::ScFormulaParserPool( const ScDocument& rDoc ) :
107 0 : mrDoc( rDoc )
108 : {
109 0 : }
110 :
111 0 : ScFormulaParserPool::~ScFormulaParserPool()
112 : {
113 0 : }
114 :
115 0 : bool ScFormulaParserPool::hasFormulaParser( const OUString& rNamespace )
116 : {
117 0 : return getFormulaParser( rNamespace ).is();
118 : }
119 :
120 0 : Reference< XFormulaParser > ScFormulaParserPool::getFormulaParser( const OUString& rNamespace )
121 : {
122 : // try to find an existing parser entry
123 0 : ParserMap::iterator aIt = maParsers.find( rNamespace );
124 0 : if( aIt != maParsers.end() )
125 0 : return aIt->second;
126 :
127 : // always create a new entry in the map (even if the following initialization fails)
128 0 : Reference< XFormulaParser >& rxParser = maParsers[ rNamespace ];
129 :
130 : // try to create a new parser object
131 0 : if( SfxObjectShell* pDocShell = mrDoc.GetDocumentShell() ) try
132 : {
133 0 : Reference< XComponent > xComponent( pDocShell->GetModel(), UNO_QUERY_THROW );
134 0 : ScParserFactoryMap& rFactoryMap = ScParserFactorySingleton::get();
135 0 : rxParser = rFactoryMap.createFormulaParser( xComponent, rNamespace );
136 : }
137 0 : catch( Exception& )
138 : {
139 : }
140 0 : return rxParser;
141 228 : }
142 :
143 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|