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