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/xml/sax/XDocumentHandler.hpp>
25 #include <com/sun/star/xml/sax/Writer.hpp>
26 #include <com/sun/star/lang/XComponent.hpp>
27 #include <com/sun/star/xml/sax/XFastParser.hpp>
28 #include <com/sun/star/io/XOutputStream.hpp>
29 #include <com/sun/star/document/XFilter.hpp>
30 #include <com/sun/star/document/XExporter.hpp>
31 #include <com/sun/star/document/XImporter.hpp>
33 #include <svx/svdmodel.hxx>
34 #include <svx/xmleohlp.hxx>
35 #include <svx/xmlgrhlp.hxx>
36 
37 #include <svx/unomodel.hxx>
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 
42 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent )
43 {
44  return SvxDrawingLayerExport( pModel, xOut, xComponent, "com.sun.star.comp.DrawingLayer.XMLExporter" );
45 }
46 
47 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent, const char* pExportService )
48 {
49  bool bDocRet = xOut.is();
50 
51  uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
53 
54  Reference< document::XEmbeddedObjectResolver > xObjectResolver;
56 
57  Reference< lang::XComponent > xSourceDoc( xComponent );
58  try
59  {
60  if( !xSourceDoc.is() )
61  {
62  xSourceDoc = new SvxUnoDrawingModel( pModel );
63  pModel->setUnoModel( Reference< XInterface >::query( xSourceDoc ) );
64  }
65 
66  uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
67 
68  if( bDocRet )
69  {
70  uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
71 
72  ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
73  if( pPersist )
74  {
76  xObjectResolver = xObjectHelper.get();
77  }
78 
80  xGraphicStorageHandler = xGraphicHelper.get();
81 
82  uno::Reference<xml::sax::XDocumentHandler> xHandler = xWriter;
83 
84  // doc export
85  xWriter->setOutputStream( xOut );
86 
87  uno::Sequence< uno::Any > aArgs( xObjectResolver.is() ? 3 : 2 );
88  aArgs[0] <<= xHandler;
89  aArgs[1] <<= xGraphicStorageHandler;
90  if( xObjectResolver.is() )
91  aArgs[2] <<= xObjectResolver;
92 
93  uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pExportService ), aArgs, xContext ), uno::UNO_QUERY );
94  if( !xFilter.is() )
95  {
96  OSL_FAIL( "com.sun.star.comp.Draw.XMLExporter service missing" );
97  bDocRet = false;
98  }
99 
100  if( bDocRet )
101  {
102  uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
103  if( xExporter.is() )
104  {
105  xExporter->setSourceDocument( xSourceDoc );
106 
107  uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
108  bDocRet = xFilter->filter( aDescriptor );
109  }
110  }
111  }
112  }
113  catch(uno::Exception const&)
114  {
116  bDocRet = false;
117  }
118 
119  if( xGraphicHelper )
120  xGraphicHelper->dispose();
121  xGraphicHelper.clear();
122  xGraphicStorageHandler = nullptr;
123 
124  if( xObjectHelper.is() )
125  xObjectHelper->dispose();
126 
127  return bDocRet;
128 }
129 
130 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
131 {
133  return SvxDrawingLayerExport( pModel, xOut, xComponent );
134 }
135 
136 //-
137 
138 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
139 {
140  return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
141 }
142 
143 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService )
144 {
145  bool bRet = true;
146 
147  uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
148  rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
149 
150  Reference< document::XEmbeddedObjectResolver > xObjectResolver;
152 
153  Reference< lang::XComponent > xTargetDocument( xComponent );
154  if( !xTargetDocument.is() )
155  {
156  xTargetDocument = new SvxUnoDrawingModel( pModel );
157  pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
158  }
159 
160  Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
161 
162  try
163  {
164  // Get service factory
166 
167  if ( xTargetModel.is() )
168  xTargetModel->lockControllers();
169 
170 
172  xGraphicStorageHandler = xGraphicHelper.get();
173 
174  ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
175  if( pPersist )
176  {
177  xObjectHelper = SvXMLEmbeddedObjectHelper::Create(
178  *pPersist,
180  xObjectResolver = xObjectHelper.get();
181  }
182 
183  // parse
184  // prepare ParserInputSrouce
185  xml::sax::InputSource aParserInput;
186  aParserInput.aInputStream = xInputStream;
187 
188  // get parser
189  Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
190 
191  // prepare filter arguments
192  Sequence<Any> aFilterArgs( 2 );
193  Any *pArgs = aFilterArgs.getArray();
194  *pArgs++ <<= xGraphicStorageHandler;
195  *pArgs++ <<= xObjectResolver;
196 
197  // get filter
198  Reference< xml::sax::XDocumentHandler > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pImportService ), aFilterArgs, xContext), UNO_QUERY );
199  uno::Reference< xml::sax::XFastParser > xFastParser = dynamic_cast<
200  xml::sax::XFastParser* >( xFilter.get() );
201  DBG_ASSERT( xFilter.is(), "Can't instantiate filter component." );
202 
203  bRet = false;
204  if( xParser.is() && xFilter.is() )
205  {
206  // connect parser and filter
207  xParser->setDocumentHandler( xFilter );
208 
209  // connect model and filter
210  uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
211  xImporter->setTargetDocument( xTargetDocument );
212 
213  // finally, parser the stream
214  if( xFastParser.is() )
215  xFastParser->parseStream( aParserInput );
216  else
217  xParser->parseStream( aParserInput );
218 
219  bRet = true;
220  }
221  }
222  catch( uno::Exception& )
223  {
225  }
226 
227  if( xGraphicHelper )
228  xGraphicHelper->dispose();
229  xGraphicHelper.clear();
230  xGraphicStorageHandler = nullptr;
231 
232  if( xObjectHelper.is() )
233  xObjectHelper->dispose();
234  xObjectHelper.clear();
235  xObjectResolver = nullptr;
236 
237  if ( xTargetModel.is() )
238  xTargetModel->unlockControllers();
239 
240  return bRet;
241 }
242 
243 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
244 {
246  return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
247 }
248 
249 /* 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:138
bool SvxDrawingLayerExport(SdrModel *pModel, const uno::Reference< io::XOutputStream > &xOut, const Reference< lang::XComponent > &xComponent)
Definition: xmlexport.cxx:42
css::uno::Reference< css::lang::XComponent > xComponent
void setUnoModel(const css::uno::Reference< css::uno::XInterface > &xModel)
Definition: svdmodel.cxx:1690
#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)