Files
libreoffice/svx/source/xml/xmlgrhlp.cxx

406 lines
14 KiB
C++
Raw Normal View History

2000-11-30 06:32:41 +00:00
/*************************************************************************
*
* $RCSfile: xmlgrhlp.cxx,v $
*
2000-12-01 09:54:16 +00:00
* $Revision: 1.2 $
2000-11-30 06:32:41 +00:00
*
* last change: $Author: ka $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (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.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#ifndef _DEBUG_HXX
#include <tools/debug.hxx>
#endif
#ifndef _SV_CVTGRF_HXX
#include <vcl/cvtgrf.hxx>
#endif
#ifndef _SV_GFXLINK_HXX
#include <vcl/gfxlink.hxx>
#endif
#include "impgrf.hxx"
#include "xmlgrhlp.hxx"
// -----------
// - Defines -
// -----------
2000-12-01 09:54:16 +00:00
#define XML_GRAPHICSTORAGE_NAME "Pictures"
#define XML_PACKAGE_URL_BASE "vnd.sun.star.Package:"
#define XML_GRAPHICOBJECT_URL_BASE "vnd.sun.star.GraphicObject:"
2000-11-30 06:32:41 +00:00
// ----------------------
// - SvXMLGraphicHelper -
// ----------------------
SvXMLGraphicHelper::SvXMLGraphicHelper() :
::cppu::WeakComponentImplHelper1< ::com::sun::star::container::XIndexContainer >( maMutex )
{
}
// -----------------------------------------------------------------------------
SvXMLGraphicHelper::~SvXMLGraphicHelper()
{
}
// -----------------------------------------------------------------------------
2000-12-01 09:54:16 +00:00
sal_Bool SvXMLGraphicHelper::ImplGetStreamNames( const ::rtl::OUString& rURLStr,
::rtl::OUString& rPictureStorageName,
::rtl::OUString& rPictureStreamName )
2000-11-30 06:32:41 +00:00
{
2000-12-01 09:54:16 +00:00
String aURLStr( rURLStr );
sal_Bool bRet = sal_False;
if( aURLStr.Len() && ( aURLStr.GetTokenCount( ':' ) == 2 ) )
{
aURLStr = aURLStr.GetToken( aURLStr.GetTokenCount( ':' ) - 1, ':' );
const sal_uInt32 nTokenCount = aURLStr.GetTokenCount( '/' );
if( 1 == nTokenCount )
{
rPictureStorageName = String( RTL_CONSTASCII_USTRINGPARAM( XML_GRAPHICSTORAGE_NAME ) );
rPictureStreamName = aURLStr;
bRet = sal_True;
}
else if( 2 == nTokenCount )
{
rPictureStorageName = aURLStr.GetToken( 0, '/' );
rPictureStreamName = aURLStr.GetToken( 1, '/' );
bRet = sal_True;
}
else
{
DBG_ERROR( "SvXMLGraphicHelper::ImplInsertGraphicURL: invalid scheme" );
}
}
2000-11-30 06:32:41 +00:00
2000-12-01 09:54:16 +00:00
return bRet;
}
// -----------------------------------------------------------------------------
SvStorageRef SvXMLGraphicHelper::ImplGetGraphicStorage( const ::rtl::OUString& rStorageName )
{
if( !mxGraphicStorage.Is() || ( rStorageName != maCurStorageName ) )
mxGraphicStorage = mpRootStorage->OpenStorage( maCurStorageName = rStorageName, STREAM_READ | STREAM_WRITE );
2000-11-30 06:32:41 +00:00
return mxGraphicStorage;
}
// -----------------------------------------------------------------------------
2000-12-01 09:54:16 +00:00
SvStorageStreamRef SvXMLGraphicHelper::ImplGetGraphicStream( const ::rtl::OUString& rPictureStorageName,
const ::rtl::OUString& rPictureStreamName,
BOOL bTruncate )
2000-11-30 06:32:41 +00:00
{
SvStorageStreamRef xStm;
2000-12-01 09:54:16 +00:00
SvStorageRef xStorage( ImplGetGraphicStorage( rPictureStorageName ) );
2000-11-30 06:32:41 +00:00
if( xStorage.Is() )
{
2000-12-01 09:54:16 +00:00
xStm = xStorage->OpenStream( rPictureStreamName,
STREAM_READ |
( ( GRAPHICHELPER_MODE_WRITE == meCreateMode ) ?
( STREAM_WRITE | ( bTruncate ? STREAM_TRUNC : 0 ) ) : 0 ) );
2000-11-30 06:32:41 +00:00
}
return xStm;
}
// -----------------------------------------------------------------------------
2000-12-01 09:54:16 +00:00
Graphic SvXMLGraphicHelper::ImplReadGraphic( const ::rtl::OUString& rPictureStorageName,
const ::rtl::OUString& rPictureStreamName )
{
Graphic aGraphic;
SvStorageStreamRef xStm( ImplGetGraphicStream( rPictureStorageName, rPictureStreamName, FALSE ) );
if( xStm.Is() )
GetGrfFilter()->ImportGraphic( aGraphic, String(), *xStm );
return aGraphic;
}
// -----------------------------------------------------------------------------
void SvXMLGraphicHelper::ImplWriteGraphic( const ::rtl::OUString& rPictureStorageName,
const ::rtl::OUString& rPictureStreamName )
2000-11-30 06:32:41 +00:00
{
2000-12-01 09:54:16 +00:00
GraphicObject aGrfObject( ByteString( String( rPictureStreamName ), RTL_TEXTENCODING_ASCII_US ) );
2000-11-30 06:32:41 +00:00
if( aGrfObject.GetType() != GRAPHIC_NONE )
{
2000-12-01 09:54:16 +00:00
SvStorageStreamRef xStm( ImplGetGraphicStream( rPictureStorageName, rPictureStreamName, TRUE ) );
2000-11-30 06:32:41 +00:00
if( xStm.Is() )
{
Graphic aGraphic( (Graphic&) aGrfObject.GetGraphic() );
const GfxLink aGfxLink( aGraphic.GetLink() );
if( aGfxLink.GetDataSize() )
xStm->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
else
{
if( aGraphic.GetType() == GRAPHIC_BITMAP )
{
GraphicFilter* pFilter = GetGrfFilter();
String aFormat;
if( aGraphic.IsAnimated() )
aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "GIF" ) );
else
aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "PNG" ) );
pFilter->ExportGraphic( aGraphic, String(), *xStm,
pFilter->GetExportFormatNumberForShortName( aFormat ) );
}
else if( aGraphic.GetType() == GRAPHIC_GDIMETAFILE )
( (GDIMetaFile&) aGraphic.GetGDIMetaFile() ).Write( *xStm );
}
}
}
}
// -----------------------------------------------------------------------------
2000-12-01 09:54:16 +00:00
void SvXMLGraphicHelper::ImplInsertGraphicURL( const ::rtl::OUString& rURLStr, sal_uInt32 nInsertPos )
2000-11-30 06:32:41 +00:00
{
2000-12-01 09:54:16 +00:00
::rtl::OUString aPictureStorageName, aPictureStreamName;
if( ( maIdSet.find( rURLStr ) == maIdSet.end() ) &&
ImplGetStreamNames( rURLStr, aPictureStorageName, aPictureStreamName ) )
{
const static ::rtl::OUString aBaseURL( RTL_CONSTASCII_USTRINGPARAM( XML_GRAPHICOBJECT_URL_BASE ) );
if( GRAPHICHELPER_MODE_READ == meCreateMode )
{
const GraphicObject aObj( ImplReadGraphic( aPictureStorageName, aPictureStreamName ) );
if( aObj.GetType() != GRAPHIC_NONE )
{
maGrfVector.push_back( aObj );
( maVector[ nInsertPos ] = aBaseURL ) += String( aObj.GetUniqueID().GetBuffer(), RTL_TEXTENCODING_ASCII_US );
}
else
maVector[ nInsertPos ] = String();
}
else if( mbDirect )
ImplWriteGraphic( aPictureStorageName, aPictureStreamName );
2000-11-30 06:32:41 +00:00
2000-12-01 09:54:16 +00:00
maIdSet.insert( rURLStr );
}
2000-11-30 06:32:41 +00:00
}
// -----------------------------------------------------------------------------
void SvXMLGraphicHelper::Init( SvStorage& rXMLStorage,
SvXMLGraphicHelperMode eCreateMode,
BOOL bDirect )
{
mpRootStorage = &rXMLStorage;
meCreateMode = eCreateMode;
mbDirect = bDirect;
}
// -----------------------------------------------------------------------------
SvXMLGraphicHelper* SvXMLGraphicHelper::Create( SvStorage& rXMLStorage,
SvXMLGraphicHelperMode eCreateMode,
BOOL bDirect )
{
SvXMLGraphicHelper* pThis = new SvXMLGraphicHelper;
pThis->acquire();
pThis->Init( rXMLStorage, eCreateMode, bDirect );
return pThis;
}
// -----------------------------------------------------------------------------
void SvXMLGraphicHelper::Destroy( SvXMLGraphicHelper* pSvXMLGraphicHelper )
{
if( pSvXMLGraphicHelper )
{
pSvXMLGraphicHelper->Flush();
pSvXMLGraphicHelper->release();
}
}
// -----------------------------------------------------------------------------
void SvXMLGraphicHelper::Flush()
{
2000-12-01 09:54:16 +00:00
if( ( GRAPHICHELPER_MODE_WRITE == meCreateMode ) && !mbDirect )
2000-11-30 06:32:41 +00:00
{
2000-12-01 09:54:16 +00:00
::_STL::set< ::rtl::OUString >::iterator aIter( maIdSet.begin() ), aEnd( maIdSet.end() );
2000-11-30 06:32:41 +00:00
while( aIter != aEnd )
2000-12-01 09:54:16 +00:00
{
::rtl::OUString aPictureStorageName, aPictureStreamName;
if( ImplGetStreamNames( *aIter++, aPictureStorageName, aPictureStreamName ) )
ImplWriteGraphic( aPictureStorageName, aPictureStreamName );
}
2000-11-30 06:32:41 +00:00
}
}
// -----------------------------------------------------------------------------
void SAL_CALL SvXMLGraphicHelper::insertByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& rElement )
throw( ::com::sun::star::lang::IllegalArgumentException,
::com::sun::star::lang::IndexOutOfBoundsException,
::com::sun::star::lang::WrappedTargetException,
::com::sun::star::uno::RuntimeException )
{
::osl::MutexGuard aGuard( maMutex );
2000-12-01 09:54:16 +00:00
::rtl::OUString aStr;
2000-11-30 06:32:41 +00:00
rElement >>= aStr;
if( aStr.getLength() )
{
if( nIndex == (sal_Int32) maVector.size() )
maVector.push_back( aStr );
else
{
if( nIndex > (sal_Int32) maVector.size() )
maVector.resize( nIndex + 1 );
maVector[ nIndex ] = aStr;
}
2000-12-01 09:54:16 +00:00
ImplInsertGraphicURL( aStr, nIndex );
2000-11-30 06:32:41 +00:00
}
}
// -----------------------------------------------------------------------------
void SAL_CALL SvXMLGraphicHelper::removeByIndex( sal_Int32 nIndex )
throw( ::com::sun::star::lang::IndexOutOfBoundsException,
::com::sun::star::lang::WrappedTargetException,
::com::sun::star::uno::RuntimeException )
{
::osl::MutexGuard aGuard( maMutex );
DBG_ASSERT( nIndex < getCount(), "invalid Index" );
maVector.erase( maVector.begin() + nIndex );
}
// -----------------------------------------------------------------------------
void SAL_CALL SvXMLGraphicHelper::replaceByIndex( sal_Int32 nIndex, const ::com::sun::star::uno::Any& rElement )
throw( ::com::sun::star::lang::IllegalArgumentException,
::com::sun::star::lang::IndexOutOfBoundsException,
::com::sun::star::lang::WrappedTargetException,
::com::sun::star::uno::RuntimeException )
{
2000-12-01 09:54:16 +00:00
::osl::MutexGuard aGuard( maMutex );
::rtl::OUString aStr;
2000-11-30 06:32:41 +00:00
rElement >>= aStr;
if( aStr.getLength() )
{
2000-12-01 09:54:16 +00:00
DBG_ASSERT( nIndex < getCount(), "invalid Index" );
2000-11-30 06:32:41 +00:00
maVector[ nIndex ] = aStr;
2000-12-01 09:54:16 +00:00
ImplInsertGraphicURL( aStr, nIndex );
2000-11-30 06:32:41 +00:00
}
}
// -----------------------------------------------------------------------------
sal_Int32 SAL_CALL SvXMLGraphicHelper::getCount()
throw( ::com::sun::star::uno::RuntimeException )
{
::osl::MutexGuard aGuard( maMutex );
return maVector.size();
}
// -----------------------------------------------------------------------------
::com::sun::star::uno::Any SAL_CALL SvXMLGraphicHelper::getByIndex( sal_Int32 nIndex )
throw( ::com::sun::star::lang::IndexOutOfBoundsException,
::com::sun::star::lang::WrappedTargetException,
::com::sun::star::uno::RuntimeException )
{
2000-12-01 09:54:16 +00:00
::osl::MutexGuard aGuard( maMutex );
::com::sun::star::uno::Any aAny;
2000-11-30 06:32:41 +00:00
DBG_ASSERT( nIndex < getCount(), "invalid Index" );
aAny <<= maVector[ nIndex ];
return aAny;
}
// -----------------------------------------------------------------------------
::com::sun::star::uno::Type SAL_CALL SvXMLGraphicHelper::getElementType()
throw( ::com::sun::star::uno::RuntimeException )
{
::osl::MutexGuard aGuard( maMutex );
return ::getCppuType( (const ::rtl::OUString*) 0 );
}
// -----------------------------------------------------------------------------
sal_Bool SAL_CALL SvXMLGraphicHelper::hasElements()
throw( ::com::sun::star::uno::RuntimeException )
{
::osl::MutexGuard aGuard( maMutex );
return maVector.size() > 0;
}