xref: /trunk/main/oox/source/xls/tablebuffer.cxx (revision ca5ec200)
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 #include "oox/xls/tablebuffer.hxx"
25 
26 #include <com/sun/star/sheet/XDatabaseRange.hpp>
27 #include "oox/helper/attributelist.hxx"
28 #include "oox/helper/binaryinputstream.hxx"
29 #include "oox/helper/propertyset.hxx"
30 #include "oox/xls/addressconverter.hxx"
31 
32 namespace oox {
33 namespace xls {
34 
35 // ============================================================================
36 
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::sheet;
39 using namespace ::com::sun::star::uno;
40 
41 using ::rtl::OUString;
42 
43 // ============================================================================
44 
TableModel()45 TableModel::TableModel() :
46     mnId( -1 ),
47     mnType( XML_worksheet ),
48     mnHeaderRows( 1 ),
49     mnTotalsRows( 0 )
50 {
51 }
52 
53 // ============================================================================
54 
Table(const WorkbookHelper & rHelper)55 Table::Table( const WorkbookHelper& rHelper ) :
56     WorkbookHelper( rHelper ),
57     maAutoFilters( rHelper ),
58     mnTokenIndex( -1 )
59 {
60 }
61 
importTable(const AttributeList & rAttribs,sal_Int16 nSheet)62 void Table::importTable( const AttributeList& rAttribs, sal_Int16 nSheet )
63 {
64     getAddressConverter().convertToCellRangeUnchecked( maModel.maRange, rAttribs.getString( XML_ref, OUString() ), nSheet );
65     maModel.maProgName    = rAttribs.getXString( XML_name, OUString() );
66     maModel.maDisplayName = rAttribs.getXString( XML_displayName, OUString() );
67     maModel.mnId          = rAttribs.getInteger( XML_id, -1 );
68     maModel.mnType        = rAttribs.getToken( XML_tableType, XML_worksheet );
69     maModel.mnHeaderRows  = rAttribs.getInteger( XML_headerRowCount, 1 );
70     maModel.mnTotalsRows  = rAttribs.getInteger( XML_totalsRowCount, 0 );
71 }
72 
importTable(SequenceInputStream & rStrm,sal_Int16 nSheet)73 void Table::importTable( SequenceInputStream& rStrm, sal_Int16 nSheet )
74 {
75     BinRange aBinRange;
76     sal_Int32 nType;
77     rStrm >> aBinRange >> nType >> maModel.mnId >> maModel.mnHeaderRows >> maModel.mnTotalsRows;
78     rStrm.skip( 32 );
79     rStrm >> maModel.maProgName >> maModel.maDisplayName;
80 
81     getAddressConverter().convertToCellRangeUnchecked( maModel.maRange, aBinRange, nSheet );
82     static const sal_Int32 spnTypes[] = { XML_worksheet, XML_TOKEN_INVALID, XML_TOKEN_INVALID, XML_queryTable };
83     maModel.mnType = STATIC_ARRAY_SELECT( spnTypes, nType, XML_TOKEN_INVALID );
84 }
85 
finalizeImport()86 void Table::finalizeImport()
87 {
88     // create database range
89     if( (maModel.mnId > 0) && (maModel.maDisplayName.getLength() > 0) ) try
90     {
91         maDBRangeName = maModel.maDisplayName;
92         Reference< XDatabaseRange > xDatabaseRange( createDatabaseRangeObject( maDBRangeName, maModel.maRange ), UNO_SET_THROW );
93         maDestRange = xDatabaseRange->getDataArea();
94 
95         // get formula token index of the database range
96         PropertySet aPropSet( xDatabaseRange );
97         if( !aPropSet.getProperty( mnTokenIndex, PROP_TokenIndex ) )
98             mnTokenIndex = -1;
99 
100         // filter settings
101         maAutoFilters.finalizeImport( xDatabaseRange );
102     }
103     catch( Exception& )
104     {
105         OSL_ENSURE( false, "Table::finalizeImport - cannot create database range" );
106     }
107 }
108 
109 // ============================================================================
110 
TableBuffer(const WorkbookHelper & rHelper)111 TableBuffer::TableBuffer( const WorkbookHelper& rHelper ) :
112     WorkbookHelper( rHelper )
113 {
114 }
115 
createTable()116 Table& TableBuffer::createTable()
117 {
118     TableVector::value_type xTable( new Table( *this ) );
119     maTables.push_back( xTable );
120     return *xTable;
121 }
122 
finalizeImport()123 void TableBuffer::finalizeImport()
124 {
125     // map all tables by identifier and display name
126     for( TableVector::iterator aIt = maTables.begin(), aEnd = maTables.end(); aIt != aEnd; ++aIt )
127         insertTableToMaps( *aIt );
128     // finalize all valid tables
129     maIdTables.forEachMem( &Table::finalizeImport );
130 }
131 
getTable(sal_Int32 nTableId) const132 TableRef TableBuffer::getTable( sal_Int32 nTableId ) const
133 {
134     return maIdTables.get( nTableId );
135 }
136 
getTable(const OUString & rDispName) const137 TableRef TableBuffer::getTable( const OUString& rDispName ) const
138 {
139     return maNameTables.get( rDispName );
140 }
141 
142 // private --------------------------------------------------------------------
143 
insertTableToMaps(const TableRef & rxTable)144 void TableBuffer::insertTableToMaps( const TableRef& rxTable )
145 {
146     sal_Int32 nTableId = rxTable->getTableId();
147     const OUString& rDispName = rxTable->getDisplayName();
148     if( (nTableId > 0) && (rDispName.getLength() > 0) )
149     {
150         OSL_ENSURE( !maIdTables.has( nTableId ), "TableBuffer::insertTableToMaps - multiple table identifier" );
151         maIdTables[ nTableId ] = rxTable;
152         OSL_ENSURE( !maNameTables.has( rDispName ), "TableBuffer::insertTableToMaps - multiple table name" );
153         maNameTables[ rDispName ] = rxTable;
154     }
155 }
156 
157 // ============================================================================
158 
159 } // namespace xls
160 } // namespace oox
161