xref: /trunk/main/oox/inc/oox/xls/formulaparser.hxx (revision e3508121)
1*e3508121SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e3508121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e3508121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e3508121SAndrew Rist  * distributed with this work for additional information
6*e3508121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e3508121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e3508121SAndrew Rist  * "License"); you may not use this file except in compliance
9*e3508121SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e3508121SAndrew Rist  *
11*e3508121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e3508121SAndrew Rist  *
13*e3508121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e3508121SAndrew Rist  * software distributed under the License is distributed on an
15*e3508121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e3508121SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e3508121SAndrew Rist  * specific language governing permissions and limitations
18*e3508121SAndrew Rist  * under the License.
19*e3508121SAndrew Rist  *
20*e3508121SAndrew Rist  *************************************************************/
21*e3508121SAndrew Rist 
22*e3508121SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef OOX_XLS_FORMULAPARSER_HXX
25cdf0e10cSrcweir #define OOX_XLS_FORMULAPARSER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "oox/xls/formulabase.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace oox {
30cdf0e10cSrcweir namespace xls {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // formula finalizer ==========================================================
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /** A generic formula token array finalizer.
35cdf0e10cSrcweir 
36cdf0e10cSrcweir     After building a formula token array from alien binary file formats, or
37cdf0e10cSrcweir     parsing an XML formula string using the com.sun.star.sheet.FormulaParser
38cdf0e10cSrcweir     service, the token array is still not ready to be put into the spreadsheet
39cdf0e10cSrcweir     document. There may be functions with a wrong number of parameters (missing
40cdf0e10cSrcweir     but required parameters, or unsupported parameters) or intermediate tokens
41cdf0e10cSrcweir     used to encode references to macro functions or add-in functions. This
42cdf0e10cSrcweir     helper processes a passed token array and builds a new compatible token
43cdf0e10cSrcweir     array.
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     Derived classes may add more functionality by overwriting the virtual
46cdf0e10cSrcweir     functions.
47cdf0e10cSrcweir  */
48cdf0e10cSrcweir class FormulaFinalizer : public OpCodeProvider, protected ApiOpCodes
49cdf0e10cSrcweir {
50cdf0e10cSrcweir public:
51cdf0e10cSrcweir     explicit            FormulaFinalizer( const OpCodeProvider& rOpCodeProv );
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     /** Finalizes and returns the passed token array. */
54cdf0e10cSrcweir     ApiTokenSequence    finalizeTokenArray( const ApiTokenSequence& rTokens );
55cdf0e10cSrcweir 
56cdf0e10cSrcweir protected:
57cdf0e10cSrcweir     /** Derived classed may try to find a function info struct from the passed
58cdf0e10cSrcweir         string extracted from an OPCODE_BAD token.
59cdf0e10cSrcweir 
60cdf0e10cSrcweir         @param rTokenData  The string that has been found in an OPCODE_BAD
61cdf0e10cSrcweir             token preceding the function parentheses.
62cdf0e10cSrcweir      */
63cdf0e10cSrcweir     virtual const FunctionInfo* resolveBadFuncName( const ::rtl::OUString& rTokenData ) const;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /** Derived classed may try to find the name of a defined name with the
66cdf0e10cSrcweir         passed index extracted from an OPCODE_NAME token.
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         @param nTokenIndex  The index of the defined name that has been found
69cdf0e10cSrcweir             in an OPCODE_NAME token preceding the function parentheses.
70cdf0e10cSrcweir      */
71cdf0e10cSrcweir     virtual ::rtl::OUString resolveDefinedName( sal_Int32 nTokenIndex ) const;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir private:
74cdf0e10cSrcweir     typedef ::std::vector< const ApiToken* > ParameterPosVector;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     const FunctionInfo* getFunctionInfo( ApiToken& orFuncToken );
77cdf0e10cSrcweir     const FunctionInfo* getExternCallInfo( ApiToken& orFuncToken, const ApiToken& rECToken );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     void                processTokens( const ApiToken* pToken, const ApiToken* pTokenEnd );
80cdf0e10cSrcweir     const ApiToken*     processParameters( const FunctionInfo& rFuncInfo, const ApiToken* pToken, const ApiToken* pTokenEnd );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     bool                isEmptyParameter( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
83cdf0e10cSrcweir     const ApiToken*     getSingleToken( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
84cdf0e10cSrcweir     const ApiToken*     skipParentheses( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
85cdf0e10cSrcweir     const ApiToken*     findParameters( ParameterPosVector& rParams, const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
86cdf0e10cSrcweir     void                appendEmptyParameter( const FunctionInfo& rFuncInfo, size_t nParam );
87cdf0e10cSrcweir     void                appendCalcOnlyParameter( const FunctionInfo& rFuncInfo, size_t nParam );
88cdf0e10cSrcweir     void                appendRequiredParameters( const FunctionInfo& rFuncInfo, size_t nParamCount );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     bool                appendFinalToken( const ApiToken& rToken );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir private:
93cdf0e10cSrcweir     ApiTokenVector      maTokens;
94cdf0e10cSrcweir };
95cdf0e10cSrcweir 
96cdf0e10cSrcweir // ============================================================================
97cdf0e10cSrcweir 
98cdf0e10cSrcweir class FormulaParserImpl;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir /** Import formula parser for OOXML and BIFF filters.
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     This class implements formula import for the OOXML and BIFF filter. One
103cdf0e10cSrcweir     instance is contained in the global filter data to prevent construction and
104cdf0e10cSrcweir     destruction of internal buffers for every imported formula.
105cdf0e10cSrcweir  */
106cdf0e10cSrcweir class FormulaParser : public FormulaProcessorBase
107cdf0e10cSrcweir {
108cdf0e10cSrcweir public:
109cdf0e10cSrcweir     explicit            FormulaParser( const WorkbookHelper& rHelper );
110cdf0e10cSrcweir     virtual             ~FormulaParser();
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     /** Converts an OOXML formula string. */
113cdf0e10cSrcweir     ApiTokenSequence    importFormula(
114cdf0e10cSrcweir                             const ::com::sun::star::table::CellAddress& rBaseAddr,
115cdf0e10cSrcweir                             const ::rtl::OUString& rFormulaString ) const;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /** Imports and converts a BIFF12 token array from the passed stream. */
118cdf0e10cSrcweir     ApiTokenSequence    importFormula(
119cdf0e10cSrcweir                             const ::com::sun::star::table::CellAddress& rBaseAddr,
120cdf0e10cSrcweir                             FormulaType eType,
121cdf0e10cSrcweir                             SequenceInputStream& rStrm ) const;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     /** Imports and converts a BIFF2-BIFF8 token array from the passed stream.
124cdf0e10cSrcweir         @param pnFmlaSize  Size of the token array. If null is passed, reads
125cdf0e10cSrcweir         it from stream (1 byte in BIFF2, 2 bytes otherwise) first. */
126cdf0e10cSrcweir     ApiTokenSequence    importFormula(
127cdf0e10cSrcweir                             const ::com::sun::star::table::CellAddress& rBaseAddr,
128cdf0e10cSrcweir                             FormulaType eType,
129cdf0e10cSrcweir                             BiffInputStream& rStrm,
130cdf0e10cSrcweir                             const sal_uInt16* pnFmlaSize = 0 ) const;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     /** Converts the passed Boolean value to a similar formula. */
133cdf0e10cSrcweir     ApiTokenSequence    convertBoolToFormula( bool bValue ) const;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     /** Converts the passed BIFF error code to a similar formula. */
136cdf0e10cSrcweir     ApiTokenSequence    convertErrorToFormula( sal_uInt8 nErrorCode ) const;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     /** Converts the passed token index of a defined name to a formula calling that name. */
139cdf0e10cSrcweir     ApiTokenSequence    convertNameToFormula( sal_Int32 nTokenIndex ) const;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     /** Converts the passed number into a HYPERLINK formula with the passed URL. */
142cdf0e10cSrcweir     ApiTokenSequence    convertNumberToHyperlink( const ::rtl::OUString& rUrl, double fValue ) const;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     /** Converts the passed XML formula to an OLE link target. */
145cdf0e10cSrcweir     ::rtl::OUString     importOleTargetLink( const ::rtl::OUString& rFormulaString );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     /** Imports and converts an OLE link target from the passed stream. */
148cdf0e10cSrcweir     ::rtl::OUString     importOleTargetLink( SequenceInputStream& rStrm );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     /** Imports and converts an OLE link target from the passed stream. */
151cdf0e10cSrcweir     ::rtl::OUString     importOleTargetLink( BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize = 0 ) const;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     /** Converts the passed formula to a macro name for a drawing shape. */
154cdf0e10cSrcweir     ::rtl::OUString     importMacroName( const ::rtl::OUString& rFormulaString );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir private:
157cdf0e10cSrcweir     ::std::auto_ptr< FormulaParserImpl > mxImpl;
158cdf0e10cSrcweir };
159cdf0e10cSrcweir 
160cdf0e10cSrcweir // ============================================================================
161cdf0e10cSrcweir 
162cdf0e10cSrcweir } // namespace xls
163cdf0e10cSrcweir } // namespace oox
164cdf0e10cSrcweir 
165cdf0e10cSrcweir #endif
166