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 "xmlImportDocumentHandler.hxx"
21 : #include <com/sun/star/sdb/CommandType.hpp>
22 : #include <com/sun/star/chart2/data/DatabaseDataProvider.hpp>
23 : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
24 : #include <com/sun/star/chart/XComplexDescriptionAccess.hpp>
25 : #include <com/sun/star/chart/ChartDataRowSource.hpp>
26 : #include <com/sun/star/reflection/ProxyFactory.hpp>
27 : #include <com/sun/star/sdb/CommandType.hpp>
28 : #include <comphelper/sequence.hxx>
29 : #include <comphelper/sequenceashashmap.hxx>
30 : #include <comphelper/documentconstants.hxx>
31 : #include <comphelper/namedvaluecollection.hxx>
32 : #include <xmloff/attrlist.hxx>
33 : #include <xmloff/xmltoken.hxx>
34 : #include <xmloff/xmlement.hxx>
35 : #include <xmloff/xmluconv.hxx>
36 : #include <xmloff/xmltkmap.hxx>
37 : #include <xmloff/xmlnmspe.hxx>
38 :
39 : #include "xmlHelper.hxx"
40 : #include "xmlEnums.hxx"
41 :
42 : #include <connectivity/dbtools.hxx>
43 :
44 : namespace rptxml
45 : {
46 : using namespace ::com::sun::star;
47 : using namespace ::xmloff::token;
48 :
49 : ::rtl::OUString lcl_createAttribute(const xmloff::token::XMLTokenEnum& _eNamespace,const xmloff::token::XMLTokenEnum& _eAttribute);
50 :
51 0 : ImportDocumentHandler::ImportDocumentHandler(uno::Reference< uno::XComponentContext > const & context)
52 : :m_bImportedChart( false )
53 0 : ,m_xContext(context)
54 : {
55 0 : }
56 : // -----------------------------------------------------------------------------
57 0 : ImportDocumentHandler::~ImportDocumentHandler()
58 : {
59 0 : if ( m_xProxy.is() )
60 : {
61 0 : m_xProxy->setDelegator( NULL );
62 0 : m_xProxy.clear();
63 : }
64 0 : }
65 0 : IMPLEMENT_GET_IMPLEMENTATION_ID(ImportDocumentHandler)
66 0 : IMPLEMENT_FORWARD_REFCOUNT( ImportDocumentHandler, ImportDocumentHandler_BASE )
67 : //------------------------------------------------------------------------
68 0 : ::rtl::OUString SAL_CALL ImportDocumentHandler::getImplementationName( ) throw(uno::RuntimeException)
69 : {
70 0 : return getImplementationName_Static();
71 : }
72 :
73 : //------------------------------------------------------------------------
74 0 : sal_Bool SAL_CALL ImportDocumentHandler::supportsService( const ::rtl::OUString& ServiceName ) throw(uno::RuntimeException)
75 : {
76 0 : return ::comphelper::existsValue(ServiceName,getSupportedServiceNames_static());
77 : }
78 :
79 : //------------------------------------------------------------------------
80 0 : uno::Sequence< ::rtl::OUString > SAL_CALL ImportDocumentHandler::getSupportedServiceNames( ) throw(uno::RuntimeException)
81 : {
82 0 : uno::Sequence< ::rtl::OUString > aSupported;
83 0 : if ( m_xServiceInfo.is() )
84 0 : aSupported = m_xServiceInfo->getSupportedServiceNames();
85 0 : return ::comphelper::concatSequences(getSupportedServiceNames_static(),aSupported);
86 : }
87 :
88 : //------------------------------------------------------------------------
89 0 : ::rtl::OUString ImportDocumentHandler::getImplementationName_Static( ) throw(uno::RuntimeException)
90 : {
91 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.report.ImportDocumentHandler"));
92 : }
93 :
94 : //------------------------------------------------------------------------
95 0 : uno::Sequence< ::rtl::OUString > ImportDocumentHandler::getSupportedServiceNames_static( ) throw(uno::RuntimeException)
96 : {
97 0 : uno::Sequence< ::rtl::OUString > aSupported(1);
98 0 : aSupported[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.report.ImportDocumentHandler"));
99 0 : return aSupported;
100 : }
101 :
102 : //------------------------------------------------------------------------
103 0 : uno::Reference< uno::XInterface > SAL_CALL ImportDocumentHandler::create( const uno::Reference< uno::XComponentContext >& _rxContext )
104 : {
105 0 : return *(new ImportDocumentHandler( _rxContext ));
106 : }
107 : // xml::sax::XDocumentHandler:
108 0 : void SAL_CALL ImportDocumentHandler::startDocument() throw (uno::RuntimeException, xml::sax::SAXException)
109 : {
110 0 : m_xDelegatee->startDocument();
111 0 : }
112 :
113 0 : void SAL_CALL ImportDocumentHandler::endDocument() throw (uno::RuntimeException, xml::sax::SAXException)
114 : {
115 0 : m_xDelegatee->endDocument();
116 0 : uno::Reference< chart2::data::XDataReceiver > xReceiver(m_xModel,uno::UNO_QUERY_THROW);
117 0 : if ( xReceiver.is() && m_bImportedChart )
118 : {
119 : // this fills the chart again
120 0 : ::comphelper::NamedValueCollection aArgs;
121 0 : aArgs.put( "CellRangeRepresentation", ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("all")) );
122 0 : aArgs.put( "HasCategories", uno::makeAny( sal_True ) );
123 0 : aArgs.put( "FirstCellAsLabel", uno::makeAny( sal_True ) );
124 0 : aArgs.put( "DataRowSource", uno::makeAny( chart::ChartDataRowSource_COLUMNS ) );
125 :
126 0 : uno::Reference< chart::XComplexDescriptionAccess > xDataProvider(m_xModel->getDataProvider(),uno::UNO_QUERY);
127 0 : if ( xDataProvider.is() )
128 : {
129 0 : const uno::Sequence< ::rtl::OUString > aColumnNames = xDataProvider->getColumnDescriptions();
130 0 : aArgs.put( "ColumnDescriptions", uno::makeAny( aColumnNames ) );
131 : }
132 :
133 0 : xReceiver->attachDataProvider( m_xDatabaseDataProvider.get() );
134 0 : xReceiver->setArguments( aArgs.getPropertyValues() );
135 0 : }
136 0 : }
137 :
138 0 : void SAL_CALL ImportDocumentHandler::startElement(const ::rtl::OUString & _sName, const uno::Reference< xml::sax::XAttributeList > & _xAttrList) throw (uno::RuntimeException, xml::sax::SAXException)
139 : {
140 0 : uno::Reference< xml::sax::XAttributeList > xNewAttribs = _xAttrList;
141 0 : bool bExport = true;
142 0 : if ( _sName == "office:report" )
143 : {
144 0 : const sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
145 0 : static const ::rtl::OUString s_sTRUE = ::xmloff::token::GetXMLToken(XML_TRUE);
146 : try
147 : {
148 0 : for(sal_Int16 i = 0; i < nLength; ++i)
149 : {
150 0 : ::rtl::OUString sLocalName;
151 0 : const rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
152 0 : const sal_Int32 nColonPos = sAttrName.indexOf( sal_Unicode(':') );
153 0 : if( -1L == nColonPos )
154 0 : sLocalName = sAttrName;
155 : else
156 0 : sLocalName = sAttrName.copy( nColonPos + 1L );
157 0 : const rtl::OUString sValue = _xAttrList->getValueByIndex( i );
158 :
159 0 : switch( m_pReportElemTokenMap->Get( XML_NAMESPACE_REPORT, sLocalName ) )
160 : {
161 : case XML_TOK_COMMAND_TYPE:
162 : {
163 0 : sal_uInt16 nRet = static_cast<sal_uInt16>(sdb::CommandType::COMMAND);
164 0 : const SvXMLEnumMapEntry* aXML_EnumMap = OXMLHelper::GetCommandTypeOptions();
165 0 : SvXMLUnitConverter::convertEnum( nRet, sValue, aXML_EnumMap );
166 0 : m_xDatabaseDataProvider->setCommandType(nRet);
167 : }
168 0 : break;
169 : case XML_TOK_COMMAND:
170 0 : m_xDatabaseDataProvider->setCommand(sValue);
171 0 : break;
172 : case XML_TOK_FILTER:
173 0 : m_xDatabaseDataProvider->setFilter(sValue);
174 0 : break;
175 : case XML_TOK_ESCAPE_PROCESSING:
176 0 : m_xDatabaseDataProvider->setEscapeProcessing(sValue == s_sTRUE);
177 0 : break;
178 : default:
179 0 : break;
180 : }
181 0 : }
182 : }
183 0 : catch(uno::Exception&)
184 : {
185 : }
186 0 : m_xDelegatee->startElement(lcl_createAttribute(XML_NP_OFFICE,XML_CHART),NULL);
187 0 : bExport = false;
188 0 : m_bImportedChart = true;
189 : }
190 0 : else if ( _sName == "rpt:master-detail-field" )
191 : {
192 0 : const sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
193 0 : static const ::rtl::OUString s_sTRUE = ::xmloff::token::GetXMLToken(XML_TRUE);
194 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
195 0 : ::std::auto_ptr<SvXMLTokenMap> pMasterElemTokenMap( OXMLHelper::GetSubDocumentElemTokenMap());
196 : SAL_WNODEPRECATED_DECLARATIONS_POP
197 : try
198 : {
199 0 : ::rtl::OUString sMasterField,sDetailField;
200 0 : for(sal_Int16 i = 0; i < nLength; ++i)
201 : {
202 0 : ::rtl::OUString sLocalName;
203 0 : const rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
204 0 : const sal_Int32 nColonPos = sAttrName.indexOf( sal_Unicode(':') );
205 0 : if( -1L == nColonPos )
206 0 : sLocalName = sAttrName;
207 : else
208 0 : sLocalName = sAttrName.copy( nColonPos + 1L );
209 0 : const rtl::OUString sValue = _xAttrList->getValueByIndex( i );
210 :
211 0 : switch( pMasterElemTokenMap->Get( XML_NAMESPACE_REPORT, sLocalName ) )
212 : {
213 : case XML_TOK_MASTER:
214 0 : sMasterField = sValue;
215 0 : break;
216 : case XML_TOK_SUB_DETAIL:
217 0 : sDetailField = sValue;
218 0 : break;
219 : }
220 0 : }
221 0 : if ( sDetailField.isEmpty() )
222 0 : sDetailField = sMasterField;
223 0 : m_aMasterFields.push_back(sMasterField);
224 0 : m_aDetailFields.push_back(sDetailField);
225 : }
226 0 : catch(uno::Exception&)
227 : {
228 : OSL_FAIL("Exception catched while filling the report definition props");
229 : }
230 0 : bExport = false;
231 : }
232 0 : else if ( _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:detail"))
233 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:formatted-text"))
234 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:master-detail-fields"))
235 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:report-component"))
236 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:report-element")))
237 0 : bExport = false;
238 0 : else if ( _sName == "chart:plot-area" )
239 : {
240 0 : sal_Bool bHasCategories = sal_True;
241 0 : const sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
242 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
243 0 : ::std::auto_ptr<SvXMLTokenMap> pMasterElemTokenMap( OXMLHelper::GetSubDocumentElemTokenMap());
244 : SAL_WNODEPRECATED_DECLARATIONS_POP
245 0 : for(sal_Int16 i = 0; i < nLength; ++i)
246 : {
247 0 : ::rtl::OUString sLocalName;
248 0 : const rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
249 0 : const sal_Int32 nColonPos = sAttrName.indexOf( sal_Unicode(':') );
250 0 : if( -1L == nColonPos )
251 0 : sLocalName = sAttrName;
252 : else
253 0 : sLocalName = sAttrName.copy( nColonPos + 1L );
254 0 : if ( sLocalName == "data-source-has-labels" )
255 : {
256 0 : const rtl::OUString sValue = _xAttrList->getValueByIndex( i );
257 0 : bHasCategories = sValue.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("both"));
258 0 : break;
259 : }
260 0 : }
261 0 : beans::PropertyValue* pArgIter = m_aArguments.getArray();
262 0 : beans::PropertyValue* pArgEnd = pArgIter + m_aArguments.getLength();
263 0 : for(;pArgIter != pArgEnd;++pArgIter)
264 : {
265 0 : if ( pArgIter->Name == "HasCategories" )
266 : {
267 0 : pArgIter->Value <<= bHasCategories;
268 0 : break;
269 : }
270 : }
271 :
272 0 : SvXMLAttributeList* pList = new SvXMLAttributeList();
273 0 : xNewAttribs = pList;
274 0 : pList->AppendAttributeList(_xAttrList);
275 0 : pList->AddAttribute(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("table:cell-range-address")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("local-table.$A$1:.$Z$65536")));
276 :
277 : }
278 :
279 0 : if ( bExport )
280 0 : m_xDelegatee->startElement(_sName,xNewAttribs);
281 0 : }
282 :
283 0 : void SAL_CALL ImportDocumentHandler::endElement(const ::rtl::OUString & _sName) throw (uno::RuntimeException, xml::sax::SAXException)
284 : {
285 0 : bool bExport = true;
286 0 : ::rtl::OUString sNewName = _sName;
287 0 : if ( _sName == "office:report" )
288 : {
289 0 : sNewName = lcl_createAttribute(XML_NP_OFFICE,XML_CHART);
290 : }
291 0 : else if ( _sName == "rpt:master-detail-fields" )
292 : {
293 0 : if ( !m_aMasterFields.empty() )
294 0 : m_xDatabaseDataProvider->setMasterFields(uno::Sequence< ::rtl::OUString>(&*m_aMasterFields.begin(),m_aMasterFields.size()));
295 0 : if ( !m_aDetailFields.empty() )
296 0 : m_xDatabaseDataProvider->setDetailFields(uno::Sequence< ::rtl::OUString>(&*m_aDetailFields.begin(),m_aDetailFields.size()));
297 0 : bExport = false;
298 : }
299 0 : else if ( _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:detail"))
300 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:formatted-text"))
301 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:master-detail-field"))
302 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:report-component"))
303 0 : || _sName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("rpt:report-element")))
304 0 : bExport = false;
305 :
306 0 : if ( bExport )
307 0 : m_xDelegatee->endElement(sNewName);
308 0 : }
309 :
310 0 : void SAL_CALL ImportDocumentHandler::characters(const ::rtl::OUString & aChars) throw (uno::RuntimeException, xml::sax::SAXException)
311 : {
312 0 : m_xDelegatee->characters(aChars);
313 0 : }
314 :
315 0 : void SAL_CALL ImportDocumentHandler::ignorableWhitespace(const ::rtl::OUString & aWhitespaces) throw (uno::RuntimeException, xml::sax::SAXException)
316 : {
317 0 : m_xDelegatee->ignorableWhitespace(aWhitespaces);
318 0 : }
319 :
320 0 : void SAL_CALL ImportDocumentHandler::processingInstruction(const ::rtl::OUString & aTarget, const ::rtl::OUString & aData) throw (uno::RuntimeException, xml::sax::SAXException)
321 : {
322 0 : m_xDelegatee->processingInstruction(aTarget,aData);
323 0 : }
324 :
325 0 : void SAL_CALL ImportDocumentHandler::setDocumentLocator(const uno::Reference< xml::sax::XLocator > & xLocator) throw (uno::RuntimeException, xml::sax::SAXException)
326 : {
327 0 : m_xDelegatee->setDocumentLocator(xLocator);
328 0 : }
329 0 : void SAL_CALL ImportDocumentHandler::initialize( const uno::Sequence< uno::Any >& _aArguments ) throw (uno::Exception, uno::RuntimeException)
330 : {
331 0 : ::osl::MutexGuard aGuard(m_aMutex);
332 0 : comphelper::SequenceAsHashMap aArgs(_aArguments);
333 0 : m_xDelegatee = aArgs.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentHandler")),m_xDelegatee);
334 0 : m_xModel = aArgs.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Model")),m_xModel);
335 :
336 : OSL_ENSURE(m_xDelegatee.is(),"No document handler avialable!");
337 0 : if ( !m_xDelegatee.is() || !m_xModel.is() )
338 0 : throw uno::Exception();
339 :
340 0 : m_xDatabaseDataProvider.set(m_xModel->getDataProvider(),uno::UNO_QUERY);
341 0 : if ( !m_xDatabaseDataProvider.is() )
342 : {
343 0 : const static ::rtl::OUString s_sDatabaseDataProvider(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.chart2.data.DatabaseDataProvider"));
344 0 : m_xDatabaseDataProvider.set(m_xContext->getServiceManager()->createInstanceWithContext(s_sDatabaseDataProvider
345 0 : ,m_xContext),uno::UNO_QUERY);
346 0 : if ( !m_xDatabaseDataProvider.is() )
347 0 : throw uno::Exception();
348 :
349 0 : m_xDatabaseDataProvider->setRowLimit(10);
350 :
351 0 : uno::Reference< chart2::data::XDataReceiver > xReceiver(m_xModel,uno::UNO_QUERY_THROW);
352 0 : xReceiver->attachDataProvider(m_xDatabaseDataProvider.get());
353 : }
354 :
355 0 : m_aArguments = m_xDatabaseDataProvider->detectArguments(NULL);
356 :
357 0 : uno::Reference< reflection::XProxyFactory > xProxyFactory = reflection::ProxyFactory::create( m_xContext );
358 0 : m_xProxy = xProxyFactory->createProxy(m_xDelegatee.get());
359 0 : ::comphelper::query_aggregation(m_xProxy,m_xDelegatee);
360 0 : m_xTypeProvider.set(m_xDelegatee,uno::UNO_QUERY);
361 0 : m_xServiceInfo.set(m_xDelegatee,uno::UNO_QUERY);
362 :
363 : // set ourself as delegator
364 0 : m_xProxy->setDelegator( *this );
365 :
366 0 : m_pReportElemTokenMap.reset(OXMLHelper::GetReportElemTokenMap());
367 0 : }
368 : // --------------------------------------------------------------------------------
369 0 : uno::Any SAL_CALL ImportDocumentHandler::queryInterface( const uno::Type& _rType ) throw (uno::RuntimeException)
370 : {
371 0 : uno::Any aReturn = ImportDocumentHandler_BASE::queryInterface(_rType);
372 0 : return aReturn.hasValue() ? aReturn : (m_xProxy.is() ? m_xProxy->queryAggregation(_rType) : aReturn);
373 : }
374 : // --------------------------------------------------------------------------------
375 0 : uno::Sequence< uno::Type > SAL_CALL ImportDocumentHandler::getTypes( ) throw (uno::RuntimeException)
376 : {
377 0 : if ( m_xTypeProvider.is() )
378 : return ::comphelper::concatSequences(
379 : ImportDocumentHandler_BASE::getTypes(),
380 0 : m_xTypeProvider->getTypes()
381 0 : );
382 0 : return ImportDocumentHandler_BASE::getTypes();
383 : }
384 :
385 : // -----------------------------------------------------------------------------
386 : } // namespace rptxml
387 : // -----------------------------------------------------------------------------
388 :
389 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|