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