From 9456afaa5aa12926b1aa6b74ece1654d47e1c650 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Fri, 11 Aug 2017 09:11:22 +0200 Subject: EPUB export: rework to use context classes Before writerperfect::exp::XMLImport grows into a God object. Also, if I'm at it, recurse into text:h to include heading text. Change-Id: Iede56d00dbede40186b6484d0be92a2ac2e4768e Reviewed-on: https://gerrit.libreoffice.org/41005 Tested-by: Jenkins Reviewed-by: Miklos Vajna --- writerperfect/source/writer/exp/txtparai.cxx | 50 +++++++++++++++ writerperfect/source/writer/exp/txtparai.hxx | 38 +++++++++++ writerperfect/source/writer/exp/xmlictxt.cxx | 66 +++++++++++++++++++ writerperfect/source/writer/exp/xmlictxt.hxx | 56 +++++++++++++++++ writerperfect/source/writer/exp/xmlimp.cxx | 94 +++++++++++++++++++++++----- writerperfect/source/writer/exp/xmlimp.hxx | 14 ++++- writerperfect/source/writer/exp/xmltext.cxx | 36 +++++++++++ writerperfect/source/writer/exp/xmltext.hxx | 34 ++++++++++ 8 files changed, 370 insertions(+), 18 deletions(-) create mode 100644 writerperfect/source/writer/exp/txtparai.cxx create mode 100644 writerperfect/source/writer/exp/txtparai.hxx create mode 100644 writerperfect/source/writer/exp/xmlictxt.cxx create mode 100644 writerperfect/source/writer/exp/xmlictxt.hxx create mode 100644 writerperfect/source/writer/exp/xmltext.cxx create mode 100644 writerperfect/source/writer/exp/xmltext.hxx (limited to 'writerperfect/source/writer') diff --git a/writerperfect/source/writer/exp/txtparai.cxx b/writerperfect/source/writer/exp/txtparai.cxx new file mode 100644 index 000000000000..9507ceb759b9 --- /dev/null +++ b/writerperfect/source/writer/exp/txtparai.cxx @@ -0,0 +1,50 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "txtparai.hxx" + +#include "xmlimp.hxx" + +using namespace com::sun::star; + +namespace writerperfect +{ +namespace exp +{ + +XMLParaContext::XMLParaContext(XMLImport &rImport) + : XMLImportContext(rImport) +{ +} + +XMLImportContext *XMLParaContext::CreateChildContext(const OUString &/*rName*/, const css::uno::Reference &/*xAttribs*/) +{ + return nullptr; +} + +void XMLParaContext::startElement(const OUString &/*rName*/, const css::uno::Reference &/*xAttribs*/) +{ + mrImport.GetGenerator().openParagraph(librevenge::RVNGPropertyList()); +} + +void XMLParaContext::endElement(const OUString &/*rName*/) +{ + mrImport.GetGenerator().closeParagraph(); +} + +void XMLParaContext::characters(const OUString &rChars) +{ + OString sCharU8 = OUStringToOString(rChars, RTL_TEXTENCODING_UTF8); + mrImport.GetGenerator().insertText(librevenge::RVNGString(sCharU8.getStr())); +} + +} // namespace exp +} // namespace writerperfect + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/writerperfect/source/writer/exp/txtparai.hxx b/writerperfect/source/writer/exp/txtparai.hxx new file mode 100644 index 000000000000..6c9f23b6cf37 --- /dev/null +++ b/writerperfect/source/writer/exp/txtparai.hxx @@ -0,0 +1,38 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_TXTPARAI_HXX +#define INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_TXTPARAI_HXX + +#include "xmlictxt.hxx" + +namespace writerperfect +{ +namespace exp +{ + +/// Handler for /. +class XMLParaContext : public XMLImportContext +{ +public: + XMLParaContext(XMLImport &rImport); + + XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) override; + + void SAL_CALL startElement(const OUString &rName, const css::uno::Reference &xAttribs) override; + void SAL_CALL endElement(const OUString &rName) override; + void SAL_CALL characters(const OUString &rChars) override; +}; + +} // namespace exp +} // namespace writerperfect + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/writerperfect/source/writer/exp/xmlictxt.cxx b/writerperfect/source/writer/exp/xmlictxt.cxx new file mode 100644 index 000000000000..5f5a731493c8 --- /dev/null +++ b/writerperfect/source/writer/exp/xmlictxt.cxx @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "xmlictxt.hxx" + +#include "xmlimp.hxx" + +using namespace com::sun::star; + +namespace writerperfect +{ +namespace exp +{ + +XMLImportContext::XMLImportContext(XMLImport &rImport) + : mrImport(rImport) +{ +} + +XMLImportContext *XMLImportContext::CreateChildContext(const OUString &rName, const css::uno::Reference &xAttribs) +{ + return mrImport.CreateContext(rName, xAttribs); +} + +void XMLImportContext::startDocument() +{ +} + +void XMLImportContext::endDocument() +{ +} + +void XMLImportContext::startElement(const OUString &/*rName*/, const css::uno::Reference &/*xAttribs*/) +{ +} + +void XMLImportContext::endElement(const OUString &/*rName*/) +{ +} + +void XMLImportContext::characters(const OUString &/*rChars*/) +{ +} + +void XMLImportContext::ignorableWhitespace(const OUString &/*rWhitespaces*/) +{ +} + +void XMLImportContext::processingInstruction(const OUString &/*rTarget*/, const OUString &/*rData*/) +{ +} + +void XMLImportContext::setDocumentLocator(const css::uno::Reference &/*xLocator*/) +{ +} + +} // namespace exp +} // namespace writerperfect + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/writerperfect/source/writer/exp/xmlictxt.hxx b/writerperfect/source/writer/exp/xmlictxt.hxx new file mode 100644 index 000000000000..0076852f25d3 --- /dev/null +++ b/writerperfect/source/writer/exp/xmlictxt.hxx @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_XMLICTXT_HXX +#define INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_XMLICTXT_HXX + +#include + +#include + +#include + +namespace writerperfect +{ +namespace exp +{ + +class XMLImport; + +/// Base class for a handler of a single XML element during ODF -> librevenge conversion. +class XMLImportContext : public cppu::WeakImplHelper + < + css::xml::sax::XDocumentHandler + > +{ +public: + XMLImportContext(XMLImport &rImport); + + virtual XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference &xAttribs); + + // XDocumentHandler + void SAL_CALL startDocument() override; + void SAL_CALL endDocument() override; + void SAL_CALL startElement(const OUString &rName, const css::uno::Reference &xAttribs) override; + void SAL_CALL endElement(const OUString &rName) override; + void SAL_CALL characters(const OUString &rChars) override; + void SAL_CALL ignorableWhitespace(const OUString &rWhitespaces) override; + void SAL_CALL processingInstruction(const OUString &rTarget, const OUString &rData) override; + void SAL_CALL setDocumentLocator(const css::uno::Reference &xLocator) override; + +protected: + XMLImport &mrImport; +}; + +} // namespace exp +} // namespace writerperfect + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/writerperfect/source/writer/exp/xmlimp.cxx b/writerperfect/source/writer/exp/xmlimp.cxx index 21c4809ffc57..b975bbcceebc 100644 --- a/writerperfect/source/writer/exp/xmlimp.cxx +++ b/writerperfect/source/writer/exp/xmlimp.cxx @@ -9,6 +9,9 @@ #include "xmlimp.hxx" +#include "xmlictxt.hxx" +#include "xmltext.hxx" + using namespace com::sun::star; namespace writerperfect @@ -16,11 +19,65 @@ namespace writerperfect namespace exp { +/// Handler for . +class XMLBodyContext : public XMLImportContext +{ +public: + XMLBodyContext(XMLImport &rImport); + + XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) override; +}; + +XMLBodyContext::XMLBodyContext(XMLImport &rImport) + : XMLImportContext(rImport) +{ +} + +XMLImportContext *XMLBodyContext::CreateChildContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) +{ + if (rName == "office:text") + return new XMLBodyContentContext(mrImport); + return nullptr; +} + +/// Handler for . +class XMLOfficeDocContext : public XMLImportContext +{ +public: + XMLOfficeDocContext(XMLImport &rImport); + + XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) override; +}; + +XMLOfficeDocContext::XMLOfficeDocContext(XMLImport &rImport) + : XMLImportContext(rImport) +{ +} + +XMLImportContext *XMLOfficeDocContext::CreateChildContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) +{ + if (rName == "office:body") + return new XMLBodyContext(mrImport); + return nullptr; +} + XMLImport::XMLImport(librevenge::RVNGTextInterface &rGenerator) : mrGenerator(rGenerator) { } +XMLImportContext *XMLImport::CreateContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) +{ + if (rName == "office:document") + return new XMLOfficeDocContext(*this); + return nullptr; +} + +librevenge::RVNGTextInterface &XMLImport::GetGenerator() const +{ + return mrGenerator; +} + void XMLImport::startDocument() { mrGenerator.startDocument(librevenge::RVNGPropertyList()); @@ -31,31 +88,38 @@ void XMLImport::endDocument() mrGenerator.endDocument(); } -void XMLImport::startElement(const OUString &rName, const css::uno::Reference &/*xAttribs*/) +void XMLImport::startElement(const OUString &rName, const css::uno::Reference &xAttribs) { - if (rName == "text:p") + rtl::Reference xContext; + if (!maContexts.empty()) { - mrGenerator.openParagraph(librevenge::RVNGPropertyList()); - mbParagraphOpened = true; + if (maContexts.top().is()) + xContext = maContexts.top()->CreateChildContext(rName, xAttribs); } + else + xContext = CreateContext(rName, xAttribs); + + if (xContext.is()) + xContext->startElement(rName, xAttribs); + + maContexts.push(xContext); } void XMLImport::endElement(const OUString &rName) { - if (rName == "text:p") - { - mrGenerator.closeParagraph(); - mbParagraphOpened = false; - } + if (maContexts.empty()) + return; + + if (maContexts.top().is()) + maContexts.top()->endElement(rName); + + maContexts.pop(); } void XMLImport::characters(const OUString &rChars) { - if (mbParagraphOpened) - { - OString sCharU8 = OUStringToOString(rChars, RTL_TEXTENCODING_UTF8); - mrGenerator.insertText(librevenge::RVNGString(sCharU8.getStr())); - } + if (maContexts.top().is()) + maContexts.top()->characters(rChars); } void XMLImport::ignorableWhitespace(const OUString &/*rWhitespaces*/) @@ -64,12 +128,10 @@ void XMLImport::ignorableWhitespace(const OUString &/*rWhitespaces*/) void XMLImport::processingInstruction(const OUString &/*rTarget*/, const OUString &/*rData*/) { - SAL_WARN("writerperfect", "XMLImport::processingInstruction: implement me"); } void XMLImport::setDocumentLocator(const css::uno::Reference &/*xLocator*/) { - SAL_WARN("writerperfect", "XMLImport::setDocumentLocator: implement me"); } } // namespace exp diff --git a/writerperfect/source/writer/exp/xmlimp.hxx b/writerperfect/source/writer/exp/xmlimp.hxx index 2b1c743fcf03..6e6cc8c82491 100644 --- a/writerperfect/source/writer/exp/xmlimp.hxx +++ b/writerperfect/source/writer/exp/xmlimp.hxx @@ -10,17 +10,23 @@ #ifndef INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_XMLIMP_HXX #define INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_XMLIMP_HXX -#include +#include +#include #include #include +#include +#include + namespace writerperfect { namespace exp { +class XMLImportContext; + /// ODT export feeds this class to make librevenge calls. class XMLImport : public cppu::WeakImplHelper < @@ -28,11 +34,15 @@ class XMLImport : public cppu::WeakImplHelper > { librevenge::RVNGTextInterface &mrGenerator; - bool mbParagraphOpened = false; + std::stack< rtl::Reference > maContexts; public: XMLImport(librevenge::RVNGTextInterface &rGenerator); + XMLImportContext *CreateContext(const OUString &rName, const css::uno::Reference &xAttribs); + + librevenge::RVNGTextInterface &GetGenerator() const; + // XDocumentHandler void SAL_CALL startDocument() override; void SAL_CALL endDocument() override; diff --git a/writerperfect/source/writer/exp/xmltext.cxx b/writerperfect/source/writer/exp/xmltext.cxx new file mode 100644 index 000000000000..5560e7523a54 --- /dev/null +++ b/writerperfect/source/writer/exp/xmltext.cxx @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "xmltext.hxx" + +#include "txtparai.hxx" + +using namespace com::sun::star; + +namespace writerperfect +{ +namespace exp +{ + +XMLBodyContentContext::XMLBodyContentContext(XMLImport &rImport) + : XMLImportContext(rImport) +{ +} + +XMLImportContext *XMLBodyContentContext::CreateChildContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) +{ + if (rName == "text:p" || rName == "text:h") + return new XMLParaContext(mrImport); + return nullptr; +} + +} // namespace exp +} // namespace writerperfect + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/writerperfect/source/writer/exp/xmltext.hxx b/writerperfect/source/writer/exp/xmltext.hxx new file mode 100644 index 000000000000..aa82aeb00122 --- /dev/null +++ b/writerperfect/source/writer/exp/xmltext.hxx @@ -0,0 +1,34 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_XMLTEXT_HXX +#define INCLUDED_WRITERPERFECT_SOURCE_WRITER_EXP_XMLTEXT_HXX + +#include "xmlictxt.hxx" + +namespace writerperfect +{ +namespace exp +{ + +/// Handler for . +class XMLBodyContentContext : public XMLImportContext +{ +public: + XMLBodyContentContext(XMLImport &rImport); + + XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference &/*xAttribs*/) override; +}; + +} // namespace exp +} // namespace writerperfect + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3