xref: /trunk/main/oox/source/xls/ooxformulaparser.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/xls/ooxformulaparser.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
27cdf0e10cSrcweir #include "oox/xls/formulaparser.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace oox {
30cdf0e10cSrcweir namespace xls {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ============================================================================
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::com::sun::star::lang;
35cdf0e10cSrcweir using namespace ::com::sun::star::sheet;
36cdf0e10cSrcweir using namespace ::com::sun::star::table;
37cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using ::rtl::OUString;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // ============================================================================
42cdf0e10cSrcweir 
43cdf0e10cSrcweir class OOXMLFormulaParserImpl : private FormulaFinalizer
44cdf0e10cSrcweir {
45cdf0e10cSrcweir public:
46cdf0e10cSrcweir     explicit            OOXMLFormulaParserImpl( const Reference< XMultiServiceFactory >& rxModelFactory );
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     Sequence< FormulaToken > parseFormula( const OUString& rFormula, const CellAddress& rReferencePos );
49cdf0e10cSrcweir 
50cdf0e10cSrcweir protected:
51cdf0e10cSrcweir     virtual const FunctionInfo* resolveBadFuncName( const OUString& rTokenData ) const;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir private:
54cdf0e10cSrcweir     ApiParserWrapper    maApiParser;
55cdf0e10cSrcweir };
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // ----------------------------------------------------------------------------
58cdf0e10cSrcweir 
OOXMLFormulaParserImpl(const Reference<XMultiServiceFactory> & rxModelFactory)59cdf0e10cSrcweir OOXMLFormulaParserImpl::OOXMLFormulaParserImpl( const Reference< XMultiServiceFactory >& rxModelFactory ) :
60cdf0e10cSrcweir     FormulaFinalizer( OpCodeProvider( rxModelFactory, FILTER_OOXML, BIFF_UNKNOWN, true ) ),
61cdf0e10cSrcweir     maApiParser( rxModelFactory, *this )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
parseFormula(const OUString & rFormula,const CellAddress & rReferencePos)65cdf0e10cSrcweir Sequence< FormulaToken > OOXMLFormulaParserImpl::parseFormula( const OUString& rFormula, const CellAddress& rReferencePos )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     return finalizeTokenArray( maApiParser.parseFormula( rFormula, rReferencePos ) );
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
resolveBadFuncName(const OUString & rTokenData) const70cdf0e10cSrcweir const FunctionInfo* OOXMLFormulaParserImpl::resolveBadFuncName( const OUString& rTokenData ) const
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     /*  Try to parse calls to library functions. The format of such a function
73cdf0e10cSrcweir         call is assumed to be
74cdf0e10cSrcweir             "'<path-to-office-install>\Library\<libname>'!<funcname>". */
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     // the string has to start with an apostroph (followed by the library URL)
77cdf0e10cSrcweir     if( (rTokenData.getLength() >= 6) && (rTokenData[ 0 ] == '\'') )
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         // library URL and function name are separated by an exclamation mark
80cdf0e10cSrcweir         sal_Int32 nExclamPos = rTokenData.lastIndexOf( '!' );
81cdf0e10cSrcweir         if( (1 < nExclamPos) && (nExclamPos + 1 < rTokenData.getLength()) && (rTokenData[ nExclamPos - 1 ] == '\'') )
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             // find the last backslash that separates library path and name
84cdf0e10cSrcweir             sal_Int32 nFileSep = rTokenData.lastIndexOf( '\\', nExclamPos - 2 );
85cdf0e10cSrcweir             if( nFileSep > 1 )
86cdf0e10cSrcweir             {
87cdf0e10cSrcweir                 // find preceding backslash that separates the last directory name
88cdf0e10cSrcweir                 sal_Int32 nDirSep = rTokenData.lastIndexOf( '\\', nFileSep - 1 );
89cdf0e10cSrcweir                 // function library is located in a directory called 'library'
90cdf0e10cSrcweir                 if( (nDirSep > 0) && rTokenData.matchIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "\\LIBRARY\\" ), nDirSep ) )
91cdf0e10cSrcweir                 {
92cdf0e10cSrcweir                     // try to find a function info for the function name
93cdf0e10cSrcweir                     OUString aFuncName = rTokenData.copy( nExclamPos + 1 ).toAsciiUpperCase();
94cdf0e10cSrcweir                     const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( aFuncName );
95cdf0e10cSrcweir                     if( pFuncInfo && (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) )
96cdf0e10cSrcweir                     {
97cdf0e10cSrcweir                         // check that the name of the library matches
98cdf0e10cSrcweir                         OUString aLibName = rTokenData.copy( nFileSep + 1, nExclamPos - nFileSep - 2 );
99cdf0e10cSrcweir                         if( pFuncInfo->meFuncLibType == getFuncLibTypeFromLibraryName( aLibName ) )
100cdf0e10cSrcweir                             return pFuncInfo;
101cdf0e10cSrcweir                     }
102cdf0e10cSrcweir                 }
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir     return 0;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir // ============================================================================
110cdf0e10cSrcweir 
111cdf0e10cSrcweir class OOXMLFormulaPrinterImpl : public OpCodeProvider
112cdf0e10cSrcweir {
113cdf0e10cSrcweir public:
114cdf0e10cSrcweir     explicit            OOXMLFormulaPrinterImpl( const Reference< XMultiServiceFactory >& rxModelFactory );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir private:
117cdf0e10cSrcweir     ApiParserWrapper    maApiParser;
118cdf0e10cSrcweir };
119cdf0e10cSrcweir 
120cdf0e10cSrcweir // ----------------------------------------------------------------------------
121cdf0e10cSrcweir 
OOXMLFormulaPrinterImpl(const Reference<XMultiServiceFactory> & rxModelFactory)122cdf0e10cSrcweir OOXMLFormulaPrinterImpl::OOXMLFormulaPrinterImpl( const Reference< XMultiServiceFactory >& rxModelFactory ) :
123cdf0e10cSrcweir     OpCodeProvider( rxModelFactory, FILTER_OOXML, BIFF_UNKNOWN, false ),
124cdf0e10cSrcweir     maApiParser( rxModelFactory, *this )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir // ============================================================================
129cdf0e10cSrcweir 
OOXMLFormulaParser_getSupportedServiceNames()130cdf0e10cSrcweir Sequence< OUString > OOXMLFormulaParser_getSupportedServiceNames()
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     Sequence< OUString > aServiceNames( 1 );
133cdf0e10cSrcweir     aServiceNames[ 0 ] = CREATE_OUSTRING( "com.sun.star.sheet.FilterFormulaParser" );
134cdf0e10cSrcweir     return aServiceNames;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
OOXMLFormulaParser_getImplementationName()137cdf0e10cSrcweir OUString OOXMLFormulaParser_getImplementationName()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     return CREATE_OUSTRING( "com.sun.star.comp.oox.xls.FormulaParser" );
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
OOXMLFormulaParser_createInstance(const Reference<XComponentContext> &)142cdf0e10cSrcweir Reference< XInterface > SAL_CALL OOXMLFormulaParser_createInstance( const Reference< XComponentContext >& ) throw( Exception )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject* >( new OOXMLFormulaParser );
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir // ============================================================================
148cdf0e10cSrcweir 
OOXMLFormulaParser()149cdf0e10cSrcweir OOXMLFormulaParser::OOXMLFormulaParser()
150cdf0e10cSrcweir {
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
~OOXMLFormulaParser()153cdf0e10cSrcweir OOXMLFormulaParser::~OOXMLFormulaParser()
154cdf0e10cSrcweir {
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir // com.sun.star.lang.XServiceInfo interface -----------------------------------
158cdf0e10cSrcweir 
getImplementationName()159cdf0e10cSrcweir OUString SAL_CALL OOXMLFormulaParser::getImplementationName() throw( RuntimeException )
160cdf0e10cSrcweir {
161cdf0e10cSrcweir     return OOXMLFormulaParser_getImplementationName();
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
supportsService(const OUString & rService)164cdf0e10cSrcweir sal_Bool SAL_CALL OOXMLFormulaParser::supportsService( const OUString& rService ) throw( RuntimeException )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     const Sequence< OUString > aServices( OOXMLFormulaParser_getSupportedServiceNames() );
167cdf0e10cSrcweir     const OUString* pArray = aServices.getConstArray();
168cdf0e10cSrcweir     const OUString* pArrayEnd = pArray + aServices.getLength();
169cdf0e10cSrcweir     return ::std::find( pArray, pArrayEnd, rService ) != pArrayEnd;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
getSupportedServiceNames()172cdf0e10cSrcweir Sequence< OUString > SAL_CALL OOXMLFormulaParser::getSupportedServiceNames() throw( RuntimeException )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     return OOXMLFormulaParser_getSupportedServiceNames();
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir // com.sun.star.lang.XInitialization interface --------------------------------
178cdf0e10cSrcweir 
initialize(const Sequence<Any> & rArgs)179cdf0e10cSrcweir void SAL_CALL OOXMLFormulaParser::initialize( const Sequence< Any >& rArgs ) throw( Exception, RuntimeException )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir     OSL_ENSURE( rArgs.hasElements(), "OOXMLFormulaParser::initialize - missing arguments" );
182cdf0e10cSrcweir     if( !rArgs.hasElements() )
183cdf0e10cSrcweir         throw RuntimeException();
184cdf0e10cSrcweir     mxComponent.set( rArgs[ 0 ], UNO_QUERY_THROW );
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir // com.sun.star.sheet.XFilterFormulaParser interface --------------------------
188cdf0e10cSrcweir 
getSupportedNamespace()189cdf0e10cSrcweir OUString SAL_CALL OOXMLFormulaParser::getSupportedNamespace() throw( RuntimeException )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir     return CREATE_OUSTRING( "http://schemas.microsoft.com/office/excel/formula" );
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // com.sun.star.sheet.XFormulaParser interface --------------------------------
195cdf0e10cSrcweir 
parseFormula(const OUString & rFormula,const CellAddress & rReferencePos)196cdf0e10cSrcweir Sequence< FormulaToken > SAL_CALL OOXMLFormulaParser::parseFormula(
197cdf0e10cSrcweir         const OUString& rFormula, const CellAddress& rReferencePos ) throw( RuntimeException )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     if( !mxParserImpl )
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         Reference< XMultiServiceFactory > xModelFactory( mxComponent, UNO_QUERY_THROW );
202cdf0e10cSrcweir         mxParserImpl.reset( new OOXMLFormulaParserImpl( xModelFactory ) );
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir     return mxParserImpl->parseFormula( rFormula, rReferencePos );
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
printFormula(const Sequence<FormulaToken> &,const CellAddress &)207cdf0e10cSrcweir OUString SAL_CALL OOXMLFormulaParser::printFormula(
208cdf0e10cSrcweir         const Sequence< FormulaToken >& /*rTokens*/, const CellAddress& /*rReferencePos*/ ) throw( RuntimeException )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     // not implemented
211cdf0e10cSrcweir     throw RuntimeException();
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir // ============================================================================
215cdf0e10cSrcweir 
216cdf0e10cSrcweir } // namespace xls
217cdf0e10cSrcweir } // namespace oox
218