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