Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef OOX_XLS_FORMULAPARSER_HXX
30 : : #define OOX_XLS_FORMULAPARSER_HXX
31 : :
32 : : #include "formulabase.hxx"
33 : :
34 : : namespace oox {
35 : : namespace xls {
36 : :
37 : : // formula finalizer ==========================================================
38 : :
39 : : /** A generic formula token array finalizer.
40 : :
41 : : After building a formula token array from alien binary file formats, or
42 : : parsing an XML formula string using the com.sun.star.sheet.FormulaParser
43 : : service, the token array is still not ready to be put into the spreadsheet
44 : : document. There may be functions with a wrong number of parameters (missing
45 : : but required parameters, or unsupported parameters) or intermediate tokens
46 : : used to encode references to macro functions or add-in functions. This
47 : : helper processes a passed token array and builds a new compatible token
48 : : array.
49 : :
50 : : Derived classes may add more functionality by overwriting the virtual
51 : : functions.
52 : : */
53 [ - + ]: 24 : class FormulaFinalizer : public OpCodeProvider, protected ApiOpCodes
54 : : {
55 : : public:
56 : : explicit FormulaFinalizer( const OpCodeProvider& rOpCodeProv );
57 : :
58 : : /** Finalizes and returns the passed token array. */
59 : : ApiTokenSequence finalizeTokenArray( const ApiTokenSequence& rTokens );
60 : :
61 : : protected:
62 : : /** Derived classed may try to find a function info struct from the passed
63 : : string extracted from an OPCODE_BAD token.
64 : :
65 : : @param rTokenData The string that has been found in an OPCODE_BAD
66 : : token preceding the function parentheses.
67 : : */
68 : : virtual const FunctionInfo* resolveBadFuncName( const ::rtl::OUString& rTokenData ) const;
69 : :
70 : : /** Derived classed may try to find the name of a defined name with the
71 : : passed index extracted from an OPCODE_NAME token.
72 : :
73 : : @param nTokenIndex The index of the defined name that has been found
74 : : in an OPCODE_NAME token preceding the function parentheses.
75 : : */
76 : : virtual ::rtl::OUString resolveDefinedName( sal_Int32 nTokenIndex ) const;
77 : :
78 : : private:
79 : : typedef ::std::vector< const ApiToken* > ParameterPosVector;
80 : :
81 : : const FunctionInfo* getFunctionInfo( ApiToken& orFuncToken );
82 : : const FunctionInfo* getExternCallInfo( ApiToken& orFuncToken, const ApiToken& rECToken );
83 : :
84 : : void processTokens( const ApiToken* pToken, const ApiToken* pTokenEnd );
85 : : const ApiToken* processParameters( const FunctionInfo& rFuncInfo, const ApiToken* pToken, const ApiToken* pTokenEnd );
86 : :
87 : : bool isEmptyParameter( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
88 : : const ApiToken* getSingleToken( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
89 : : const ApiToken* skipParentheses( const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
90 : : const ApiToken* findParameters( ParameterPosVector& rParams, const ApiToken* pToken, const ApiToken* pTokenEnd ) const;
91 : : void appendEmptyParameter( const FunctionInfo& rFuncInfo, size_t nParam );
92 : : void appendCalcOnlyParameter( const FunctionInfo& rFuncInfo, size_t nParam );
93 : : void appendRequiredParameters( const FunctionInfo& rFuncInfo, size_t nParamCount );
94 : :
95 : : bool appendFinalToken( const ApiToken& rToken );
96 : :
97 : : private:
98 : : ApiTokenVector maTokens;
99 : : };
100 : :
101 : : // ============================================================================
102 : :
103 : : class FormulaParserImpl;
104 : :
105 : : /** Import formula parser for OOXML and BIFF filters.
106 : :
107 : : This class implements formula import for the OOXML and BIFF filter. One
108 : : instance is contained in the global filter data to prevent construction and
109 : : destruction of internal buffers for every imported formula.
110 : : */
111 : : class FormulaParser : public FormulaProcessorBase
112 : : {
113 : : public:
114 : : explicit FormulaParser( const WorkbookHelper& rHelper );
115 : : virtual ~FormulaParser();
116 : :
117 : : /** Converts an OOXML formula string. */
118 : : ApiTokenSequence importFormula(
119 : : const ::com::sun::star::table::CellAddress& rBaseAddr,
120 : : const ::rtl::OUString& rFormulaString ) const;
121 : :
122 : : /** Imports and converts a BIFF12 token array from the passed stream. */
123 : : ApiTokenSequence importFormula(
124 : : const ::com::sun::star::table::CellAddress& rBaseAddr,
125 : : FormulaType eType,
126 : : SequenceInputStream& rStrm ) const;
127 : :
128 : : /** Imports and converts a BIFF2-BIFF8 token array from the passed stream.
129 : : @param pnFmlaSize Size of the token array. If null is passed, reads
130 : : it from stream (1 byte in BIFF2, 2 bytes otherwise) first. */
131 : : ApiTokenSequence importFormula(
132 : : const ::com::sun::star::table::CellAddress& rBaseAddr,
133 : : FormulaType eType,
134 : : BiffInputStream& rStrm,
135 : : const sal_uInt16* pnFmlaSize = 0 ) const;
136 : :
137 : : /** Converts the passed Boolean value to a similar formula. */
138 : : ApiTokenSequence convertBoolToFormula( bool bValue ) const;
139 : :
140 : : /** Converts the passed BIFF error code to a similar formula. */
141 : : ApiTokenSequence convertErrorToFormula( sal_uInt8 nErrorCode ) const;
142 : :
143 : : /** Converts the passed token index of a defined name to a formula calling that name. */
144 : : ApiTokenSequence convertNameToFormula( sal_Int32 nTokenIndex ) const;
145 : :
146 : : /** Converts the passed XML formula to an OLE link target. */
147 : : ::rtl::OUString importOleTargetLink( const ::rtl::OUString& rFormulaString );
148 : :
149 : : /** Imports and converts an OLE link target from the passed stream. */
150 : : ::rtl::OUString importOleTargetLink( SequenceInputStream& rStrm );
151 : :
152 : : /** Converts the passed formula to a macro name for a drawing shape. */
153 : : ::rtl::OUString importMacroName( const ::rtl::OUString& rFormulaString );
154 : :
155 : : private:
156 : : ::std::auto_ptr< FormulaParserImpl > mxImpl;
157 : : };
158 : :
159 : : // ============================================================================
160 : :
161 : : } // namespace xls
162 : : } // namespace oox
163 : :
164 : : #endif
165 : :
166 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|