xref: /aoo42x/main/svx/source/xml/xmlgrhlp.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
31*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_EMBED_ElementModes_HPP_
32*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
38*cdf0e10cSrcweir #include <cppuhelper/compbase4.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
41*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
42*cdf0e10cSrcweir #include <unotools/tempfile.hxx>
43*cdf0e10cSrcweir #include <tools/debug.hxx>
44*cdf0e10cSrcweir #include <vcl/cvtgrf.hxx>
45*cdf0e10cSrcweir #include <vcl/gfxlink.hxx>
46*cdf0e10cSrcweir #include <vcl/metaact.hxx>
47*cdf0e10cSrcweir #include <tools/zcodec.hxx>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "svtools/filter.hxx"
50*cdf0e10cSrcweir #include "svx/xmlgrhlp.hxx"
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <algorithm>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir // -----------
55*cdf0e10cSrcweir // - Defines -
56*cdf0e10cSrcweir // -----------
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir using namespace com::sun::star;
59*cdf0e10cSrcweir using namespace com::sun::star::uno;
60*cdf0e10cSrcweir using namespace com::sun::star::io;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir #define XML_GRAPHICSTORAGE_NAME		"Pictures"
65*cdf0e10cSrcweir #define XML_PACKAGE_URL_BASE		"vnd.sun.star.Package:"
66*cdf0e10cSrcweir #define XML_GRAPHICOBJECT_URL_BASE	"vnd.sun.star.GraphicObject:"
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // ---------------------------
69*cdf0e10cSrcweir // - SvXMLGraphicInputStream -
70*cdf0e10cSrcweir // ---------------------------
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir const MetaCommentAction* ImplCheckForEPS( GDIMetaFile& rMtf )
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir 	static ByteString aComment( (const sal_Char*)"EPSReplacementGraphic" );
75*cdf0e10cSrcweir 	const MetaCommentAction* pComment = NULL;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	if ( ( rMtf.GetActionCount() >= 2 )
78*cdf0e10cSrcweir 			&& ( rMtf.FirstAction()->GetType() == META_EPS_ACTION )
79*cdf0e10cSrcweir 			&& ( ((const MetaAction*)rMtf.GetAction( 1 ))->GetType() == META_COMMENT_ACTION )
80*cdf0e10cSrcweir 			&& ( ((const MetaCommentAction*)rMtf.GetAction( 1 ))->GetComment() == aComment ) )
81*cdf0e10cSrcweir 		pComment = (const MetaCommentAction*)rMtf.GetAction( 1 );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	return pComment;
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir class SvXMLGraphicInputStream : public::cppu::WeakImplHelper1< XInputStream >
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir private:
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     virtual sal_Int32	SAL_CALL	readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
91*cdf0e10cSrcweir 	virtual sal_Int32	SAL_CALL	readSomeBytes(Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
92*cdf0e10cSrcweir 	virtual void		SAL_CALL	skipBytes(sal_Int32 nBytesToSkip) throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
93*cdf0e10cSrcweir 	virtual sal_Int32	SAL_CALL	available() throw(NotConnectedException, RuntimeException);
94*cdf0e10cSrcweir 	virtual void		SAL_CALL	closeInput() throw(NotConnectedException, RuntimeException);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir private:
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     ::utl::TempFile                 maTmp;
99*cdf0e10cSrcweir     Reference< XInputStream >       mxStmWrapper;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir                                     // not available
102*cdf0e10cSrcweir                                     SvXMLGraphicInputStream();
103*cdf0e10cSrcweir                                     SvXMLGraphicInputStream( const SvXMLGraphicInputStream& );
104*cdf0e10cSrcweir     SvXMLGraphicInputStream&        operator==( SvXMLGraphicInputStream& );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir public:
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir                                     SvXMLGraphicInputStream( const ::rtl::OUString& rGraphicId );
109*cdf0e10cSrcweir     virtual                         ~SvXMLGraphicInputStream();
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     sal_Bool                        Exists() const { return mxStmWrapper.is(); }
112*cdf0e10cSrcweir };
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir // -----------------------------------------------------------------------------
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir SvXMLGraphicInputStream::SvXMLGraphicInputStream( const ::rtl::OUString& rGraphicId )
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	String			aGraphicId( rGraphicId );
119*cdf0e10cSrcweir 	GraphicObject	aGrfObject( ByteString( aGraphicId, RTL_TEXTENCODING_ASCII_US ) );
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     maTmp.EnableKillingFile();
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	if( aGrfObject.GetType() != GRAPHIC_NONE )
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir         SvStream* pStm = ::utl::UcbStreamHelper::CreateStream( maTmp.GetURL(), STREAM_WRITE | STREAM_TRUNC );
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         if( pStm )
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir 			Graphic			aGraphic( (Graphic&) aGrfObject.GetGraphic() );
130*cdf0e10cSrcweir 			const GfxLink	aGfxLink( aGraphic.GetLink() );
131*cdf0e10cSrcweir             sal_Bool        bRet = sal_False;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 			if( aGfxLink.GetDataSize() && aGfxLink.GetData() )
134*cdf0e10cSrcweir             {
135*cdf0e10cSrcweir 				pStm->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
136*cdf0e10cSrcweir 			    bRet = ( pStm->GetError() == 0 );
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir 			else
139*cdf0e10cSrcweir 			{
140*cdf0e10cSrcweir 				if( aGraphic.GetType() == GRAPHIC_BITMAP )
141*cdf0e10cSrcweir 				{
142*cdf0e10cSrcweir 					GraphicFilter*  pFilter = GraphicFilter::GetGraphicFilter();
143*cdf0e10cSrcweir                     String          aFormat;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 					if( aGraphic.IsAnimated() )
146*cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "gif" ) );
147*cdf0e10cSrcweir 					else
148*cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "png" ) );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 					bRet = ( pFilter->ExportGraphic( aGraphic, String(), *pStm, pFilter->GetExportFormatNumberForShortName( aFormat ) ) == 0 );
151*cdf0e10cSrcweir 				}
152*cdf0e10cSrcweir 				else if( aGraphic.GetType() == GRAPHIC_GDIMETAFILE )
153*cdf0e10cSrcweir 				{
154*cdf0e10cSrcweir 					pStm->SetVersion( SOFFICE_FILEFORMAT_8 );
155*cdf0e10cSrcweir 					pStm->SetCompressMode( COMPRESSMODE_ZBITMAP );
156*cdf0e10cSrcweir 					( (GDIMetaFile&) aGraphic.GetGDIMetaFile() ).Write( *pStm, GDIMETAFILE_WRITE_REPLACEMENT_RENDERGRAPHIC );
157*cdf0e10cSrcweir     			    bRet = ( pStm->GetError() == 0 );
158*cdf0e10cSrcweir 				}
159*cdf0e10cSrcweir 			}
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir             if( bRet )
162*cdf0e10cSrcweir             {
163*cdf0e10cSrcweir                 pStm->Seek( 0 );
164*cdf0e10cSrcweir                 mxStmWrapper = new ::utl::OInputStreamWrapper( pStm, sal_True );
165*cdf0e10cSrcweir             }
166*cdf0e10cSrcweir             else
167*cdf0e10cSrcweir                 delete pStm;
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir // -----------------------------------------------------------------------------
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir SvXMLGraphicInputStream::~SvXMLGraphicInputStream()
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir // -----------------------------------------------------------------------------
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir sal_Int32 SAL_CALL SvXMLGraphicInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
181*cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, RuntimeException )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
184*cdf0e10cSrcweir         throw NotConnectedException();
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     return mxStmWrapper->readBytes( rData, nBytesToRead );
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir // -----------------------------------------------------------------------------
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir sal_Int32 SAL_CALL SvXMLGraphicInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
192*cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, RuntimeException )
193*cdf0e10cSrcweir {
194*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
195*cdf0e10cSrcweir         throw NotConnectedException() ;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     return mxStmWrapper->readSomeBytes( rData, nMaxBytesToRead );
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir // -----------------------------------------------------------------------------
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicInputStream::skipBytes( sal_Int32 nBytesToSkip )
203*cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, RuntimeException )
204*cdf0e10cSrcweir {
205*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
206*cdf0e10cSrcweir         throw NotConnectedException() ;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     mxStmWrapper->skipBytes( nBytesToSkip );
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir // -----------------------------------------------------------------------------
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir sal_Int32 SAL_CALL SvXMLGraphicInputStream::available() throw( NotConnectedException, RuntimeException )
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
216*cdf0e10cSrcweir         throw NotConnectedException() ;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     return mxStmWrapper->available();
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir // -----------------------------------------------------------------------------
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicInputStream::closeInput() throw( NotConnectedException, RuntimeException )
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
226*cdf0e10cSrcweir         throw NotConnectedException() ;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir     mxStmWrapper->closeInput();
229*cdf0e10cSrcweir }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir // ----------------------------
232*cdf0e10cSrcweir // - SvXMLGraphicOutputStream -
233*cdf0e10cSrcweir // ----------------------------
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir class SvXMLGraphicOutputStream : public::cppu::WeakImplHelper1< XOutputStream >
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir private:
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir     // XOutputStream
240*cdf0e10cSrcweir     virtual void SAL_CALL           writeBytes( const Sequence< sal_Int8 >& rData ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
241*cdf0e10cSrcweir     virtual void SAL_CALL           flush() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
242*cdf0e10cSrcweir     virtual void SAL_CALL           closeOutput() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir private:
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir     ::utl::TempFile*                mpTmp;
247*cdf0e10cSrcweir     SvStream*                       mpOStm;
248*cdf0e10cSrcweir     Reference< XOutputStream >      mxStmWrapper;
249*cdf0e10cSrcweir     GraphicObject                   maGrfObj;
250*cdf0e10cSrcweir     sal_Bool                        mbClosed;
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir                                     // not available
253*cdf0e10cSrcweir                                     SvXMLGraphicOutputStream( const SvXMLGraphicOutputStream& );
254*cdf0e10cSrcweir     SvXMLGraphicOutputStream&       operator==( SvXMLGraphicOutputStream& );
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir public:
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir                                     SvXMLGraphicOutputStream();
259*cdf0e10cSrcweir     virtual                         ~SvXMLGraphicOutputStream();
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     sal_Bool                        Exists() const { return mxStmWrapper.is(); }
262*cdf0e10cSrcweir     const GraphicObject&            GetGraphicObject();
263*cdf0e10cSrcweir };
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir // -----------------------------------------------------------------------------
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir SvXMLGraphicOutputStream::SvXMLGraphicOutputStream() :
268*cdf0e10cSrcweir     mpTmp( new ::utl::TempFile ),
269*cdf0e10cSrcweir     mbClosed( sal_False )
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir     mpTmp->EnableKillingFile();
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir     mpOStm = ::utl::UcbStreamHelper::CreateStream( mpTmp->GetURL(), STREAM_WRITE | STREAM_TRUNC );
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     if( mpOStm )
276*cdf0e10cSrcweir         mxStmWrapper = new ::utl::OOutputStreamWrapper( *mpOStm );
277*cdf0e10cSrcweir }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir // -----------------------------------------------------------------------------
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir SvXMLGraphicOutputStream::~SvXMLGraphicOutputStream()
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir     delete mpTmp;
284*cdf0e10cSrcweir     delete mpOStm;
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir // -----------------------------------------------------------------------------
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicOutputStream::writeBytes( const Sequence< sal_Int8 >& rData )
290*cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
293*cdf0e10cSrcweir         throw NotConnectedException() ;
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir     mxStmWrapper->writeBytes( rData );
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir // -----------------------------------------------------------------------------
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicOutputStream::flush()
301*cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
304*cdf0e10cSrcweir         throw NotConnectedException() ;
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     mxStmWrapper->flush();
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir // -----------------------------------------------------------------------------
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicOutputStream::closeOutput()
312*cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
313*cdf0e10cSrcweir {
314*cdf0e10cSrcweir     if( !mxStmWrapper.is() )
315*cdf0e10cSrcweir         throw NotConnectedException() ;
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir     mxStmWrapper->closeOutput();
318*cdf0e10cSrcweir     mxStmWrapper = Reference< XOutputStream >();
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     mbClosed = sal_True;
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir // ------------------------------------------------------------------------------
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir const GraphicObject& SvXMLGraphicOutputStream::GetGraphicObject()
326*cdf0e10cSrcweir {
327*cdf0e10cSrcweir     if( mbClosed && ( maGrfObj.GetType() == GRAPHIC_NONE ) && mpOStm )
328*cdf0e10cSrcweir     {
329*cdf0e10cSrcweir     	Graphic aGraphic;
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir         mpOStm->Seek( 0 );
332*cdf0e10cSrcweir 		sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW;
333*cdf0e10cSrcweir 		sal_uInt16 pDeterminedFormat = GRFILTER_FORMAT_DONTKNOW;
334*cdf0e10cSrcweir         GraphicFilter::GetGraphicFilter()->ImportGraphic( aGraphic, String(), *mpOStm ,nFormat,&pDeterminedFormat );
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 		if (pDeterminedFormat == GRFILTER_FORMAT_DONTKNOW)
337*cdf0e10cSrcweir 		{
338*cdf0e10cSrcweir 			//Read the first two byte to check whether it is a gzipped stream, is so it may be in wmz or emz format
339*cdf0e10cSrcweir 			//unzip them and try again
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir 			sal_uInt8    sFirstBytes[ 2 ];
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir 			mpOStm->Seek( STREAM_SEEK_TO_END );
344*cdf0e10cSrcweir 			sal_uIntPtr nStreamLen = mpOStm->Tell();
345*cdf0e10cSrcweir 			mpOStm->Seek( 0 );
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir 			if ( !nStreamLen )
348*cdf0e10cSrcweir 			{
349*cdf0e10cSrcweir 				SvLockBytes* pLockBytes = mpOStm->GetLockBytes();
350*cdf0e10cSrcweir 				if ( pLockBytes  )
351*cdf0e10cSrcweir 					pLockBytes->SetSynchronMode( sal_True );
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir 				mpOStm->Seek( STREAM_SEEK_TO_END );
354*cdf0e10cSrcweir 				nStreamLen = mpOStm->Tell();
355*cdf0e10cSrcweir 				mpOStm->Seek( 0 );
356*cdf0e10cSrcweir 			}
357*cdf0e10cSrcweir 			if( nStreamLen >= 2 )
358*cdf0e10cSrcweir 			{
359*cdf0e10cSrcweir 				//read two byte
360*cdf0e10cSrcweir 				mpOStm->Read( sFirstBytes, 2 );
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 				if( sFirstBytes[0] == 0x1f && sFirstBytes[1] == 0x8b )
363*cdf0e10cSrcweir 				{
364*cdf0e10cSrcweir 					SvMemoryStream* pDest = new SvMemoryStream;
365*cdf0e10cSrcweir 					ZCodec aZCodec( 0x8000, 0x8000 );
366*cdf0e10cSrcweir 					aZCodec.BeginCompression(ZCODEC_GZ_LIB);
367*cdf0e10cSrcweir 					mpOStm->Seek( 0 );
368*cdf0e10cSrcweir 					aZCodec.Decompress( *mpOStm, *pDest );
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir 					if (aZCodec.EndCompression() && pDest )
371*cdf0e10cSrcweir 					{
372*cdf0e10cSrcweir 						pDest->Seek( STREAM_SEEK_TO_END );
373*cdf0e10cSrcweir 						sal_uIntPtr nStreamLen_ = pDest->Tell();
374*cdf0e10cSrcweir 						if (nStreamLen_)
375*cdf0e10cSrcweir 						{
376*cdf0e10cSrcweir 							pDest->Seek(0L);
377*cdf0e10cSrcweir 					        GraphicFilter::GetGraphicFilter()->ImportGraphic( aGraphic, String(), *pDest ,nFormat,&pDeterminedFormat );
378*cdf0e10cSrcweir 						}
379*cdf0e10cSrcweir 					}
380*cdf0e10cSrcweir 					delete pDest;
381*cdf0e10cSrcweir 				}
382*cdf0e10cSrcweir 			}
383*cdf0e10cSrcweir 		}
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 		maGrfObj = aGraphic;
386*cdf0e10cSrcweir         if( maGrfObj.GetType() != GRAPHIC_NONE )
387*cdf0e10cSrcweir 	    {
388*cdf0e10cSrcweir 		    delete mpOStm, mpOStm = NULL;
389*cdf0e10cSrcweir 			delete mpTmp, mpTmp = NULL;
390*cdf0e10cSrcweir 		}
391*cdf0e10cSrcweir     }
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir     return maGrfObj;
394*cdf0e10cSrcweir }
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir // ----------------------
397*cdf0e10cSrcweir // - SvXMLGraphicHelper -
398*cdf0e10cSrcweir // ----------------------
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir SvXMLGraphicHelper::SvXMLGraphicHelper( SvXMLGraphicHelperMode eCreateMode ) :
401*cdf0e10cSrcweir 	::cppu::WeakComponentImplHelper2< ::com::sun::star::document::XGraphicObjectResolver,
402*cdf0e10cSrcweir                                       ::com::sun::star::document::XBinaryStreamResolver >( maMutex )
403*cdf0e10cSrcweir {
404*cdf0e10cSrcweir 	Init( NULL, eCreateMode, sal_False );
405*cdf0e10cSrcweir }
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir SvXMLGraphicHelper::SvXMLGraphicHelper() :
408*cdf0e10cSrcweir 	::cppu::WeakComponentImplHelper2< ::com::sun::star::document::XGraphicObjectResolver,
409*cdf0e10cSrcweir                                       ::com::sun::star::document::XBinaryStreamResolver >( maMutex )
410*cdf0e10cSrcweir {
411*cdf0e10cSrcweir }
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir // -----------------------------------------------------------------------------
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir SvXMLGraphicHelper::~SvXMLGraphicHelper()
416*cdf0e10cSrcweir {
417*cdf0e10cSrcweir }
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir // -----------------------------------------------------------------------------
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicHelper::disposing()
422*cdf0e10cSrcweir {
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir // -----------------------------------------------------------------------------
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir sal_Bool SvXMLGraphicHelper::ImplGetStreamNames( const ::rtl::OUString& rURLStr,
428*cdf0e10cSrcweir 												 ::rtl::OUString& rPictureStorageName,
429*cdf0e10cSrcweir 												 ::rtl::OUString& rPictureStreamName )
430*cdf0e10cSrcweir {
431*cdf0e10cSrcweir 	String		aURLStr( rURLStr );
432*cdf0e10cSrcweir 	sal_Bool	bRet = sal_False;
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir 	if( aURLStr.Len() )
435*cdf0e10cSrcweir 	{
436*cdf0e10cSrcweir 		aURLStr = aURLStr.GetToken( aURLStr.GetTokenCount( ':' ) - 1, ':' );
437*cdf0e10cSrcweir 		const sal_uInt32 nTokenCount = aURLStr.GetTokenCount( '/' );
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir 		if( 1 == nTokenCount )
440*cdf0e10cSrcweir 		{
441*cdf0e10cSrcweir 			rPictureStorageName = String( RTL_CONSTASCII_USTRINGPARAM( XML_GRAPHICSTORAGE_NAME ) );
442*cdf0e10cSrcweir 			rPictureStreamName = aURLStr;
443*cdf0e10cSrcweir 			bRet = sal_True;
444*cdf0e10cSrcweir 		}
445*cdf0e10cSrcweir 		else if( 2 == nTokenCount )
446*cdf0e10cSrcweir 		{
447*cdf0e10cSrcweir 			rPictureStorageName = aURLStr.GetToken( 0, '/' );
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir 			DBG_ASSERT( rPictureStorageName.getLength() &&
450*cdf0e10cSrcweir 					   rPictureStorageName.getStr()[ 0 ] != '#',
451*cdf0e10cSrcweir 					   "invalid relative URL" );
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir 			rPictureStreamName = aURLStr.GetToken( 1, '/' );
454*cdf0e10cSrcweir 			bRet = sal_True;
455*cdf0e10cSrcweir 		}
456*cdf0e10cSrcweir 		else
457*cdf0e10cSrcweir 		{
458*cdf0e10cSrcweir 			DBG_ERROR( "SvXMLGraphicHelper::ImplInsertGraphicURL: invalid scheme" );
459*cdf0e10cSrcweir 		}
460*cdf0e10cSrcweir 	}
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir 	return bRet;
463*cdf0e10cSrcweir }
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir // -----------------------------------------------------------------------------
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir uno::Reference < embed::XStorage > SvXMLGraphicHelper::ImplGetGraphicStorage( const ::rtl::OUString& rStorageName )
468*cdf0e10cSrcweir {
469*cdf0e10cSrcweir     uno::Reference < embed::XStorage > xRetStorage;
470*cdf0e10cSrcweir     if( mxRootStorage.is() )
471*cdf0e10cSrcweir 	{
472*cdf0e10cSrcweir         try
473*cdf0e10cSrcweir         {
474*cdf0e10cSrcweir             xRetStorage = mxRootStorage->openStorageElement(
475*cdf0e10cSrcweir                 maCurStorageName = rStorageName,
476*cdf0e10cSrcweir                 ( GRAPHICHELPER_MODE_WRITE == meCreateMode )
477*cdf0e10cSrcweir                     ? embed::ElementModes::READWRITE
478*cdf0e10cSrcweir                     : embed::ElementModes::READ );
479*cdf0e10cSrcweir         }
480*cdf0e10cSrcweir         catch ( uno::Exception& )
481*cdf0e10cSrcweir         {
482*cdf0e10cSrcweir         }
483*cdf0e10cSrcweir         //#i43196# try again to open the storage element - this time readonly
484*cdf0e10cSrcweir         if(!xRetStorage.is())
485*cdf0e10cSrcweir         {
486*cdf0e10cSrcweir             try
487*cdf0e10cSrcweir             {
488*cdf0e10cSrcweir                 xRetStorage = mxRootStorage->openStorageElement( maCurStorageName = rStorageName, embed::ElementModes::READ );
489*cdf0e10cSrcweir             }
490*cdf0e10cSrcweir             catch ( uno::Exception& )
491*cdf0e10cSrcweir             {
492*cdf0e10cSrcweir             }
493*cdf0e10cSrcweir         }
494*cdf0e10cSrcweir     }
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir 	return xRetStorage;
497*cdf0e10cSrcweir }
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir // -----------------------------------------------------------------------------
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir SvxGraphicHelperStream_Impl SvXMLGraphicHelper::ImplGetGraphicStream( const ::rtl::OUString& rPictureStorageName,
502*cdf0e10cSrcweir 															  const ::rtl::OUString& rPictureStreamName,
503*cdf0e10cSrcweir 															  sal_Bool bTruncate )
504*cdf0e10cSrcweir {
505*cdf0e10cSrcweir     SvxGraphicHelperStream_Impl aRet;
506*cdf0e10cSrcweir     aRet.xStorage = ImplGetGraphicStorage( rPictureStorageName );
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir     if( aRet.xStorage.is() )
509*cdf0e10cSrcweir 	{
510*cdf0e10cSrcweir         sal_Int32 nMode = embed::ElementModes::READ;
511*cdf0e10cSrcweir         if ( GRAPHICHELPER_MODE_WRITE == meCreateMode )
512*cdf0e10cSrcweir         {
513*cdf0e10cSrcweir             nMode = embed::ElementModes::READWRITE;
514*cdf0e10cSrcweir             if ( bTruncate )
515*cdf0e10cSrcweir                 nMode |= embed::ElementModes::TRUNCATE;
516*cdf0e10cSrcweir         }
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir         aRet.xStream = aRet.xStorage->openStreamElement( rPictureStreamName, nMode );
519*cdf0e10cSrcweir         if( aRet.xStream.is() && ( GRAPHICHELPER_MODE_WRITE == meCreateMode ) )
520*cdf0e10cSrcweir 		{
521*cdf0e10cSrcweir //REMOVE				::rtl::OUString aPropName( RTL_CONSTASCII_USTRINGPARAM("Encrypted") );
522*cdf0e10cSrcweir 			::rtl::OUString aPropName( RTL_CONSTASCII_USTRINGPARAM("UseCommonStoragePasswordEncryption") );
523*cdf0e10cSrcweir             uno::Reference < beans::XPropertySet > xProps( aRet.xStream, uno::UNO_QUERY );
524*cdf0e10cSrcweir             xProps->setPropertyValue( aPropName, uno::makeAny( sal_True) );
525*cdf0e10cSrcweir 		}
526*cdf0e10cSrcweir 	}
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir     return aRet;
529*cdf0e10cSrcweir }
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir // -----------------------------------------------------------------------------
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir String SvXMLGraphicHelper::ImplGetGraphicMimeType( const String& rFileName ) const
534*cdf0e10cSrcweir {
535*cdf0e10cSrcweir     struct XMLGraphicMimeTypeMapper
536*cdf0e10cSrcweir     {
537*cdf0e10cSrcweir 	    const char*	pExt;
538*cdf0e10cSrcweir 	    const char*	pMimeType;
539*cdf0e10cSrcweir     };
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir     static XMLGraphicMimeTypeMapper aMapper[] =
542*cdf0e10cSrcweir     {
543*cdf0e10cSrcweir         { "gif", "image/gif" },
544*cdf0e10cSrcweir         { "png", "image/png" },
545*cdf0e10cSrcweir         { "jpg", "image/jpeg" },
546*cdf0e10cSrcweir         { "tif", "image/tiff" },
547*cdf0e10cSrcweir         { "svg", "image/svg+xml" }
548*cdf0e10cSrcweir     };
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir     String aMimeType;
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir     if( ( rFileName.Len() >= 4 ) && ( rFileName.GetChar( rFileName.Len() - 4 ) == '.' ) )
553*cdf0e10cSrcweir     {
554*cdf0e10cSrcweir         const ByteString aExt( rFileName.Copy( rFileName.Len() - 3 ), RTL_TEXTENCODING_ASCII_US );
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir         for( long i = 0, nCount = sizeof( aMapper ) / sizeof( aMapper[ 0 ] ); ( i < nCount ) && !aMimeType.Len(); i++ )
557*cdf0e10cSrcweir             if( aExt == aMapper[ i ].pExt )
558*cdf0e10cSrcweir                 aMimeType = String( aMapper[ i ].pMimeType, RTL_TEXTENCODING_ASCII_US );
559*cdf0e10cSrcweir     }
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir     return aMimeType;
562*cdf0e10cSrcweir }
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir // -----------------------------------------------------------------------------
565*cdf0e10cSrcweir 
566*cdf0e10cSrcweir Graphic SvXMLGraphicHelper::ImplReadGraphic( const ::rtl::OUString& rPictureStorageName,
567*cdf0e10cSrcweir 											 const ::rtl::OUString& rPictureStreamName )
568*cdf0e10cSrcweir {
569*cdf0e10cSrcweir 	Graphic				aGraphic;
570*cdf0e10cSrcweir     SvxGraphicHelperStream_Impl aStream( ImplGetGraphicStream( rPictureStorageName, rPictureStreamName, sal_False ) );
571*cdf0e10cSrcweir     if( aStream.xStream.is() )
572*cdf0e10cSrcweir     {
573*cdf0e10cSrcweir         SvStream* pStream = utl::UcbStreamHelper::CreateStream( aStream.xStream );
574*cdf0e10cSrcweir         GraphicFilter::GetGraphicFilter()->ImportGraphic( aGraphic, String(), *pStream );
575*cdf0e10cSrcweir         delete pStream;
576*cdf0e10cSrcweir     }
577*cdf0e10cSrcweir 
578*cdf0e10cSrcweir 	return aGraphic;
579*cdf0e10cSrcweir }
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir // -----------------------------------------------------------------------------
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir sal_Bool SvXMLGraphicHelper::ImplWriteGraphic( const ::rtl::OUString& rPictureStorageName,
584*cdf0e10cSrcweir 											   const ::rtl::OUString& rPictureStreamName,
585*cdf0e10cSrcweir 											   const ::rtl::OUString& rGraphicId )
586*cdf0e10cSrcweir {
587*cdf0e10cSrcweir 	String			aGraphicId( rGraphicId );
588*cdf0e10cSrcweir 	GraphicObject	aGrfObject( ByteString( aGraphicId, RTL_TEXTENCODING_ASCII_US ) );
589*cdf0e10cSrcweir 	sal_Bool		bRet = sal_False;
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir 	if( aGrfObject.GetType() != GRAPHIC_NONE )
592*cdf0e10cSrcweir 	{
593*cdf0e10cSrcweir         SvxGraphicHelperStream_Impl aStream( ImplGetGraphicStream( rPictureStorageName, rPictureStreamName, sal_False ) );
594*cdf0e10cSrcweir         if( aStream.xStream.is() )
595*cdf0e10cSrcweir         {
596*cdf0e10cSrcweir 			Graphic			aGraphic( (Graphic&) aGrfObject.GetGraphic() );
597*cdf0e10cSrcweir 			const GfxLink	aGfxLink( aGraphic.GetLink() );
598*cdf0e10cSrcweir             const ::rtl::OUString  aMimeType( ImplGetGraphicMimeType( rPictureStreamName ) );
599*cdf0e10cSrcweir             uno::Any        aAny;
600*cdf0e10cSrcweir             uno::Reference < beans::XPropertySet > xProps( aStream.xStream, uno::UNO_QUERY );
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir             // set stream properties (MediaType/Compression)
603*cdf0e10cSrcweir             if( aMimeType.getLength() )
604*cdf0e10cSrcweir             {
605*cdf0e10cSrcweir 	            aAny <<= aMimeType;
606*cdf0e10cSrcweir                 xProps->setPropertyValue( String( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ), aAny );
607*cdf0e10cSrcweir             }
608*cdf0e10cSrcweir 
609*cdf0e10cSrcweir             const sal_Bool bCompressed = ( ( 0 == aMimeType.getLength() ) || ( aMimeType == ::rtl::OUString::createFromAscii( "image/tiff" ) ) );
610*cdf0e10cSrcweir             aAny <<= bCompressed;
611*cdf0e10cSrcweir             xProps->setPropertyValue( String( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) ), aAny );
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir             SvStream* pStream = utl::UcbStreamHelper::CreateStream( aStream.xStream );
614*cdf0e10cSrcweir 			if( aGfxLink.GetDataSize() && aGfxLink.GetData() )
615*cdf0e10cSrcweir                 pStream->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
616*cdf0e10cSrcweir 			else
617*cdf0e10cSrcweir 			{
618*cdf0e10cSrcweir 				if( aGraphic.GetType() == GRAPHIC_BITMAP )
619*cdf0e10cSrcweir 				{
620*cdf0e10cSrcweir 					GraphicFilter*  pFilter = GraphicFilter::GetGraphicFilter();
621*cdf0e10cSrcweir                     String          aFormat;
622*cdf0e10cSrcweir 
623*cdf0e10cSrcweir 					if( aGraphic.IsAnimated() )
624*cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "gif" ) );
625*cdf0e10cSrcweir 					else
626*cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "png" ) );
627*cdf0e10cSrcweir 
628*cdf0e10cSrcweir                     bRet = ( pFilter->ExportGraphic( aGraphic, String(), *pStream,
629*cdf0e10cSrcweir 													 pFilter->GetExportFormatNumberForShortName( aFormat ) ) == 0 );
630*cdf0e10cSrcweir 				}
631*cdf0e10cSrcweir 				else if( aGraphic.GetType() == GRAPHIC_GDIMETAFILE )
632*cdf0e10cSrcweir 				{
633*cdf0e10cSrcweir 					pStream->SetVersion( SOFFICE_FILEFORMAT_8 );
634*cdf0e10cSrcweir 					pStream->SetCompressMode( COMPRESSMODE_ZBITMAP );
635*cdf0e10cSrcweir 
636*cdf0e10cSrcweir 					// SJ: first check if this metafile is just a eps file, then we will store the eps instead of svm
637*cdf0e10cSrcweir 					GDIMetaFile& rMtf( (GDIMetaFile&)aGraphic.GetGDIMetaFile() );
638*cdf0e10cSrcweir 					const MetaCommentAction* pComment = ImplCheckForEPS( rMtf );
639*cdf0e10cSrcweir 					if ( pComment )
640*cdf0e10cSrcweir 					{
641*cdf0e10cSrcweir 						sal_uInt32	nSize = pComment->GetDataSize();
642*cdf0e10cSrcweir 						const sal_uInt8* pData = pComment->GetData();
643*cdf0e10cSrcweir 						if ( nSize && pData )
644*cdf0e10cSrcweir 							pStream->Write( pData, nSize );
645*cdf0e10cSrcweir 
646*cdf0e10cSrcweir 						const MetaEPSAction* pAct = ( (const MetaEPSAction*)rMtf.FirstAction() );
647*cdf0e10cSrcweir 						const GfxLink&		 rLink = pAct->GetLink();
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir 						pStream->Write( rLink.GetData(), rLink.GetDataSize() );
650*cdf0e10cSrcweir 					}
651*cdf0e10cSrcweir 					else
652*cdf0e10cSrcweir 						rMtf.Write( *pStream, GDIMETAFILE_WRITE_REPLACEMENT_RENDERGRAPHIC );
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir 					bRet = ( pStream->GetError() == 0 );
655*cdf0e10cSrcweir 				}
656*cdf0e10cSrcweir 			}
657*cdf0e10cSrcweir             uno::Reference < embed::XTransactedObject > xStorage(
658*cdf0e10cSrcweir                                     aStream.xStorage, uno::UNO_QUERY);
659*cdf0e10cSrcweir             delete pStream;
660*cdf0e10cSrcweir             aStream.xStream->getOutputStream()->closeOutput();
661*cdf0e10cSrcweir             if( xStorage.is() )
662*cdf0e10cSrcweir                 xStorage->commit();
663*cdf0e10cSrcweir 		}
664*cdf0e10cSrcweir 	}
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir 	return bRet;
667*cdf0e10cSrcweir }
668*cdf0e10cSrcweir 
669*cdf0e10cSrcweir // -----------------------------------------------------------------------------
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir void SvXMLGraphicHelper::ImplInsertGraphicURL( const ::rtl::OUString& rURLStr, sal_uInt32 nInsertPos, rtl::OUString& rRequestedFileName )
672*cdf0e10cSrcweir {
673*cdf0e10cSrcweir 	rtl::OUString aURLString( rURLStr );
674*cdf0e10cSrcweir 	::rtl::OUString	aPictureStorageName, aPictureStreamName;
675*cdf0e10cSrcweir 	if( ( maURLSet.find( aURLString ) != maURLSet.end() ) )
676*cdf0e10cSrcweir 	{
677*cdf0e10cSrcweir 		URLPairVector::iterator aIter( maGrfURLs.begin() ), aEnd( maGrfURLs.end() );
678*cdf0e10cSrcweir 		while( aIter != aEnd )
679*cdf0e10cSrcweir 		{
680*cdf0e10cSrcweir 			if( aURLString == (*aIter).first )
681*cdf0e10cSrcweir 			{
682*cdf0e10cSrcweir 				maGrfURLs[ nInsertPos ].second = (*aIter).second;
683*cdf0e10cSrcweir 				aIter = aEnd;
684*cdf0e10cSrcweir 			}
685*cdf0e10cSrcweir 			else
686*cdf0e10cSrcweir 				aIter++;
687*cdf0e10cSrcweir 		}
688*cdf0e10cSrcweir 	}
689*cdf0e10cSrcweir 	else if( ImplGetStreamNames( aURLString, aPictureStorageName, aPictureStreamName ) )
690*cdf0e10cSrcweir 	{
691*cdf0e10cSrcweir 		URLPair& rURLPair = maGrfURLs[ nInsertPos ];
692*cdf0e10cSrcweir 
693*cdf0e10cSrcweir 		if( GRAPHICHELPER_MODE_READ == meCreateMode )
694*cdf0e10cSrcweir 		{
695*cdf0e10cSrcweir 			const GraphicObject aObj( ImplReadGraphic( aPictureStorageName, aPictureStreamName ) );
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir 			if( aObj.GetType() != GRAPHIC_NONE )
698*cdf0e10cSrcweir 			{
699*cdf0e10cSrcweir 				const static ::rtl::OUString aBaseURL( RTL_CONSTASCII_USTRINGPARAM( XML_GRAPHICOBJECT_URL_BASE ) );
700*cdf0e10cSrcweir 
701*cdf0e10cSrcweir 				maGrfObjs.push_back( aObj );
702*cdf0e10cSrcweir 				rURLPair.second = aBaseURL;
703*cdf0e10cSrcweir 				rURLPair.second += String( aObj.GetUniqueID().GetBuffer(), RTL_TEXTENCODING_ASCII_US );
704*cdf0e10cSrcweir 			}
705*cdf0e10cSrcweir 			else
706*cdf0e10cSrcweir 				rURLPair.second = String();
707*cdf0e10cSrcweir 		}
708*cdf0e10cSrcweir 		else
709*cdf0e10cSrcweir 		{
710*cdf0e10cSrcweir 			const String		aGraphicObjectId( aPictureStreamName );
711*cdf0e10cSrcweir             const ByteString    aAsciiObjectID( aGraphicObjectId, RTL_TEXTENCODING_ASCII_US );
712*cdf0e10cSrcweir 			const GraphicObject	aGrfObject( aAsciiObjectID );
713*cdf0e10cSrcweir 			if( aGrfObject.GetType() != GRAPHIC_NONE )
714*cdf0e10cSrcweir 			{
715*cdf0e10cSrcweir 				String			aStreamName( aGraphicObjectId );
716*cdf0e10cSrcweir 				Graphic			aGraphic( (Graphic&) aGrfObject.GetGraphic() );
717*cdf0e10cSrcweir 				const GfxLink	aGfxLink( aGraphic.GetLink() );
718*cdf0e10cSrcweir 				String			aExtension;
719*cdf0e10cSrcweir 
720*cdf0e10cSrcweir 				if( aGfxLink.GetDataSize() )
721*cdf0e10cSrcweir 				{
722*cdf0e10cSrcweir 					switch( aGfxLink.GetType() )
723*cdf0e10cSrcweir 					{
724*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_EPS_BUFFER ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".eps" ) ); break;
725*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_GIF ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".gif" ) ); break;
726*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_JPG ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".jpg" ) ); break;
727*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_PNG ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".png" ) ); break;
728*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_TIF ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".tif" ) ); break;
729*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_WMF ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".wmf" ) ); break;
730*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_MET ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".met" ) ); break;
731*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_PCT ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".pct" ) ); break;
732*cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_SVG ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".svg" ) ); break;
733*cdf0e10cSrcweir 
734*cdf0e10cSrcweir 						default:
735*cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".grf" ) );
736*cdf0e10cSrcweir 						break;
737*cdf0e10cSrcweir 					}
738*cdf0e10cSrcweir 				}
739*cdf0e10cSrcweir 				else
740*cdf0e10cSrcweir 				{
741*cdf0e10cSrcweir 					if( aGrfObject.GetType() == GRAPHIC_BITMAP )
742*cdf0e10cSrcweir 					{
743*cdf0e10cSrcweir 						if( aGrfObject.IsAnimated() )
744*cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".gif" ) );
745*cdf0e10cSrcweir 						else
746*cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".png" ) );
747*cdf0e10cSrcweir 					}
748*cdf0e10cSrcweir 					else if( aGrfObject.GetType() == GRAPHIC_GDIMETAFILE )
749*cdf0e10cSrcweir 					{
750*cdf0e10cSrcweir 						// SJ: first check if this metafile is just a eps file, then we will store the eps instead of svm
751*cdf0e10cSrcweir 						GDIMetaFile& rMtf( (GDIMetaFile&)aGraphic.GetGDIMetaFile() );
752*cdf0e10cSrcweir 						if ( ImplCheckForEPS( rMtf ) )
753*cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".eps" ) );
754*cdf0e10cSrcweir 						else
755*cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".svm" ) );
756*cdf0e10cSrcweir 					}
757*cdf0e10cSrcweir 				}
758*cdf0e10cSrcweir 
759*cdf0e10cSrcweir 				rtl::OUString aURLEntry;
760*cdf0e10cSrcweir 				const String sPictures( RTL_CONSTASCII_USTRINGPARAM( "Pictures/" ) );
761*cdf0e10cSrcweir 
762*cdf0e10cSrcweir 				if ( rRequestedFileName.getLength() )
763*cdf0e10cSrcweir 				{
764*cdf0e10cSrcweir 					aURLEntry = sPictures;
765*cdf0e10cSrcweir 					aURLEntry += rRequestedFileName;
766*cdf0e10cSrcweir 					aURLEntry += aExtension;
767*cdf0e10cSrcweir 
768*cdf0e10cSrcweir 					URLPairVector::iterator aIter( maGrfURLs.begin() ), aEnd( maGrfURLs.end() );
769*cdf0e10cSrcweir 					while( aIter != aEnd )
770*cdf0e10cSrcweir 					{
771*cdf0e10cSrcweir 						if( aURLEntry == (*aIter).second )
772*cdf0e10cSrcweir 							break;
773*cdf0e10cSrcweir 						aIter++;
774*cdf0e10cSrcweir 					}
775*cdf0e10cSrcweir 					if ( aIter == aEnd )
776*cdf0e10cSrcweir 						aStreamName = rRequestedFileName;
777*cdf0e10cSrcweir 				}
778*cdf0e10cSrcweir 
779*cdf0e10cSrcweir 				aStreamName += aExtension;
780*cdf0e10cSrcweir 
781*cdf0e10cSrcweir 				if( mbDirect && aStreamName.Len() )
782*cdf0e10cSrcweir 					ImplWriteGraphic( aPictureStorageName, aStreamName, aGraphicObjectId );
783*cdf0e10cSrcweir 
784*cdf0e10cSrcweir 				rURLPair.second = sPictures;
785*cdf0e10cSrcweir 				rURLPair.second += aStreamName;
786*cdf0e10cSrcweir 			}
787*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
788*cdf0e10cSrcweir             else
789*cdf0e10cSrcweir             {
790*cdf0e10cSrcweir                 ByteString sMessage = "graphic object with ID '";
791*cdf0e10cSrcweir                 sMessage += aAsciiObjectID;
792*cdf0e10cSrcweir                 sMessage += "' has an unknown type";
793*cdf0e10cSrcweir                 OSL_ENSURE( false, sMessage.GetBuffer() );
794*cdf0e10cSrcweir             }
795*cdf0e10cSrcweir #endif
796*cdf0e10cSrcweir 		}
797*cdf0e10cSrcweir 
798*cdf0e10cSrcweir 		maURLSet.insert( aURLString );
799*cdf0e10cSrcweir 	}
800*cdf0e10cSrcweir }
801*cdf0e10cSrcweir 
802*cdf0e10cSrcweir // -----------------------------------------------------------------------------
803*cdf0e10cSrcweir 
804*cdf0e10cSrcweir void SvXMLGraphicHelper::Init( const uno::Reference < embed::XStorage >& rXMLStorage,
805*cdf0e10cSrcweir 							   SvXMLGraphicHelperMode eCreateMode,
806*cdf0e10cSrcweir 							   sal_Bool bDirect )
807*cdf0e10cSrcweir {
808*cdf0e10cSrcweir     mxRootStorage = rXMLStorage;
809*cdf0e10cSrcweir 	meCreateMode = eCreateMode;
810*cdf0e10cSrcweir 	mbDirect = ( ( GRAPHICHELPER_MODE_READ == meCreateMode ) ? bDirect : sal_True );
811*cdf0e10cSrcweir }
812*cdf0e10cSrcweir 
813*cdf0e10cSrcweir // -----------------------------------------------------------------------------
814*cdf0e10cSrcweir 
815*cdf0e10cSrcweir SvXMLGraphicHelper* SvXMLGraphicHelper::Create( const uno::Reference < embed::XStorage >& rXMLStorage,
816*cdf0e10cSrcweir 												SvXMLGraphicHelperMode eCreateMode,
817*cdf0e10cSrcweir 												sal_Bool bDirect )
818*cdf0e10cSrcweir {
819*cdf0e10cSrcweir 	SvXMLGraphicHelper* pThis = new SvXMLGraphicHelper;
820*cdf0e10cSrcweir 
821*cdf0e10cSrcweir 	pThis->acquire();
822*cdf0e10cSrcweir     pThis->Init( rXMLStorage, eCreateMode, bDirect );
823*cdf0e10cSrcweir 
824*cdf0e10cSrcweir 	return pThis;
825*cdf0e10cSrcweir }
826*cdf0e10cSrcweir 
827*cdf0e10cSrcweir // -----------------------------------------------------------------------------
828*cdf0e10cSrcweir 
829*cdf0e10cSrcweir SvXMLGraphicHelper*	SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode eCreateMode )
830*cdf0e10cSrcweir {
831*cdf0e10cSrcweir 	SvXMLGraphicHelper* pThis = new SvXMLGraphicHelper;
832*cdf0e10cSrcweir 
833*cdf0e10cSrcweir 	pThis->acquire();
834*cdf0e10cSrcweir 	pThis->Init( NULL, eCreateMode, sal_False );
835*cdf0e10cSrcweir 
836*cdf0e10cSrcweir 	return pThis;
837*cdf0e10cSrcweir }
838*cdf0e10cSrcweir 
839*cdf0e10cSrcweir // -----------------------------------------------------------------------------
840*cdf0e10cSrcweir 
841*cdf0e10cSrcweir void SvXMLGraphicHelper::Destroy( SvXMLGraphicHelper* pSvXMLGraphicHelper )
842*cdf0e10cSrcweir {
843*cdf0e10cSrcweir 	if( pSvXMLGraphicHelper )
844*cdf0e10cSrcweir 	{
845*cdf0e10cSrcweir 		pSvXMLGraphicHelper->dispose();
846*cdf0e10cSrcweir 		pSvXMLGraphicHelper->release();
847*cdf0e10cSrcweir 	}
848*cdf0e10cSrcweir }
849*cdf0e10cSrcweir 
850*cdf0e10cSrcweir // -----------------------------------------------------------------------------
851*cdf0e10cSrcweir 
852*cdf0e10cSrcweir // XGraphicObjectResolver
853*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicHelper::resolveGraphicObjectURL( const ::rtl::OUString& rURL )
854*cdf0e10cSrcweir 	throw(uno::RuntimeException)
855*cdf0e10cSrcweir {
856*cdf0e10cSrcweir 	::osl::MutexGuard   aGuard( maMutex );
857*cdf0e10cSrcweir 	const sal_Int32     nIndex = maGrfURLs.size();
858*cdf0e10cSrcweir 
859*cdf0e10cSrcweir 	rtl::OUString aURL( rURL );
860*cdf0e10cSrcweir 	rtl::OUString aUserData;
861*cdf0e10cSrcweir 	rtl::OUString aRequestedFileName;
862*cdf0e10cSrcweir 
863*cdf0e10cSrcweir 	sal_Int32 nUser = rURL.indexOf( '?', 0 );
864*cdf0e10cSrcweir 	if ( nUser >= 0 )
865*cdf0e10cSrcweir 	{
866*cdf0e10cSrcweir 		aURL = rtl::OUString( rURL.copy( 0, nUser ) );
867*cdf0e10cSrcweir 		nUser++;
868*cdf0e10cSrcweir 		aUserData = rURL.copy( nUser, rURL.getLength() - nUser );
869*cdf0e10cSrcweir 	}
870*cdf0e10cSrcweir 	if ( aUserData.getLength() )
871*cdf0e10cSrcweir 	{
872*cdf0e10cSrcweir         sal_Int32 nIndex2 = 0;
873*cdf0e10cSrcweir         do
874*cdf0e10cSrcweir         {
875*cdf0e10cSrcweir 			rtl::OUString aToken = aUserData.getToken( 0, ';', nIndex2 );
876*cdf0e10cSrcweir 			sal_Int32 n = aToken.indexOf( '=' );
877*cdf0e10cSrcweir 			if ( ( n > 0 ) && ( ( n + 1 ) < aToken.getLength() ) )
878*cdf0e10cSrcweir 			{
879*cdf0e10cSrcweir 				rtl::OUString aParam( aToken.copy( 0, n ) );
880*cdf0e10cSrcweir 				rtl::OUString aValue( aToken.copy( n + 1, aToken.getLength() - ( n + 1 ) ) );
881*cdf0e10cSrcweir 
882*cdf0e10cSrcweir 				const rtl::OUString sRequestedName( RTL_CONSTASCII_USTRINGPARAM("requestedName") );
883*cdf0e10cSrcweir 				if ( aParam.match( sRequestedName ) )
884*cdf0e10cSrcweir 					aRequestedFileName = aValue;
885*cdf0e10cSrcweir 			}
886*cdf0e10cSrcweir         }
887*cdf0e10cSrcweir         while ( nIndex2 >= 0 );
888*cdf0e10cSrcweir 	}
889*cdf0e10cSrcweir 
890*cdf0e10cSrcweir     maGrfURLs.push_back( ::std::make_pair( aURL, ::rtl::OUString() ) );
891*cdf0e10cSrcweir 	ImplInsertGraphicURL( aURL, nIndex, aRequestedFileName );
892*cdf0e10cSrcweir 
893*cdf0e10cSrcweir     return maGrfURLs[ nIndex ].second;
894*cdf0e10cSrcweir }
895*cdf0e10cSrcweir 
896*cdf0e10cSrcweir // -----------------------------------------------------------------------------
897*cdf0e10cSrcweir 
898*cdf0e10cSrcweir // XBinaryStreamResolver
899*cdf0e10cSrcweir Reference< XInputStream > SAL_CALL SvXMLGraphicHelper::getInputStream( const ::rtl::OUString& rURL )
900*cdf0e10cSrcweir     throw( RuntimeException )
901*cdf0e10cSrcweir {
902*cdf0e10cSrcweir     Reference< XInputStream >   xRet;
903*cdf0e10cSrcweir     ::rtl::OUString                    aPictureStorageName, aGraphicId;
904*cdf0e10cSrcweir 
905*cdf0e10cSrcweir 
906*cdf0e10cSrcweir 	if( ( GRAPHICHELPER_MODE_WRITE == meCreateMode ) &&
907*cdf0e10cSrcweir         ImplGetStreamNames( rURL, aPictureStorageName, aGraphicId ) )
908*cdf0e10cSrcweir     {
909*cdf0e10cSrcweir         SvXMLGraphicInputStream* pInputStream = new SvXMLGraphicInputStream( aGraphicId );
910*cdf0e10cSrcweir 
911*cdf0e10cSrcweir         if( pInputStream->Exists() )
912*cdf0e10cSrcweir             xRet = pInputStream;
913*cdf0e10cSrcweir         else
914*cdf0e10cSrcweir             delete pInputStream;
915*cdf0e10cSrcweir     }
916*cdf0e10cSrcweir 
917*cdf0e10cSrcweir     return xRet;
918*cdf0e10cSrcweir }
919*cdf0e10cSrcweir 
920*cdf0e10cSrcweir // -----------------------------------------------------------------------------
921*cdf0e10cSrcweir 
922*cdf0e10cSrcweir Reference< XOutputStream > SAL_CALL SvXMLGraphicHelper::createOutputStream()
923*cdf0e10cSrcweir     throw( RuntimeException )
924*cdf0e10cSrcweir {
925*cdf0e10cSrcweir     Reference< XOutputStream > xRet;
926*cdf0e10cSrcweir 
927*cdf0e10cSrcweir 	if( GRAPHICHELPER_MODE_READ == meCreateMode )
928*cdf0e10cSrcweir     {
929*cdf0e10cSrcweir         SvXMLGraphicOutputStream* pOutputStream = new SvXMLGraphicOutputStream;
930*cdf0e10cSrcweir 
931*cdf0e10cSrcweir         if( pOutputStream->Exists() )
932*cdf0e10cSrcweir             maGrfStms.push_back( xRet = pOutputStream );
933*cdf0e10cSrcweir         else
934*cdf0e10cSrcweir             delete pOutputStream;
935*cdf0e10cSrcweir     }
936*cdf0e10cSrcweir 
937*cdf0e10cSrcweir     return xRet;
938*cdf0e10cSrcweir }
939*cdf0e10cSrcweir 
940*cdf0e10cSrcweir // -----------------------------------------------------------------------------
941*cdf0e10cSrcweir 
942*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicHelper::resolveOutputStream( const Reference< XOutputStream >& rxBinaryStream )
943*cdf0e10cSrcweir     throw( RuntimeException )
944*cdf0e10cSrcweir {
945*cdf0e10cSrcweir     ::rtl::OUString aRet;
946*cdf0e10cSrcweir 
947*cdf0e10cSrcweir 	if( ( GRAPHICHELPER_MODE_READ == meCreateMode ) && rxBinaryStream.is() )
948*cdf0e10cSrcweir     {
949*cdf0e10cSrcweir         if( ::std::find( maGrfStms.begin(), maGrfStms.end(), rxBinaryStream ) != maGrfStms.end() )
950*cdf0e10cSrcweir         {
951*cdf0e10cSrcweir             SvXMLGraphicOutputStream* pOStm = static_cast< SvXMLGraphicOutputStream* >( rxBinaryStream.get() );
952*cdf0e10cSrcweir 
953*cdf0e10cSrcweir             if( pOStm )
954*cdf0e10cSrcweir             {
955*cdf0e10cSrcweir                 const GraphicObject&    rGrfObj = pOStm->GetGraphicObject();
956*cdf0e10cSrcweir                 const ::rtl::OUString          aId( ::rtl::OUString::createFromAscii( rGrfObj.GetUniqueID().GetBuffer() ) );
957*cdf0e10cSrcweir 
958*cdf0e10cSrcweir                 if( aId.getLength() )
959*cdf0e10cSrcweir                 {
960*cdf0e10cSrcweir                     aRet = ::rtl::OUString::createFromAscii( XML_GRAPHICOBJECT_URL_BASE );
961*cdf0e10cSrcweir                     aRet += aId;
962*cdf0e10cSrcweir                 }
963*cdf0e10cSrcweir             }
964*cdf0e10cSrcweir         }
965*cdf0e10cSrcweir     }
966*cdf0e10cSrcweir 
967*cdf0e10cSrcweir     return aRet;
968*cdf0e10cSrcweir }
969*cdf0e10cSrcweir 
970*cdf0e10cSrcweir 
971*cdf0e10cSrcweir // --------------------------------------------------------------------------------
972*cdf0e10cSrcweir 
973*cdf0e10cSrcweir // for instantiation via service manager
974*cdf0e10cSrcweir namespace svx
975*cdf0e10cSrcweir {
976*cdf0e10cSrcweir 
977*cdf0e10cSrcweir namespace impl
978*cdf0e10cSrcweir {
979*cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper4<
980*cdf0e10cSrcweir         lang::XInitialization,
981*cdf0e10cSrcweir         document::XGraphicObjectResolver,
982*cdf0e10cSrcweir         document::XBinaryStreamResolver,
983*cdf0e10cSrcweir         lang::XServiceInfo >
984*cdf0e10cSrcweir     SvXMLGraphicImportExportHelper_Base;
985*cdf0e10cSrcweir class MutexContainer
986*cdf0e10cSrcweir {
987*cdf0e10cSrcweir public:
988*cdf0e10cSrcweir     virtual ~MutexContainer();
989*cdf0e10cSrcweir 
990*cdf0e10cSrcweir protected:
991*cdf0e10cSrcweir     mutable ::osl::Mutex m_aMutex;
992*cdf0e10cSrcweir };
993*cdf0e10cSrcweir MutexContainer::~MutexContainer()
994*cdf0e10cSrcweir {}
995*cdf0e10cSrcweir } // namespace impl
996*cdf0e10cSrcweir 
997*cdf0e10cSrcweir class SvXMLGraphicImportExportHelper :
998*cdf0e10cSrcweir     public impl::MutexContainer,
999*cdf0e10cSrcweir     public impl::SvXMLGraphicImportExportHelper_Base
1000*cdf0e10cSrcweir {
1001*cdf0e10cSrcweir public:
1002*cdf0e10cSrcweir     SvXMLGraphicImportExportHelper( SvXMLGraphicHelperMode eMode );
1003*cdf0e10cSrcweir 
1004*cdf0e10cSrcweir protected:
1005*cdf0e10cSrcweir     // is called from WeakComponentImplHelper when XComponent::dispose() was
1006*cdf0e10cSrcweir     // called from outside
1007*cdf0e10cSrcweir     virtual void SAL_CALL disposing();
1008*cdf0e10cSrcweir 
1009*cdf0e10cSrcweir     // ____ XInitialization ____
1010*cdf0e10cSrcweir     // one argument is allowed, which is the XStorage
1011*cdf0e10cSrcweir     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
1012*cdf0e10cSrcweir         throw (Exception,
1013*cdf0e10cSrcweir                RuntimeException);
1014*cdf0e10cSrcweir 
1015*cdf0e10cSrcweir     // ____ XGraphicObjectResolver ____
1016*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL resolveGraphicObjectURL( const ::rtl::OUString& aURL )
1017*cdf0e10cSrcweir         throw (RuntimeException);
1018*cdf0e10cSrcweir 
1019*cdf0e10cSrcweir     // ____ XBinaryStreamResolver ____
1020*cdf0e10cSrcweir     virtual Reference< io::XInputStream > SAL_CALL getInputStream( const ::rtl::OUString& aURL )
1021*cdf0e10cSrcweir         throw (RuntimeException);
1022*cdf0e10cSrcweir     virtual Reference< io::XOutputStream > SAL_CALL createOutputStream()
1023*cdf0e10cSrcweir         throw (RuntimeException);
1024*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL resolveOutputStream( const Reference< io::XOutputStream >& aBinaryStream )
1025*cdf0e10cSrcweir         throw (RuntimeException);
1026*cdf0e10cSrcweir 
1027*cdf0e10cSrcweir     // ____ XServiceInfo ____
1028*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName()
1029*cdf0e10cSrcweir         throw (RuntimeException);
1030*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
1031*cdf0e10cSrcweir         throw (RuntimeException);
1032*cdf0e10cSrcweir     virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames()
1033*cdf0e10cSrcweir         throw (RuntimeException);
1034*cdf0e10cSrcweir 
1035*cdf0e10cSrcweir private:
1036*cdf0e10cSrcweir     SvXMLGraphicHelperMode              m_eGraphicHelperMode;
1037*cdf0e10cSrcweir     Reference< XGraphicObjectResolver > m_xGraphicObjectResolver;
1038*cdf0e10cSrcweir     Reference< XBinaryStreamResolver >  m_xBinaryStreamResolver;
1039*cdf0e10cSrcweir };
1040*cdf0e10cSrcweir 
1041*cdf0e10cSrcweir SvXMLGraphicImportExportHelper::SvXMLGraphicImportExportHelper( SvXMLGraphicHelperMode eMode ) :
1042*cdf0e10cSrcweir         impl::SvXMLGraphicImportExportHelper_Base( m_aMutex ),
1043*cdf0e10cSrcweir         m_eGraphicHelperMode( eMode )
1044*cdf0e10cSrcweir {}
1045*cdf0e10cSrcweir 
1046*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicImportExportHelper::disposing()
1047*cdf0e10cSrcweir {
1048*cdf0e10cSrcweir     Reference< XComponent > xComp( m_xGraphicObjectResolver, UNO_QUERY );
1049*cdf0e10cSrcweir     OSL_ASSERT( xComp.is());
1050*cdf0e10cSrcweir     if( xComp.is())
1051*cdf0e10cSrcweir         xComp->dispose();
1052*cdf0e10cSrcweir     // m_xBinaryStreamResolver is a reference to the same object => don't call
1053*cdf0e10cSrcweir     // dispose() again
1054*cdf0e10cSrcweir }
1055*cdf0e10cSrcweir 
1056*cdf0e10cSrcweir // ____ XInitialization ____
1057*cdf0e10cSrcweir void SAL_CALL SvXMLGraphicImportExportHelper::initialize(
1058*cdf0e10cSrcweir     const Sequence< Any >& aArguments )
1059*cdf0e10cSrcweir     throw (Exception, RuntimeException)
1060*cdf0e10cSrcweir {
1061*cdf0e10cSrcweir     Reference< embed::XStorage > xStorage;
1062*cdf0e10cSrcweir     if( aArguments.getLength() > 0 )
1063*cdf0e10cSrcweir         aArguments[0] >>= xStorage;
1064*cdf0e10cSrcweir 
1065*cdf0e10cSrcweir     SvXMLGraphicHelper * pHelper( SvXMLGraphicHelper::Create( xStorage, m_eGraphicHelperMode ));
1066*cdf0e10cSrcweir     m_xGraphicObjectResolver.set( pHelper );
1067*cdf0e10cSrcweir     m_xBinaryStreamResolver.set( pHelper );
1068*cdf0e10cSrcweir     // SvXMLGraphicHelper::Create calls acquire.  Since we have two references
1069*cdf0e10cSrcweir     // now it is safe (and necessary) to undo this acquire
1070*cdf0e10cSrcweir     pHelper->release();
1071*cdf0e10cSrcweir }
1072*cdf0e10cSrcweir 
1073*cdf0e10cSrcweir // ____ XGraphicObjectResolver ____
1074*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportExportHelper::resolveGraphicObjectURL( const ::rtl::OUString& aURL )
1075*cdf0e10cSrcweir     throw (uno::RuntimeException)
1076*cdf0e10cSrcweir {
1077*cdf0e10cSrcweir     return m_xGraphicObjectResolver->resolveGraphicObjectURL( aURL );
1078*cdf0e10cSrcweir }
1079*cdf0e10cSrcweir 
1080*cdf0e10cSrcweir 
1081*cdf0e10cSrcweir // ____ XBinaryStreamResolver ____
1082*cdf0e10cSrcweir Reference< io::XInputStream > SAL_CALL SvXMLGraphicImportExportHelper::getInputStream( const ::rtl::OUString& aURL )
1083*cdf0e10cSrcweir     throw (uno::RuntimeException)
1084*cdf0e10cSrcweir {
1085*cdf0e10cSrcweir     return m_xBinaryStreamResolver->getInputStream( aURL );
1086*cdf0e10cSrcweir }
1087*cdf0e10cSrcweir Reference< io::XOutputStream > SAL_CALL SvXMLGraphicImportExportHelper::createOutputStream()
1088*cdf0e10cSrcweir     throw (uno::RuntimeException)
1089*cdf0e10cSrcweir {
1090*cdf0e10cSrcweir     return m_xBinaryStreamResolver->createOutputStream();
1091*cdf0e10cSrcweir }
1092*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportExportHelper::resolveOutputStream( const Reference< io::XOutputStream >& aBinaryStream )
1093*cdf0e10cSrcweir     throw (uno::RuntimeException)
1094*cdf0e10cSrcweir {
1095*cdf0e10cSrcweir     return m_xBinaryStreamResolver->resolveOutputStream( aBinaryStream );
1096*cdf0e10cSrcweir }
1097*cdf0e10cSrcweir 
1098*cdf0e10cSrcweir // ____ XServiceInfo ____
1099*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportExportHelper::getImplementationName()
1100*cdf0e10cSrcweir     throw (uno::RuntimeException)
1101*cdf0e10cSrcweir {
1102*cdf0e10cSrcweir     if( m_eGraphicHelperMode == GRAPHICHELPER_MODE_READ )
1103*cdf0e10cSrcweir         return SvXMLGraphicImportHelper_getImplementationName();
1104*cdf0e10cSrcweir     return SvXMLGraphicExportHelper_getImplementationName();
1105*cdf0e10cSrcweir }
1106*cdf0e10cSrcweir ::sal_Bool SAL_CALL SvXMLGraphicImportExportHelper::supportsService( const ::rtl::OUString& ServiceName )
1107*cdf0e10cSrcweir     throw (uno::RuntimeException)
1108*cdf0e10cSrcweir {
1109*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames());
1110*cdf0e10cSrcweir     const ::rtl::OUString * pBegin = aServiceNames.getConstArray();
1111*cdf0e10cSrcweir     const ::rtl::OUString * pEnd = pBegin + aServiceNames.getLength();
1112*cdf0e10cSrcweir     return (::std::find( pBegin, pEnd, ServiceName ) != pEnd);
1113*cdf0e10cSrcweir }
1114*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SvXMLGraphicImportExportHelper::getSupportedServiceNames()
1115*cdf0e10cSrcweir     throw (uno::RuntimeException)
1116*cdf0e10cSrcweir {
1117*cdf0e10cSrcweir     if( m_eGraphicHelperMode == GRAPHICHELPER_MODE_READ )
1118*cdf0e10cSrcweir         return SvXMLGraphicImportHelper_getSupportedServiceNames();
1119*cdf0e10cSrcweir     return SvXMLGraphicExportHelper_getSupportedServiceNames();
1120*cdf0e10cSrcweir }
1121*cdf0e10cSrcweir 
1122*cdf0e10cSrcweir // import
1123*cdf0e10cSrcweir Reference< XInterface > SAL_CALL SvXMLGraphicImportHelper_createInstance(const Reference< XMultiServiceFactory > & /* rSMgr */ )
1124*cdf0e10cSrcweir     throw( Exception )
1125*cdf0e10cSrcweir {
1126*cdf0e10cSrcweir     return static_cast< XWeak* >( new SvXMLGraphicImportExportHelper( GRAPHICHELPER_MODE_READ ));
1127*cdf0e10cSrcweir }
1128*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportHelper_getImplementationName()
1129*cdf0e10cSrcweir     throw()
1130*cdf0e10cSrcweir {
1131*cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.GraphicImportHelper" ));
1132*cdf0e10cSrcweir }
1133*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SvXMLGraphicImportHelper_getSupportedServiceNames()
1134*cdf0e10cSrcweir     throw()
1135*cdf0e10cSrcweir {
1136*cdf0e10cSrcweir     // XGraphicObjectResolver and XBinaryStreamResolver are not part of any service
1137*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aSupportedServiceNames( 2 );
1138*cdf0e10cSrcweir     aSupportedServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.GraphicObjectResolver" ) );
1139*cdf0e10cSrcweir     aSupportedServiceNames[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.BinaryStreamResolver" ) );
1140*cdf0e10cSrcweir     return aSupportedServiceNames;
1141*cdf0e10cSrcweir }
1142*cdf0e10cSrcweir 
1143*cdf0e10cSrcweir // export
1144*cdf0e10cSrcweir Reference< XInterface > SAL_CALL SvXMLGraphicExportHelper_createInstance(const Reference< XMultiServiceFactory > & /* rSMgr */ )
1145*cdf0e10cSrcweir     throw( Exception )
1146*cdf0e10cSrcweir {
1147*cdf0e10cSrcweir     return static_cast< XWeak* >( new SvXMLGraphicImportExportHelper( GRAPHICHELPER_MODE_WRITE ));
1148*cdf0e10cSrcweir }
1149*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicExportHelper_getImplementationName()
1150*cdf0e10cSrcweir     throw()
1151*cdf0e10cSrcweir {
1152*cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.GraphicExportHelper" ));
1153*cdf0e10cSrcweir }
1154*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SvXMLGraphicExportHelper_getSupportedServiceNames()
1155*cdf0e10cSrcweir     throw()
1156*cdf0e10cSrcweir {
1157*cdf0e10cSrcweir     // XGraphicObjectResolver and XBinaryStreamResolver are not part of any service
1158*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aSupportedServiceNames( 2 );
1159*cdf0e10cSrcweir     aSupportedServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.GraphicObjectResolver" ) );
1160*cdf0e10cSrcweir     aSupportedServiceNames[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.BinaryStreamResolver" ) );
1161*cdf0e10cSrcweir     return aSupportedServiceNames;
1162*cdf0e10cSrcweir }
1163*cdf0e10cSrcweir 
1164*cdf0e10cSrcweir } // namespace svx
1165