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