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/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"
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
;
39 class ScParserFactoryMap
42 explicit ScParserFactoryMap();
44 Reference
< XFormulaParser
> createFormulaParser(
45 const Reference
< XComponent
>& rxComponent
,
46 const OUString
& rNamespace
);
49 typedef std::unordered_map
<
50 OUString
, Reference
< XSingleComponentFactory
>,
51 OUStringHash
, std::equal_to
< OUString
> > FactoryMap
;
53 Reference
< XComponentContext
> mxContext
; /// Global component context.
54 FactoryMap maFactories
; /// All parser factories, mapped by formula namespace.
57 ScParserFactoryMap::ScParserFactoryMap() :
58 mxContext( ::comphelper::getProcessComponentContext() )
60 if( mxContext
.is() ) try
62 // enumerate all implementations of the FormulaParser service
63 Reference
< XContentEnumerationAccess
> xFactoryEA( mxContext
->getServiceManager(), UNO_QUERY_THROW
);
64 Reference
< XEnumeration
> xEnum( xFactoryEA
->createContentEnumeration( OUString( "com.sun.star.sheet.FilterFormulaParser" ) ), UNO_SET_THROW
);
65 while( xEnum
->hasMoreElements() ) try // single try/catch for every element
67 // create an instance of the formula parser implementation
68 Reference
< XSingleComponentFactory
> xCompFactory( xEnum
->nextElement(), UNO_QUERY_THROW
);
69 Reference
< XFilterFormulaParser
> xParser( xCompFactory
->createInstanceWithContext( mxContext
), UNO_QUERY_THROW
);
71 // store factory in the map
72 OUString aNamespace
= xParser
->getSupportedNamespace();
73 if( !aNamespace
.isEmpty() )
74 maFactories
[ aNamespace
] = xCompFactory
;
85 Reference
< XFormulaParser
> ScParserFactoryMap::createFormulaParser(
86 const Reference
< XComponent
>& rxComponent
, const OUString
& rNamespace
)
88 Reference
< XFormulaParser
> xParser
;
89 FactoryMap::const_iterator aIt
= maFactories
.find( rNamespace
);
90 if( aIt
!= maFactories
.end() ) try
92 Sequence
< Any
> aArgs( 1 );
93 aArgs
[ 0 ] <<= rxComponent
;
94 xParser
.set( aIt
->second
->createInstanceWithArgumentsAndContext( aArgs
, mxContext
), UNO_QUERY_THROW
);
102 struct ScParserFactorySingleton
: public ::rtl::Static
< ScParserFactoryMap
, ScParserFactorySingleton
> {};
106 ScFormulaParserPool::ScFormulaParserPool( const ScDocument
& rDoc
) :
111 ScFormulaParserPool::~ScFormulaParserPool()
115 bool ScFormulaParserPool::hasFormulaParser( const OUString
& rNamespace
)
117 return getFormulaParser( rNamespace
).is();
120 Reference
< XFormulaParser
> ScFormulaParserPool::getFormulaParser( const OUString
& rNamespace
)
122 // try to find an existing parser entry
123 ParserMap::iterator aIt
= maParsers
.find( rNamespace
);
124 if( aIt
!= maParsers
.end() )
127 // always create a new entry in the map (even if the following initialization fails)
128 Reference
< XFormulaParser
>& rxParser
= maParsers
[ rNamespace
];
130 // try to create a new parser object
131 if( SfxObjectShell
* pDocShell
= mrDoc
.GetDocumentShell() ) try
133 Reference
< XComponent
> xComponent( pDocShell
->GetModel(), UNO_QUERY_THROW
);
134 ScParserFactoryMap
& rFactoryMap
= ScParserFactorySingleton::get();
135 rxParser
= rFactoryMap
.createFormulaParser( xComponent
, rNamespace
);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */