Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <tools/debug.hxx>
30 : : #include <tools/diagnose_ex.h>
31 : : #include <com/sun/star/xml/sax/InputSource.hpp>
32 : : #include <com/sun/star/xml/sax/XParser.hpp>
33 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
35 : : #include <com/sun/star/lang/XComponent.hpp>
36 : : #include <com/sun/star/io/XActiveDataSource.hpp>
37 : : #include <com/sun/star/xml/sax/SAXParseException.hpp>
38 : : #include <com/sun/star/io/XOutputStream.hpp>
39 : : #include <com/sun/star/document/XFilter.hpp>
40 : : #include <com/sun/star/document/XExporter.hpp>
41 : : #include <com/sun/star/document/XImporter.hpp>
42 : : #include <comphelper/processfactory.hxx>
43 : : #include <unotools/streamwrap.hxx>
44 : : #include <sot/storage.hxx>
45 : : #include <svx/svdmodel.hxx>
46 : : #include <svx/xmleohlp.hxx>
47 : : #include <svx/xmlgrhlp.hxx>
48 : :
49 : : #include <svx/unomodel.hxx>
50 : :
51 : : using ::rtl::OUString;
52 : : using namespace ::com::sun::star;
53 : : using namespace ::com::sun::star::uno;
54 : :
55 : 0 : sal_Bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent )
56 : : {
57 : 0 : return SvxDrawingLayerExport( pModel, xOut, xComponent, "com.sun.star.comp.DrawingLayer.XMLExporter" );
58 : : }
59 : :
60 : 0 : sal_Bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent, const char* pExportService )
61 : : {
62 : 0 : sal_Bool bDocRet = xOut.is();
63 : :
64 : 0 : Reference< document::XGraphicObjectResolver > xGraphicResolver;
65 : 0 : SvXMLGraphicHelper *pGraphicHelper = 0;
66 : :
67 : 0 : Reference< document::XEmbeddedObjectResolver > xObjectResolver;
68 : 0 : SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
69 : :
70 : 0 : Reference< lang::XComponent > xSourceDoc( xComponent );
71 : : try
72 : : {
73 [ # # ]: 0 : if( !xSourceDoc.is() )
74 : : {
75 [ # # ][ # # ]: 0 : xSourceDoc = new SvxUnoDrawingModel( pModel );
76 [ # # ][ # # ]: 0 : pModel->setUnoModel( Reference< XInterface >::query( xSourceDoc ) );
77 : : }
78 : :
79 [ # # ]: 0 : uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
80 [ # # ]: 0 : if( !xServiceFactory.is() )
81 : : {
82 : : OSL_FAIL( "got no service manager" );
83 : 0 : bDocRet = sal_False;
84 : : }
85 : :
86 [ # # ]: 0 : if( bDocRet )
87 : : {
88 [ # # ][ # # ]: 0 : uno::Reference< uno::XInterface > xWriter( xServiceFactory->createInstance( OUString( "com.sun.star.xml.sax.Writer" ) ) );
89 [ # # ]: 0 : if( !xWriter.is() )
90 : : {
91 : : OSL_FAIL( "com.sun.star.xml.sax.Writer service missing" );
92 : 0 : bDocRet = sal_False;
93 : : }
94 : :
95 : 0 : ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
96 [ # # ]: 0 : if( pPersist )
97 : : {
98 [ # # ]: 0 : pObjectHelper = SvXMLEmbeddedObjectHelper::Create( *pPersist, EMBEDDEDOBJECTHELPER_MODE_WRITE );
99 [ # # ][ # # ]: 0 : xObjectResolver = pObjectHelper;
100 : : }
101 : :
102 [ # # ]: 0 : pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_WRITE );
103 [ # # ][ # # ]: 0 : xGraphicResolver = pGraphicHelper;
104 : :
105 [ # # ]: 0 : if( bDocRet )
106 : : {
107 [ # # ]: 0 : uno::Reference<xml::sax::XDocumentHandler> xHandler( xWriter, uno::UNO_QUERY );
108 : :
109 : : // doc export
110 [ # # ]: 0 : uno::Reference< io::XActiveDataSource > xDocSrc( xWriter, uno::UNO_QUERY );
111 [ # # ][ # # ]: 0 : xDocSrc->setOutputStream( xOut );
112 : :
113 [ # # ][ # # ]: 0 : uno::Sequence< uno::Any > aArgs( xObjectResolver.is() ? 3 : 2 );
114 [ # # ][ # # ]: 0 : aArgs[0] <<= xHandler;
115 [ # # ][ # # ]: 0 : aArgs[1] <<= xGraphicResolver;
116 [ # # ]: 0 : if( xObjectResolver.is() )
117 [ # # ][ # # ]: 0 : aArgs[2] <<= xObjectResolver;
118 : :
119 [ # # ][ # # ]: 0 : uno::Reference< document::XFilter > xFilter( xServiceFactory->createInstanceWithArguments( OUString::createFromAscii( pExportService ), aArgs ), uno::UNO_QUERY );
[ # # ]
120 [ # # ]: 0 : if( !xFilter.is() )
121 : : {
122 : : OSL_FAIL( "com.sun.star.comp.Draw.XMLExporter service missing" );
123 : 0 : bDocRet = sal_False;
124 : : }
125 : :
126 [ # # ]: 0 : if( bDocRet )
127 : : {
128 [ # # ]: 0 : uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
129 [ # # ]: 0 : if( xExporter.is() )
130 : : {
131 [ # # ][ # # ]: 0 : xExporter->setSourceDocument( xSourceDoc );
132 : :
133 [ # # ]: 0 : uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
134 [ # # ][ # # ]: 0 : bDocRet = xFilter->filter( aDescriptor );
[ # # ]
135 : 0 : }
136 [ # # ]: 0 : }
137 : 0 : }
138 [ # # ]: 0 : }
139 : : }
140 [ # # ]: 0 : catch(uno::Exception const&)
141 : : {
142 : : DBG_UNHANDLED_EXCEPTION();
143 : 0 : bDocRet = sal_False;
144 : : }
145 : :
146 [ # # ]: 0 : if( pGraphicHelper )
147 [ # # ]: 0 : SvXMLGraphicHelper::Destroy( pGraphicHelper );
148 [ # # ]: 0 : xGraphicResolver = 0;
149 : :
150 [ # # ]: 0 : if( pObjectHelper )
151 [ # # ]: 0 : SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
152 [ # # ]: 0 : xObjectResolver = 0;
153 : :
154 : 0 : return bDocRet;
155 : : }
156 : :
157 : 0 : sal_Bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
158 : : {
159 : 0 : Reference< lang::XComponent > xComponent;
160 [ # # ]: 0 : return SvxDrawingLayerExport( pModel, xOut, xComponent );
161 : : }
162 : :
163 : : //-////////////////////////////////////////////////////////////////////
164 : :
165 : 0 : sal_Bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
166 : : {
167 : 0 : return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
168 : : }
169 : :
170 : 0 : sal_Bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService )
171 : : {
172 : 0 : sal_uInt32 nRet = 0;
173 : :
174 : 0 : Reference< document::XGraphicObjectResolver > xGraphicResolver;
175 : 0 : SvXMLGraphicHelper *pGraphicHelper = 0;
176 : :
177 : 0 : Reference< document::XEmbeddedObjectResolver > xObjectResolver;
178 : 0 : SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
179 : :
180 : 0 : Reference< lang::XComponent > xTargetDocument( xComponent );
181 [ # # ]: 0 : if( !xTargetDocument.is() )
182 : : {
183 [ # # ][ # # ]: 0 : xTargetDocument = new SvxUnoDrawingModel( pModel );
184 [ # # ][ # # ]: 0 : pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
185 : : }
186 : :
187 [ # # ]: 0 : Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
188 : :
189 : : try
190 : : {
191 : : // Get service factory
192 [ # # ]: 0 : Reference< lang::XMultiServiceFactory > xServiceFactory = comphelper::getProcessServiceFactory();
193 : : DBG_ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
194 : :
195 [ # # ]: 0 : if( !xServiceFactory.is() )
196 : 0 : nRet = 1;
197 : :
198 [ # # ]: 0 : if( 0 == nRet )
199 : : {
200 [ # # ]: 0 : if ( xTargetModel.is() )
201 [ # # ][ # # ]: 0 : xTargetModel->lockControllers();
202 : :
203 : : // -------------------------------------
204 : :
205 [ # # ]: 0 : pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_READ );
206 [ # # ][ # # ]: 0 : xGraphicResolver = pGraphicHelper;
207 : :
208 : 0 : ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
209 [ # # ]: 0 : if( pPersist )
210 : : {
211 : : pObjectHelper = SvXMLEmbeddedObjectHelper::Create(
212 : : *pPersist,
213 [ # # ]: 0 : EMBEDDEDOBJECTHELPER_MODE_READ );
214 [ # # ][ # # ]: 0 : xObjectResolver = pObjectHelper;
215 : : }
216 : : }
217 : :
218 : : // -------------------------------------
219 : :
220 [ # # ]: 0 : if( 0 == nRet )
221 : : {
222 : :
223 : : // parse
224 : : // prepare ParserInputSrouce
225 [ # # ]: 0 : xml::sax::InputSource aParserInput;
226 [ # # ]: 0 : aParserInput.aInputStream = xInputStream;
227 : :
228 : : // get parser
229 [ # # ][ # # ]: 0 : Reference< xml::sax::XParser > xParser( xServiceFactory->createInstance( OUString("com.sun.star.xml.sax.Parser") ), UNO_QUERY );
[ # # ]
230 : : DBG_ASSERT( xParser.is(), "Can't create parser" );
231 : :
232 : : // prepare filter arguments
233 [ # # ]: 0 : Sequence<Any> aFilterArgs( 2 );
234 [ # # ]: 0 : Any *pArgs = aFilterArgs.getArray();
235 [ # # ]: 0 : *pArgs++ <<= xGraphicResolver;
236 [ # # ]: 0 : *pArgs++ <<= xObjectResolver;
237 : :
238 : : // get filter
239 [ # # ][ # # ]: 0 : Reference< xml::sax::XDocumentHandler > xFilter( xServiceFactory->createInstanceWithArguments( OUString::createFromAscii( pImportService ), aFilterArgs), UNO_QUERY );
[ # # ]
240 : : DBG_ASSERT( xFilter.is(), "Can't instantiate filter component." );
241 : :
242 : 0 : nRet = 1;
243 [ # # ][ # # ]: 0 : if( xParser.is() && xFilter.is() )
[ # # ]
244 : : {
245 : : // connect parser and filter
246 [ # # ][ # # ]: 0 : xParser->setDocumentHandler( xFilter );
247 : :
248 : : // connect model and filter
249 [ # # ]: 0 : uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
250 [ # # ][ # # ]: 0 : xImporter->setTargetDocument( xTargetDocument );
251 : :
252 : : // finally, parser the stream
253 [ # # ][ # # ]: 0 : xParser->parseStream( aParserInput );
254 : :
255 : 0 : nRet = 0;
256 [ # # ][ # # ]: 0 : }
257 [ # # ]: 0 : }
258 : : }
259 [ # # ]: 0 : catch( uno::Exception& )
260 : : {
261 : : DBG_UNHANDLED_EXCEPTION();
262 : : }
263 : :
264 [ # # ]: 0 : if( pGraphicHelper )
265 [ # # ]: 0 : SvXMLGraphicHelper::Destroy( pGraphicHelper );
266 [ # # ]: 0 : xGraphicResolver = 0;
267 : :
268 [ # # ]: 0 : if( pObjectHelper )
269 [ # # ]: 0 : SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
270 [ # # ]: 0 : xObjectResolver = 0;
271 : :
272 [ # # ]: 0 : if ( xTargetModel.is() )
273 [ # # ][ # # ]: 0 : xTargetModel->unlockControllers();
274 : :
275 : 0 : return nRet == 0;
276 : : }
277 : :
278 : 0 : sal_Bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
279 : : {
280 : 0 : Reference< lang::XComponent > xComponent;
281 [ # # ]: 0 : return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
282 : : }
283 : :
284 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|