20#include <com/sun/star/embed/ElementModes.hpp>
21#include <com/sun/star/embed/XTransactedObject.hpp>
22#include <osl/diagnose.h>
29#include <com/sun/star/beans/XPropertySet.hpp>
30#include <com/sun/star/xml/sax/InputSource.hpp>
31#include <com/sun/star/io/IOException.hpp>
32#include <com/sun/star/xml/sax/FastParser.hpp>
33#include <com/sun/star/xml/sax/FastToken.hpp>
34#include <com/sun/star/xml/sax/Parser.hpp>
35#include <com/sun/star/xml/sax/Writer.hpp>
36#include <com/sun/star/xml/sax/SAXParseException.hpp>
37#include <com/sun/star/document/XStorageBasedDocument.hpp>
53using namespace css::xml::sax;
56using ::xmloff::token::XML_BLOCK_LIST;
57using ::xmloff::token::XML_UNFORMATTED_TEXT;
58using ::xmloff::token::GetXMLToken;
68 m_xRoot =
m_xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
76 OUString sObjReplacements(
"ObjectReplacements" );
77 if (
m_xRoot->hasByName( sObjReplacements ) )
79 uno::Reference< document::XStorageBasedDocument > xDocStor(
m_xDoc->GetDocShell()->GetModel(), uno::UNO_QUERY_THROW );
80 uno::Reference< embed::XStorage > xStr( xDocStor->getDocumentStorage() );
83 m_xRoot->copyElementTo( sObjReplacements, xStr, sObjReplacements );
84 uno::Reference< embed::XTransactedObject > xTrans( xStr, uno::UNO_QUERY );
90 catch( uno::Exception& )
98 OUString aStreamName = aFolderName +
".xml";
101 m_xRoot =
m_xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
102 uno::Reference < io::XStream >
xStream =
m_xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
104 uno::Reference< uno::XComponentContext > xContext =
107 xml::sax::InputSource aParserInput;
108 aParserInput.sSystemId =
m_aNames[nIdx]->m_aPackageName;
110 aParserInput.aInputStream =
xStream->getInputStream();
117 uno::Reference< xml::sax::XFastParser > xParser = xml::sax::FastParser::create(xContext);
118 xParser->setFastDocumentHandler( xFilter );
119 xParser->setTokenHandler( xTokenHandler );
121 xParser->registerNamespace(
"http://openoffice.org/2000/text", FastToken::NAMESPACE |
XML_NAMESPACE_TEXT );
122 xParser->registerNamespace(
"http://openoffice.org/2000/office", FastToken::NAMESPACE |
XML_NAMESPACE_OFFICE );
127 xParser->parseStream( aParserInput );
129 catch( xml::sax::SAXParseException& )
133 catch( xml::sax::SAXException& )
137 catch( io::IOException& )
145 catch( uno::Exception& )
158 { SvMacroItemId::SwStartInsGlossary,
"OnInsertStart" },
159 { SvMacroItemId::SwEndInsGlossary,
"OnInsertDone" },
160 { SvMacroItemId::NONE,
nullptr }
181 uno::Reference < io::XStream > xDocStream =
m_xRoot->openStreamElement(
182 "atevent.xml", embed::ElementModes::READ );
183 OSL_ENSURE(xDocStream.is(),
"Can't create stream");
184 if ( !xDocStream.is() )
187 uno::Reference<io::XInputStream> xInputStream = xDocStream->getInputStream();
190 xml::sax::InputSource aParserInput;
191 aParserInput.sSystemId =
m_aName;
192 aParserInput.aInputStream = xInputStream;
195 uno::Reference< uno::XComponentContext > xContext =
203 Sequence<Any> aFilterArguments{
Any(uno::Reference<XNameReplace>(pDescriptor)) };
206 OUString sFilterComponent = bOasis
207 ? OUString(
"com.sun.star.comp.Writer.XMLOasisAutotextEventsImporter")
208 : OUString(
"com.sun.star.comp.Writer.XMLAutotextEventsImporter");
209 uno::Reference< XInterface > xFilterInt =
210 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
211 sFilterComponent, aFilterArguments, xContext);
217 Reference<css::xml::sax::XFastParser> xFastParser(xFilterInt, UNO_QUERY);
218 Reference<css::xml::sax::XFastDocumentHandler> xFastDocHandler(xFilterInt, UNO_QUERY);
221 xFastParser->parseStream(aParserInput);
223 else if (xFastDocHandler)
225 Reference<css::xml::sax::XFastParser> xParser
226 = css::xml::sax::FastParser::create(xContext);
227 xParser->setFastDocumentHandler(xFastDocHandler);
228 xParser->parseStream(aParserInput);
232 Reference<css::xml::sax::XDocumentHandler> xDocHandler(xFilterInt, UNO_QUERY);
233 OSL_ENSURE( xDocHandler.is(),
"can't instantiate autotext-events filter");
234 if ( !xDocHandler.is() )
236 Reference<css::xml::sax::XParser> xParser = css::xml::sax::Parser::create(xContext);
237 xParser->setDocumentHandler(xDocHandler);
238 xParser->parseStream(aParserInput);
241 catch( xml::sax::SAXParseException& )
246 catch( xml::sax::SAXException& )
251 catch( io::IOException& )
258 pDescriptor->copyMacrosIntoTable(rMacroTable);
260 catch( uno::Exception& )
273 OUString aStreamName = aFolderName +
".xml";
278 bool bTextOnly =
true;
280 m_xRoot =
m_xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
281 if ( !
m_xRoot->hasByName( aStreamName ) || !
m_xRoot->isStreamElement( aStreamName ) )
284 aStreamName =
"content.xml";
287 uno::Reference < io::XStream > xContents =
m_xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
288 uno::Reference< uno::XComponentContext > xContext =
291 xml::sax::InputSource aParserInput;
292 aParserInput.sSystemId =
m_aName;
293 aParserInput.aInputStream = xContents->getInputStream();
296 uno::Reference< xml::sax::XFastDocumentHandler > xFilter =
new SwXMLTextBlockImport( xContext, rText, bTextOnly );
300 uno::Reference< xml::sax::XFastParser > xParser = xml::sax::FastParser::create(xContext);
301 xParser->setFastDocumentHandler( xFilter );
302 xParser->setTokenHandler( xTokenHandler );
304 xParser->registerNamespace(
"urn:oasis:names:tc:opendocument:xmlns:office:1.0", FastToken::NAMESPACE |
XML_NAMESPACE_OFFICE );
305 xParser->registerNamespace(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0", FastToken::NAMESPACE |
XML_NAMESPACE_TEXT );
310 xParser->parseStream( aParserInput );
312 catch( xml::sax::SAXParseException& )
316 catch( xml::sax::SAXException& )
320 catch( io::IOException& )
327 catch ( uno::Exception& )
329 SAL_WARN(
"sw",
"Tried to open non-existent folder or stream: " << aStreamName <<
" derived from autocorr of: " << OUString(rShort));
336 std::u16string_view rText,
const OUString& rPackageName )
346 OUString aStreamName = rPackageName +
".xml";
348 uno::Reference< uno::XComponentContext > xContext =
351 uno::Reference < xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
356 m_xRoot =
m_xBlkRoot->openStorageElement( rPackageName, embed::ElementModes::WRITE );
357 uno::Reference < io::XStream > xDocStream =
m_xRoot->openStreamElement( aStreamName,
358 embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
360 uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
361 xSet->setPropertyValue(
"MediaType",
Any(OUString(
"text/xml" )) );
362 uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
363 xWriter->setOutputStream(xOut);
367 xExp->exportDoc( rText );
369 uno::Reference < embed::XTransactedObject > xTrans(
m_xRoot, uno::UNO_QUERY );
375 uno::Reference < embed::XTransactedObject > xTmpTrans(
m_xBlkRoot, uno::UNO_QUERY );
376 if ( xTmpTrans.is() )
380 catch ( uno::Exception& )
410 uno::Reference< uno::XComponentContext > xContext =
413 xml::sax::InputSource aParserInput;
414 aParserInput.sSystemId = sDocName;
416 uno::Reference < io::XStream > xDocStream =
m_xBlkRoot->openStreamElement( sDocName, embed::ElementModes::READ );
417 aParserInput.aInputStream = xDocStream->getInputStream();
420 uno::Reference< xml::sax::XFastDocumentHandler > xFilter =
new SwXMLBlockListImport( xContext, *
this );
424 uno::Reference< xml::sax::XFastParser > xParser = xml::sax::FastParser::create(xContext);
425 xParser->setFastDocumentHandler( xFilter );
426 xParser->registerNamespace(
"http://openoffice.org/2001/block-list", FastToken::NAMESPACE |
XML_NAMESPACE_BLOCKLIST );
427 xParser->setTokenHandler( xTokenHandler );
430 xParser->parseStream( aParserInput );
432 catch ( uno::Exception& )
443 uno::Reference< uno::XComponentContext > xContext =
446 uno::Reference < xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
459 embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
461 uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
462 xSet->setPropertyValue(
"MediaType",
Any(OUString(
"text/xml" )) );
463 uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
464 xWriter->setOutputStream(xOut);
470 uno::Reference < embed::XTransactedObject > xTrans(
m_xBlkRoot, uno::UNO_QUERY );
474 catch ( uno::Exception& )
494 uno::Reference< uno::XComponentContext > xContext =
498 uno::Reference< lang::XComponent > xModelComp =
499 m_xDoc->GetDocShell()->GetModel();
500 OSL_ENSURE( xModelComp.is(),
"XMLWriter::Write: got no model" );
501 if( !xModelComp.is() )
515 uno::Reference < io::XStream > xDocStream =
m_xRoot->openStreamElement(
"atevent.xml",
516 embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
518 uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
519 xSet->setPropertyValue(
"MediaType",
Any(OUString(
"text/xml" )) );
520 uno::Reference < io::XOutputStream > xOutputStream = xDocStream->getOutputStream();
523 uno::Reference< xml::sax::XWriter > xSaxWriter =
524 xml::sax::Writer::create( xContext );
527 xSaxWriter->setOutputStream( xOutputStream );
530 uno::Reference<XNameAccess> xEvents =
534 Sequence<Any> aParams{
Any(xSaxWriter),
Any(xEvents) };
537 OUString sFilterComponent = bOasis
538 ? OUString(
"com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter")
539 : OUString(
"com.sun.star.comp.Writer.XMLAutotextEventsExporter");
540 uno::Reference< document::XExporter > xExporter(
541 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
542 sFilterComponent, aParams, xContext), UNO_QUERY);
543 OSL_ENSURE( xExporter.is(),
544 "can't instantiate export filter component" );
548 xExporter->setSourceDocument( xModelComp );
551 Sequence<beans::PropertyValue> aFilterProps( 0 );
552 uno::Reference < document::XFilter > xFilter( xExporter,
554 xFilter->filter( aFilterProps );
560 uno::Reference < embed::XTransactedObject > xTmpTrans(
m_xRoot, uno::UNO_QUERY );
561 if ( xTmpTrans.is() )
564 uno::Reference < embed::XTransactedObject > xTrans(
m_xBlkRoot, uno::UNO_QUERY );
570 catch ( uno::Exception& )
const struct SvEventDescription aAutotextEvents[]
constexpr OUStringLiteral XMLN_BLOCKLIST
void SetBlockMode(bool bSet)
sal_Int32 GetVersion() const
const OUString & GetBaseURL() const
OUString GetPackageName(sal_uInt16) const
Return longname for index.
sal_uInt16 GetIndex(const OUString &) const
Get count of Text Blocks.
rtl::Reference< SwDoc > m_xDoc
ErrCode Read(const Reader &)
virtual bool IsOnlyTextBlock(const OUString &rShort) const override
css::uno::Reference< css::embed::XStorage > m_xBlkRoot
void MakeBlockText(std::u16string_view rText)
static OUString GeneratePackageName(std::u16string_view rShort)
ErrCode GetBlockText(std::u16string_view rShort, OUString &rText)
tools::SvRef< SfxMedium > m_xMedium
virtual ErrCode GetMacroTable(sal_uInt16, SvxMacroTableDtor &rMacroTable) override
virtual ErrCode SetMacroTable(sal_uInt16 nIdx, const SvxMacroTableDtor &rMacroTable) override
ErrCode PutBlockText(const OUString &rShort, std::u16string_view rText, const OUString &rPackageName)
virtual void CloseFile() override
css::uno::Reference< css::embed::XStorage > m_xRoot
virtual ErrCode OpenFile(bool bReadOnly=true) override
#define TOOLS_WARN_EXCEPTION(area, stream)
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
#define ERR_SWG_WRITE_ERROR
#define ERR_SWG_READ_ERROR
constexpr sal_uInt16 XML_NAMESPACE_BLOCKLIST
constexpr sal_uInt16 XML_NAMESPACE_TEXT
constexpr sal_uInt16 XML_NAMESPACE_OFFICE