xref: /aoo41x/main/sc/source/filter/inc/xilink.hxx (revision 38d50f7b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SC_XILINK_HXX
25 #define SC_XILINK_HXX
26 
27 #include <map>
28 #include "xllink.hxx"
29 #include "xiroot.hxx"
30 
31 /* ============================================================================
32 Classes for import of different kinds of internal/external references.
33 - 3D cell and cell range links
34 - External cell and cell range links
35 - External defined names
36 - Add-in functions
37 - DDE links
38 - OLE object links
39 ============================================================================ */
40 
41 // Excel sheet indexes ========================================================
42 
43 /** A buffer containing information about names and creation order of sheets.
44 
45     The first purpose of this buffer is to translate original Excel
46     sheet names into Calc sheet indexes. This is not trivial because the filter
47     may rename the Calc sheets during creation. This buffer stores the original
48     Excel sheet names with the corresponding Calc sheet indexes.
49 
50     The second purpose is to store the creation order of all sheets inside the
51     Excel workbook. The creation order list is contained in the TABID record
52     and needed to import the change log. Example: If the list contains 3;1;2
53     this means that the second sheet in the file was created first, than the
54     third sheet in the file was created and finally the first sheet.
55  */
56 class XclImpTabInfo
57 {
58 public:
59     // original Excel sheet names ---------------------------------------------
60 
61     /** Appends an original Excel sheet name with corresponding Calc sheet index. */
62     void                AppendXclTabName( const String& rXclTabName, SCTAB nScTab );
63     /** Inserts a Calc sheet index (increases all following sheet indexes). */
64     void                InsertScTab( SCTAB nScTab );
65 
66     /** Returns the Calc sheet index from the passed original Excel sheet name. */
67     SCTAB               GetScTabFromXclName( const String& rXclTabName ) const;
68 
69     // record creation order - TABID record -----------------------------------
70 
71     /** Reads the TABID record. */
72     void                ReadTabid( XclImpStream& rStrm );
73 
74     /** Returns the current sheet index calculated from creation index.
75         @param nCreatedId  The creation index of the sheet (1-based).
76         @param nMaxTabId  All values greater than this parameter are not used to find the index.
77         @return  The 0-based index of the sheet nCreatedId if it is contained in the list.
78         Example: The buffer is 3;5;2;4;1, nCreatedId is 1 and nMaxTabId is 3. The function will
79         return 2 which is the 0-based index of sheet 1 in the list 3;2;1. */
80     sal_uInt16          GetCurrentIndex( sal_uInt16 nCreatedId, sal_uInt16 nMaxTabId = 0xFFFF ) const;
81 
82 private:
83     typedef ::std::map< String, SCTAB > XclTabNameMap;
84 
85     XclTabNameMap       maTabNames;     /// All Excel sheet names with Calc sheet index.
86     ScfUInt16Vec        maTabIdVec;     /// The vector with sheet indexes.
87 };
88 
89 // External names =============================================================
90 
91 /** Type of an external name. */
92 enum XclImpExtNameType
93 {
94     xlExtName,                  /// An external defined name.
95     xlExtAddIn,                 /// An add-in function name.
96     xlExtDDE,                   /// A DDE link range.
97     xlExtOLE,                   /// An OLE object link.
98     xlExtEuroConvert            /// An external in Excel, but internal in OO function name.
99 };
100 
101 // ----------------------------------------------------------------------------
102 
103 class XclImpCachedMatrix;
104 class ScTokenArray;
105 class XclImpSupbook;
106 
107 /** Stores contents of an external name.
108     @descr Supported: External defined names, AddIn names, DDE links and OLE objects. */
109 class XclImpExtName
110 {
111 public:
112     /** Reads the external name from the stream. */
113     explicit            XclImpExtName( const XclImpSupbook& rSupbook, XclImpStream& rStrm,
114                                         XclSupbookType eSubType, ExcelToSc* pFormulaConv );
115                         ~XclImpExtName();
116 
117     /** Create and apply the cached list of this DDE Link to the document. */
118     void                CreateDdeData( ScDocument& rDoc,
119                             const String& rApplc, const String& rExtDoc ) const;
120 
121     void                CreateExtNameData( ScDocument& rDoc, sal_uInt16 nFileId ) const;
122 
123     bool                HasFormulaTokens() const;
124 
GetType() const125     inline XclImpExtNameType GetType() const { return meType; }
GetName() const126     inline const String& GetName() const { return maName; }
GetStorageId() const127     inline sal_uInt32   GetStorageId() const { return mnStorageId; }
128 
129 private:
130     typedef ::std::auto_ptr< XclImpCachedMatrix > XclImpCachedMatrixPtr;
131     typedef ::std::auto_ptr< ScTokenArray >       TokenArrayPtr;
132 
133     XclImpCachedMatrixPtr mxDdeMatrix;      /// Cached results of the DDE link.
134     TokenArrayPtr       mxArray;            /// Formula tokens for external name.
135     String              maName;             /// The name of the external name.
136     sal_uInt32          mnStorageId;        /// Storage ID for OLE object storages.
137     XclImpExtNameType   meType;             /// Type of the external name.
138 };
139 
140 // Import link manager ========================================================
141 
142 class XclImpLinkManagerImpl;
143 
144 /** This is the central class for the import of all internal/external links.
145     @descr  This manager stores all data about external documents with their sheets
146     and cached cell contents. Additionally it handles external names, such as add-in
147     function names, DDE links, and OLE object links.
148     File contents in BIFF8:
149     - Record SUPBOOK: Contains the name of an external document and the names of its sheets.
150     This record is optionally followed by NAME, EXTERNNAME, XCT and CRN records.
151     - Record XCT: Contains the number and sheet index of the following CRN records.
152     - Record CRN: Contains addresses (row and column) and values of external referenced cells.
153     - Record NAME: Contains defined names of the own workbook.
154     - Record EXTERNNAME: Contains external defined names, DDE links, or OLE object links.
155     - Record EXTERNSHEET: Contains indexes to URLs of external documents (SUPBOOKs)
156     and sheet indexes for each external reference used anywhere in the workbook.
157     This record follows a list of SUPBOOK records (with their attached records).
158 */
159 class XclImpLinkManager : protected XclImpRoot
160 {
161 public:
162     explicit            XclImpLinkManager( const XclImpRoot& rRoot );
163                         ~XclImpLinkManager();
164 
165     /** Reads the EXTERNSHEET record. */
166     void                ReadExternsheet( XclImpStream& rStrm );
167     /** Reads a SUPBOOK record. */
168     void                ReadSupbook( XclImpStream& rStrm );
169     /** Reads an XCT record and appends it to the current SUPBOOK. */
170     void                ReadXct( XclImpStream& rStrm );
171     /** Reads a CRN record and appends it to the current SUPBOOK. */
172     void                ReadCrn( XclImpStream& rStrm );
173     /** Reads an EXTERNNAME record and appends it to the current SUPBOOK. */
174     void                ReadExternname( XclImpStream& rStrm, ExcelToSc* pFormulaConv = NULL );
175 
176     /** Returns true, if the specified XTI entry contains an internal reference. */
177     bool                IsSelfRef( sal_uInt16 nXtiIndex ) const;
178     /** Returns the Calc sheet index range of the specified XTI entry.
179         @return  true = XTI data found, returned sheet index range is valid. */
180     bool                GetScTabRange(
181                             SCTAB& rnFirstScTab, SCTAB& rnLastScTab,
182                             sal_uInt16 nXtiIndex ) const;
183     /** Returns the specified external name or 0 on error. */
184     const XclImpExtName* GetExternName( sal_uInt16 nXtiIndex, sal_uInt16 nExtName ) const;
185 
186     const String* GetSupbookUrl( sal_uInt16 nXtiIndex ) const;
187 
188     const String& GetSupbookTabName( sal_uInt16 nXti, sal_uInt16 nXtiTab ) const;
189 
190     /** Tries to decode the URL of the specified XTI entry to OLE or DDE link components.
191         @descr  For DDE links: Decodes to application name and topic.
192         For OLE object links: Decodes to class name and document URL.
193         @return  true = decoding was successful, returned strings are valid (not empty). */
194     bool                GetLinkData( String& rApplic, String& rTopic, sal_uInt16 nXtiIndex ) const;
195     /** Returns the specified macro name or an empty string on error. */
196     const String&       GetMacroName( sal_uInt16 nExtSheet, sal_uInt16 nExtName ) const;
197 
198 private:
199     typedef ::std::auto_ptr< XclImpLinkManagerImpl > XclImpLinkMgrImplPtr;
200     XclImpLinkMgrImplPtr mxImpl;
201 };
202 
203 // ============================================================================
204 
205 #endif
206 
207