tdf#143399 DOCX import: fix lost endnotes or footnotes

in a document containing both of them.

Regression from commit 7dd8f8aace536a8e60e87e61ee1d90d61fba15eb
"tdf#120351 DOCX import: fix slow endnote import".

Change-Id: I0fe764f3b48dd2688afa4b7cf0ee6658737ef9fe
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119239
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/ooxmlexport/data/tdf143399.docx b/sw/qa/extras/ooxmlexport/data/tdf143399.docx
new file mode 100644
index 0000000..191e46f
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf143399.docx
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
index e383984..9944636 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
@@ -1160,6 +1160,23 @@
    assertXPathContent(pXml, "/w:endnotes/w:endnote[4]/w:p/w:r[2]/w:t[2]", "new line");
}

DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testTdf143399, "tdf143399.docx")
{
    xmlDocUniquePtr pXml = parseExport("word/footnotes.xml");
    CPPUNIT_ASSERT(pXml);
    // These were 0 (lost text content of documents both with footnotes and endnotes)
    assertXPath(pXml, "/w:footnotes/w:footnote[3]/w:p/w:r[3]/w:t", 1);
    assertXPathContent(pXml, "/w:footnotes/w:footnote[3]/w:p/w:r[3]/w:t", "Footnotes_graphic2");
    assertXPath(pXml, "/w:footnotes/w:footnote[4]/w:p/w:r[3]/w:t", 1);
    assertXPathContent(pXml, "/w:footnotes/w:footnote[4]/w:p/w:r[3]/w:t", "Footnotes_grahic");

    xmlDocUniquePtr pXml2 = parseExport("word/endnotes.xml");
    CPPUNIT_ASSERT(pXml);
    // This was 0 (lost text content of the run with endnoteRef)
    assertXPath(pXml2, "/w:endnotes/w:endnote[3]/w:p/w:r[3]/w:t", 1);
    assertXPathContent(pXml2, "/w:endnotes/w:endnote[3]/w:p/w:r[3]/w:t[1]", "Endnotes");
}

DECLARE_OOXMLEXPORT_TEST(testContSectBreakHeaderFooter, "cont-sect-break-header-footer.docx")
{
    // Load a document with a continuous section break on page 2.
diff --git a/writerfilter/source/ooxml/OOXMLDocumentImpl.cxx b/writerfilter/source/ooxml/OOXMLDocumentImpl.cxx
index be39704..00228fb 100644
--- a/writerfilter/source/ooxml/OOXMLDocumentImpl.cxx
+++ b/writerfilter/source/ooxml/OOXMLDocumentImpl.cxx
@@ -53,7 +53,8 @@
OOXMLDocumentImpl::OOXMLDocumentImpl(OOXMLStream::Pointer_t const & pStream, const uno::Reference<task::XStatusIndicator>& xStatusIndicator, bool bSkipImages, const uno::Sequence<beans::PropertyValue>& rDescriptor)
    : mpStream(pStream)
    , mxStatusIndicator(xStatusIndicator)
    , mpXNoteStream()
    , mpXFootnoteStream()
    , mpXEndnoteStream()
    , mnXNoteId(0)
    , mbIsSubstream(false)
    , mbSkipImages(bSkipImages)
@@ -271,8 +272,8 @@
                                        Id aType,
                                        const sal_Int32 nNoteId)
{
    if (!mpXNoteStream)
        mpXNoteStream = getXNoteStream(OOXMLStream::FOOTNOTES, nNoteId);
    if (!mpXFootnoteStream)
        mpXFootnoteStream = getXNoteStream(OOXMLStream::FOOTNOTES, nNoteId);

    Id nId;
    switch (aType)
@@ -286,15 +287,15 @@
        break;
    }

    resolveFastSubStreamWithId(rStream, mpXNoteStream, nId);
    resolveFastSubStreamWithId(rStream, mpXFootnoteStream, nId);
}

void OOXMLDocumentImpl::resolveEndnote(Stream & rStream,
                                       Id aType,
                                       const sal_Int32 nNoteId)
{
    if (!mpXNoteStream)
        mpXNoteStream = getXNoteStream(OOXMLStream::ENDNOTES, nNoteId);
    if (!mpXEndnoteStream)
       mpXEndnoteStream = getXNoteStream(OOXMLStream::ENDNOTES, nNoteId);

    Id nId;
    switch (aType)
@@ -308,7 +309,7 @@
        break;
    }

    resolveFastSubStreamWithId(rStream, mpXNoteStream, nId);
    resolveFastSubStreamWithId(rStream, mpXEndnoteStream, nId);
}

void OOXMLDocumentImpl::resolveCommentsExtendedStream(Stream& rStream)
diff --git a/writerfilter/source/ooxml/OOXMLDocumentImpl.hxx b/writerfilter/source/ooxml/OOXMLDocumentImpl.hxx
index 5572d0c..fc26f8a 100644
--- a/writerfilter/source/ooxml/OOXMLDocumentImpl.hxx
+++ b/writerfilter/source/ooxml/OOXMLDocumentImpl.hxx
@@ -37,7 +37,8 @@
{
    OOXMLStream::Pointer_t mpStream;
    css::uno::Reference<css::task::XStatusIndicator> mxStatusIndicator;
    writerfilter::Reference<Stream>::Pointer_t mpXNoteStream;
    writerfilter::Reference<Stream>::Pointer_t mpXFootnoteStream;
    writerfilter::Reference<Stream>::Pointer_t mpXEndnoteStream;
    sal_Int32 mnXNoteId;

    css::uno::Reference<css::frame::XModel> mxModel;