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 "ooxformulaparser.hxx"
21 :
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/uno/XComponentContext.hpp>
24 : #include <osl/diagnose.h>
25 : #include <cppuhelper/supportsservice.hxx>
26 : #include "formulaparser.hxx"
27 :
28 : namespace oox {
29 : namespace xls {
30 :
31 : using namespace ::com::sun::star::lang;
32 : using namespace ::com::sun::star::sheet;
33 : using namespace ::com::sun::star::table;
34 : using namespace ::com::sun::star::uno;
35 :
36 : using ::rtl::OUString;
37 :
38 0 : class OOXMLFormulaParserImpl : private FormulaFinalizer
39 : {
40 : public:
41 : explicit OOXMLFormulaParserImpl( const Reference< XMultiServiceFactory >& rxModelFactory );
42 :
43 : Sequence< FormulaToken > parseFormula( const OUString& rFormula, const CellAddress& rReferencePos );
44 :
45 : protected:
46 : virtual const FunctionInfo* resolveBadFuncName( const OUString& rTokenData ) const SAL_OVERRIDE;
47 :
48 : private:
49 : ApiParserWrapper maApiParser;
50 : };
51 :
52 0 : OOXMLFormulaParserImpl::OOXMLFormulaParserImpl( const Reference< XMultiServiceFactory >& rxModelFactory ) :
53 : FormulaFinalizer( OpCodeProvider( rxModelFactory, FILTER_OOXML, BIFF_UNKNOWN, true ) ),
54 0 : maApiParser( rxModelFactory, *this )
55 : {
56 0 : }
57 :
58 0 : Sequence< FormulaToken > OOXMLFormulaParserImpl::parseFormula( const OUString& rFormula, const CellAddress& rReferencePos )
59 : {
60 0 : return finalizeTokenArray( maApiParser.parseFormula( rFormula, rReferencePos ) );
61 : }
62 :
63 0 : const FunctionInfo* OOXMLFormulaParserImpl::resolveBadFuncName( const OUString& rTokenData ) const
64 : {
65 : /* Try to parse calls to library functions. The format of such a function
66 : call is assumed to be
67 : "'<path-to-office-install>\Library\<libname>'!<funcname>". */
68 :
69 : // the string has to start with an apostroph (followed by the library URL)
70 0 : if( (rTokenData.getLength() >= 6) && (rTokenData[ 0 ] == '\'') )
71 : {
72 : // library URL and function name are separated by an exclamation mark
73 0 : sal_Int32 nExclamPos = rTokenData.lastIndexOf( '!' );
74 0 : if( (1 < nExclamPos) && (nExclamPos + 1 < rTokenData.getLength()) && (rTokenData[ nExclamPos - 1 ] == '\'') )
75 : {
76 : // find the last backslash that separates library path and name
77 0 : sal_Int32 nFileSep = rTokenData.lastIndexOf( '\\', nExclamPos - 2 );
78 0 : if( nFileSep > 1 )
79 : {
80 : // find preceding backslash that separates the last directory name
81 0 : sal_Int32 nDirSep = rTokenData.lastIndexOf( '\\', nFileSep - 1 );
82 : // function library is located in a directory called 'library'
83 0 : if( (nDirSep > 0) && rTokenData.matchIgnoreAsciiCase( "\\LIBRARY\\", nDirSep ) )
84 : {
85 : // try to find a function info for the function name
86 0 : OUString aFuncName = rTokenData.copy( nExclamPos + 1 ).toAsciiUpperCase();
87 0 : const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( aFuncName );
88 0 : if( pFuncInfo && (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) )
89 : {
90 : // check that the name of the library matches
91 0 : OUString aLibName = rTokenData.copy( nFileSep + 1, nExclamPos - nFileSep - 2 );
92 0 : if( pFuncInfo->meFuncLibType == getFuncLibTypeFromLibraryName( aLibName ) )
93 0 : return pFuncInfo;
94 0 : }
95 : }
96 : }
97 : }
98 : }
99 0 : return 0;
100 : }
101 :
102 0 : OOXMLFormulaParser::OOXMLFormulaParser()
103 : {
104 0 : }
105 :
106 0 : OOXMLFormulaParser::~OOXMLFormulaParser()
107 : {
108 0 : }
109 :
110 : // com.sun.star.lang.XServiceInfo interface -----------------------------------
111 0 : OUString SAL_CALL OOXMLFormulaParser::getImplementationName() throw( RuntimeException, std::exception )
112 : {
113 0 : return OUString( "com.sun.star.comp.oox.xls.FormulaParser");
114 : }
115 :
116 0 : sal_Bool SAL_CALL OOXMLFormulaParser::supportsService( const OUString& rService ) throw( RuntimeException, std::exception )
117 : {
118 0 : return cppu::supportsService(this, rService);
119 : }
120 :
121 0 : Sequence< OUString > SAL_CALL OOXMLFormulaParser::getSupportedServiceNames() throw( RuntimeException, std::exception )
122 : {
123 0 : Sequence< OUString > aServiceNames( 1 );
124 0 : aServiceNames[ 0 ] = "com.sun.star.sheet.FilterFormulaParser";
125 0 : return aServiceNames;
126 : }
127 :
128 : // com.sun.star.lang.XInitialization interface --------------------------------
129 :
130 0 : void SAL_CALL OOXMLFormulaParser::initialize( const Sequence< Any >& rArgs ) throw( Exception, RuntimeException, std::exception )
131 : {
132 : OSL_ENSURE( rArgs.hasElements(), "OOXMLFormulaParser::initialize - missing arguments" );
133 0 : if( !rArgs.hasElements() )
134 0 : throw RuntimeException();
135 0 : mxComponent.set( rArgs[ 0 ], UNO_QUERY_THROW );
136 0 : }
137 :
138 : // com.sun.star.sheet.XFilterFormulaParser interface --------------------------
139 :
140 0 : OUString SAL_CALL OOXMLFormulaParser::getSupportedNamespace() throw( RuntimeException, std::exception )
141 : {
142 0 : return OUString( "http://schemas.microsoft.com/office/excel/formula");
143 : }
144 :
145 : // com.sun.star.sheet.XFormulaParser interface --------------------------------
146 :
147 0 : Sequence< FormulaToken > SAL_CALL OOXMLFormulaParser::parseFormula(
148 : const OUString& rFormula, const CellAddress& rReferencePos ) throw( RuntimeException, std::exception )
149 : {
150 0 : if( !mxParserImpl )
151 : {
152 0 : Reference< XMultiServiceFactory > xModelFactory( mxComponent, UNO_QUERY_THROW );
153 0 : mxParserImpl.reset( new OOXMLFormulaParserImpl( xModelFactory ) );
154 : }
155 0 : return mxParserImpl->parseFormula( rFormula, rReferencePos );
156 : }
157 :
158 0 : OUString SAL_CALL OOXMLFormulaParser::printFormula(
159 : const Sequence< FormulaToken >& /*rTokens*/, const CellAddress& /*rReferencePos*/ ) throw( RuntimeException, std::exception )
160 : {
161 : // not implemented
162 0 : throw RuntimeException();
163 : }
164 :
165 : } // namespace xls
166 : } // namespace oox
167 :
168 :
169 : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
170 0 : com_sun_star_comp_oox_xls_FormulaParser_get_implementation(::com::sun::star::uno::XComponentContext*,
171 : ::com::sun::star::uno::Sequence<css::uno::Any> const &)
172 : {
173 0 : return cppu::acquire(new oox::xls::OOXMLFormulaParser());
174 30 : }
175 :
176 :
177 :
178 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|