LibreOffice Module sc (master)  1
xedbdata.cxx
Go to the documentation of this file.
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 
10 #include <xedbdata.hxx>
11 #include <excrecds.hxx>
12 #include <dbdata.hxx>
13 #include <document.hxx>
14 #include <oox/export/utils.hxx>
15 #include <oox/token/namespaces.hxx>
16 
17 using namespace oox;
18 
19 namespace {
20 
22 class XclExpTablesImpl5 : public XclExpTables
23 {
24 public:
25  explicit XclExpTablesImpl5( const XclExpRoot& rRoot );
26 
27  virtual void Save( XclExpStream& rStrm ) override;
28  virtual void SaveXml( XclExpXmlStream& rStrm ) override;
29 };
30 
32 class XclExpTablesImpl8 : public XclExpTables
33 {
34 public:
35  explicit XclExpTablesImpl8( const XclExpRoot& rRoot );
36 
37  virtual void Save( XclExpStream& rStrm ) override;
38  virtual void SaveXml( XclExpXmlStream& rStrm ) override;
39 };
40 
41 }
42 
43 XclExpTablesImpl5::XclExpTablesImpl5( const XclExpRoot& rRoot ) :
44  XclExpTables( rRoot )
45 {
46 }
47 
48 void XclExpTablesImpl5::Save( XclExpStream& /*rStrm*/ )
49 {
50  // not implemented
51 }
52 
53 void XclExpTablesImpl5::SaveXml( XclExpXmlStream& /*rStrm*/ )
54 {
55  // not applicable
56 }
57 
58 
59 XclExpTablesImpl8::XclExpTablesImpl8( const XclExpRoot& rRoot ) :
60  XclExpTables( rRoot )
61 {
62 }
63 
64 void XclExpTablesImpl8::Save( XclExpStream& /*rStrm*/ )
65 {
66  // not implemented
67 }
68 
69 void XclExpTablesImpl8::SaveXml( XclExpXmlStream& rStrm )
70 {
71 
72  sax_fastparser::FSHelperPtr& pWorksheetStrm = rStrm.GetCurrentStream();
73  pWorksheetStrm->startElement(XML_tableParts);
74  for (auto const& it : maTables)
75  {
76  OUString aRelId;
78  XclXmlUtils::GetStreamName("xl/tables/", "table", it.mnTableId),
79  XclXmlUtils::GetStreamName("../tables/", "table", it.mnTableId),
80  pWorksheetStrm->getOutputStream(),
81  CREATE_XL_CONTENT_TYPE("table"),
83  &aRelId);
84 
85  pWorksheetStrm->singleElement(XML_tablePart, FSNS(XML_r, XML_id), aRelId.toUtf8());
86 
87  rStrm.PushStream( pTableStrm);
88  SaveTableXml( rStrm, it);
89  rStrm.PopStream();
90  }
91  pWorksheetStrm->endElement( XML_tableParts);
92 }
93 
94 
96  XclExpRoot( rRoot )
97 {
98 }
99 
101 {
102 }
103 
105 {
106  // All non-const to be able to call RefreshTableColumnNames().
107  ScDocument& rDoc = GetDoc();
108  ScDBCollection* pDBColl = rDoc.GetDBCollection();
109  if (!pDBColl)
110  return;
111 
112  ScDBCollection::NamedDBs& rDBs = pDBColl->getNamedDBs();
113  if (rDBs.empty())
114  return;
115 
116  sal_Int32 nTableId = 0;
117  for (const auto& rxDB : rDBs)
118  {
119  ScDBData* pDBData = rxDB.get();
120  pDBData->RefreshTableColumnNames( &rDoc); // currently not in sync, so refresh
122  pDBData->GetArea( aRange);
123  SCTAB nTab = aRange.aStart.Tab();
124  TablesMapType::iterator it = maTablesMap.find( nTab);
125  if (it == maTablesMap.end())
126  {
128  switch( GetBiff() )
129  {
130  case EXC_BIFF5:
131  pNew = new XclExpTablesImpl5( GetRoot());
132  break;
133  case EXC_BIFF8:
134  pNew = new XclExpTablesImpl8( GetRoot());
135  break;
136  default:
137  assert(!"Unknown BIFF type!");
138  continue; // for
139  }
140  ::std::pair< TablesMapType::iterator, bool > ins( maTablesMap.insert( ::std::make_pair( nTab, pNew)));
141  if (!ins.second)
142  {
143  assert(!"XclExpTablesManager::Initialize - XclExpTables insert failed");
144  continue; // for
145  }
146  it = ins.first;
147  }
148  it->second->AppendTable( pDBData, ++nTableId);
149  }
150 }
151 
153 {
154  TablesMapType::iterator it = maTablesMap.find(nTab);
155  return it == maTablesMap.end() ? nullptr : it->second;
156 }
157 
158 XclExpTables::Entry::Entry( const ScDBData* pData, sal_Int32 nTableId ) :
159  mpData(pData), mnTableId(nTableId)
160 {
161 }
162 
164  XclExpRoot(rRoot)
165 {
166 }
167 
169 {
170 }
171 
172 void XclExpTables::AppendTable( const ScDBData* pData, sal_Int32 nTableId )
173 {
174  maTables.emplace_back( pData, nTableId);
175 }
176 
177 void XclExpTables::SaveTableXml( XclExpXmlStream& rStrm, const Entry& rEntry )
178 {
179  const ScDBData& rData = *rEntry.mpData;
181  rData.GetArea( aRange);
182  sax_fastparser::FSHelperPtr& pTableStrm = rStrm.GetCurrentStream();
183  pTableStrm->startElement( XML_table,
184  XML_xmlns, rStrm.getNamespaceURL(OOX_NS(xls)).toUtf8(),
185  XML_id, OString::number( rEntry.mnTableId),
186  XML_name, rData.GetName().toUtf8(),
187  XML_displayName, rData.GetName().toUtf8(),
188  XML_ref, XclXmlUtils::ToOString(rStrm.GetRoot().GetDoc(), aRange),
189  XML_headerRowCount, ToPsz10(rData.HasHeader()),
190  XML_totalsRowCount, ToPsz10(rData.HasTotals()),
191  XML_totalsRowShown, ToPsz10(rData.HasTotals()) // we don't support that but if there are totals they are shown
192  // OOXTODO: XML_comment, ...,
193  // OOXTODO: XML_connectionId, ...,
194  // OOXTODO: XML_dataCellStyle, ...,
195  // OOXTODO: XML_dataDxfId, ...,
196  // OOXTODO: XML_headerRowBorderDxfId, ...,
197  // OOXTODO: XML_headerRowCellStyle, ...,
198  // OOXTODO: XML_headerRowDxfId, ...,
199  // OOXTODO: XML_insertRow, ...,
200  // OOXTODO: XML_insertRowShift, ...,
201  // OOXTODO: XML_published, ...,
202  // OOXTODO: XML_tableBorderDxfId, ...,
203  // OOXTODO: XML_tableType, ...,
204  // OOXTODO: XML_totalsRowBorderDxfId, ...,
205  // OOXTODO: XML_totalsRowCellStyle, ...,
206  // OOXTODO: XML_totalsRowDxfId, ...
207  );
208 
209  if (rData.HasAutoFilter())
210  {
211  /* TODO: does this need to exclude totals row? */
212 
213  /* TODO: in OOXML 12.3.21 Table Definition Part has information
214  * that an applied autoFilter has child elements
215  * <af:filterColumn><af:filters><af:filter>.
216  * When not applied but buttons hidden, Excel writes, for example,
217  * <filterColumn colId="0" hiddenButton="1"/> */
218 
219  ExcAutoFilterRecs aAutoFilter( rStrm.GetRoot(), aRange.aStart.Tab(), &rData);
220  aAutoFilter.SaveXml( rStrm);
221  }
222 
223  const std::vector< OUString >& rColNames = rData.GetTableColumnNames();
224  if (!rColNames.empty())
225  {
226  pTableStrm->startElement(XML_tableColumns,
227  XML_count, OString::number(aRange.aEnd.Col() - aRange.aStart.Col() + 1));
228 
229  for (size_t i=0, n=rColNames.size(); i < n; ++i)
230  {
231  // OOXTODO: write <calculatedColumnFormula> once we support it, in
232  // which case we'd need start/endElement XML_tableColumn for such
233  // column.
234 
235  // OOXTODO: write <totalsRowFormula> once we support it.
236 
237  pTableStrm->singleElement( XML_tableColumn,
238  XML_id, OString::number(i+1),
239  XML_name, rColNames[i].toUtf8()
240  // OOXTODO: XML_dataCellStyle, ...,
241  // OOXTODO: XML_dataDxfId, ...,
242  // OOXTODO: XML_headerRowCellStyle, ...,
243  // OOXTODO: XML_headerRowDxfId, ...,
244  // OOXTODO: XML_queryTableFieldId, ...,
245  // OOXTODO: XML_totalsRowCellStyle, ...,
246  // OOXTODO: XML_totalsRowDxfId, ...,
247  // OOXTODO: XML_totalsRowFunction, ...,
248  // OOXTODO: XML_totalsRowLabel, ...,
249  // OOXTODO: XML_uniqueName, ...
250  );
251  }
252 
253  pTableStrm->endElement( XML_tableColumns);
254  }
255 
256  // OOXTODO: write <tableStyleInfo> once we have table styles.
257 
258  pTableStrm->endElement( XML_table);
259 }
260 
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool HasAutoFilter() const
Definition: dbdata.hxx:204
static OString ToOString(const Color &rColor)
Definition: xestream.cxx:698
SC_DLLPUBLIC ScDBCollection * GetDBCollection() const
Definition: document.hxx:816
ScAddress aStart
Definition: address.hxx:500
const XclExpRoot & GetRoot() const
Returns the filter root data.
Definition: xestream.hxx:290
This class is used to export Excel record streams.
Definition: xestream.hxx:71
virtual void SaveXml(XclExpXmlStream &rStrm) override
Definition: excrecds.cxx:983
bool HasHeader() const
Definition: dbdata.hxx:130
#define CREATE_OFFICEDOC_RELATION_TYPE(ascii)
XclExpTablesManager(const XclExpRoot &rRoot)
Definition: xedbdata.cxx:95
sal_Int64 n
const ContentProperties & rData
ScAddress aEnd
Definition: address.hxx:501
ScDocument & GetDoc() const
Returns reference to the destination document (import) or source document (export).
Definition: xlroot.cxx:278
const ScDBData * mpData
Definition: xedbdata.hxx:39
Access to global data from other classes.
Definition: xeroot.hxx:113
OUString getNamespaceURL(sal_Int32 nNSID) const
virtual ~XclExpTables() override
Definition: xedbdata.cxx:168
virtual ~XclExpTablesManager() override
Definition: xedbdata.cxx:100
MS Excel 4.0.
Definition: xlconst.hxx:35
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
void PushStream(sax_fastparser::FSHelperPtr const &aStream)
Definition: xestream.cxx:916
SCTAB Tab() const
Definition: address.hxx:271
sax_fastparser::FSHelperPtr & GetCurrentStream()
Definition: xestream.cxx:910
void PopStream()
Definition: xestream.cxx:921
MS Excel 5.0, MS Excel 7.0 (95)
Definition: xlconst.hxx:36
SC_DLLPUBLIC const ::std::vector< OUString > & GetTableColumnNames() const
Definition: dbdata.hxx:149
int i
Entry(const ScDBData *pData, sal_Int32 nTableId)
used as [n] in table[n].xml part name.
Definition: xedbdata.cxx:158
const OUString & GetName() const
Definition: dbdata.hxx:122
std::shared_ptr< FastSerializerHelper > FSHelperPtr
void GetArea(SCTAB &rTab, SCCOL &rCol1, SCROW &rRow1, SCCOL &rCol2, SCROW &rRow2) const
Definition: dbdata.cxx:301
SCCOL Col() const
Definition: address.hxx:267
const XclExpRoot & GetRoot() const
Returns this root instance - for code readability in derived classes.
Definition: xeroot.hxx:119
static OUString GetStreamName(const char *sStreamDir, const char *sStream, sal_Int32 nId)
Definition: xestream.cxx:683
void AppendTable(const ScDBData *pData, sal_Int32 nTableId)
Definition: xedbdata.cxx:172
Stores global named database ranges.
Definition: dbdata.hxx:235
#define CREATE_XL_CONTENT_TYPE(ascii)
rtl::Reference< XclExpTables > GetTablesBySheet(SCTAB nTab)
Definition: xedbdata.cxx:152
static void SaveTableXml(XclExpXmlStream &rStrm, const Entry &rEntry)
Definition: xedbdata.cxx:177
sal_Int32 mnTableId
Definition: xedbdata.hxx:40
bool empty() const
Definition: dbdata.cxx:1186
XclBiff GetBiff() const
Returns the current BIFF version of the importer/exporter.
Definition: xlroot.hxx:140
constexpr sal_Int32 FSNS(sal_Int32 namespc, sal_Int32 element)
NamedDBs & getNamedDBs()
Definition: dbdata.hxx:315
SC_DLLPUBLIC void RefreshTableColumnNames(ScDocument *pDoc)
Refresh/update the column names with the header row's cell contents.
Definition: dbdata.cxx:780
TablesMapType maTablesMap
Definition: xedbdata.hxx:64
sax_fastparser::FSHelperPtr CreateOutputStream(const OUString &sFullStream, const OUString &sRelativeStream, const css::uno::Reference< css::io::XOutputStream > &xParentRelation, const char *sContentType, const char *sRelationshipType, OUString *pRelationshipId=nullptr)
Definition: xestream.cxx:939
XclExpTables(const XclExpRoot &rRoot)
Definition: xedbdata.cxx:163
bool HasTotals() const
Definition: dbdata.hxx:132
sal_Int16 SCTAB
Definition: types.hxx:23