LibreOffice Module svx (master)  1
xmlexport.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <tools/debug.hxx>
21 #include <tools/diagnose_ex.h>
22 #include <com/sun/star/xml/sax/InputSource.hpp>
23 #include <com/sun/star/xml/sax/Parser.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
26 #include <com/sun/star/xml/sax/Writer.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <com/sun/star/io/XActiveDataSource.hpp>
29 #include <com/sun/star/xml/sax/SAXParseException.hpp>
30 #include <com/sun/star/xml/sax/XFastParser.hpp>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 #include <com/sun/star/document/XFilter.hpp>
33 #include <com/sun/star/document/XExporter.hpp>
34 #include <com/sun/star/document/XImporter.hpp>
36 #include <unotools/streamwrap.hxx>
37 #include <sot/storage.hxx>
38 #include <svx/svdmodel.hxx>
39 #include <svx/xmleohlp.hxx>
40 #include <svx/xmlgrhlp.hxx>
41 
42 #include <svx/unomodel.hxx>
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 
47 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent )
48 {
49  return SvxDrawingLayerExport( pModel, xOut, xComponent, "com.sun.star.comp.DrawingLayer.XMLExporter" );
50 }
51 
52 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent, const char* pExportService )
53 {
54  bool bDocRet = xOut.is();
55 
56  uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
58 
59  Reference< document::XEmbeddedObjectResolver > xObjectResolver;
61 
62  Reference< lang::XComponent > xSourceDoc( xComponent );
63  try
64  {
65  if( !xSourceDoc.is() )
66  {
67  xSourceDoc = new SvxUnoDrawingModel( pModel );
68  pModel->setUnoModel( Reference< XInterface >::query( xSourceDoc ) );
69  }
70 
71  uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
72 
73  if( bDocRet )
74  {
75  uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
76 
77  ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
78  if( pPersist )
79  {
81  xObjectResolver = xObjectHelper.get();
82  }
83 
85  xGraphicStorageHandler = xGraphicHelper.get();
86 
87  uno::Reference<xml::sax::XDocumentHandler> xHandler = xWriter;
88 
89  // doc export
90  xWriter->setOutputStream( xOut );
91 
92  uno::Sequence< uno::Any > aArgs( xObjectResolver.is() ? 3 : 2 );
93  aArgs[0] <<= xHandler;
94  aArgs[1] <<= xGraphicStorageHandler;
95  if( xObjectResolver.is() )
96  aArgs[2] <<= xObjectResolver;
97 
98  uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pExportService ), aArgs, xContext ), uno::UNO_QUERY );
99  if( !xFilter.is() )
100  {
101  OSL_FAIL( "com.sun.star.comp.Draw.XMLExporter service missing" );
102  bDocRet = false;
103  }
104 
105  if( bDocRet )
106  {
107  uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
108  if( xExporter.is() )
109  {
110  xExporter->setSourceDocument( xSourceDoc );
111 
112  uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
113  bDocRet = xFilter->filter( aDescriptor );
114  }
115  }
116  }
117  }
118  catch(uno::Exception const&)
119  {
121  bDocRet = false;
122  }
123 
124  if( xGraphicHelper )
125  xGraphicHelper->dispose();
126  xGraphicHelper.clear();
127  xGraphicStorageHandler = nullptr;
128 
129  if( xObjectHelper.is() )
130  xObjectHelper->dispose();
131 
132  return bDocRet;
133 }
134 
135 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
136 {
138  return SvxDrawingLayerExport( pModel, xOut, xComponent );
139 }
140 
141 //-
142 
143 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
144 {
145  return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
146 }
147 
148 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService )
149 {
150  bool bRet = true;
151 
152  uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
153  rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
154 
155  Reference< document::XEmbeddedObjectResolver > xObjectResolver;
157 
158  Reference< lang::XComponent > xTargetDocument( xComponent );
159  if( !xTargetDocument.is() )
160  {
161  xTargetDocument = new SvxUnoDrawingModel( pModel );
162  pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
163  }
164 
165  Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
166 
167  try
168  {
169  // Get service factory
171 
172  if ( xTargetModel.is() )
173  xTargetModel->lockControllers();
174 
175 
177  xGraphicStorageHandler = xGraphicHelper.get();
178 
179  ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
180  if( pPersist )
181  {
182  xObjectHelper = SvXMLEmbeddedObjectHelper::Create(
183  *pPersist,
185  xObjectResolver = xObjectHelper.get();
186  }
187 
188  // parse
189  // prepare ParserInputSrouce
190  xml::sax::InputSource aParserInput;
191  aParserInput.aInputStream = xInputStream;
192 
193  // get parser
194  Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
195 
196  // prepare filter arguments
197  Sequence<Any> aFilterArgs( 2 );
198  Any *pArgs = aFilterArgs.getArray();
199  *pArgs++ <<= xGraphicStorageHandler;
200  *pArgs++ <<= xObjectResolver;
201 
202  // get filter
203  Reference< xml::sax::XDocumentHandler > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pImportService ), aFilterArgs, xContext), UNO_QUERY );
204  uno::Reference< xml::sax::XFastParser > xFastParser = dynamic_cast<
205  xml::sax::XFastParser* >( xFilter.get() );
206  DBG_ASSERT( xFilter.is(), "Can't instantiate filter component." );
207 
208  bRet = false;
209  if( xParser.is() && xFilter.is() )
210  {
211  // connect parser and filter
212  xParser->setDocumentHandler( xFilter );
213 
214  // connect model and filter
215  uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
216  xImporter->setTargetDocument( xTargetDocument );
217 
218  // finally, parser the stream
219  if( xFastParser.is() )
220  xFastParser->parseStream( aParserInput );
221  else
222  xParser->parseStream( aParserInput );
223 
224  bRet = true;
225  }
226  }
227  catch( uno::Exception& )
228  {
230  }
231 
232  if( xGraphicHelper )
233  xGraphicHelper->dispose();
234  xGraphicHelper.clear();
235  xGraphicStorageHandler = nullptr;
236 
237  if( xObjectHelper.is() )
238  xObjectHelper->dispose();
239  xObjectHelper.clear();
240  xObjectResolver = nullptr;
241 
242  if ( xTargetModel.is() )
243  xTargetModel->unlockControllers();
244 
245  return bRet;
246 }
247 
248 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
249 {
251  return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
252 }
253 
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool SvxDrawingLayerImport(SdrModel *pModel, const uno::Reference< io::XInputStream > &xInputStream, const Reference< lang::XComponent > &xComponent)
Definition: xmlexport.cxx:143
bool SvxDrawingLayerExport(SdrModel *pModel, const uno::Reference< io::XOutputStream > &xOut, const Reference< lang::XComponent > &xComponent)
Definition: xmlexport.cxx:47
css::uno::Reference< css::lang::XComponent > xComponent
void setUnoModel(const css::uno::Reference< css::uno::XInterface > &xModel)
Definition: svdmodel.cxx:1704
#define DBG_UNHANDLED_EXCEPTION(...)
#define DBG_ASSERT(sCon, aError)
static rtl::Reference< SvXMLEmbeddedObjectHelper > Create(const css::uno::Reference< css::embed::XStorage > &,::comphelper::IEmbeddedHelper &rDocPersist, SvXMLEmbeddedObjectHelperMode eCreateMode)
::comphelper::IEmbeddedHelper * GetPersist() const
Definition: svdmodel.hxx:351
Reference< XComponentContext > getProcessComponentContext()
static rtl::Reference< SvXMLGraphicHelper > Create(const css::uno::Reference< css::embed::XStorage > &rXMLStorage, SvXMLGraphicHelperMode eCreateMode)