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