diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2021-06-11 12:23:45 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2021-06-19 11:12:44 +0200 |
commit | 686c35a3c37674d08c1f8585a621143c959b3f29 (patch) | |
tree | d5add6b678d48c825d2a0a2cc3985b75cb8aae6d | |
parent | e6cb7f9ab25bca32dc85aef8ff127e070cd109b6 (diff) |
indexing: indexing paragraph text with the ModelTraverser
This adds the basic paragraph text export for indexing, which uses
the new ModelTraverser, which is inspired by AccessibilityCheck
(and in the future it will be changed to use the ModelTraverser
too, when it matures). ModelTraverser implements traversing through
the model and executes a handler for a element of a model (which
can be various things).
IndexingExport class implements indexing export that uses the
ModelTraverser to write into the indexing xml document, that uses
the tools::XmlWriter.
Change-Id: I7a6a5de332534270fe894a881131e5eb5ea9d881
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117356
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | sw/CppunitTest_sw_indexingexport.mk | 1 | ||||
-rw-r--r-- | sw/Library_sw.mk | 2 | ||||
-rw-r--r-- | sw/qa/extras/indexing/IndexingExportTest.cxx | 29 | ||||
-rw-r--r-- | sw/source/core/inc/ModelTraverser.hxx | 49 | ||||
-rw-r--r-- | sw/source/core/model/ModelTraverser.cxx | 39 | ||||
-rw-r--r-- | sw/source/filter/inc/IndexingExport.hxx | 32 | ||||
-rw-r--r-- | sw/source/filter/indexing/IndexingExport.cxx | 71 |
7 files changed, 223 insertions, 0 deletions
diff --git a/sw/CppunitTest_sw_indexingexport.mk b/sw/CppunitTest_sw_indexingexport.mk index 06b2b448f32a..a3884fee8fb6 100644 --- a/sw/CppunitTest_sw_indexingexport.mk +++ b/sw/CppunitTest_sw_indexingexport.mk @@ -46,6 +46,7 @@ $(eval $(call gb_CppunitTest_set_include,sw_indexingexport,\ -I$(SRCDIR)/sw/inc \ -I$(SRCDIR)/sw/source/core/inc \ -I$(SRCDIR)/sw/source/uibase/inc \ + -I$(SRCDIR)/sw/source/filter/inc \ -I$(SRCDIR)/sw/qa/inc \ $$(INCLUDE) \ )) diff --git a/sw/Library_sw.mk b/sw/Library_sw.mk index 3ba73c8ae9af..d85a2f563ba7 100644 --- a/sw/Library_sw.mk +++ b/sw/Library_sw.mk @@ -352,6 +352,7 @@ $(eval $(call gb_Library_add_exception_objects,sw,\ sw/source/core/layout/unusedf \ sw/source/core/layout/virtoutp \ sw/source/core/layout/wsfrm \ + sw/source/core/model/ModelTraverser \ sw/source/core/objectpositioning/anchoredobjectposition \ sw/source/core/objectpositioning/ascharanchoredobjectposition \ sw/source/core/objectpositioning/environmentofanchoredobject \ @@ -569,6 +570,7 @@ $(eval $(call gb_Library_add_exception_objects,sw,\ sw/source/filter/xml/xmltext \ sw/source/filter/xml/xmltexte \ sw/source/filter/xml/xmltexti \ + sw/source/filter/indexing/IndexingExport \ sw/source/uibase/app/appenv \ sw/source/uibase/app/apphdl \ sw/source/uibase/app/applab \ diff --git a/sw/qa/extras/indexing/IndexingExportTest.cxx b/sw/qa/extras/indexing/IndexingExportTest.cxx index eed303c3e97b..9d943e3b9593 100644 --- a/sw/qa/extras/indexing/IndexingExportTest.cxx +++ b/sw/qa/extras/indexing/IndexingExportTest.cxx @@ -13,6 +13,8 @@ #include <docsh.hxx> #include <unotxdoc.hxx> +#include <IndexingExport.hxx> + namespace { constexpr OUStringLiteral DATA_DIRECTORY = u"sw/qa/extras/indexing/data/"; @@ -47,6 +49,33 @@ void IndexingExportTest::testIndexingExport() { SwDoc* pDoc = createDoc("IndexingExport_VariousParagraphs.odt"); CPPUNIT_ASSERT(pDoc); + + SvMemoryStream aMemoryStream; + sw::IndexingExport aIndexingExport(aMemoryStream, pDoc); + aIndexingExport.runExport(); + aMemoryStream.Seek(0); + + xmlDocUniquePtr pXmlDoc = parseXmlStream(&aMemoryStream); + CPPUNIT_ASSERT(pXmlDoc); + + assertXPath(pXmlDoc, "/indexing"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[1]", "Title"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[2]", "Heading 1"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[3]", "Heading 2"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[4]", "Paragraph 1"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[5]", "Paragraph 2"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[6]", "Bullet 1"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[7]", "Bullet 2"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[8]", "Bullet 3"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[9]", "Paragraph 3"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[10]", "Paragraph 4"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[11]", "List 1"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[12]", "List 2"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[13]", "List 3"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[14]", "Left"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[15]", "Center"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[16]", "Right"); + assertXPathContent(pXmlDoc, "/indexing/paragraph[17]", "Bold Italic Underline Strikeout"); } CPPUNIT_TEST_SUITE_REGISTRATION(IndexingExportTest); diff --git a/sw/source/core/inc/ModelTraverser.hxx b/sw/source/core/inc/ModelTraverser.hxx new file mode 100644 index 000000000000..ab38032773df --- /dev/null +++ b/sw/source/core/inc/ModelTraverser.hxx @@ -0,0 +1,49 @@ +/* -*- 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/. + * + */ + +#pragma once + +#include <doc.hxx> + +class SwNode; + +namespace sw +{ +class SW_DLLPUBLIC ModelTraverseHandler +{ +public: + virtual ~ModelTraverseHandler() {} + + virtual void handleNode(SwNode* pNode) = 0; +}; + +class SW_DLLPUBLIC ModelTraverser +{ +private: + std::vector<std::shared_ptr<ModelTraverseHandler>> mpNodeHandler; + SwDoc* m_pDoc; + +public: + ModelTraverser(SwDoc* pDoc) + : m_pDoc(pDoc) + { + } + + void traverse(); + + void addNodeHandler(std::shared_ptr<ModelTraverseHandler> pHandler) + { + mpNodeHandler.push_back(pHandler); + } +}; + +} // end sw namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/model/ModelTraverser.cxx b/sw/source/core/model/ModelTraverser.cxx new file mode 100644 index 000000000000..f46e173eca9f --- /dev/null +++ b/sw/source/core/model/ModelTraverser.cxx @@ -0,0 +1,39 @@ +/* -*- 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 <ModelTraverser.hxx> +#include <node.hxx> +#include <ndarr.hxx> + +namespace sw +{ +void ModelTraverser::traverse() +{ + if (m_pDoc == nullptr) + return; + + auto const& pNodes = m_pDoc->GetNodes(); + SwNode* pNode = nullptr; + for (sal_uLong n = 0; n < pNodes.Count(); ++n) + { + pNode = pNodes[n]; + if (pNode) + { + for (auto& pNodeHandler : mpNodeHandler) + { + pNodeHandler->handleNode(pNode); + } + } + } +} + +} // end sw namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/filter/inc/IndexingExport.hxx b/sw/source/filter/inc/IndexingExport.hxx new file mode 100644 index 000000000000..782ddf7f5f0f --- /dev/null +++ b/sw/source/filter/inc/IndexingExport.hxx @@ -0,0 +1,32 @@ +/* -*- 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/. + * + */ + +#pragma once + +#include <ModelTraverser.hxx> +#include <tools/XmlWriter.hxx> + +namespace sw +{ +class SW_DLLPUBLIC IndexingExport +{ +private: + ModelTraverser m_aModelTraverser; + tools::XmlWriter m_aXmlWriter; + +public: + IndexingExport(SvStream& rStream, SwDoc* pDoc); + + bool runExport(); +}; + +} // end sw namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/filter/indexing/IndexingExport.cxx b/sw/source/filter/indexing/IndexingExport.cxx new file mode 100644 index 000000000000..8df7d56f7ff6 --- /dev/null +++ b/sw/source/filter/indexing/IndexingExport.cxx @@ -0,0 +1,71 @@ +/* -*- 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 <IndexingExport.hxx> + +#include <ndtxt.hxx> + +namespace sw +{ +namespace +{ +class IndexingNodeHandler : public ModelTraverseHandler +{ +private: + tools::XmlWriter& m_rXmlWriter; + +public: + IndexingNodeHandler(tools::XmlWriter& rXmlWriter) + : m_rXmlWriter(rXmlWriter) + { + } + + void handleNode(SwNode* pNode) override + { + if (!pNode->IsTextNode()) + return; + + SwTextNode* pTextNode = pNode->GetTextNode(); + const OUString& rString + = pTextNode->GetText().replaceAll(OUStringChar(CH_TXTATR_BREAKWORD), ""); + m_rXmlWriter.startElement("paragraph"); + m_rXmlWriter.attribute("index", pTextNode->GetIndex()); + m_rXmlWriter.content(rString); + m_rXmlWriter.endElement(); + } +}; + +} // end anonymous namespace + +IndexingExport::IndexingExport(SvStream& rStream, SwDoc* pDoc) + : m_aModelTraverser(pDoc) + , m_aXmlWriter(&rStream) +{ +} + +bool IndexingExport::runExport() +{ + bool bResult = m_aXmlWriter.startDocument(2); + if (!bResult) + return false; + + m_aXmlWriter.startElement("indexing"); + m_aModelTraverser.addNodeHandler(std::make_shared<IndexingNodeHandler>(m_aXmlWriter)); + m_aModelTraverser.traverse(); + m_aXmlWriter.endElement(); + + m_aXmlWriter.endDocument(); + + return true; +} + +} // end sw namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |