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