xref: /aoo42x/main/oox/source/xls/tablebuffer.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/xls/tablebuffer.hxx"
29 
30 #include <com/sun/star/sheet/XDatabaseRange.hpp>
31 #include "oox/helper/attributelist.hxx"
32 #include "oox/helper/binaryinputstream.hxx"
33 #include "oox/helper/propertyset.hxx"
34 #include "oox/xls/addressconverter.hxx"
35 
36 namespace oox {
37 namespace xls {
38 
39 // ============================================================================
40 
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::sheet;
43 using namespace ::com::sun::star::uno;
44 
45 using ::rtl::OUString;
46 
47 // ============================================================================
48 
49 TableModel::TableModel() :
50     mnId( -1 ),
51     mnType( XML_worksheet ),
52     mnHeaderRows( 1 ),
53     mnTotalsRows( 0 )
54 {
55 }
56 
57 // ============================================================================
58 
59 Table::Table( const WorkbookHelper& rHelper ) :
60     WorkbookHelper( rHelper ),
61     maAutoFilters( rHelper ),
62     mnTokenIndex( -1 )
63 {
64 }
65 
66 void Table::importTable( const AttributeList& rAttribs, sal_Int16 nSheet )
67 {
68     getAddressConverter().convertToCellRangeUnchecked( maModel.maRange, rAttribs.getString( XML_ref, OUString() ), nSheet );
69     maModel.maProgName    = rAttribs.getXString( XML_name, OUString() );
70     maModel.maDisplayName = rAttribs.getXString( XML_displayName, OUString() );
71     maModel.mnId          = rAttribs.getInteger( XML_id, -1 );
72     maModel.mnType        = rAttribs.getToken( XML_tableType, XML_worksheet );
73     maModel.mnHeaderRows  = rAttribs.getInteger( XML_headerRowCount, 1 );
74     maModel.mnTotalsRows  = rAttribs.getInteger( XML_totalsRowCount, 0 );
75 }
76 
77 void Table::importTable( SequenceInputStream& rStrm, sal_Int16 nSheet )
78 {
79     BinRange aBinRange;
80     sal_Int32 nType;
81     rStrm >> aBinRange >> nType >> maModel.mnId >> maModel.mnHeaderRows >> maModel.mnTotalsRows;
82     rStrm.skip( 32 );
83     rStrm >> maModel.maProgName >> maModel.maDisplayName;
84 
85     getAddressConverter().convertToCellRangeUnchecked( maModel.maRange, aBinRange, nSheet );
86     static const sal_Int32 spnTypes[] = { XML_worksheet, XML_TOKEN_INVALID, XML_TOKEN_INVALID, XML_queryTable };
87     maModel.mnType = STATIC_ARRAY_SELECT( spnTypes, nType, XML_TOKEN_INVALID );
88 }
89 
90 void Table::finalizeImport()
91 {
92     // create database range
93     if( (maModel.mnId > 0) && (maModel.maDisplayName.getLength() > 0) ) try
94     {
95         maDBRangeName = maModel.maDisplayName;
96         Reference< XDatabaseRange > xDatabaseRange( createDatabaseRangeObject( maDBRangeName, maModel.maRange ), UNO_SET_THROW );
97         maDestRange = xDatabaseRange->getDataArea();
98 
99         // get formula token index of the database range
100         PropertySet aPropSet( xDatabaseRange );
101         if( !aPropSet.getProperty( mnTokenIndex, PROP_TokenIndex ) )
102             mnTokenIndex = -1;
103 
104         // filter settings
105         maAutoFilters.finalizeImport( xDatabaseRange );
106     }
107     catch( Exception& )
108     {
109         OSL_ENSURE( false, "Table::finalizeImport - cannot create database range" );
110     }
111 }
112 
113 // ============================================================================
114 
115 TableBuffer::TableBuffer( const WorkbookHelper& rHelper ) :
116     WorkbookHelper( rHelper )
117 {
118 }
119 
120 Table& TableBuffer::createTable()
121 {
122     TableVector::value_type xTable( new Table( *this ) );
123     maTables.push_back( xTable );
124     return *xTable;
125 }
126 
127 void TableBuffer::finalizeImport()
128 {
129     // map all tables by identifier and display name
130     for( TableVector::iterator aIt = maTables.begin(), aEnd = maTables.end(); aIt != aEnd; ++aIt )
131         insertTableToMaps( *aIt );
132     // finalize all valid tables
133     maIdTables.forEachMem( &Table::finalizeImport );
134 }
135 
136 TableRef TableBuffer::getTable( sal_Int32 nTableId ) const
137 {
138     return maIdTables.get( nTableId );
139 }
140 
141 TableRef TableBuffer::getTable( const OUString& rDispName ) const
142 {
143     return maNameTables.get( rDispName );
144 }
145 
146 // private --------------------------------------------------------------------
147 
148 void TableBuffer::insertTableToMaps( const TableRef& rxTable )
149 {
150     sal_Int32 nTableId = rxTable->getTableId();
151     const OUString& rDispName = rxTable->getDisplayName();
152     if( (nTableId > 0) && (rDispName.getLength() > 0) )
153     {
154         OSL_ENSURE( !maIdTables.has( nTableId ), "TableBuffer::insertTableToMaps - multiple table identifier" );
155         maIdTables[ nTableId ] = rxTable;
156         OSL_ENSURE( !maNameTables.has( rDispName ), "TableBuffer::insertTableToMaps - multiple table name" );
157         maNameTables[ rDispName ] = rxTable;
158     }
159 }
160 
161 // ============================================================================
162 
163 } // namespace xls
164 } // namespace oox
165