1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tablebuffer.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/xls/tablebuffer.hxx"
32 #include <com/sun/star/sheet/XDatabaseRanges.hpp>
33 #include <com/sun/star/sheet/XDatabaseRange.hpp>
34 #include "properties.hxx"
35 #include "oox/helper/attributelist.hxx"
36 #include "oox/helper/containerhelper.hxx"
37 #include "oox/helper/propertyset.hxx"
38 #include "oox/helper/recordinputstream.hxx"
39 #include "oox/xls/addressconverter.hxx"
41 using ::rtl::OUString
;
42 using ::com::sun::star::uno::Reference
;
43 using ::com::sun::star::uno::Exception
;
44 using ::com::sun::star::uno::UNO_QUERY_THROW
;
45 using ::com::sun::star::container::XNameAccess
;
46 using ::com::sun::star::sheet::XDatabaseRanges
;
47 using ::com::sun::star::sheet::XDatabaseRange
;
52 // ============================================================================
54 TableModel::TableModel() :
56 mnType( XML_worksheet
),
62 // ============================================================================
64 Table::Table( const WorkbookHelper
& rHelper
) :
65 WorkbookHelper( rHelper
),
70 void Table::importTable( const AttributeList
& rAttribs
, sal_Int16 nSheet
)
72 getAddressConverter().convertToCellRangeUnchecked( maModel
.maRange
, rAttribs
.getString( XML_ref
, OUString() ), nSheet
);
73 maModel
.maProgName
= rAttribs
.getXString( XML_name
, OUString() );
74 maModel
.maDisplayName
= rAttribs
.getXString( XML_displayName
, OUString() );
75 maModel
.mnId
= rAttribs
.getInteger( XML_id
, -1 );
76 maModel
.mnType
= rAttribs
.getToken( XML_tableType
, XML_worksheet
);
77 maModel
.mnHeaderRows
= rAttribs
.getInteger( XML_headerRowCount
, 1 );
78 maModel
.mnTotalsRows
= rAttribs
.getInteger( XML_totalsRowCount
, 0 );
81 void Table::importTable( RecordInputStream
& rStrm
, sal_Int16 nSheet
)
85 rStrm
>> aBinRange
>> nType
>> maModel
.mnId
>> maModel
.mnHeaderRows
>> maModel
.mnTotalsRows
;
87 rStrm
>> maModel
.maProgName
>> maModel
.maDisplayName
;
89 getAddressConverter().convertToCellRangeUnchecked( maModel
.maRange
, aBinRange
, nSheet
);
90 static const sal_Int32 spnTypes
[] = { XML_worksheet
, XML_TOKEN_INVALID
, XML_TOKEN_INVALID
, XML_queryTable
};
91 maModel
.mnType
= STATIC_ARRAY_SELECT( spnTypes
, nType
, XML_TOKEN_INVALID
);
94 void Table::finalizeImport()
96 // validate cell range
97 maDestRange
= maModel
.maRange
;
98 bool bValidRange
= getAddressConverter().validateCellRange( maDestRange
, true, true );
100 // create database range
101 if( bValidRange
&& (maModel
.mnId
> 0) && (maModel
.maDisplayName
.getLength() > 0) ) try
103 // find an unused name
104 Reference
< XDatabaseRanges
> xDatabaseRanges
= getDatabaseRanges();
105 Reference
< XNameAccess
> xNameAccess( xDatabaseRanges
, UNO_QUERY_THROW
);
106 OUString aName
= ContainerHelper::getUnusedName( xNameAccess
, maModel
.maDisplayName
, '_' );
107 xDatabaseRanges
->addNewByName( aName
, maModel
.maRange
);
108 Reference
< XDatabaseRange
> xDatabaseRange( xDatabaseRanges
->getByName( aName
), UNO_QUERY_THROW
);
109 PropertySet
aPropSet( xDatabaseRange
);
110 if( !aPropSet
.getProperty( mnTokenIndex
, PROP_TokenIndex
) )
115 OSL_ENSURE( false, "Table::finalizeImport - cannot create database range" );
119 // ============================================================================
121 TableBuffer::TableBuffer( const WorkbookHelper
& rHelper
) :
122 WorkbookHelper( rHelper
)
126 TableRef
TableBuffer::importTable( const AttributeList
& rAttribs
, sal_Int16 nSheet
)
128 TableRef
xTable( new Table( *this ) );
129 xTable
->importTable( rAttribs
, nSheet
);
130 insertTable( xTable
);
134 TableRef
TableBuffer::importTable( RecordInputStream
& rStrm
, sal_Int16 nSheet
)
136 TableRef
xTable( new Table( *this ) );
137 xTable
->importTable( rStrm
, nSheet
);
138 insertTable( xTable
);
142 void TableBuffer::finalizeImport()
144 maIdTables
.forEachMem( &Table::finalizeImport
);
147 TableRef
TableBuffer::getTable( sal_Int32 nTableId
) const
149 return maIdTables
.get( nTableId
);
152 TableRef
TableBuffer::getTable( const OUString
& rDispName
) const
154 return maNameTables
.get( rDispName
);
157 // private --------------------------------------------------------------------
159 void TableBuffer::insertTable( TableRef xTable
)
161 sal_Int32 nTableId
= xTable
->getTableId();
162 const OUString
& rDispName
= xTable
->getDisplayName();
163 if( (nTableId
> 0) && (rDispName
.getLength() > 0) )
165 OSL_ENSURE( maIdTables
.find( nTableId
) == maIdTables
.end(), "TableBuffer::insertTable - multiple table identifier" );
166 maIdTables
[ nTableId
] = xTable
;
167 OSL_ENSURE( maNameTables
.find( rDispName
) == maNameTables
.end(), "TableBuffer::insertTable - multiple table name" );
168 maNameTables
[ rDispName
] = xTable
;
172 // ============================================================================