Line data Source code
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 <com/sun/star/embed/ElementModes.hpp>
21 : #include <com/sun/star/embed/XTransactedObject.hpp>
22 : #include <svl/macitem.hxx>
23 : #include <svtools/unoevent.hxx>
24 : #include <sfx2/docfile.hxx>
25 : #include <unotools/streamwrap.hxx>
26 : #include <comphelper/componentcontext.hxx>
27 : #include <comphelper/processfactory.hxx>
28 : #include <com/sun/star/xml/sax/InputSource.hpp>
29 : #include <com/sun/star/io/XActiveDataSource.hpp>
30 : #include <com/sun/star/xml/sax/Parser.hpp>
31 : #include <com/sun/star/xml/sax/Writer.hpp>
32 : #include <com/sun/star/document/XStorageBasedDocument.hpp>
33 : #include <doc.hxx>
34 : #include <docsh.hxx>
35 : #include <shellio.hxx>
36 : #include <SwXMLTextBlocks.hxx>
37 : #include <SwXMLBlockImport.hxx>
38 : #include <SwXMLBlockExport.hxx>
39 : #include <swevent.hxx>
40 : #include <swerror.h>
41 :
42 :
43 : #define STREAM_STGREAD ( STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE )
44 : #define STREAM_STGWRITE ( STREAM_READ | STREAM_WRITE | STREAM_SHARE_DENYWRITE )
45 :
46 : const char XMLN_BLOCKLIST[] = "BlockList.xml";
47 :
48 : using namespace ::com::sun::star;
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::container;
51 : using ::rtl::OUString;
52 :
53 : using ::xmloff::token::XML_BLOCK_LIST;
54 : using ::xmloff::token::XML_UNFORMATTED_TEXT;
55 : using ::xmloff::token::GetXMLToken;
56 :
57 0 : sal_uLong SwXMLTextBlocks::GetDoc( sal_uInt16 nIdx )
58 : {
59 0 : String aFolderName ( GetPackageName ( nIdx ) );
60 :
61 0 : if (!IsOnlyTextBlock ( nIdx ) )
62 : {
63 : try
64 : {
65 0 : xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
66 0 : xMedium = new SfxMedium( xRoot, GetBaseURL(), OUString( "writer8" ) );
67 0 : SwReader aReader( *xMedium, aFolderName, pDoc );
68 0 : ReadXML->SetBlockMode( sal_True );
69 0 : aReader.Read( *ReadXML );
70 0 : ReadXML->SetBlockMode( sal_False );
71 : // Ole objects fails to display when inserted into document
72 : // because the ObjectReplacement folder ( and contents are missing )
73 0 : rtl::OUString sObjReplacements( RTL_CONSTASCII_USTRINGPARAM( "ObjectReplacements" ) );
74 0 : if ( xRoot->hasByName( sObjReplacements ) )
75 : {
76 0 : uno::Reference< document::XStorageBasedDocument > xDocStor( pDoc->GetDocShell()->GetModel(), uno::UNO_QUERY_THROW );
77 0 : uno::Reference< embed::XStorage > xStr( xDocStor->getDocumentStorage() );
78 0 : if ( xStr.is() )
79 : {
80 0 : xRoot->copyElementTo( sObjReplacements, xStr, sObjReplacements );
81 0 : uno::Reference< embed::XTransactedObject > xTrans( xStr, uno::UNO_QUERY );
82 0 : if ( xTrans.is() )
83 0 : xTrans->commit();
84 0 : }
85 0 : }
86 : }
87 0 : catch( uno::Exception& )
88 : {
89 : }
90 :
91 0 : xRoot = 0;
92 : }
93 : else
94 : {
95 0 : String aStreamName = aFolderName + rtl::OUString(".xml");
96 : try
97 : {
98 0 : xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
99 0 : uno::Reference < io::XStream > xStream = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
100 :
101 : uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
102 0 : comphelper::getProcessServiceFactory();
103 : OSL_ENSURE( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
104 0 : if( !xServiceFactory.is() )
105 : {
106 : // Throw an exception ?
107 : }
108 :
109 0 : xml::sax::InputSource aParserInput;
110 0 : aParserInput.sSystemId = aNames[nIdx]->aPackageName;
111 :
112 0 : aParserInput.aInputStream = xStream->getInputStream();
113 :
114 : // get filter
115 : // #110680#
116 : // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, aCur, sal_True );
117 0 : uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xServiceFactory, *this, aCur, sal_True );
118 :
119 : // connect parser and filter
120 0 : uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(comphelper::getComponentContext(xServiceFactory));
121 0 : xParser->setDocumentHandler( xFilter );
122 :
123 : // parse
124 : try
125 : {
126 0 : xParser->parseStream( aParserInput );
127 : }
128 0 : catch( xml::sax::SAXParseException& )
129 : {
130 : // re throw ?
131 : }
132 0 : catch( xml::sax::SAXException& )
133 : {
134 : // re throw ?
135 : }
136 0 : catch( io::IOException& )
137 : {
138 : // re throw ?
139 : }
140 :
141 0 : bInfoChanged = sal_False;
142 0 : MakeBlockText(aCur);
143 : }
144 0 : catch( uno::Exception& )
145 : {
146 : }
147 :
148 0 : xRoot = 0;
149 : }
150 0 : return 0;
151 : }
152 :
153 : // event description for autotext events; this constant should really be
154 : // taken from unocore/unoevents.cxx or ui/unotxt.cxx
155 : const struct SvEventDescription aAutotextEvents[] =
156 : {
157 : { SW_EVENT_START_INS_GLOSSARY, "OnInsertStart" },
158 : { SW_EVENT_END_INS_GLOSSARY, "OnInsertDone" },
159 : { 0, NULL }
160 : };
161 :
162 0 : sal_uLong SwXMLTextBlocks::GetMacroTable( sal_uInt16 nIdx,
163 : SvxMacroTableDtor& rMacroTbl,
164 : sal_Bool bFileAlreadyOpen )
165 : {
166 : // set current auto text
167 :
168 0 : aShort = aNames[nIdx]->aShort;
169 0 : aLong = aNames[nIdx]->aLong;
170 0 : aPackageName = aNames[nIdx]->aPackageName;
171 :
172 0 : sal_uLong nRet = 0;
173 :
174 : // open stream in proper sub-storage
175 0 : if( !bFileAlreadyOpen )
176 : {
177 0 : CloseFile();
178 0 : nRet = OpenFile ( sal_True );
179 : }
180 0 : if ( 0 == nRet )
181 : {
182 : try
183 : {
184 0 : xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::READ );
185 0 : long nTmp = SOT_FORMATSTR_ID_STARWRITER_60;
186 0 : bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp );
187 :
188 0 : OUString sStreamName(RTL_CONSTASCII_USTRINGPARAM("atevent.xml"));
189 0 : uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement(
190 0 : sStreamName, embed::ElementModes::READ );
191 : OSL_ENSURE(xDocStream.is(), "Can't create stream");
192 0 : if ( xDocStream.is() )
193 : {
194 0 : uno::Reference<io::XInputStream> xInputStream = xDocStream->getInputStream();
195 :
196 : // prepare ParserInputSrouce
197 0 : xml::sax::InputSource aParserInput;
198 0 : aParserInput.sSystemId = aName;
199 0 : aParserInput.aInputStream = xInputStream;
200 :
201 : // get service factory
202 : uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
203 0 : comphelper::getProcessServiceFactory();
204 : uno::Reference< uno::XComponentContext > xContext =
205 0 : comphelper::getProcessComponentContext();
206 0 : if ( xServiceFactory.is() )
207 : {
208 :
209 : // get parser
210 0 : uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
211 :
212 : // create descriptor and reference to it. Either
213 : // both or neither must be kept because of the
214 : // reference counting!
215 : SvMacroTableEventDescriptor* pDescriptor =
216 0 : new SvMacroTableEventDescriptor(aAutotextEvents);
217 0 : uno::Reference<XNameReplace> xReplace = pDescriptor;
218 0 : Sequence<Any> aFilterArguments( 1 );
219 0 : aFilterArguments[0] <<= xReplace;
220 :
221 : // get filter
222 : OUString sFilterComponent = bOasis
223 : ? OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLOasisAutotextEventsImporter"))
224 0 : : OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLAutotextEventsImporter"));
225 : uno::Reference< xml::sax::XDocumentHandler > xFilter(
226 0 : xServiceFactory->createInstanceWithArguments(
227 0 : sFilterComponent, aFilterArguments),
228 0 : UNO_QUERY );
229 : OSL_ENSURE( xFilter.is(),
230 : "can't instantiate atevents filter");
231 0 : if ( xFilter.is() )
232 : {
233 : // connect parser and filter
234 0 : xParser->setDocumentHandler( xFilter );
235 :
236 : // connect model and filter
237 : uno::Reference<document::XImporter> xImporter( xFilter,
238 0 : UNO_QUERY );
239 :
240 : // we don't need a model
241 : // xImporter->setTargetDocument( xModelComponent );
242 :
243 : // parse the stream
244 : try
245 : {
246 0 : xParser->parseStream( aParserInput );
247 : }
248 0 : catch( xml::sax::SAXParseException& )
249 : {
250 : // workaround for #83452#: SetSize doesn't work
251 : // nRet = ERR_SWG_READ_ERROR;
252 : }
253 0 : catch( xml::sax::SAXException& )
254 : {
255 0 : nRet = ERR_SWG_READ_ERROR;
256 : }
257 0 : catch( io::IOException& )
258 : {
259 0 : nRet = ERR_SWG_READ_ERROR;
260 : }
261 :
262 : // and finally, copy macro into table
263 0 : if (0 == nRet)
264 0 : pDescriptor->copyMacrosIntoTable(rMacroTbl);
265 : }
266 : else
267 0 : nRet = ERR_SWG_READ_ERROR;
268 :
269 : }
270 : else
271 0 : nRet = ERR_SWG_READ_ERROR;
272 : }
273 : else
274 0 : nRet = ERR_SWG_READ_ERROR;
275 : }
276 0 : catch( uno::Exception& )
277 : {
278 0 : nRet = ERR_SWG_READ_ERROR;
279 : }
280 : }
281 : else
282 0 : nRet = ERR_SWG_READ_ERROR;
283 :
284 : // success!
285 0 : return nRet;
286 : }
287 :
288 :
289 0 : sal_uLong SwXMLTextBlocks::GetBlockText( const String& rShort, String& rText )
290 : {
291 0 : sal_uLong n = 0;
292 0 : sal_Bool bTextOnly = sal_True;
293 0 : String aFolderName = GeneratePackageName ( rShort );
294 0 : String aStreamName = aFolderName + rtl::OUString(".xml");
295 0 : rText.Erase();
296 :
297 : try
298 : {
299 0 : xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
300 0 : uno::Reference < container::XNameAccess > xAccess( xRoot, uno::UNO_QUERY );
301 0 : if ( !xAccess->hasByName( aStreamName ) || !xRoot->isStreamElement( aStreamName ) )
302 : {
303 0 : bTextOnly = sal_False;
304 0 : aStreamName = rtl::OUString("content.xml");
305 : }
306 :
307 0 : uno::Reference < io::XStream > xContents = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
308 : uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
309 0 : comphelper::getProcessServiceFactory();
310 : uno::Reference< uno::XComponentContext > xContext =
311 0 : comphelper::getProcessComponentContext();
312 : OSL_ENSURE( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
313 0 : if( !xServiceFactory.is() )
314 : {
315 : // Throw an exception ?
316 : }
317 :
318 0 : xml::sax::InputSource aParserInput;
319 0 : aParserInput.sSystemId = aName;
320 0 : aParserInput.aInputStream = xContents->getInputStream();
321 :
322 : // get filter
323 : // #110680#
324 : // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, rText, bTextOnly );
325 0 : uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xServiceFactory, *this, rText, bTextOnly );
326 :
327 : // connect parser and filter
328 0 : uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(xContext);
329 0 : xParser->setDocumentHandler( xFilter );
330 :
331 : // parse
332 : try
333 : {
334 0 : xParser->parseStream( aParserInput );
335 : }
336 0 : catch( xml::sax::SAXParseException& )
337 : {
338 : // re throw ?
339 : }
340 0 : catch( xml::sax::SAXException& )
341 : {
342 : // re throw ?
343 : }
344 0 : catch( io::IOException& )
345 : {
346 : // re throw ?
347 : }
348 :
349 0 : xRoot = 0;
350 : }
351 0 : catch ( uno::Exception& )
352 : {
353 : OSL_FAIL( "Tried to open non-existent folder or stream!");
354 : }
355 :
356 0 : return n;
357 : }
358 :
359 0 : sal_uLong SwXMLTextBlocks::PutBlockText( const String& rShort, const String& ,
360 : const String& rText, const String& rPackageName )
361 : {
362 0 : GetIndex ( rShort );
363 : /*
364 : if (xBlkRoot->IsContained ( rPackageName ) )
365 : {
366 : xBlkRoot->Remove ( rPackageName );
367 : xBlkRoot->Commit ( );
368 : }
369 : */
370 0 : String aFolderName( rPackageName );
371 0 : String aStreamName = aFolderName + rtl::OUString(".xml");
372 :
373 : uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
374 0 : comphelper::getProcessServiceFactory();
375 : uno::Reference< uno::XComponentContext > xContext =
376 0 : comphelper::getProcessComponentContext();
377 : OSL_ENSURE( xServiceFactory.is(),
378 : "XMLReader::Read: got no service manager" );
379 0 : if( !xServiceFactory.is() )
380 : {
381 : // Throw an exception ?
382 : }
383 :
384 0 : uno::Reference < xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
385 0 : sal_uLong nRes = 0;
386 :
387 : try
388 : {
389 0 : xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::WRITE );
390 0 : uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( aStreamName,
391 0 : embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
392 :
393 0 : uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
394 0 : OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") );
395 0 : Any aAny;
396 0 : aAny <<= aMime;
397 0 : xSet->setPropertyValue( rtl::OUString("MediaType"), aAny );
398 0 : uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
399 0 : uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY);
400 0 : xSrc->setOutputStream(xOut);
401 :
402 : uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter,
403 0 : uno::UNO_QUERY);
404 :
405 : // #110680#
406 : // SwXMLTextBlockExport aExp(*this, GetXMLToken ( XML_UNFORMATTED_TEXT ), xHandler);
407 0 : SwXMLTextBlockExport aExp( xServiceFactory, *this, GetXMLToken ( XML_UNFORMATTED_TEXT ), xHandler);
408 :
409 0 : aExp.exportDoc( rText );
410 :
411 0 : uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY );
412 0 : if ( xTrans.is() )
413 0 : xTrans->commit();
414 :
415 0 : if (! (nFlags & SWXML_NOROOTCOMMIT) )
416 : {
417 0 : uno::Reference < embed::XTransactedObject > xTmpTrans( xBlkRoot, uno::UNO_QUERY );
418 0 : if ( xTmpTrans.is() )
419 0 : xTmpTrans->commit();
420 0 : }
421 : }
422 0 : catch ( uno::Exception& )
423 : {
424 0 : nRes = ERR_SWG_WRITE_ERROR;
425 : }
426 :
427 0 : xRoot = 0;
428 :
429 : //TODO/LATER: error handling
430 : /*
431 : sal_uLong nErr = xBlkRoot->GetError();
432 : sal_uLong nRes = 0;
433 : if( nErr == SVSTREAM_DISK_FULL )
434 : nRes = ERR_W4W_WRITE_FULL;
435 : else if( nErr != SVSTREAM_OK )
436 : nRes = ERR_SWG_WRITE_ERROR;
437 : */
438 0 : if( !nRes ) // damit ueber GetText & nCur aufs Doc zugegriffen
439 0 : MakeBlockText( rText );
440 :
441 0 : return nRes;
442 : }
443 :
444 0 : void SwXMLTextBlocks::ReadInfo( void )
445 : {
446 : try
447 : {
448 0 : const OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_BLOCKLIST ) );
449 0 : uno::Reference < container::XNameAccess > xAccess( xBlkRoot, uno::UNO_QUERY );
450 0 : if ( xAccess.is() && xAccess->hasByName( sDocName ) && xBlkRoot->isStreamElement( sDocName ) )
451 : {
452 : uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
453 0 : comphelper::getProcessServiceFactory();
454 : uno::Reference< uno::XComponentContext > xContext =
455 0 : comphelper::getProcessComponentContext();
456 : OSL_ENSURE( xServiceFactory.is(),
457 : "XMLReader::Read: got no service manager" );
458 0 : if( !xServiceFactory.is() )
459 : {
460 : // Throw an exception ?
461 : }
462 :
463 0 : xml::sax::InputSource aParserInput;
464 0 : aParserInput.sSystemId = sDocName;
465 :
466 0 : uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName, embed::ElementModes::READ );
467 0 : aParserInput.aInputStream = xDocStream->getInputStream();
468 :
469 : // get filter
470 : // #110680#
471 : // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLBlockListImport( *this );
472 0 : uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLBlockListImport( xServiceFactory, *this );
473 :
474 : // connect parser and filter
475 0 : uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
476 0 : xParser->setDocumentHandler( xFilter );
477 :
478 : // parse
479 : try
480 : {
481 0 : xParser->parseStream( aParserInput );
482 : }
483 0 : catch( xml::sax::SAXParseException& )
484 : {
485 : // re throw ?
486 : }
487 0 : catch( xml::sax::SAXException& )
488 : {
489 : // re throw ?
490 : }
491 0 : catch( io::IOException& )
492 : {
493 : // re throw ?
494 0 : }
495 0 : }
496 : }
497 0 : catch ( uno::Exception& )
498 : {
499 : }
500 0 : }
501 0 : void SwXMLTextBlocks::WriteInfo( void )
502 : {
503 0 : if ( xBlkRoot.is() || 0 == OpenFile ( sal_False ) )
504 : {
505 : uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
506 0 : comphelper::getProcessServiceFactory();
507 : uno::Reference< uno::XComponentContext > xContext =
508 0 : comphelper::getProcessComponentContext();
509 : OSL_ENSURE( xServiceFactory.is(),
510 : "XMLReader::Read: got no service manager" );
511 0 : if( !xServiceFactory.is() )
512 : {
513 : // Throw an exception ?
514 : }
515 :
516 0 : uno::Reference < xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
517 0 : OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_BLOCKLIST ) );
518 :
519 : /*
520 : if ( xBlkRoot->IsContained( sDocName) )
521 : {
522 : xBlkRoot->Remove ( sDocName );
523 : xBlkRoot->Commit();
524 : }
525 : */
526 :
527 : try
528 : {
529 0 : uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName,
530 0 : embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
531 :
532 0 : uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
533 0 : OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") );
534 0 : Any aAny;
535 0 : aAny <<= aMime;
536 0 : xSet->setPropertyValue( rtl::OUString("MediaType"), aAny );
537 0 : uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
538 0 : uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY);
539 0 : xSrc->setOutputStream(xOut);
540 :
541 0 : uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter, uno::UNO_QUERY);
542 :
543 0 : SwXMLBlockListExport aExp( xServiceFactory, *this, OUString(RTL_CONSTASCII_USTRINGPARAM(XMLN_BLOCKLIST)), xHandler);
544 :
545 0 : aExp.exportDoc( XML_BLOCK_LIST );
546 :
547 0 : uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
548 0 : if ( xTrans.is() )
549 0 : xTrans->commit();
550 : }
551 0 : catch ( uno::Exception& )
552 : {
553 : }
554 :
555 0 : bInfoChanged = sal_False;
556 0 : return;
557 : }
558 : }
559 :
560 0 : sal_uLong SwXMLTextBlocks::SetMacroTable(
561 : sal_uInt16 nIdx,
562 : const SvxMacroTableDtor& rMacroTbl,
563 : sal_Bool bFileAlreadyOpen )
564 : {
565 : // set current autotext
566 0 : aShort = aNames[nIdx]->aShort;
567 0 : aLong = aNames[nIdx]->aLong;
568 0 : aPackageName = aNames[nIdx]->aPackageName;
569 :
570 : // start XML autotext event export
571 0 : sal_uLong nRes = 0;
572 :
573 : uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
574 0 : comphelper::getProcessServiceFactory();
575 : uno::Reference< uno::XComponentContext > xContext =
576 0 : comphelper::getProcessComponentContext();
577 : OSL_ENSURE( xServiceFactory.is(),
578 : "XML autotext event write:: got no service manager" );
579 0 : if( !xServiceFactory.is() )
580 0 : return ERR_SWG_WRITE_ERROR;
581 :
582 : // Get model
583 : uno::Reference< lang::XComponent > xModelComp(
584 0 : pDoc->GetDocShell()->GetModel(), UNO_QUERY );
585 : OSL_ENSURE( xModelComp.is(), "XMLWriter::Write: got no model" );
586 0 : if( !xModelComp.is() )
587 0 : return ERR_SWG_WRITE_ERROR;
588 :
589 : // open stream in proper sub-storage
590 0 : if( !bFileAlreadyOpen )
591 : {
592 0 : CloseFile(); // close (it may be open in read-only-mode)
593 0 : nRes = OpenFile ( sal_False );
594 : }
595 :
596 0 : if ( 0 == nRes )
597 : {
598 : try
599 : {
600 0 : xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::WRITE );
601 0 : OUString sStreamName( RTL_CONSTASCII_USTRINGPARAM("atevent.xml") );
602 0 : long nTmp = SOT_FORMATSTR_ID_STARWRITER_60;
603 0 : bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp );
604 :
605 0 : uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( sStreamName,
606 0 : embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
607 :
608 0 : uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
609 0 : OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") );
610 0 : Any aAny;
611 0 : aAny <<= aMime;
612 0 : xSet->setPropertyValue( rtl::OUString("MediaType"), aAny );
613 0 : uno::Reference < io::XOutputStream > xOutputStream = xDocStream->getOutputStream();
614 :
615 : // get XML writer
616 : uno::Reference< xml::sax::XWriter > xSaxWriter =
617 0 : xml::sax::Writer::create( xContext );
618 :
619 : // connect XML writer to output stream
620 0 : xSaxWriter->setOutputStream( xOutputStream );
621 : uno::Reference<xml::sax::XDocumentHandler> xDocHandler(
622 0 : xSaxWriter, UNO_QUERY);
623 :
624 : // construct events object
625 : uno::Reference<XNameAccess> xEvents =
626 0 : new SvMacroTableEventDescriptor(rMacroTbl,aAutotextEvents);
627 :
628 : // prepare arguments (prepend doc handler to given arguments)
629 0 : Sequence<Any> aParams(2);
630 0 : aParams[0] <<= xDocHandler;
631 0 : aParams[1] <<= xEvents;
632 :
633 :
634 : // get filter component
635 : OUString sFilterComponent = bOasis
636 : ? OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter"))
637 0 : : OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLAutotextEventsExporter"));
638 : uno::Reference< document::XExporter > xExporter(
639 0 : xServiceFactory->createInstanceWithArguments(
640 0 : sFilterComponent, aParams), UNO_QUERY);
641 : OSL_ENSURE( xExporter.is(),
642 : "can't instantiate export filter component" );
643 0 : if( xExporter.is() )
644 : {
645 : // connect model and filter
646 0 : xExporter->setSourceDocument( xModelComp );
647 :
648 : // filter!
649 0 : Sequence<beans::PropertyValue> aFilterProps( 0 );
650 : uno::Reference < document::XFilter > xFilter( xExporter,
651 0 : UNO_QUERY );
652 0 : xFilter->filter( aFilterProps );
653 : }
654 : else
655 0 : nRes = ERR_SWG_WRITE_ERROR;
656 :
657 : // finally, commit stream, sub-storage and storage
658 0 : uno::Reference < embed::XTransactedObject > xTmpTrans( xRoot, uno::UNO_QUERY );
659 0 : if ( xTmpTrans.is() )
660 0 : xTmpTrans->commit();
661 :
662 0 : if ( !bFileAlreadyOpen )
663 : {
664 0 : uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
665 0 : if ( xTrans.is() )
666 0 : xTrans->commit();
667 : }
668 :
669 0 : xRoot = 0;
670 : }
671 0 : catch ( uno::Exception& )
672 : {
673 0 : nRes = ERR_SWG_WRITE_ERROR;
674 : }
675 :
676 0 : if( !bFileAlreadyOpen )
677 0 : CloseFile();
678 : }
679 : else
680 0 : nRes = ERR_SWG_WRITE_ERROR;
681 :
682 0 : return nRes;
683 : }
684 :
685 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|