LCOV - code coverage report
Current view: top level - xmloff/source/core - XMLEmbeddedObjectImportContext.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 123 0.0 %
Date: 2014-04-14 Functions: 0 25 0.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 <com/sun/star/document/XImporter.hpp>
      21             : #include <com/sun/star/util/XModifiable.hpp>
      22             : #include <com/sun/star/util/XModifiable2.hpp>
      23             : #include <com/sun/star/frame/XStorable.hpp>
      24             : #include <tools/globname.hxx>
      25             : #include <comphelper/classids.hxx>
      26             : #include <xmloff/nmspmap.hxx>
      27             : #include <xmloff/xmlimp.hxx>
      28             : #include <xmloff/xmlnmspe.hxx>
      29             : #include <xmloff/xmltoken.hxx>
      30             : #include <xmloff/xmlerror.hxx>
      31             : #include <xmloff/attrlist.hxx>
      32             : #include <xmloff/XMLFilterServiceNames.h>
      33             : #include "XMLEmbeddedObjectImportContext.hxx"
      34             : 
      35             : using namespace ::com::sun::star::uno;
      36             : using namespace ::com::sun::star::util;
      37             : using namespace ::com::sun::star::beans;
      38             : using namespace ::com::sun::star::lang;
      39             : using namespace ::com::sun::star::frame;
      40             : using namespace ::com::sun::star::document;
      41             : using namespace ::com::sun::star::xml::sax;
      42             : using namespace ::xmloff::token;
      43             : 
      44             : struct XMLServiceMapEntry_Impl
      45             : {
      46             :     enum XMLTokenEnum eClass;
      47             :     const sal_Char *sFilterService;
      48             :     sal_Int32      nFilterServiceLen;
      49             : };
      50             : 
      51             : #define SERVICE_MAP_ENTRY( cls, app ) \
      52             :     { XML_##cls, \
      53             :       XML_IMPORT_FILTER_##app, sizeof(XML_IMPORT_FILTER_##app)-1}
      54             : 
      55             : const XMLServiceMapEntry_Impl aServiceMap[] =
      56             : {
      57             :     SERVICE_MAP_ENTRY( TEXT, WRITER ),
      58             :     SERVICE_MAP_ENTRY( ONLINE_TEXT, WRITER ),
      59             :     SERVICE_MAP_ENTRY( SPREADSHEET, CALC ),
      60             :     SERVICE_MAP_ENTRY( DRAWING, DRAW ),
      61             :     SERVICE_MAP_ENTRY( GRAPHICS, DRAW ),
      62             :     SERVICE_MAP_ENTRY( PRESENTATION, IMPRESS ),
      63             :     SERVICE_MAP_ENTRY( CHART, CHART ),
      64             :     { XML_TOKEN_INVALID, 0, 0 }
      65             : };
      66             : 
      67             : class XMLEmbeddedObjectImportContext_Impl : public SvXMLImportContext
      68             : {
      69             :     ::com::sun::star::uno::Reference<
      70             :         ::com::sun::star::xml::sax::XDocumentHandler > xHandler;
      71             : 
      72             : public:
      73             :     TYPEINFO_OVERRIDE();
      74             : 
      75             :     XMLEmbeddedObjectImportContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
      76             :                                     const OUString& rLName,
      77             :     const ::com::sun::star::uno::Reference<
      78             :         ::com::sun::star::xml::sax::XDocumentHandler >& rHandler );
      79             : 
      80             :     virtual ~XMLEmbeddedObjectImportContext_Impl();
      81             : 
      82             :     virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
      83             :                                    const OUString& rLocalName,
      84             :                                    const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
      85             : 
      86             :     virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
      87             : 
      88             :     virtual void EndElement() SAL_OVERRIDE;
      89             : 
      90             :     virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
      91             : };
      92             : 
      93           0 : TYPEINIT1( XMLEmbeddedObjectImportContext_Impl, SvXMLImportContext );
      94             : 
      95           0 : XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
      96             :         SvXMLImport& rImport, sal_uInt16 nPrfx,
      97             :         const OUString& rLName,
      98             :         const Reference< XDocumentHandler >& rHandler ) :
      99             :     SvXMLImportContext( rImport, nPrfx, rLName ),
     100           0 :     xHandler( rHandler )
     101             : {
     102           0 : }
     103             : 
     104           0 : XMLEmbeddedObjectImportContext_Impl::~XMLEmbeddedObjectImportContext_Impl()
     105             : {
     106           0 : }
     107             : 
     108           0 : SvXMLImportContext *XMLEmbeddedObjectImportContext_Impl::CreateChildContext(
     109             :         sal_uInt16 nPrefix,
     110             :         const OUString& rLocalName,
     111             :         const Reference< XAttributeList >& )
     112             : {
     113           0 :     return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
     114             :                                                     nPrefix, rLocalName,
     115           0 :                                                     xHandler );
     116             : }
     117             : 
     118           0 : void XMLEmbeddedObjectImportContext_Impl::StartElement(
     119             :         const Reference< XAttributeList >& xAttrList )
     120             : {
     121           0 :     xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
     122           0 :                                 GetPrefix(), GetLocalName() ),
     123           0 :                             xAttrList );
     124           0 : }
     125             : 
     126           0 : void XMLEmbeddedObjectImportContext_Impl::EndElement()
     127             : {
     128           0 :     xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
     129           0 :                                 GetPrefix(), GetLocalName() ) );
     130           0 : }
     131             : 
     132           0 : void XMLEmbeddedObjectImportContext_Impl::Characters( const OUString& rChars )
     133             : {
     134           0 :     xHandler->characters( rChars );
     135           0 : }
     136             : 
     137           0 : TYPEINIT1( XMLEmbeddedObjectImportContext, SvXMLImportContext );
     138             : 
     139           0 : sal_Bool XMLEmbeddedObjectImportContext::SetComponent(
     140             :         Reference< XComponent >& rComp )
     141             : {
     142           0 :     if( !rComp.is() || sFilterService.isEmpty() )
     143           0 :         return sal_False;
     144             : 
     145           0 :     Sequence<Any> aArgs( 0 );
     146             : 
     147           0 :     Reference< XComponentContext > xContext( GetImport().GetComponentContext() );
     148             : 
     149           0 :     xHandler = Reference < XDocumentHandler >(
     150           0 :         xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService, aArgs, xContext),
     151           0 :         UNO_QUERY);
     152             : 
     153           0 :     if( !xHandler.is() )
     154           0 :         return sal_False;
     155             : 
     156             :     try
     157             :     {
     158           0 :         Reference < XModifiable2 > xModifiable2( rComp, UNO_QUERY_THROW );
     159           0 :         xModifiable2->disableSetModified();
     160             :     }
     161           0 :     catch( Exception& )
     162             :     {
     163             :     }
     164             : 
     165           0 :     Reference < XImporter > xImporter( xHandler, UNO_QUERY );
     166           0 :     xImporter->setTargetDocument( rComp );
     167             : 
     168           0 :     xComp = rComp;  // keep ref to component only if there is a handler
     169             : 
     170           0 :     return sal_True;
     171             : }
     172             : 
     173           0 : XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
     174             :         SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
     175             :         const Reference< XAttributeList >& xAttrList ) :
     176           0 :     SvXMLImportContext( rImport, nPrfx, rLName )
     177             : {
     178           0 :     SvGlobalName aName;
     179             : 
     180           0 :     if( nPrfx == XML_NAMESPACE_MATH &&
     181           0 :         IsXMLToken( rLName, XML_MATH ) )
     182             :     {
     183           0 :         sFilterService = OUString( XML_IMPORT_FILTER_MATH );
     184           0 :         aName = SvGlobalName(SO3_SM_CLASSID);
     185             :     }
     186           0 :     else if( nPrfx == XML_NAMESPACE_OFFICE &&
     187           0 :         IsXMLToken( rLName, XML_DOCUMENT ) )
     188             :     {
     189           0 :         OUString sMime;
     190             : 
     191           0 :         sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
     192           0 :         for( sal_Int16 i=0; i < nAttrCount; i++ )
     193             :         {
     194           0 :             const OUString& rAttrName = xAttrList->getNameByIndex( i );
     195           0 :             OUString aLocalName;
     196           0 :             sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
     197           0 :             if( nPrefix == XML_NAMESPACE_OFFICE &&
     198           0 :                 IsXMLToken( aLocalName, XML_MIMETYPE ) )
     199             :             {
     200           0 :                 sMime = xAttrList->getValueByIndex( i );
     201           0 :                 break;
     202             :             }
     203           0 :         }
     204             : 
     205           0 :         OUString sClass;
     206             :         static const char * aTmp[] =
     207             :         {
     208             :             "application/vnd.oasis.openoffice.",
     209             :             "application/x-vnd.oasis.openoffice.",
     210             :             "application/vnd.oasis.opendocument.",
     211             :             "application/x-vnd.oasis.opendocument.",
     212             :             NULL
     213             :         };
     214           0 :         for (int k=0; aTmp[k]; k++)
     215             :         {
     216           0 :             OUString sTmpString = OUString::createFromAscii(aTmp[k]);
     217           0 :             if( sMime.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
     218             :             {
     219           0 :                 sClass = sMime.copy( sTmpString.getLength() );
     220           0 :                 break;
     221             :             }
     222           0 :         }
     223             : 
     224           0 :         if( !sClass.isEmpty() )
     225             :         {
     226           0 :             const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
     227           0 :             while( pEntry->eClass != XML_TOKEN_INVALID )
     228             :             {
     229           0 :                 if( IsXMLToken( sClass, pEntry->eClass ) )
     230             :                 {
     231           0 :                     sFilterService = OUString( pEntry->sFilterService,
     232             :                                                pEntry->nFilterServiceLen,
     233           0 :                                                RTL_TEXTENCODING_ASCII_US );
     234             : 
     235           0 :                     switch( pEntry->eClass )
     236             :                     {
     237           0 :                     case XML_TEXT:          aName = SvGlobalName(SO3_SW_CLASSID); break;
     238           0 :                     case XML_ONLINE_TEXT:   aName = SvGlobalName(SO3_SWWEB_CLASSID); break;
     239           0 :                     case XML_SPREADSHEET:   aName = SvGlobalName(SO3_SC_CLASSID); break;
     240             :                     case XML_DRAWING:
     241             :                     case XML_GRAPHICS:
     242           0 :                     case XML_IMAGE:     aName = SvGlobalName(SO3_SDRAW_CLASSID); break;
     243           0 :                     case XML_PRESENTATION:  aName = SvGlobalName(SO3_SIMPRESS_CLASSID); break;
     244           0 :                     case XML_CHART:         aName = SvGlobalName(SO3_SCH_CLASSID); break;
     245             :                     default:
     246           0 :                         break;
     247             :                     }
     248             : 
     249           0 :                     break;
     250             :                 }
     251           0 :                 pEntry++;
     252             :             }
     253           0 :         }
     254             :     }
     255             : 
     256           0 :     sCLSID = aName.GetHexName();
     257           0 : }
     258             : 
     259           0 : XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
     260             : {
     261           0 : }
     262             : 
     263           0 : SvXMLImportContext *XMLEmbeddedObjectImportContext::CreateChildContext(
     264             :         sal_uInt16 nPrefix, const OUString& rLocalName,
     265             :         const Reference< XAttributeList >& )
     266             : {
     267           0 :     if( xHandler.is() )
     268           0 :         return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
     269             :                                                         nPrefix, rLocalName,
     270           0 :                                                         xHandler );
     271             :     else
     272           0 :         return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
     273             : }
     274             : 
     275           0 : void XMLEmbeddedObjectImportContext::StartElement(
     276             :         const Reference< XAttributeList >& rAttrList )
     277             : {
     278           0 :     if( xHandler.is() )
     279             :     {
     280           0 :         xHandler->startDocument();
     281             :         // #i34042: copy namepspace declarations
     282           0 :         SvXMLAttributeList *pAttrList = new SvXMLAttributeList( rAttrList );
     283           0 :         Reference< XAttributeList > xAttrList( pAttrList );
     284           0 :         const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap();
     285           0 :         sal_uInt16 nPos = rNamespaceMap.GetFirstKey();
     286           0 :         while( USHRT_MAX != nPos )
     287             :         {
     288           0 :             OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) );
     289           0 :             if( xAttrList->getValueByName( aAttrName ).isEmpty() )
     290             :             {
     291             :                 pAttrList->AddAttribute( aAttrName,
     292           0 :                                           rNamespaceMap.GetNameByKey( nPos ) );
     293             :             }
     294           0 :             nPos = rNamespaceMap.GetNextKey( nPos );
     295           0 :         }
     296           0 :         xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
     297           0 :                                     GetPrefix(), GetLocalName() ),
     298           0 :                                 xAttrList );
     299             :     }
     300           0 : }
     301             : 
     302           0 : void XMLEmbeddedObjectImportContext::EndElement()
     303             : {
     304           0 :     if( xHandler.is() )
     305             :     {
     306           0 :         xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
     307           0 :                                     GetPrefix(), GetLocalName() ) );
     308           0 :         xHandler->endDocument();
     309             : 
     310             :     try
     311             :     {
     312           0 :         Reference < XModifiable2 > xModifiable2( xComp, UNO_QUERY_THROW );
     313           0 :         xModifiable2->enableSetModified();
     314           0 :         xModifiable2->setModified( sal_True ); // trigger new replacement image generation
     315             :     }
     316           0 :     catch( Exception& )
     317             :     {
     318             :     }
     319             :     }
     320           0 : }
     321             : 
     322           0 : void XMLEmbeddedObjectImportContext::Characters( const OUString& rChars )
     323             : {
     324           0 :     if( xHandler.is() )
     325           0 :         xHandler->characters( rChars );
     326           0 : }
     327             : 
     328             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10