LibreOffice Module reportdesign (master) 1
xmlExportDocumentHandler.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 * 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
21#include <officecfg/Office/Common.hxx>
22#include <com/sun/star/sdb/CommandType.hpp>
23#include <com/sun/star/chart2/data/XDatabaseDataProvider.hpp>
24#include <com/sun/star/chart/XComplexDescriptionAccess.hpp>
25#include <com/sun/star/reflection/ProxyFactory.hpp>
30#include <utility>
31#include <xmloff/xmltoken.hxx>
32#include <xmloff/xmlement.hxx>
33#include <xmloff/xmluconv.hxx>
34#include <rtl/ustrbuf.hxx>
36
37namespace rptxml
38{
39using namespace ::com::sun::star;
40using namespace ::xmloff::token;
41
42static void lcl_exportPrettyPrinting(const uno::Reference< xml::sax::XDocumentHandler >& _xDelegatee)
43{
44 if ( officecfg::Office::Common::Save::Document::PrettyPrinting::get() )
45 {
46 _xDelegatee->ignorableWhitespace(" ");
47 }
48}
49
51{
52 return
53 // ...if it's in our map, make the prefix
54 xmloff::token::GetXMLToken(_eNamespace) +
55 ":" +
56 xmloff::token::GetXMLToken(_eAttribute);
57}
58
59static void lcl_correctCellAddress(const OUString & _sName, const uno::Reference< xml::sax::XAttributeList > & xAttribs)
60{
61 comphelper::AttributeList* pList = dynamic_cast<comphelper::AttributeList*>(xAttribs.get());
62 assert(pList && "can only succeed");
63 OUString sCellAddress = pList->getValueByName(_sName);
64 const sal_Int32 nPos = sCellAddress.lastIndexOf('$');
65 if ( nPos != -1 )
66 {
67 sCellAddress = OUString::Concat(sCellAddress.subView(0,nPos)) + "$65535";
68 pList->RemoveAttribute(_sName);
69 pList->AddAttribute(_sName,sCellAddress);
70 }
71}
72
73ExportDocumentHandler::ExportDocumentHandler(uno::Reference< uno::XComponentContext > context) :
74 m_xContext(std::move(context))
75 ,m_nColumnCount(0)
76 ,m_bTableRowsStarted(false)
77 ,m_bFirstRowExported(false)
78 ,m_bCountColumnHeader(false)
79{
80}
81
82ExportDocumentHandler::~ExportDocumentHandler()
83{
84 if ( m_xProxy.is() )
85 {
86 m_xProxy->setDelegator( nullptr );
87 m_xProxy.clear();
88 }
89}
91
92OUString SAL_CALL ExportDocumentHandler::getImplementationName( )
93{
94 return "com.sun.star.comp.report.ExportDocumentHandler";
95}
96
97sal_Bool SAL_CALL ExportDocumentHandler::supportsService( const OUString& ServiceName )
98{
100}
101
102uno::Sequence< OUString > SAL_CALL ExportDocumentHandler::getSupportedServiceNames( )
103{
104 uno::Sequence< OUString > aSupported;
105 if ( m_xServiceInfo.is() )
106 aSupported = m_xServiceInfo->getSupportedServiceNames();
107 return ::comphelper::concatSequences(uno::Sequence< OUString > { "com.sun.star.report.ExportDocumentHandler" },aSupported);
108}
109
110// xml::sax::XDocumentHandler:
111void SAL_CALL ExportDocumentHandler::startDocument()
112{
113 m_xDelegatee->startDocument();
114}
115
116void SAL_CALL ExportDocumentHandler::endDocument()
117{
118 m_xDelegatee->endDocument();
119}
120
121void SAL_CALL ExportDocumentHandler::startElement(const OUString & _sName, const uno::Reference< xml::sax::XAttributeList > & xAttribs)
122{
123 bool bExport = true;
124 if ( _sName == "office:chart" )
125 {
127 OUStringBuffer sValue;
128 static const SvXMLEnumMapEntry<sal_uInt16> aXML_CommandTypeEnumMap[] =
129 {
130 { XML_TABLE, sdb::CommandType::TABLE },
131 { XML_QUERY, sdb::CommandType::QUERY },
132 { XML_TOKEN_INVALID, 0 }
133 };
134 if ( SvXMLUnitConverter::convertEnum( sValue, static_cast<sal_uInt16>(m_xDatabaseDataProvider->getCommandType()),aXML_CommandTypeEnumMap ) )
135 {
136 pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_COMMAND_TYPE),sValue.makeStringAndClear());
137 }
138 const OUString sCommand = m_xDatabaseDataProvider->getCommand();
139 if ( !sCommand.isEmpty() )
140 pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_COMMAND),sCommand);
141
142 const OUString sFilter( m_xDatabaseDataProvider->getFilter() );
143 if ( !sFilter.isEmpty() )
144 pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_FILTER),sFilter);
145
146 const bool bEscapeProcessing( m_xDatabaseDataProvider->getEscapeProcessing() );
147 if ( !bEscapeProcessing )
148 pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_ESCAPE_PROCESSING),::xmloff::token::GetXMLToken( XML_FALSE ));
149
151
152 m_xDelegatee->startElement(lcl_createAttribute(XML_NP_OFFICE,XML_REPORT),pList);
153
154 const OUString sTableCalc = lcl_createAttribute(XML_NP_TABLE,XML_CALCULATION_SETTINGS);
155 m_xDelegatee->startElement(sTableCalc,nullptr);
156 pList = new comphelper::AttributeList();
157 pList->AddAttribute(lcl_createAttribute(XML_NP_TABLE,XML_DATE_VALUE),"1899-12-30");
158
159 const OUString sNullDate = lcl_createAttribute(XML_NP_TABLE,XML_NULL_DATE);
160 m_xDelegatee->startElement(sNullDate,pList);
161 m_xDelegatee->endElement(sNullDate);
162 m_xDelegatee->endElement(sTableCalc);
163 bExport = false;
164 }
165 else if ( _sName == "table:table" )
166 {
167 m_xDelegatee->startElement(lcl_createAttribute(XML_NP_RPT,XML_DETAIL),nullptr);
168 lcl_exportPrettyPrinting(m_xDelegatee);
169 }
170 else if ( _sName == "table:table-header-rows" )
171 {
172 m_bCountColumnHeader = true;
173 }
174 else if ( m_bCountColumnHeader && _sName == "table:table-cell" )
175 {
176 ++m_nColumnCount;
177 }
178 else if ( _sName == "table:table-rows" )
179 {
180 m_xDelegatee->startElement(_sName,xAttribs);
181 exportTableRows();
182 bExport = false;
183 m_bTableRowsStarted = true;
184 m_bFirstRowExported = true;
185 }
186 else if ( m_bTableRowsStarted && m_bFirstRowExported && (_sName == "table:table-row" || _sName == "table:table-cell") )
187 bExport = false;
188 else if ( _sName == "chart:plot-area" )
189 {
190 comphelper::AttributeList* pList = dynamic_cast<comphelper::AttributeList*>(xAttribs.get());
191 assert(pList && "can only succeed");
192 pList->RemoveAttribute("table:cell-range-address");
193 }
194 else if ( _sName == "chart:categories" )
195 {
196 static OUString s_sCellAddress(lcl_createAttribute(XML_NP_TABLE,XML_CELL_RANGE_ADDRESS));
197 lcl_correctCellAddress(s_sCellAddress,xAttribs);
198 }
199 else if ( _sName == "chart:series" )
200 {
201 static OUString s_sCellAddress(lcl_createAttribute(XML_NP_CHART,XML_VALUES_CELL_RANGE_ADDRESS));
202 lcl_correctCellAddress(s_sCellAddress,xAttribs);
203 }
204 else if ( m_bTableRowsStarted && !m_bFirstRowExported && _sName == "table:table-cell" )
205 {
206 comphelper::AttributeList* pList = dynamic_cast<comphelper::AttributeList*>(xAttribs.get());
207 assert(pList && "can only succeed");
208 static OUString s_sValue(lcl_createAttribute(XML_NP_OFFICE,XML_VALUE));
209 pList->RemoveAttribute(s_sValue);
210 }
211 else if ( m_bTableRowsStarted && _sName == "text:p" )
212 {
213 bExport = false;
214 }
215 if ( bExport )
216 m_xDelegatee->startElement(_sName,xAttribs);
217}
218
219void SAL_CALL ExportDocumentHandler::endElement(const OUString & _sName)
220{
221 bool bExport = true;
222 OUString sNewName = _sName;
223 if ( _sName == "office:chart" )
224 {
226 }
227 else if ( _sName == "table:table" )
228 {
229 m_xDelegatee->endElement(_sName);
230 lcl_exportPrettyPrinting(m_xDelegatee);
232 }
233 else if ( _sName == "table:table-header-rows" )
234 {
235 m_bCountColumnHeader = false;
236 }
237 else if ( _sName == "table:table-rows" )
238 m_bTableRowsStarted = false;
239 else if ( m_bTableRowsStarted && m_bFirstRowExported && (_sName == "table:table-row" || _sName == "table:table-cell") )
240 bExport = false;
241 else if ( m_bTableRowsStarted && _sName == "table:table-row" )
242 m_bFirstRowExported = true;
243 else if ( m_bTableRowsStarted && _sName == "text:p" )
244 {
245 bExport = !m_bFirstRowExported;
246 }
247
248 if ( bExport )
249 m_xDelegatee->endElement(sNewName);
250}
251
252void SAL_CALL ExportDocumentHandler::characters(const OUString & aChars)
253{
254 if ( !(m_bTableRowsStarted || m_bFirstRowExported) )
255 {
256 m_xDelegatee->characters(aChars);
257 }
258}
259
260void SAL_CALL ExportDocumentHandler::ignorableWhitespace(const OUString & aWhitespaces)
261{
262 m_xDelegatee->ignorableWhitespace(aWhitespaces);
263}
264
265void SAL_CALL ExportDocumentHandler::processingInstruction(const OUString & aTarget, const OUString & aData)
266{
267 m_xDelegatee->processingInstruction(aTarget,aData);
268}
269
270void SAL_CALL ExportDocumentHandler::setDocumentLocator(const uno::Reference< xml::sax::XLocator > & xLocator)
271{
272 m_xDelegatee->setDocumentLocator(xLocator);
273}
274void SAL_CALL ExportDocumentHandler::initialize( const uno::Sequence< uno::Any >& _aArguments )
275{
276 std::unique_lock aGuard(m_aMutex);
277 comphelper::SequenceAsHashMap aArgs(_aArguments);
278 m_xDelegatee = aArgs.getUnpackedValueOrDefault("DocumentHandler",m_xDelegatee);
280
281 OSL_ENSURE(m_xDelegatee.is(),"No document handler available!");
282 if ( !m_xDelegatee.is() || !m_xModel.is() )
283 throw uno::Exception("no delegatee and no model", nullptr);
284
285 m_xDatabaseDataProvider.set(m_xModel->getDataProvider(),uno::UNO_QUERY_THROW);
286 if ( !m_xDatabaseDataProvider->getActiveConnection().is() )
287 throw uno::Exception("no active connection", nullptr);
288
289 uno::Reference< reflection::XProxyFactory > xProxyFactory = reflection::ProxyFactory::create( m_xContext );
290 m_xProxy = xProxyFactory->createProxy(m_xDelegatee);
291 ::comphelper::query_aggregation(m_xProxy,m_xDelegatee);
292 m_xTypeProvider.set(m_xDelegatee,uno::UNO_QUERY);
293 m_xServiceInfo.set(m_xDelegatee,uno::UNO_QUERY);
294
295 // set ourself as delegator
296 m_xProxy->setDelegator( *this );
297 const OUString sCommand = m_xDatabaseDataProvider->getCommand();
298 if ( !sCommand.isEmpty() )
299 m_aColumns = ::dbtools::getFieldNamesByCommandDescriptor(m_xDatabaseDataProvider->getActiveConnection()
300 ,m_xDatabaseDataProvider->getCommandType()
301 ,sCommand);
302
303 uno::Reference< chart::XComplexDescriptionAccess > xDataProvider(m_xDatabaseDataProvider,uno::UNO_QUERY);
304 if ( !xDataProvider.is() )
305 return;
306
307 m_aColumns.realloc(1);
308 const uno::Sequence< OUString > aColumnNames = xDataProvider->getColumnDescriptions();
309 for(const auto& rColumnName : aColumnNames)
310 {
311 if ( !rColumnName.isEmpty() )
312 {
313 sal_Int32 nCount = m_aColumns.getLength();
314 m_aColumns.realloc(nCount+1);
315 m_aColumns.getArray()[nCount] = rColumnName;
316 }
317 }
318}
319
320uno::Any SAL_CALL ExportDocumentHandler::queryInterface( const uno::Type& _rType )
321{
322 uno::Any aReturn = ExportDocumentHandler_BASE::queryInterface(_rType);
323 return aReturn.hasValue() ? aReturn : (m_xProxy.is() ? m_xProxy->queryAggregation(_rType) : aReturn);
324}
325
326uno::Sequence< uno::Type > SAL_CALL ExportDocumentHandler::getTypes( )
327{
328 if ( m_xTypeProvider.is() )
329 return ::comphelper::concatSequences(
330 ExportDocumentHandler_BASE::getTypes(),
331 m_xTypeProvider->getTypes()
332 );
333 return ExportDocumentHandler_BASE::getTypes();
334}
335
336void ExportDocumentHandler::exportTableRows()
337{
338 const OUString sRow( lcl_createAttribute(XML_NP_TABLE, XML_TABLE_ROW) );
339 m_xDelegatee->startElement(sRow,nullptr);
340
341 const OUString sValueType( lcl_createAttribute(XML_NP_OFFICE, XML_VALUE_TYPE) );
342
343 const OUString sCell( lcl_createAttribute(XML_NP_TABLE, XML_TABLE_CELL) );
344 const OUString sP( lcl_createAttribute(XML_NP_TEXT, XML_P) );
345 const OUString sFtext(lcl_createAttribute(XML_NP_RPT,XML_FORMATTED_TEXT) );
346 const OUString sRElement(lcl_createAttribute(XML_NP_RPT,XML_REPORT_ELEMENT) );
347 const OUString sRComponent( lcl_createAttribute(XML_NP_RPT,XML_REPORT_COMPONENT) ) ;
348 const OUString sFormulaAttrib( lcl_createAttribute(XML_NP_RPT,XML_FORMULA) );
349 static constexpr OUStringLiteral s_sFloat = u"float";
350
352 pCellAtt->AddAttribute(sValueType, "string");
353
354 bool bRemoveString = true;
355 const sal_Int32 nCount = m_aColumns.getLength();
356 if ( m_nColumnCount > nCount )
357 {
358 const sal_Int32 nEmptyCellCount = m_nColumnCount - nCount;
359 for(sal_Int32 i = 0; i < nEmptyCellCount ; ++i)
360 {
361 m_xDelegatee->startElement(sCell,pCellAtt);
362 if ( bRemoveString )
363 {
364 bRemoveString = false;
365 pCellAtt->RemoveAttribute(sValueType);
366 pCellAtt->AddAttribute(sValueType,s_sFloat);
367 }
368 m_xDelegatee->startElement(sP,nullptr);
369 m_xDelegatee->endElement(sP);
370 m_xDelegatee->endElement(sCell);
371 }
372 }
373 for(const auto& rColumn : std::as_const(m_aColumns))
374 {
375 OUString sFormula = "field:[" + rColumn + "]";
377 pList->AddAttribute(sFormulaAttrib,sFormula);
378
379 m_xDelegatee->startElement(sCell,pCellAtt);
380 if ( bRemoveString )
381 {
382 bRemoveString = false;
383 pCellAtt->RemoveAttribute(sValueType);
384 pCellAtt->AddAttribute(sValueType,s_sFloat);
385 }
386 m_xDelegatee->startElement(sP,nullptr);
387 m_xDelegatee->startElement(sFtext,pList);
388 m_xDelegatee->startElement(sRElement,nullptr);
389 m_xDelegatee->startElement(sRComponent,nullptr);
390
391 m_xDelegatee->endElement(sRComponent);
392 m_xDelegatee->endElement(sRElement);
393 m_xDelegatee->endElement(sFtext);
394 m_xDelegatee->endElement(sP);
395 m_xDelegatee->endElement(sCell);
396 }
397
398 m_xDelegatee->endElement(sRow);
399}
400
401} // namespace rptxml
402
403
404extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
406 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
407{
408 return cppu::acquire(new rptxml::ExportDocumentHandler(context));
409}
410
411
412/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
static bool convertEnum(EnumT &rEnum, std::u16string_view rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
virtual OUString SAL_CALL getValueByName(const OUString &aName) override
void AddAttribute(const OUString &sName, const OUString &sValue)
void RemoveAttribute(const OUString &sName)
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
ExportDocumentHandler(css::uno::Reference< css::uno::XComponentContext > context)
int nCount
Reference< frame::XModel > m_xModel
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII
OString sFormula
float u
std::mutex m_aMutex
sal_uInt16 nPos
constexpr OUStringLiteral aData
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
static void lcl_exportPrettyPrinting(const uno::Reference< xml::sax::XDocumentHandler > &_xDelegatee)
OUString lcl_createAttribute(const xmloff::token::XMLTokenEnum &_eNamespace, const xmloff::token::XMLTokenEnum &_eAttribute)
static void lcl_correctCellAddress(const OUString &_sName, const uno::Reference< xml::sax::XAttributeList > &xAttribs)
IMPLEMENT_GET_IMPLEMENTATION_ID(DrawController)
XML_MIMETYPE
XML_QUERY
XML_TABLE_ROW
XML_FORMATTED_TEXT
XML_FILTER
XML_REPORT_COMPONENT
XML_TABLE
XML_DATE_VALUE
XML_CELL_RANGE_ADDRESS
XML_TOKEN_INVALID
XML_COMMAND
XML_REPORT
XML_COMMAND_TYPE
XML_NP_OFFICE
XML_NP_TEXT
XML_REPORT_ELEMENT
XML_FORMULA
XML_NP_TABLE
XML_ESCAPE_PROCESSING
XML_VALUE
XML_FALSE
XML_P
XML_VALUE_TYPE
XML_NP_CHART
XML_NP_RPT
XML_TABLE_CELL
XML_CALCULATION_SETTINGS
XML_NULL_DATE
XML_DETAIL
XML_VALUES_CELL_RANGE_ADDRESS
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
bool hasValue()
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * reportdesign_ExportDocumentHandler_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)