1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "xmlExportDocumentHandler.hxx"
#include <com/sun/star/sdb/CommandType.hpp>
#include <com/sun/star/chart2/data/XDatabaseDataProvider.hpp>
#include <com/sun/star/chart/XComplexDescriptionAccess.hpp>
#include <com/sun/star/reflection/ProxyFactory.hpp>
#include <comphelper/sequenceashashmap.hxx>
#include <comphelper/documentconstants.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <xmloff/attrlist.hxx>
#include <xmloff/xmltoken.hxx>
#include <xmloff/xmlement.hxx>
#include <xmloff/xmluconv.hxx>
#include <unotools/saveopt.hxx>
#include <rtl/ustrbuf.hxx>
#include <connectivity/dbtools.hxx>
namespace rptxml
{
using namespace ::com::sun::star;
using namespace ::xmloff::token;
void lcl_exportPrettyPrinting(const uno::Reference< xml::sax::XDocumentHandler >& _xDelegatee)
{
SvtSaveOptions aSaveOpt;
if ( aSaveOpt.IsPrettyPrinting() )
{
_xDelegatee->ignorableWhitespace(" ");
}
}
OUString lcl_createAttribute(const xmloff::token::XMLTokenEnum& _eNamespace,const xmloff::token::XMLTokenEnum& _eAttribute)
{
OUStringBuffer sQName;
// ...if it's in our map, make the prefix
sQName.append ( xmloff::token::GetXMLToken(_eNamespace) );
sQName.append ( ':' );
sQName.append ( xmloff::token::GetXMLToken(_eAttribute) );
return sQName.makeStringAndClear();
}
void lcl_correctCellAddress(const OUString & _sName, const uno::Reference< xml::sax::XAttributeList > & xAttribs)
{
SvXMLAttributeList* pList = SvXMLAttributeList::getImplementation(xAttribs);
OUString sCellAddress = pList->getValueByName(_sName);
const sal_Int32 nPos = sCellAddress.lastIndexOf('$');
if ( nPos != -1 )
{
sCellAddress = sCellAddress.copy(0,nPos) + "$65535";
pList->RemoveAttribute(_sName);
pList->AddAttribute(_sName,sCellAddress);
}
}
ExportDocumentHandler::ExportDocumentHandler(uno::Reference< uno::XComponentContext > const & context) :
m_xContext(context)
,m_nColumnCount(0)
,m_bTableRowsStarted(false)
,m_bFirstRowExported(false)
,m_bCountColumnHeader(false)
{
}
ExportDocumentHandler::~ExportDocumentHandler()
{
if ( m_xProxy.is() )
{
m_xProxy->setDelegator( nullptr );
m_xProxy.clear();
}
}
IMPLEMENT_GET_IMPLEMENTATION_ID(ExportDocumentHandler)
OUString SAL_CALL ExportDocumentHandler::getImplementationName( )
{
return getImplementationName_Static();
}
sal_Bool SAL_CALL ExportDocumentHandler::supportsService( const OUString& ServiceName )
{
return cppu::supportsService(this, ServiceName);
}
uno::Sequence< OUString > SAL_CALL ExportDocumentHandler::getSupportedServiceNames( )
{
uno::Sequence< OUString > aSupported;
if ( m_xServiceInfo.is() )
aSupported = m_xServiceInfo->getSupportedServiceNames();
return ::comphelper::concatSequences(getSupportedServiceNames_static(),aSupported);
}
OUString ExportDocumentHandler::getImplementationName_Static( )
{
return OUString("com.sun.star.comp.report.ExportDocumentHandler");
}
uno::Sequence< OUString > ExportDocumentHandler::getSupportedServiceNames_static( )
{
uno::Sequence< OUString > aSupported { "com.sun.star.report.ExportDocumentHandler" };
return aSupported;
}
uno::Reference< uno::XInterface > SAL_CALL ExportDocumentHandler::create( const uno::Reference< uno::XComponentContext >& _rxContext )
{
return *(new ExportDocumentHandler( _rxContext ));
}
// xml::sax::XDocumentHandler:
void SAL_CALL ExportDocumentHandler::startDocument()
{
m_xDelegatee->startDocument();
}
void SAL_CALL ExportDocumentHandler::endDocument()
{
m_xDelegatee->endDocument();
}
void SAL_CALL ExportDocumentHandler::startElement(const OUString & _sName, const uno::Reference< xml::sax::XAttributeList > & xAttribs)
{
bool bExport = true;
if ( _sName == "office:chart" )
{
SvXMLAttributeList* pList = new SvXMLAttributeList();
uno::Reference< xml::sax::XAttributeList > xNewAttribs = pList;
OUStringBuffer sValue;
static const SvXMLEnumMapEntry aXML_CommnadTypeEnumMap[] =
{
{ XML_TABLE, sdb::CommandType::TABLE },
{ XML_QUERY, sdb::CommandType::QUERY },
{ XML_TOKEN_INVALID, 0 }
};
if ( SvXMLUnitConverter::convertEnum( sValue, static_cast<sal_uInt16>(m_xDatabaseDataProvider->getCommandType()),aXML_CommnadTypeEnumMap ) )
{
pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_COMMAND_TYPE),sValue.makeStringAndClear());
}
const OUString sComamnd = m_xDatabaseDataProvider->getCommand();
if ( !sComamnd.isEmpty() )
pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_COMMAND),sComamnd);
const OUString sFilter( m_xDatabaseDataProvider->getFilter() );
if ( !sFilter.isEmpty() )
pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_FILTER),sFilter);
const bool bEscapeProcessing( m_xDatabaseDataProvider->getEscapeProcessing() );
if ( !bEscapeProcessing )
pList->AddAttribute(lcl_createAttribute(XML_NP_RPT,XML_ESCAPE_PROCESSING),::xmloff::token::GetXMLToken( XML_FALSE ));
pList->AddAttribute(lcl_createAttribute(XML_NP_OFFICE,XML_MIMETYPE),MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII);
m_xDelegatee->startElement(lcl_createAttribute(XML_NP_OFFICE,XML_REPORT),xNewAttribs);
const OUString sTableCalc = lcl_createAttribute(XML_NP_TABLE,XML_CALCULATION_SETTINGS);
m_xDelegatee->startElement(sTableCalc,nullptr);
pList = new SvXMLAttributeList();
uno::Reference< xml::sax::XAttributeList > xNullAttr = pList;
pList->AddAttribute(lcl_createAttribute(XML_NP_TABLE,XML_DATE_VALUE),"1899-12-30");
const OUString sNullDate = lcl_createAttribute(XML_NP_TABLE,XML_NULL_DATE);
m_xDelegatee->startElement(sNullDate,xNullAttr);
m_xDelegatee->endElement(sNullDate);
m_xDelegatee->endElement(sTableCalc);
bExport = false;
}
else if ( _sName == "table:table" )
{
m_xDelegatee->startElement(lcl_createAttribute(XML_NP_RPT,XML_DETAIL),nullptr);
lcl_exportPrettyPrinting(m_xDelegatee);
}
else if ( _sName == "table:table-header-rows" )
{
m_bCountColumnHeader = true;
}
else if ( m_bCountColumnHeader && _sName == "table:table-cell" )
{
++m_nColumnCount;
}
else if ( _sName == "table:table-rows" )
{
m_xDelegatee->startElement(_sName,xAttribs);
exportTableRows();
bExport = false;
m_bTableRowsStarted = true;
m_bFirstRowExported = true;
}
else if ( m_bTableRowsStarted && m_bFirstRowExported && (_sName == "table:table-row" || _sName == "table:table-cell") )
bExport = false;
else if ( _sName == "chart:plot-area" )
{
SvXMLAttributeList* pList = SvXMLAttributeList::getImplementation(xAttribs);
pList->RemoveAttribute("table:cell-range-address");
}
else if ( _sName == "chart:categories" )
{
static OUString s_sCellAddress(lcl_createAttribute(XML_NP_TABLE,XML_CELL_RANGE_ADDRESS));
lcl_correctCellAddress(s_sCellAddress,xAttribs);
}
else if ( _sName == "chart:series" )
{
static OUString s_sCellAddress(lcl_createAttribute(XML_NP_CHART,XML_VALUES_CELL_RANGE_ADDRESS));
lcl_correctCellAddress(s_sCellAddress,xAttribs);
}
else if ( m_bTableRowsStarted && !m_bFirstRowExported && _sName == "table:table-cell" )
{
SvXMLAttributeList* pList = SvXMLAttributeList::getImplementation(xAttribs);
static OUString s_sValue(lcl_createAttribute(XML_NP_OFFICE,XML_VALUE));
pList->RemoveAttribute(s_sValue);
}
else if ( m_bTableRowsStarted && _sName == "text:p" )
{
bExport = false;
}
if ( bExport )
m_xDelegatee->startElement(_sName,xAttribs);
}
void SAL_CALL ExportDocumentHandler::endElement(const OUString & _sName)
{
bool bExport = true;
OUString sNewName = _sName;
if ( _sName == "office:chart" )
{
sNewName = lcl_createAttribute(XML_NP_OFFICE,XML_REPORT);
}
else if ( _sName == "table:table" )
{
m_xDelegatee->endElement(_sName);
lcl_exportPrettyPrinting(m_xDelegatee);
sNewName = lcl_createAttribute(XML_NP_RPT,XML_DETAIL);
}
else if ( _sName == "table:table-header-rows" )
{
m_bCountColumnHeader = false;
}
else if ( _sName == "table:table-rows" )
m_bTableRowsStarted = false;
else if ( m_bTableRowsStarted && m_bFirstRowExported && (_sName == "table:table-row" || _sName == "table:table-cell") )
bExport = false;
else if ( m_bTableRowsStarted && _sName == "table:table-row" )
m_bFirstRowExported = true;
else if ( m_bTableRowsStarted && _sName == "text:p" )
{
bExport = !m_bFirstRowExported;
}
if ( bExport )
m_xDelegatee->endElement(sNewName);
}
void SAL_CALL ExportDocumentHandler::characters(const OUString & aChars)
{
if ( !(m_bTableRowsStarted || m_bFirstRowExported) )
{
m_xDelegatee->characters(aChars);
}
}
void SAL_CALL ExportDocumentHandler::ignorableWhitespace(const OUString & aWhitespaces)
{
m_xDelegatee->ignorableWhitespace(aWhitespaces);
}
void SAL_CALL ExportDocumentHandler::processingInstruction(const OUString & aTarget, const OUString & aData)
{
m_xDelegatee->processingInstruction(aTarget,aData);
}
void SAL_CALL ExportDocumentHandler::setDocumentLocator(const uno::Reference< xml::sax::XLocator > & xLocator)
{
m_xDelegatee->setDocumentLocator(xLocator);
}
void SAL_CALL ExportDocumentHandler::initialize( const uno::Sequence< uno::Any >& _aArguments )
{
::osl::MutexGuard aGuard(m_aMutex);
comphelper::SequenceAsHashMap aArgs(_aArguments);
m_xDelegatee = aArgs.getUnpackedValueOrDefault("DocumentHandler",m_xDelegatee);
m_xModel = aArgs.getUnpackedValueOrDefault("Model",m_xModel);
OSL_ENSURE(m_xDelegatee.is(),"No document handler avialable!");
if ( !m_xDelegatee.is() || !m_xModel.is() )
throw uno::Exception();
m_xDatabaseDataProvider.set(m_xModel->getDataProvider(),uno::UNO_QUERY_THROW);
if ( !m_xDatabaseDataProvider->getActiveConnection().is() )
throw uno::Exception();
uno::Reference< reflection::XProxyFactory > xProxyFactory = reflection::ProxyFactory::create( m_xContext );
m_xProxy = xProxyFactory->createProxy(m_xDelegatee.get());
::comphelper::query_aggregation(m_xProxy,m_xDelegatee);
m_xTypeProvider.set(m_xDelegatee,uno::UNO_QUERY);
m_xServiceInfo.set(m_xDelegatee,uno::UNO_QUERY);
// set ourself as delegator
m_xProxy->setDelegator( *this );
const OUString sCommand = m_xDatabaseDataProvider->getCommand();
if ( !sCommand.isEmpty() )
m_aColumns = ::dbtools::getFieldNamesByCommandDescriptor(m_xDatabaseDataProvider->getActiveConnection()
,m_xDatabaseDataProvider->getCommandType()
,sCommand);
uno::Reference< chart::XComplexDescriptionAccess > xDataProvider(m_xDatabaseDataProvider,uno::UNO_QUERY);
if ( xDataProvider.is() )
{
m_aColumns.realloc(1);
uno::Sequence< OUString > aColumnNames = xDataProvider->getColumnDescriptions();
for(sal_Int32 i = 0 ; i < aColumnNames.getLength();++i)
{
if ( !aColumnNames[i].isEmpty() )
{
sal_Int32 nCount = m_aColumns.getLength();
m_aColumns.realloc(nCount+1);
m_aColumns[nCount] = aColumnNames[i];
}
}
}
}
uno::Any SAL_CALL ExportDocumentHandler::queryInterface( const uno::Type& _rType )
{
uno::Any aReturn = ExportDocumentHandler_BASE::queryInterface(_rType);
return aReturn.hasValue() ? aReturn : (m_xProxy.is() ? m_xProxy->queryAggregation(_rType) : aReturn);
}
uno::Sequence< uno::Type > SAL_CALL ExportDocumentHandler::getTypes( )
{
if ( m_xTypeProvider.is() )
return ::comphelper::concatSequences(
ExportDocumentHandler_BASE::getTypes(),
m_xTypeProvider->getTypes()
);
return ExportDocumentHandler_BASE::getTypes();
}
void ExportDocumentHandler::exportTableRows()
{
const OUString sRow( lcl_createAttribute(XML_NP_TABLE, XML_TABLE_ROW) );
m_xDelegatee->startElement(sRow,nullptr);
const OUString sValueType( lcl_createAttribute(XML_NP_OFFICE, XML_VALUE_TYPE) );
const OUString sCell( lcl_createAttribute(XML_NP_TABLE, XML_TABLE_CELL) );
const OUString sP( lcl_createAttribute(XML_NP_TEXT, XML_P) );
const OUString sFtext(lcl_createAttribute(XML_NP_RPT,XML_FORMATTED_TEXT) );
const OUString sRElement(lcl_createAttribute(XML_NP_RPT,XML_REPORT_ELEMENT) );
const OUString sRComponent( lcl_createAttribute(XML_NP_RPT,XML_REPORT_COMPONENT) ) ;
const OUString sFormulaAttrib( lcl_createAttribute(XML_NP_RPT,XML_FORMULA) );
static const char s_sFloat[] = "float";
SvXMLAttributeList* pCellAtt = new SvXMLAttributeList();
uno::Reference< xml::sax::XAttributeList > xCellAtt = pCellAtt;
pCellAtt->AddAttribute(sValueType, "string");
bool bRemoveString = true;
OUString sFormula;
const sal_Int32 nCount = m_aColumns.getLength();
if ( m_nColumnCount > nCount )
{
const sal_Int32 nEmptyCellCount = m_nColumnCount - nCount;
for(sal_Int32 i = 0; i < nEmptyCellCount ; ++i)
{
m_xDelegatee->startElement(sCell,xCellAtt);
if ( bRemoveString )
{
bRemoveString = false;
pCellAtt->RemoveAttribute(sValueType);
pCellAtt->AddAttribute(sValueType,s_sFloat);
}
m_xDelegatee->startElement(sP,nullptr);
m_xDelegatee->endElement(sP);
m_xDelegatee->endElement(sCell);
}
}
for(sal_Int32 i = 0; i < nCount ; ++i)
{
sFormula = "field:[";
sFormula += m_aColumns[i];
sFormula += "]";
SvXMLAttributeList* pList = new SvXMLAttributeList();
uno::Reference< xml::sax::XAttributeList > xAttribs = pList;
pList->AddAttribute(sFormulaAttrib,sFormula);
m_xDelegatee->startElement(sCell,xCellAtt);
if ( bRemoveString )
{
bRemoveString = false;
pCellAtt->RemoveAttribute(sValueType);
pCellAtt->AddAttribute(sValueType,s_sFloat);
}
m_xDelegatee->startElement(sP,nullptr);
m_xDelegatee->startElement(sFtext,xAttribs);
m_xDelegatee->startElement(sRElement,nullptr);
m_xDelegatee->startElement(sRComponent,nullptr);
m_xDelegatee->endElement(sRComponent);
m_xDelegatee->endElement(sRElement);
m_xDelegatee->endElement(sFtext);
m_xDelegatee->endElement(sP);
m_xDelegatee->endElement(sCell);
}
m_xDelegatee->endElement(sRow);
}
} // namespace rptxml
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|