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
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
37using namespace ::com::sun::star;
38using namespace ::com::sun::star::uno;
39
40bool 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
45bool 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 auto pArgs = aArgs.getArray();
87 pArgs[0] <<= xHandler;
88 pArgs[1] <<= xGraphicStorageHandler;
89 if( xObjectResolver.is() )
90 pArgs[2] <<= xObjectResolver;
91
92 uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pExportService ), aArgs, xContext ), uno::UNO_QUERY );
93 if( !xFilter.is() )
94 {
95 OSL_FAIL( "com.sun.star.comp.Draw.XMLExporter service missing" );
96 bDocRet = false;
97 }
98
99 if( bDocRet )
100 {
101 uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
102 if( xExporter.is() )
103 {
104 xExporter->setSourceDocument( xSourceDoc );
105
106 uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
107 bDocRet = xFilter->filter( aDescriptor );
108 }
109 }
110 }
111 }
112 catch(uno::Exception const&)
113 {
115 bDocRet = false;
116 }
117
118 if( xGraphicHelper )
119 xGraphicHelper->dispose();
120 xGraphicHelper.clear();
121 xGraphicStorageHandler = nullptr;
122
123 if( xObjectHelper.is() )
124 xObjectHelper->dispose();
125
126 return bDocRet;
127}
128
129bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
130{
131 Reference< lang::XComponent > xComponent;
132 return SvxDrawingLayerExport( pModel, xOut, xComponent );
133}
134
135//-
136
137bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
138{
139 return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
140}
141
142bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService )
143{
144 bool bRet = true;
145
146 uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
148
149 Reference< document::XEmbeddedObjectResolver > xObjectResolver;
151
152 Reference< lang::XComponent > xTargetDocument( xComponent );
153 if( !xTargetDocument.is() )
154 {
155 xTargetDocument = new SvxUnoDrawingModel( pModel );
156 pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
157 }
158
159 Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
160
161 try
162 {
163 // Get service factory
164 Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
165
166 if ( xTargetModel.is() )
167 xTargetModel->lockControllers();
168
169
171 xGraphicStorageHandler = xGraphicHelper.get();
172
173 ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
174 if( pPersist )
175 {
176 xObjectHelper = SvXMLEmbeddedObjectHelper::Create(
177 *pPersist,
179 xObjectResolver = xObjectHelper.get();
180 }
181
182 // parse
183 // prepare ParserInputSource
184 xml::sax::InputSource aParserInput;
185 aParserInput.aInputStream = xInputStream;
186
187 // prepare filter arguments
188 Sequence<Any> aFilterArgs( 2 );
189 Any *pArgs = aFilterArgs.getArray();
190 *pArgs++ <<= xGraphicStorageHandler;
191 *pArgs++ <<= xObjectResolver;
192
193 // get filter
194 Reference< XInterface > xFilter = xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pImportService ), aFilterArgs, xContext);
195 SAL_WARN_IF( !xFilter, "svx", "Can't instantiate filter component " << pImportService);
196 uno::Reference< xml::sax::XFastParser > xFastParser( xFilter, UNO_QUERY );
197 assert(xFastParser);
198
199 bRet = false;
200 if( xFastParser.is() )
201 {
202 // connect model and filter
203 uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
204 xImporter->setTargetDocument( xTargetDocument );
205
206 // finally, parser the stream
207 xFastParser->parseStream( aParserInput );
208
209 bRet = true;
210 }
211 }
212 catch( uno::Exception& )
213 {
215 }
216
217 if( xGraphicHelper )
218 xGraphicHelper->dispose();
219 xGraphicHelper.clear();
220 xGraphicStorageHandler = nullptr;
221
222 if( xObjectHelper.is() )
223 xObjectHelper->dispose();
224 xObjectHelper.clear();
225 xObjectResolver = nullptr;
226
227 if ( xTargetModel.is() )
228 xTargetModel->unlockControllers();
229
230 return bRet;
231}
232
233bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
234{
235 Reference< lang::XComponent > xComponent;
236 return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
237}
238
239/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::comphelper::IEmbeddedHelper * GetPersist() const
Definition: svdmodel.hxx:355
void setUnoModel(const css::uno::Reference< css::uno::XInterface > &xModel)
Definition: svdmodel.cxx:1583
static rtl::Reference< SvXMLEmbeddedObjectHelper > Create(const css::uno::Reference< css::embed::XStorage > &, ::comphelper::IEmbeddedHelper &rDocPersist, SvXMLEmbeddedObjectHelperMode eCreateMode)
static rtl::Reference< SvXMLGraphicHelper > Create(const css::uno::Reference< css::embed::XStorage > &rXMLStorage, SvXMLGraphicHelperMode eCreateMode)
#define DBG_UNHANDLED_EXCEPTION(...)
#define SAL_WARN_IF(condition, area, stream)
Reference< XComponentContext > getProcessComponentContext()
bool SvxDrawingLayerExport(SdrModel *pModel, const uno::Reference< io::XOutputStream > &xOut, const Reference< lang::XComponent > &xComponent)
Definition: xmlexport.cxx:40
bool SvxDrawingLayerImport(SdrModel *pModel, const uno::Reference< io::XInputStream > &xInputStream, const Reference< lang::XComponent > &xComponent)
Definition: xmlexport.cxx:137