Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "tablebuffer.hxx"
21 :
22 : #include <com/sun/star/sheet/XDatabaseRange.hpp>
23 : #include <com/sun/star/sheet/XDatabaseRanges.hpp>
24 : #include <oox/helper/attributelist.hxx>
25 : #include <oox/helper/binaryinputstream.hxx>
26 : #include <oox/helper/propertyset.hxx>
27 : #include <oox/token/properties.hxx>
28 : #include "addressconverter.hxx"
29 :
30 : namespace oox {
31 : namespace xls {
32 :
33 : using namespace ::com::sun::star::container;
34 : using namespace ::com::sun::star::sheet;
35 : using namespace ::com::sun::star::uno;
36 :
37 0 : TableModel::TableModel() :
38 : mnId( -1 ),
39 : mnType( XML_worksheet ),
40 : mnHeaderRows( 1 ),
41 0 : mnTotalsRows( 0 )
42 : {
43 0 : }
44 :
45 0 : Table::Table( const WorkbookHelper& rHelper ) :
46 : WorkbookHelper( rHelper ),
47 : maAutoFilters( rHelper ),
48 0 : mnTokenIndex( -1 )
49 : {
50 0 : }
51 :
52 0 : void Table::importTable( const AttributeList& rAttribs, sal_Int16 nSheet )
53 : {
54 0 : getAddressConverter().convertToCellRangeUnchecked( maModel.maRange, rAttribs.getString( XML_ref, OUString() ), nSheet );
55 0 : maModel.maProgName = rAttribs.getXString( XML_name, OUString() );
56 0 : maModel.maDisplayName = rAttribs.getXString( XML_displayName, OUString() );
57 0 : maModel.mnId = rAttribs.getInteger( XML_id, -1 );
58 0 : maModel.mnType = rAttribs.getToken( XML_tableType, XML_worksheet );
59 0 : maModel.mnHeaderRows = rAttribs.getInteger( XML_headerRowCount, 1 );
60 0 : maModel.mnTotalsRows = rAttribs.getInteger( XML_totalsRowCount, 0 );
61 0 : }
62 :
63 0 : void Table::importTable( SequenceInputStream& rStrm, sal_Int16 nSheet )
64 : {
65 0 : BinRange aBinRange;
66 : sal_Int32 nType;
67 0 : rStrm >> aBinRange >> nType >> maModel.mnId >> maModel.mnHeaderRows >> maModel.mnTotalsRows;
68 0 : rStrm.skip( 32 );
69 0 : rStrm >> maModel.maProgName >> maModel.maDisplayName;
70 :
71 0 : getAddressConverter().convertToCellRangeUnchecked( maModel.maRange, aBinRange, nSheet );
72 : static const sal_Int32 spnTypes[] = { XML_worksheet, XML_TOKEN_INVALID, XML_TOKEN_INVALID, XML_queryTable };
73 0 : maModel.mnType = STATIC_ARRAY_SELECT( spnTypes, nType, XML_TOKEN_INVALID );
74 0 : }
75 :
76 0 : void Table::finalizeImport()
77 : {
78 : // Create database range. Note that Excel 2007 and later names database
79 : // ranges (or tables in their terminology) as Table1, Table2 etc. We need
80 : // to import them as named db ranges because they may be referenced by
81 : // name in formula expressions.
82 0 : if( (maModel.mnId > 0) && !maModel.maDisplayName.isEmpty() ) try
83 : {
84 0 : maDBRangeName = maModel.maDisplayName;
85 : Reference< XDatabaseRange > xDatabaseRange(
86 0 : createDatabaseRangeObject( maDBRangeName, maModel.maRange ), UNO_SET_THROW);
87 0 : maDestRange = xDatabaseRange->getDataArea();
88 :
89 : // get formula token index of the database range
90 0 : PropertySet aPropSet( xDatabaseRange );
91 0 : if( !aPropSet.getProperty( mnTokenIndex, PROP_TokenIndex ) )
92 0 : mnTokenIndex = -1;
93 : }
94 0 : catch( Exception& )
95 : {
96 : OSL_FAIL( "Table::finalizeImport - cannot create database range" );
97 : }
98 0 : }
99 :
100 0 : void Table::applyAutoFilters()
101 : {
102 0 : if( !maDBRangeName.isEmpty() )
103 : {
104 : try
105 : {
106 : // get the range ( maybe we should cache the xDatabaseRange from finalizeImport )
107 0 : PropertySet aDocProps( getDocument() );
108 0 : Reference< XDatabaseRanges > xDatabaseRanges( aDocProps.getAnyProperty( PROP_DatabaseRanges ), UNO_QUERY_THROW );
109 0 : Reference< XDatabaseRange > xDatabaseRange( xDatabaseRanges->getByName( maDBRangeName ), UNO_QUERY );
110 0 : maAutoFilters.finalizeImport( xDatabaseRange );
111 : }
112 0 : catch( Exception& )
113 : {
114 : OSL_FAIL( "Table::applyAutofilters - cannot create filter" );
115 : }
116 : }
117 0 : }
118 :
119 128 : TableBuffer::TableBuffer( const WorkbookHelper& rHelper ) :
120 128 : WorkbookHelper( rHelper )
121 : {
122 128 : }
123 :
124 0 : Table& TableBuffer::createTable()
125 : {
126 0 : TableVector::value_type xTable( new Table( *this ) );
127 0 : maTables.push_back( xTable );
128 0 : return *xTable;
129 : }
130 :
131 128 : void TableBuffer::finalizeImport()
132 : {
133 : // map all tables by identifier and display name
134 128 : for( TableVector::iterator aIt = maTables.begin(), aEnd = maTables.end(); aIt != aEnd; ++aIt )
135 0 : insertTableToMaps( *aIt );
136 : // finalize all valid tables
137 128 : maIdTables.forEachMem( &Table::finalizeImport );
138 128 : }
139 :
140 260 : void TableBuffer::applyAutoFilters()
141 : {
142 260 : maIdTables.forEachMem( &Table::applyAutoFilters );
143 260 : }
144 :
145 0 : TableRef TableBuffer::getTable( sal_Int32 nTableId ) const
146 : {
147 0 : return maIdTables.get( nTableId );
148 : }
149 :
150 0 : TableRef TableBuffer::getTable( const OUString& rDispName ) const
151 : {
152 0 : return maNameTables.get( rDispName );
153 : }
154 :
155 : // private --------------------------------------------------------------------
156 :
157 0 : void TableBuffer::insertTableToMaps( const TableRef& rxTable )
158 : {
159 0 : sal_Int32 nTableId = rxTable->getTableId();
160 0 : const OUString& rDispName = rxTable->getDisplayName();
161 0 : if( (nTableId > 0) && !rDispName.isEmpty() )
162 : {
163 : OSL_ENSURE( !maIdTables.has( nTableId ), "TableBuffer::insertTableToMaps - multiple table identifier" );
164 0 : maIdTables[ nTableId ] = rxTable;
165 : OSL_ENSURE( !maNameTables.has( rDispName ), "TableBuffer::insertTableToMaps - multiple table name" );
166 0 : maNameTables[ rDispName ] = rxTable;
167 : }
168 0 : }
169 :
170 : } // namespace xls
171 48 : } // namespace oox
172 :
173 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|