1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlexport.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <tools/debug.hxx>
34 #include <com/sun/star/xml/sax/InputSource.hpp>
35 #include <com/sun/star/xml/sax/XParser.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
38 #include <com/sun/star/lang/XComponent.hpp>
39 #include <com/sun/star/io/XActiveDataSource.hpp>
40 #include <com/sun/star/xml/sax/SAXParseException.hpp>
41 #include <com/sun/star/io/XOutputStream.hpp>
42 #include <com/sun/star/document/XFilter.hpp>
43 #include <com/sun/star/document/XExporter.hpp>
44 #include <com/sun/star/document/XImporter.hpp>
45 #include <comphelper/processfactory.hxx>
46 #include <unotools/streamwrap.hxx>
47 #include <sot/storage.hxx>
48 #include <svx/svdmodel.hxx>
49 #include <xmleohlp.hxx>
50 #include <xmlgrhlp.hxx>
52 #include <svx/unomodel.hxx>
54 using ::rtl::OUString
;
55 using namespace ::com::sun::star
;
56 using namespace ::com::sun::star::uno
;
58 sal_Bool
SvxDrawingLayerExport( SdrModel
* pModel
, uno::Reference
<io::XOutputStream
> xOut
, Reference
< lang::XComponent
> xComponent
)
60 return SvxDrawingLayerExport( pModel
, xOut
, xComponent
, "com.sun.star.comp.DrawingLayer.XMLExporter" );
63 sal_Bool
SvxDrawingLayerExport( SdrModel
* pModel
, uno::Reference
<io::XOutputStream
> xOut
, Reference
< lang::XComponent
> xComponent
, const char* pExportService
)
65 sal_Bool bDocRet
= xOut
.is();
67 Reference
< document::XGraphicObjectResolver
> xGraphicResolver
;
68 SvXMLGraphicHelper
*pGraphicHelper
= 0;
70 Reference
< document::XEmbeddedObjectResolver
> xObjectResolver
;
71 SvXMLEmbeddedObjectHelper
*pObjectHelper
= 0;
75 if( !xComponent
.is() )
77 xComponent
= new SvxUnoDrawingModel( pModel
);
78 pModel
->setUnoModel( Reference
< XInterface
>::query( xComponent
) );
81 uno::Reference
< lang::XMultiServiceFactory
> xServiceFactory( ::comphelper::getProcessServiceFactory() );
82 if( !xServiceFactory
.is() )
84 DBG_ERROR( "got no service manager" );
90 uno::Reference
< uno::XInterface
> xWriter( xServiceFactory
->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ) );
93 DBG_ERROR( "com.sun.star.xml.sax.Writer service missing" );
97 ::comphelper::IEmbeddedHelper
*pPersist
= pModel
->GetPersist();
100 pObjectHelper
= SvXMLEmbeddedObjectHelper::Create( *pPersist
, EMBEDDEDOBJECTHELPER_MODE_WRITE
);
101 xObjectResolver
= pObjectHelper
;
104 pGraphicHelper
= SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_WRITE
);
105 xGraphicResolver
= pGraphicHelper
;
109 uno::Reference
<xml::sax::XDocumentHandler
> xHandler( xWriter
, uno::UNO_QUERY
);
112 uno::Reference
< io::XActiveDataSource
> xDocSrc( xWriter
, uno::UNO_QUERY
);
113 xDocSrc
->setOutputStream( xOut
);
115 uno::Sequence
< uno::Any
> aArgs( xObjectResolver
.is() ? 3 : 2 );
116 aArgs
[0] <<= xHandler
;
117 aArgs
[1] <<= xGraphicResolver
;
118 if( xObjectResolver
.is() )
119 aArgs
[2] <<= xObjectResolver
;
121 uno::Reference
< document::XFilter
> xFilter( xServiceFactory
->createInstanceWithArguments( OUString::createFromAscii( pExportService
), aArgs
), uno::UNO_QUERY
);
124 DBG_ERROR( "com.sun.star.comp.Draw.XMLExporter service missing" );
130 uno::Reference
< document::XExporter
> xExporter( xFilter
, uno::UNO_QUERY
);
133 xExporter
->setSourceDocument( xComponent
);
135 uno::Sequence
< beans::PropertyValue
> aDescriptor( 0 );
136 bDocRet
= xFilter
->filter( aDescriptor
);
142 catch(uno::Exception e
)
144 #if OSL_DEBUG_LEVEL > 1
145 ByteString
aError( "uno Exception caught while exporting:\n" );
146 aError
+= ByteString( String( e
.Message
), RTL_TEXTENCODING_ASCII_US
);
147 DBG_ERROR( aError
.GetBuffer() );
153 SvXMLGraphicHelper::Destroy( pGraphicHelper
);
154 xGraphicResolver
= 0;
157 SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper
);
163 sal_Bool
SvxDrawingLayerExport( SdrModel
* pModel
, uno::Reference
<io::XOutputStream
> xOut
)
165 Reference
< lang::XComponent
> xComponent
;
166 return SvxDrawingLayerExport( pModel
, xOut
, xComponent
);
169 //-////////////////////////////////////////////////////////////////////
171 sal_Bool
SvxDrawingLayerImport( SdrModel
* pModel
, uno::Reference
<io::XInputStream
> xInputStream
, Reference
< lang::XComponent
> xComponent
)
173 return SvxDrawingLayerImport( pModel
, xInputStream
, xComponent
, "com.sun.star.comp.Draw.XMLOasisImporter" );
176 sal_Bool
SvxDrawingLayerImport( SdrModel
* pModel
, uno::Reference
<io::XInputStream
> xInputStream
, Reference
< lang::XComponent
> xComponent
, const char* pImportService
)
180 Reference
< document::XGraphicObjectResolver
> xGraphicResolver
;
181 SvXMLGraphicHelper
*pGraphicHelper
= 0;
183 Reference
< document::XEmbeddedObjectResolver
> xObjectResolver
;
184 SvXMLEmbeddedObjectHelper
*pObjectHelper
= 0;
186 if( !xComponent
.is() )
188 xComponent
= new SvxUnoDrawingModel( pModel
);
189 pModel
->setUnoModel( Reference
< XInterface
>::query( xComponent
) );
192 Reference
< frame::XModel
> xModel( xComponent
, UNO_QUERY
);
196 // Get service factory
197 Reference
< lang::XMultiServiceFactory
> xServiceFactory
= comphelper::getProcessServiceFactory();
198 DBG_ASSERT( xServiceFactory
.is(), "XMLReader::Read: got no service manager" );
200 if( !xServiceFactory
.is() )
205 xModel
->lockControllers();
207 // -------------------------------------
209 pGraphicHelper
= SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_READ
);
210 xGraphicResolver
= pGraphicHelper
;
212 ::comphelper::IEmbeddedHelper
*pPersist
= pModel
->GetPersist();
215 pObjectHelper
= SvXMLEmbeddedObjectHelper::Create(
217 EMBEDDEDOBJECTHELPER_MODE_READ
);
218 xObjectResolver
= pObjectHelper
;
222 // -------------------------------------
228 // prepare ParserInputSrouce
229 xml::sax::InputSource aParserInput
;
230 aParserInput
.aInputStream
= xInputStream
;
233 Reference
< xml::sax::XParser
> xParser( xServiceFactory
->createInstance( OUString::createFromAscii("com.sun.star.xml.sax.Parser") ), UNO_QUERY
);
234 DBG_ASSERT( xParser
.is(), "Can't create parser" );
236 // prepare filter arguments
237 Sequence
<Any
> aFilterArgs( 2 );
238 Any
*pArgs
= aFilterArgs
.getArray();
239 *pArgs
++ <<= xGraphicResolver
;
240 *pArgs
++ <<= xObjectResolver
;
243 Reference
< xml::sax::XDocumentHandler
> xFilter( xServiceFactory
->createInstanceWithArguments( OUString::createFromAscii( pImportService
), aFilterArgs
), UNO_QUERY
);
244 DBG_ASSERT( xFilter
.is(), "Can't instantiate filter component." );
247 if( xParser
.is() && xFilter
.is() )
249 // connect parser and filter
250 xParser
->setDocumentHandler( xFilter
);
252 // connect model and filter
253 uno::Reference
< document::XImporter
> xImporter( xFilter
, UNO_QUERY
);
254 xImporter
->setTargetDocument( xComponent
);
256 // finally, parser the stream
257 xParser
->parseStream( aParserInput
);
263 catch( xml::sax::SAXParseException
& r
)
265 #if OSL_DEBUG_LEVEL > 1
266 ByteString
aError( "SAX parse exception catched while importing:\n" );
267 aError
+= ByteString( String( r
.Message
), RTL_TEXTENCODING_ASCII_US
);
268 DBG_ERROR( aError
.GetBuffer() );
270 (void) r
; // avoid warnings
273 catch( xml::sax::SAXException
& r
)
275 #if OSL_DEBUG_LEVEL > 1
276 ByteString
aError( "SAX exception catched while importing:\n" );
277 aError
+= ByteString( String( r
.Message
), RTL_TEXTENCODING_ASCII_US
);
278 DBG_ERROR( aError
.GetBuffer() );
280 (void) r
; // avoid warnings
283 catch( io::IOException
& r
)
285 #if OSL_DEBUG_LEVEL > 1
286 ByteString
aError( "IO exception catched while importing:\n" );
287 aError
+= ByteString( String( r
.Message
), RTL_TEXTENCODING_ASCII_US
);
288 DBG_ERROR( aError
.GetBuffer() );
290 (void) r
; // avoid warnings
293 catch( uno::Exception
& r
)
295 #if OSL_DEBUG_LEVEL > 1
296 ByteString
aError( "uno exception catched while importing:\n" );
297 aError
+= ByteString( String( r
.Message
), RTL_TEXTENCODING_ASCII_US
);
298 DBG_ERROR( aError
.GetBuffer() );
300 (void) r
; // avoid warnings
305 SvXMLGraphicHelper::Destroy( pGraphicHelper
);
306 xGraphicResolver
= 0;
309 SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper
);
313 xModel
->unlockControllers();
318 sal_Bool
SvxDrawingLayerImport( SdrModel
* pModel
, uno::Reference
<io::XInputStream
> xInputStream
)
320 Reference
< lang::XComponent
> xComponent
;
321 return SvxDrawingLayerImport( pModel
, xInputStream
, xComponent
);