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