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 : #ifndef OOX_XLS_FORMULAPARSER_HXX
21 : #define OOX_XLS_FORMULAPARSER_HXX
22 :
23 : #include "formulabase.hxx"
24 :
25 : namespace oox {
26 : namespace xls {
27 :
28 : // formula finalizer ==========================================================
29 :
30 : /** A generic formula token array finalizer.
31 :
32 : After building a formula token array from alien binary file formats, or
33 : parsing an XML formula string using the com.sun.star.sheet.FormulaParser
34 : service, the token array is still not ready to be put into the spreadsheet
35 : document. There may be functions with a wrong number of parameters (missing
36 : but required parameters, or unsupported parameters) or intermediate tokens
37 : used to encode references to macro functions or add-in functions. This
38 : helper processes a passed token array and builds a new compatible token
39 : array.
40 :
41 : Derived classes may add more functionality by overwriting the virtual
42 : functions.
43 : */
44 11 : class FormulaFinalizer : public OpCodeProvider, protected ApiOpCodes
45 : {
46 : public:
47 : explicit FormulaFinalizer( const OpCodeProvider& rOpCodeProv );
48 :
49 : /** Finalizes and returns the passed token array. */
50 : ApiTokenSequence finalizeTokenArray( const ApiTokenSequence& rTokens );
51 :
52 : protected:
53 : /** Derived classed may try to find a function info struct from the passed
54 : string extracted from an OPCODE_BAD token.
55 :
56 : @param rTokenData The string that has been found in an OPCODE_BAD
57 : token preceding the function parentheses.
58 : */
59 : virtual const FunctionInfo* resolveBadFuncName( const ::rtl::OUString& rTokenData ) const;
60 :
61 : /** Derived classed may try to find the name of a defined name with the
62 : passed index extracted from an OPCODE_NAME token.
63 :
64 : @param nTokenIndex The index of the defined name that has been found
65 : in an OPCODE_NAME token preceding the function parentheses.
66 : */
67 : virtual ::rtl::OUString resolveDefinedName( sal_Int32 nTokenIndex ) const;
68 :
69 : private:
70 : typedef ::std::vector< const ApiToken* > ParameterPosVector;
71 :
72 : const FunctionInfo* getFunctionInfo( ApiToken& orFuncToken );
73 : const FunctionInfo* getExternCallInfo( ApiToken& orFuncToken, const ApiToken& rECToken );
74 :
75 : void processTokens( const ApiToken* pToken, const ApiToken* pTokenEnd );
76 : const ApiToken* processParameters( const FunctionInfo& rFuncInfo, const ApiToken* pToken, const ApiToken* pTokenEnd );
77 :
78 : bool isEmptyParameter( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
79 : const ApiToken* getSingleToken( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
80 : const ApiToken* skipParentheses( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
81 : const ApiToken* findParameters( ParameterPosVector& rParams, const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
82 : void appendEmptyParameter( const FunctionInfo& rFuncInfo, size_t nParam );
83 : void appendCalcOnlyParameter( const FunctionInfo& rFuncInfo, size_t nParam );
84 : void appendRequiredParameters( const FunctionInfo& rFuncInfo, size_t nParamCount );
85 :
86 : bool appendFinalToken( const ApiToken& rToken );
87 :
88 : private:
89 : ApiTokenVector maTokens;
90 : };
91 :
92 : // ============================================================================
93 :
94 : class FormulaParserImpl;
95 :
96 : /** Import formula parser for OOXML and BIFF filters.
97 :
98 : This class implements formula import for the OOXML and BIFF filter. One
99 : instance is contained in the global filter data to prevent construction and
100 : destruction of internal buffers for every imported formula.
101 : */
102 : class FormulaParser : public FormulaProcessorBase
103 : {
104 : public:
105 : explicit FormulaParser( const WorkbookHelper& rHelper );
106 : virtual ~FormulaParser();
107 :
108 : /** Converts an OOXML formula string. */
109 : ApiTokenSequence importFormula(
110 : const ::com::sun::star::table::CellAddress& rBaseAddr,
111 : const ::rtl::OUString& rFormulaString ) const;
112 :
113 : /** Imports and converts a BIFF12 token array from the passed stream. */
114 : ApiTokenSequence importFormula(
115 : const ::com::sun::star::table::CellAddress& rBaseAddr,
116 : FormulaType eType,
117 : SequenceInputStream& rStrm ) const;
118 :
119 : /** Imports and converts a BIFF2-BIFF8 token array from the passed stream.
120 : @param pnFmlaSize Size of the token array. If null is passed, reads
121 : it from stream (1 byte in BIFF2, 2 bytes otherwise) first. */
122 : ApiTokenSequence importFormula(
123 : const ::com::sun::star::table::CellAddress& rBaseAddr,
124 : FormulaType eType,
125 : BiffInputStream& rStrm,
126 : const sal_uInt16* pnFmlaSize = 0 ) const;
127 :
128 : /** Converts the passed Boolean value to a similar formula. */
129 : ApiTokenSequence convertBoolToFormula( bool bValue ) const;
130 :
131 : /** Converts the passed BIFF error code to a similar formula. */
132 : ApiTokenSequence convertErrorToFormula( sal_uInt8 nErrorCode ) const;
133 :
134 : /** Converts the passed token index of a defined name to a formula calling that name. */
135 : ApiTokenSequence convertNameToFormula( sal_Int32 nTokenIndex ) const;
136 :
137 : /** Converts the passed XML formula to an OLE link target. */
138 : ::rtl::OUString importOleTargetLink( const ::rtl::OUString& rFormulaString );
139 :
140 : /** Imports and converts an OLE link target from the passed stream. */
141 : ::rtl::OUString importOleTargetLink( SequenceInputStream& rStrm );
142 :
143 : /** Converts the passed formula to a macro name for a drawing shape. */
144 : ::rtl::OUString importMacroName( const ::rtl::OUString& rFormulaString );
145 :
146 : private:
147 : ::std::auto_ptr< FormulaParserImpl > mxImpl;
148 : };
149 :
150 : // ============================================================================
151 :
152 : } // namespace xls
153 : } // namespace oox
154 :
155 : #endif
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|