summaryrefslogtreecommitdiff
path: root/writerperfect/source/writer
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.co.uk>2017-08-11 09:11:22 +0200
committerMiklos Vajna <vmiklos@collabora.co.uk>2017-08-11 10:41:21 +0200
commit9456afaa5aa12926b1aa6b74ece1654d47e1c650 (patch)
treef513f82cfbb258d7585f436f412c8106b6de44bf /writerperfect/source/writer
parent7d30d029f4f95eed91c814469d700445ed6b019b (diff)
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 <ci@libreoffice.org> Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Diffstat (limited to 'writerperfect/source/writer')
-rw-r--r--writerperfect/source/writer/exp/txtparai.cxx50
-rw-r--r--writerperfect/source/writer/exp/txtparai.hxx38
-rw-r--r--writerperfect/source/writer/exp/xmlictxt.cxx66
-rw-r--r--writerperfect/source/writer/exp/xmlictxt.hxx56
-rw-r--r--writerperfect/source/writer/exp/xmlimp.cxx94
-rw-r--r--writerperfect/source/writer/exp/xmlimp.hxx14
-rw-r--r--writerperfect/source/writer/exp/xmltext.cxx36
-rw-r--r--writerperfect/source/writer/exp/xmltext.hxx34
8 files changed, 370 insertions, 18 deletions
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<css::xml::sax::XAttributeList> &/*xAttribs*/)
+{
+ return nullptr;
+}
+
+void XMLParaContext::startElement(const OUString &/*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList> &/*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 <text:p>/<text:h>.
+class XMLParaContext : public XMLImportContext
+{
+public:
+ XMLParaContext(XMLImport &rImport);
+
+ XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &/*xAttribs*/) override;
+
+ void SAL_CALL startElement(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &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<css::xml::sax::XAttributeList> &xAttribs)
+{
+ return mrImport.CreateContext(rName, xAttribs);
+}
+
+void XMLImportContext::startDocument()
+{
+}
+
+void XMLImportContext::endDocument()
+{
+}
+
+void XMLImportContext::startElement(const OUString &/*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList> &/*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<css::xml::sax::XLocator> &/*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 <cppuhelper/implbase.hxx>
+
+#include <librevenge/librevenge.h>
+
+#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
+
+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<css::xml::sax::XAttributeList> &xAttribs);
+
+ // XDocumentHandler
+ void SAL_CALL startDocument() override;
+ void SAL_CALL endDocument() override;
+ void SAL_CALL startElement(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &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<css::xml::sax::XLocator> &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 <office:body>.
+class XMLBodyContext : public XMLImportContext
+{
+public:
+ XMLBodyContext(XMLImport &rImport);
+
+ XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &/*xAttribs*/) override;
+};
+
+XMLBodyContext::XMLBodyContext(XMLImport &rImport)
+ : XMLImportContext(rImport)
+{
+}
+
+XMLImportContext *XMLBodyContext::CreateChildContext(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &/*xAttribs*/)
+{
+ if (rName == "office:text")
+ return new XMLBodyContentContext(mrImport);
+ return nullptr;
+}
+
+/// Handler for <office:document>.
+class XMLOfficeDocContext : public XMLImportContext
+{
+public:
+ XMLOfficeDocContext(XMLImport &rImport);
+
+ XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &/*xAttribs*/) override;
+};
+
+XMLOfficeDocContext::XMLOfficeDocContext(XMLImport &rImport)
+ : XMLImportContext(rImport)
+{
+}
+
+XMLImportContext *XMLOfficeDocContext::CreateChildContext(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &/*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<css::xml::sax::XAttributeList> &/*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<css::xml::sax::XAttributeList> &/*xAttribs*/)
+void XMLImport::startElement(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &xAttribs)
{
- if (rName == "text:p")
+ rtl::Reference<XMLImportContext> 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<css::xml::sax::XLocator> &/*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 <cppuhelper/implbase.hxx>
+#include <memory>
+#include <stack>
#include <librevenge/librevenge.h>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
+#include <cppuhelper/implbase.hxx>
+#include <rtl/ref.hxx>
+
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<XMLImportContext> > maContexts;
public:
XMLImport(librevenge::RVNGTextInterface &rGenerator);
+ XMLImportContext *CreateContext(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &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<css::xml::sax::XAttributeList> &/*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 <office:text>.
+class XMLBodyContentContext : public XMLImportContext
+{
+public:
+ XMLBodyContentContext(XMLImport &rImport);
+
+ XMLImportContext *CreateChildContext(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &/*xAttribs*/) override;
+};
+
+} // namespace exp
+} // namespace writerperfect
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */