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