LCOV - code coverage report
Current view: top level - xmlscript/source/xmlflat_imexp - xmlbas_export.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 83 198 41.9 %
Date: 2014-04-11 Functions: 12 24 50.0 %
Legend: Lines: hit not hit

          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 "xmlbas_export.hxx"
      21             : #include <xmlscript/xmlns.h>
      22             : #include <xmlscript/xml_helper.hxx>
      23             : #include <com/sun/star/beans/XPropertySet.hpp>
      24             : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
      25             : #include <com/sun/star/script/XLibraryContainer2.hpp>
      26             : #include <com/sun/star/script/XLibraryContainerPassword.hpp>
      27             : #include <com/sun/star/document/XEmbeddedScripts.hpp>
      28             : #include <cppuhelper/implementationentry.hxx>
      29             : #include <cppuhelper/supportsservice.hxx>
      30             : 
      31             : using namespace ::com::sun::star;
      32             : using namespace ::com::sun::star::lang;
      33             : using namespace ::com::sun::star::uno;
      34             : 
      35             : namespace xmlscript
      36             : {
      37             : 
      38             :     // component operations
      39             : 
      40           2 :     OUString getImplementationName_XMLBasicExporter()
      41             :     {
      42             :         static OUString* pImplName = 0;
      43           2 :         if ( !pImplName )
      44             :         {
      45           2 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
      46           2 :             if ( !pImplName )
      47             :             {
      48           2 :                 static OUString aImplName( "com.sun.star.comp.xmlscript.XMLBasicExporter" );
      49           2 :                 pImplName = &aImplName;
      50           2 :             }
      51             :         }
      52           2 :         return *pImplName;
      53             :     }
      54             : 
      55           0 :     Sequence< OUString > getSupportedServiceNames_XMLBasicExporter()
      56             :     {
      57             :         static Sequence< OUString >* pNames = 0;
      58           0 :         if ( !pNames )
      59             :         {
      60           0 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
      61           0 :             if ( !pNames )
      62             :             {
      63           0 :                 static Sequence< OUString > aNames(1);
      64           0 :                 aNames.getArray()[0] = "com.sun.star.document.XMLBasicExporter";
      65           0 :                 pNames = &aNames;
      66           0 :             }
      67             :         }
      68           0 :         return *pNames;
      69             :     }
      70             : 
      71           2 :     OUString getImplementationName_XMLOasisBasicExporter()
      72             :     {
      73             :         static OUString* pImplName = 0;
      74           2 :         if ( !pImplName )
      75             :         {
      76           2 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
      77           2 :             if ( !pImplName )
      78             :             {
      79           2 :                 static OUString aImplName( "com.sun.star.comp.xmlscript.XMLOasisBasicExporter" );
      80           2 :                 pImplName = &aImplName;
      81           2 :             }
      82             :         }
      83           2 :         return *pImplName;
      84             :     }
      85             : 
      86           1 :     Sequence< OUString > getSupportedServiceNames_XMLOasisBasicExporter()
      87             :     {
      88             :         static Sequence< OUString >* pNames = 0;
      89           1 :         if ( !pNames )
      90             :         {
      91           1 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
      92           1 :             if ( !pNames )
      93             :             {
      94           1 :                 static Sequence< OUString > aNames(1);
      95           1 :                 aNames.getArray()[0] = "com.sun.star.document.XMLOasisBasicExporter";
      96           1 :                 pNames = &aNames;
      97           1 :             }
      98             :         }
      99           1 :         return *pNames;
     100             :     }
     101             : 
     102             :     // XMLBasicExporterBase
     103             : 
     104           2 :     XMLBasicExporterBase::XMLBasicExporterBase( const Reference< XComponentContext >& rxContext, bool bOasis )
     105             :         :m_xContext( rxContext )
     106           2 :         ,m_bOasis( bOasis )
     107             :     {
     108           2 :     }
     109             : 
     110           2 :     XMLBasicExporterBase::~XMLBasicExporterBase()
     111             :     {
     112           2 :     }
     113             : 
     114             :     // XServiceInfo
     115           0 :     sal_Bool XMLBasicExporterBase::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
     116             :     {
     117           0 :         return cppu::supportsService(this, rServiceName);
     118             :     }
     119             : 
     120             :     // XInitialization
     121             : 
     122           2 :     void XMLBasicExporterBase::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException, std::exception)
     123             :     {
     124           2 :         ::osl::MutexGuard aGuard( m_aMutex );
     125             : 
     126           2 :         if ( aArguments.getLength() == 1 )
     127             :         {
     128           2 :             aArguments[0] >>= m_xHandler;
     129             : 
     130           2 :             if ( !m_xHandler.is() )
     131             :             {
     132           0 :                 throw RuntimeException( "XMLBasicExporterBase::initialize: invalid argument format!", Reference< XInterface >() );
     133             :             }
     134             :         }
     135             :         else
     136             :         {
     137           0 :             throw RuntimeException( "XMLBasicExporterBase::initialize: invalid number of arguments!", Reference< XInterface >() );
     138           2 :         }
     139           2 :     }
     140             : 
     141             :     // XExporter
     142             : 
     143           2 :     void XMLBasicExporterBase::setSourceDocument( const Reference< XComponent >& rxDoc )
     144             :         throw (IllegalArgumentException, RuntimeException, std::exception)
     145             :     {
     146           2 :         ::osl::MutexGuard aGuard( m_aMutex );
     147             : 
     148           2 :         m_xModel.set( rxDoc, UNO_QUERY );
     149             : 
     150           2 :         if ( !m_xModel.is() )
     151             :         {
     152           0 :             throw IllegalArgumentException( "XMLBasicExporter::setSourceDocument: no document model!", Reference< XInterface >(), 1 );
     153           2 :         }
     154           2 :     }
     155             : 
     156             :     // XFilter
     157             : 
     158           2 : sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /*aDescriptor*/ )
     159             :         throw (RuntimeException, std::exception)
     160             :     {
     161           2 :         ::osl::MutexGuard aGuard( m_aMutex );
     162             : 
     163           2 :         bool bReturn = true;
     164             : 
     165             :         try
     166             :         {
     167           2 :             if ( m_xHandler.is() )
     168             :             {
     169           2 :                 m_xHandler->startDocument();
     170             : 
     171             :                 // ooo/script prefix and URI
     172           2 :                 OUString aPrefix;
     173           4 :                 OUString aURI;
     174           2 :                 if ( m_bOasis )
     175             :                 {
     176           2 :                     aPrefix = XMLNS_OOO_PREFIX;
     177           2 :                     aURI = XMLNS_OOO_URI;
     178             :                 }
     179             :                 else
     180             :                 {
     181           0 :                     aPrefix = XMLNS_SCRIPT_PREFIX;
     182           0 :                     aURI = XMLNS_SCRIPT_URI;
     183             :                 }
     184             : 
     185             :                 // ooo/script:libraries element
     186           4 :                 OUString aLibContElementName( aPrefix );
     187           2 :                 aLibContElementName += ":libraries";
     188           2 :                 XMLElement* pLibContElement = new XMLElement( aLibContElementName );
     189           4 :                 Reference< xml::sax::XAttributeList > xLibContAttribs( pLibContElement );
     190             : 
     191             :                 // ooo/script namespace attribute
     192           2 :                 pLibContElement->addAttribute( "xmlns:" + aPrefix, aURI );
     193             : 
     194             :                 // xlink namespace attribute
     195           2 :                 pLibContElement->addAttribute( "xmlns:" XMLNS_XLINK_PREFIX, XMLNS_XLINK_URI );
     196             : 
     197             :                 // <ooo/script:libraries...
     198           2 :                 m_xHandler->ignorableWhitespace( OUString() );
     199           2 :                 m_xHandler->startElement( aLibContElementName, xLibContAttribs );
     200             : 
     201           4 :                 Reference< script::XLibraryContainer2 > xLibContainer;
     202             : 
     203             :                 // try the XEmbeddedScripts interface
     204           4 :                 Reference< document::XEmbeddedScripts > xDocumentScripts( m_xModel, UNO_QUERY );
     205           2 :                 if ( xDocumentScripts.is() )
     206           2 :                     xLibContainer.set( xDocumentScripts->getBasicLibraries().get() );
     207             : 
     208           2 :                 if ( !xLibContainer.is() )
     209             :                 {
     210             :                     // try the "BasicLibraries" property (old-style, for compatibility)
     211           0 :                     Reference< beans::XPropertySet > xPSet( m_xModel, UNO_QUERY );
     212           0 :                     if ( xPSet.is() )
     213           0 :                         xPSet->getPropertyValue("BasicLibraries" ) >>= xLibContainer;
     214             :                 }
     215             : 
     216             :                 SAL_WARN_IF( !xLibContainer.is(), "xmlscript.xmlflat", "XMLBasicExporterBase::filter: nowhere to export to!" );
     217             : 
     218           2 :                 if ( xLibContainer.is() )
     219             :                 {
     220           2 :                     Sequence< OUString > aLibNames = xLibContainer->getElementNames();
     221           2 :                     sal_Int32 nLibCount = aLibNames.getLength();
     222           2 :                     const OUString* pLibNames = aLibNames.getConstArray();
     223           2 :                     for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
     224             :                     {
     225           0 :                         OUString aLibName( pLibNames[i] );
     226             : 
     227           0 :                         if ( xLibContainer->hasByName( aLibName ) )
     228             :                         {
     229           0 :                             OUString aTrueStr( "true" );
     230             : 
     231           0 :                             if ( xLibContainer->isLibraryLink( aLibName ) )
     232             :                             {
     233             :                                 // ooo/script:library-linked element
     234           0 :                                 OUString aLibElementName( aPrefix );
     235           0 :                                 aLibElementName +=  ":library-linked";
     236           0 :                                 XMLElement* pLibElement = new XMLElement( aLibElementName );
     237           0 :                                 Reference< xml::sax::XAttributeList > xLibAttribs;
     238           0 :                                 xLibAttribs = static_cast< xml::sax::XAttributeList* >( pLibElement );
     239             : 
     240             :                                 // ooo/script:name attribute
     241           0 :                                 pLibElement->addAttribute( aPrefix + ":name", aLibName );
     242             : 
     243           0 :                                 OUString aLinkURL( xLibContainer->getLibraryLinkURL( aLibName ) );
     244           0 :                                 if ( !aLinkURL.isEmpty() )
     245             :                                 {
     246             :                                     // xlink:href attribute
     247           0 :                                     pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":href", aLinkURL );
     248             : 
     249             :                                     // xlink:type attribute
     250           0 :                                     pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":type", "simple" );
     251             :                                 }
     252             : 
     253           0 :                                 if ( xLibContainer->isLibraryReadOnly( aLibName ) )
     254             :                                 {
     255             :                                     // ooo/script:readonly attribute
     256           0 :                                     pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
     257             :                                 }
     258             : 
     259             :                                 // <ooo/script:library-linked...
     260           0 :                                 m_xHandler->ignorableWhitespace( OUString() );
     261           0 :                                 m_xHandler->startElement( aLibElementName, xLibAttribs );
     262             : 
     263             :                                 // ...ooo/script:library-linked>
     264           0 :                                 m_xHandler->ignorableWhitespace( OUString() );
     265           0 :                                 m_xHandler->endElement( aLibElementName );
     266             :                             }
     267             :                             else
     268             :                             {
     269             :                                 // ooo/script:library-embedded element
     270           0 :                                 OUString aLibElementName( aPrefix );
     271           0 :                                 aLibElementName += ":library-embedded";
     272           0 :                                 XMLElement* pLibElement = new XMLElement( aLibElementName );
     273           0 :                                 Reference< xml::sax::XAttributeList > xLibAttribs;
     274           0 :                                 xLibAttribs = static_cast< xml::sax::XAttributeList* >( pLibElement );
     275             : 
     276             :                                 // ooo/script:name attribute
     277           0 :                                 pLibElement->addAttribute( aPrefix + ":name", aLibName );
     278             : 
     279           0 :                                 if ( xLibContainer->isLibraryReadOnly( aLibName ) )
     280             :                                 {
     281             :                                     // ooo/script:readonly attribute
     282           0 :                                     pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
     283             :                                 }
     284             : 
     285             :                                 // TODO: password protected libraries
     286           0 :                                 Reference< script::XLibraryContainerPassword > xPasswd( xLibContainer, UNO_QUERY );
     287           0 :                                 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aLibName ) )
     288           0 :                                     continue;
     289             : 
     290             :                                 // <ooo/script:library-embedded...
     291           0 :                                 m_xHandler->ignorableWhitespace( OUString() );
     292           0 :                                 m_xHandler->startElement( aLibElementName, xLibAttribs );
     293             : 
     294           0 :                                 if ( !xLibContainer->isLibraryLoaded( aLibName ) )
     295           0 :                                     xLibContainer->loadLibrary( aLibName );
     296             : 
     297           0 :                                 Reference< container::XNameContainer > xLib;
     298           0 :                                 xLibContainer->getByName( aLibName ) >>= xLib;
     299             : 
     300           0 :                                 if ( xLib.is() )
     301             :                                 {
     302           0 :                                     Sequence< OUString > aModNames = xLib->getElementNames();
     303           0 :                                     sal_Int32 nModCount = aModNames.getLength();
     304           0 :                                     const OUString* pModNames = aModNames.getConstArray();
     305           0 :                                     for ( sal_Int32 j = 0 ; j < nModCount ; ++j )
     306             :                                     {
     307           0 :                                         OUString aModName( pModNames[j] );
     308           0 :                                         if ( xLib->hasByName( aModName ) )
     309             :                                         {
     310             :                                             // ooo/script:module element
     311           0 :                                             OUString aModElementName( aPrefix );
     312           0 :                                             aModElementName += ":module";
     313           0 :                                             XMLElement* pModElement = new XMLElement( aModElementName );
     314           0 :                                             Reference< xml::sax::XAttributeList > xModAttribs;
     315           0 :                                             xModAttribs = static_cast< xml::sax::XAttributeList* >( pModElement );
     316             : 
     317             :                                             // ooo/script:name attribute
     318           0 :                                             pModElement->addAttribute( aPrefix + ":name", aModName );
     319             : 
     320             :                                             // <ooo/script:module...
     321           0 :                                             m_xHandler->ignorableWhitespace( OUString() );
     322           0 :                                             m_xHandler->startElement( aModElementName, xModAttribs );
     323             : 
     324             :                                             // ooo/script:source-code element
     325           0 :                                             OUString aSourceElementName( aPrefix );
     326           0 :                                             aSourceElementName += ":source-code";
     327           0 :                                             XMLElement* pSourceElement = new XMLElement( aSourceElementName );
     328           0 :                                             Reference< xml::sax::XAttributeList > xSourceAttribs;
     329           0 :                                             xSourceAttribs = static_cast< xml::sax::XAttributeList* >( pSourceElement );
     330             : 
     331             :                                             // <ooo/script:source-code...
     332           0 :                                             m_xHandler->ignorableWhitespace( OUString() );
     333           0 :                                             m_xHandler->startElement( aSourceElementName, xSourceAttribs );
     334             : 
     335             :                                             // module data
     336             :                                             // TODO: write encrypted data for password protected libraries
     337           0 :                                             OUString aSource;
     338           0 :                                             xLib->getByName( aModName ) >>= aSource;
     339           0 :                                             m_xHandler->characters( aSource );
     340             : 
     341             :                                             // TODO: <ooo/script:byte-code>
     342             : 
     343             :                                             // ...ooo/script:source-code>
     344           0 :                                             m_xHandler->ignorableWhitespace( OUString() );
     345           0 :                                             m_xHandler->endElement( aSourceElementName );
     346             : 
     347             :                                             // ...ooo/script:module>
     348           0 :                                             m_xHandler->ignorableWhitespace( OUString() );
     349           0 :                                             m_xHandler->endElement( aModElementName );
     350             :                                         }
     351           0 :                                     }
     352             :                                 }
     353             : 
     354             :                                 // ...ooo/script:library-embedded>
     355           0 :                                 m_xHandler->ignorableWhitespace( OUString() );
     356           0 :                                 m_xHandler->endElement( aLibElementName );
     357           0 :                             }
     358             :                         }
     359           2 :                     }
     360             :                 }
     361             : 
     362             :                 // ...ooo/script:libraries>
     363           2 :                 m_xHandler->ignorableWhitespace( OUString() );
     364           2 :                 m_xHandler->endElement( aLibContElementName );
     365             : 
     366           4 :                 m_xHandler->endDocument();
     367             :             }
     368             :         }
     369           0 :         catch ( const container::NoSuchElementException& e )
     370             :         {
     371             :             SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught NoSuchElementException reason " << e.Message );
     372           0 :             bReturn = false;
     373             :         }
     374           0 :         catch ( const lang::IllegalArgumentException& e )
     375             :         {
     376             :             SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught IllegalArgumentException reason " << e.Message );
     377           0 :             bReturn = false;
     378             :         }
     379           0 :         catch ( const lang::WrappedTargetException& e )
     380             :         {
     381             :             SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught WrappedTargetException reason " << e.Message );
     382           0 :             bReturn = false;
     383             :         }
     384           0 :         catch ( const xml::sax::SAXException& e )
     385             :         {
     386             :             SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught SAXException reason " << e.Message );
     387           0 :             bReturn = false;
     388             :         }
     389             : 
     390           2 :         return bReturn;
     391             :     }
     392             : 
     393           0 :     void XMLBasicExporterBase::cancel()
     394             :         throw (RuntimeException, std::exception)
     395             :     {
     396           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     397             : 
     398             :         // cancel export
     399           0 :     }
     400             : 
     401             :     // XMLBasicExporter
     402             : 
     403           0 :     XMLBasicExporter::XMLBasicExporter( const Reference< XComponentContext >& rxContext )
     404           0 :         :XMLBasicExporterBase( rxContext, false )
     405             :     {
     406           0 :     }
     407             : 
     408           0 :     XMLBasicExporter::~XMLBasicExporter()
     409             :     {
     410           0 :     }
     411             : 
     412             :     // XServiceInfo
     413             : 
     414           0 :     OUString XMLBasicExporter::getImplementationName(  ) throw (RuntimeException, std::exception)
     415             :     {
     416           0 :         return getImplementationName_XMLBasicExporter();
     417             :     }
     418             : 
     419           0 :     Sequence< OUString > XMLBasicExporter::getSupportedServiceNames(  ) throw (RuntimeException, std::exception)
     420             :     {
     421           0 :         return getSupportedServiceNames_XMLBasicExporter();
     422             :     }
     423             : 
     424             :     // XMLOasisBasicExporter
     425             : 
     426           2 :     XMLOasisBasicExporter::XMLOasisBasicExporter( const Reference< XComponentContext >& rxContext )
     427           2 :         :XMLBasicExporterBase( rxContext, true )
     428             :     {
     429           2 :     }
     430             : 
     431           4 :     XMLOasisBasicExporter::~XMLOasisBasicExporter()
     432             :     {
     433           4 :     }
     434             : 
     435             :     // XServiceInfo
     436             : 
     437           0 :     OUString XMLOasisBasicExporter::getImplementationName(  ) throw (RuntimeException, std::exception)
     438             :     {
     439           0 :         return getImplementationName_XMLOasisBasicExporter();
     440             :     }
     441             : 
     442           0 :     Sequence< OUString > XMLOasisBasicExporter::getSupportedServiceNames(  ) throw (RuntimeException, std::exception)
     443             :     {
     444           0 :         return getSupportedServiceNames_XMLOasisBasicExporter();
     445             :     }
     446             : 
     447             :     // component operations
     448             : 
     449           0 :     Reference< XInterface > SAL_CALL create_XMLBasicExporter(
     450             :         Reference< XComponentContext > const & xContext )
     451             :         SAL_THROW(())
     452             :     {
     453           0 :         return static_cast< lang::XTypeProvider * >( new XMLBasicExporter( xContext ) );
     454             :     }
     455             : 
     456           2 :     Reference< XInterface > SAL_CALL create_XMLOasisBasicExporter(
     457             :         Reference< XComponentContext > const & xContext )
     458             :         SAL_THROW(())
     459             :     {
     460           2 :         return static_cast< lang::XTypeProvider * >( new XMLOasisBasicExporter( xContext ) );
     461             :     }
     462             : 
     463             : }   // namespace xmlscript
     464             : 
     465             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10