1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
26 #include <comphelper/processfactory.hxx>
27 #include <sfx2/objsh.hxx>
28 #include <document.hxx>
30 using namespace ::com::sun::star::beans
;
31 using namespace ::com::sun::star::container
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::sheet
;
34 using namespace ::com::sun::star::uno
;
38 class ScParserFactoryMap
41 explicit ScParserFactoryMap();
43 Reference
< XFormulaParser
> createFormulaParser(
44 const Reference
< XComponent
>& rxComponent
,
45 const OUString
& rNamespace
);
48 typedef std::unordered_map
<
49 OUString
, Reference
< XSingleComponentFactory
> > FactoryMap
;
51 Reference
< XComponentContext
> mxContext
; /// Global component context.
52 FactoryMap maFactories
; /// All parser factories, mapped by formula namespace.
55 ScParserFactoryMap::ScParserFactoryMap() :
56 mxContext( ::comphelper::getProcessComponentContext() )
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
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
);
72 // store factory in the map
73 OUString aNamespace
= xParser
->getSupportedNamespace();
74 if( !aNamespace
.isEmpty() )
75 maFactories
[ aNamespace
] = xCompFactory
;
86 Reference
< XFormulaParser
> ScParserFactoryMap::createFormulaParser(
87 const Reference
< XComponent
>& rxComponent
, const OUString
& rNamespace
)
89 Reference
< XFormulaParser
> xParser
;
90 FactoryMap::const_iterator aIt
= maFactories
.find( rNamespace
);
91 if( aIt
!= maFactories
.end() ) try
93 Sequence
< Any
> aArgs( 1 );
94 aArgs
[ 0 ] <<= rxComponent
;
95 xParser
.set( aIt
->second
->createInstanceWithArgumentsAndContext( aArgs
, mxContext
), UNO_QUERY_THROW
);
105 ScFormulaParserPool::ScFormulaParserPool( const ScDocument
& rDoc
) :
110 ScFormulaParserPool::~ScFormulaParserPool()
114 bool ScFormulaParserPool::hasFormulaParser( const OUString
& rNamespace
)
116 return getFormulaParser( rNamespace
).is();
119 Reference
< XFormulaParser
> ScFormulaParserPool::getFormulaParser( const OUString
& rNamespace
)
121 // try to find an existing parser entry
122 ParserMap::iterator aIt
= maParsers
.find( rNamespace
);
123 if( aIt
!= maParsers
.end() )
126 // always create a new entry in the map (even if the following initialization fails)
127 Reference
< XFormulaParser
>& rxParser
= maParsers
[ rNamespace
];
129 // try to create a new parser object
130 if( SfxObjectShell
* pDocShell
= mrDoc
.GetDocumentShell() ) try
132 static ScParserFactoryMap theScParserFactoryMap
;
134 Reference
< XComponent
> xComponent( pDocShell
->GetModel(), UNO_QUERY_THROW
);
135 rxParser
= theScParserFactoryMap
.createFormulaParser( xComponent
, rNamespace
);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */