[API CHANGE] Merge sfx2::XmlDump into css::qa::XDumper

Both sfx2::XmlDump and css::qa::XDumper were (only) used to dump various data
from ChartModel and ChartView instances in debugging (e.g., F12 and Shift-F12 in
an SW_DEBUG=1 Writer) and testing scenarios.  The problem with XmlDump was that
it was used as the target of a dynamic_cast from a UNO type, which is dangerous
(see the upcoming commit introducing loplugin:unocast for details).  One fix
would have been to replace that dynamic_cast with a use of XUnoTunnel, and make
ChartModel and ChartView implement that.  But those classes already implement
XDumper, so it looks more natural to use XDumper for that purpose too.

However, the information that was obtained via XDumper was rather different from
the information that was obtained via XmlDump.  So make an incompatible change
to the (unpublished) XDumper, adding a kind parameter distinguishing the
original behavior of XDumper (kind == "shapes") from the original behavior of
XmlDump (kind == "").

Change-Id: Ia7379bedfc45ab62a298fdc2f030cebaf21c4885
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144145
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <[email protected]>
diff --git a/chart2/inc/ChartModel.hxx b/chart2/inc/ChartModel.hxx
index b877e47..8c876d8 100644
--- a/chart2/inc/ChartModel.hxx
+++ b/chart2/inc/ChartModel.hxx
@@ -52,10 +52,11 @@
#include <cppuhelper/implbase.hxx>
#include <comphelper/interfacecontainer2.hxx>
#include <vcl/GraphicObject.hxx>
#include <sfx2/xmldump.hxx>

#include <memory>

typedef struct _xmlTextWriter* xmlTextWriterPtr;

namespace com::sun::star::awt { class XRequestCallback; }
namespace com::sun::star::chart2::data { class XDataProvider; }
namespace com::sun::star::document { class XFilter; }
@@ -113,7 +114,7 @@
class ChartView;

class OOO_DLLPUBLIC_CHARTTOOLS SAL_LOPLUGIN_ANNOTATE("crosscast") ChartModel final :
    public impl::ChartModel_Base, public sfx2::XmlDump
    public impl::ChartModel_Base
{

private:
@@ -452,7 +453,7 @@
    virtual void SAL_CALL update() override;

    // XDumper
    virtual OUString SAL_CALL dump() override;
    virtual OUString SAL_CALL dump(OUString const & kind) override;

    // normal methods
    css::uno::Reference< css::util::XNumberFormatsSupplier > const &
@@ -474,10 +475,9 @@

    const rtl::Reference< ::chart::ChartTypeManager > & getTypeManager() const { return m_xChartTypeManager; }

    /// See sfx2::XmlDump::dumpAsXml().
    void dumpAsXml(xmlTextWriterPtr pWriter) const override;

private:
    void dumpAsXml(xmlTextWriterPtr pWriter) const;

    sal_Int32 mnStart;
    sal_Int32 mnEnd;
};
diff --git a/chart2/inc/ChartView.hxx b/chart2/inc/ChartView.hxx
index 5c7e70e..5f066587 100644
--- a/chart2/inc/ChartView.hxx
+++ b/chart2/inc/ChartView.hxx
@@ -43,7 +43,6 @@
#include <memory>

#include <vcl/timer.hxx>
#include <sfx2/xmldump.hxx>

namespace com::sun::star::drawing { class XDrawPage; }
namespace com::sun::star::drawing { class XShapes; }
@@ -97,7 +96,6 @@
        >
        , public ExplicitValueProvider
        , private SfxListener
        , public sfx2::XmlDump
{
private:
    void init();
@@ -180,14 +178,13 @@
    virtual ::sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< ::sal_Int8 >& aIdentifier ) override;

    // XDumper
    virtual OUString SAL_CALL dump() override;
    virtual OUString SAL_CALL dump(OUString const & kind) override;

    void setViewDirty();

    css::uno::Reference<css::uno::XComponentContext> const& getComponentContext() { return m_xCC;}

    /// See sfx2::XmlDump::dumpAsXml().
    void dumpAsXml(xmlTextWriterPtr pWriter) const override;
    void dumpAsXml(xmlTextWriterPtr pWriter) const;

private: //methods
    void createShapes();
diff --git a/chart2/qa/extras/xshape/chart2xshape.cxx b/chart2/qa/extras/xshape/chart2xshape.cxx
index 23e3f93..92605a7 100644
--- a/chart2/qa/extras/xshape/chart2xshape.cxx
+++ b/chart2/qa/extras/xshape/chart2xshape.cxx
@@ -63,6 +63,11 @@

namespace
{
OUString getShapeDump(css::uno::Reference<css::chart::XChartDocument> const& doc)
{
    return css::uno::Reference<css::qa::XDumper>(doc, css::uno::UNO_QUERY_THROW)->dump("shapes");
}

bool checkDumpAgainstFile(std::u16string_view rDump, std::u16string_view aFilePath,
                          char const* toleranceFile)
{
@@ -80,8 +85,7 @@
{
    uno::Reference<chart::XChartDocument> xChartDoc(getChartCompFromSheet(0, 0, mxComponent),
                                                    UNO_QUERY_THROW);
    uno::Reference<qa::XDumper> xDumper(xChartDoc, UNO_QUERY_THROW);
    return xDumper->dump();
    return getShapeDump(xChartDoc);
}

xmlDocUniquePtr Chart2XShapeTest::getXShapeDumpXmlDoc()
@@ -132,8 +136,7 @@
    // - Attr: sizeX
    loadFromURL(u"pptx/tdf149204.pptx");
    uno::Reference<chart::XChartDocument> xChartDoc = getChartDocFromDrawImpress(0, 0);
    uno::Reference<qa::XDumper> xDumper(xChartDoc, UNO_QUERY_THROW);
    compareAgainstReference(xDumper->dump(), u"tdf149204.xml");
    compareAgainstReference(getShapeDump(xChartDoc), u"tdf149204.xml");
}

void Chart2XShapeTest::testTdf151424()
@@ -251,8 +254,7 @@
{
    loadFromURL(u"pptx/tdf88154_LabelRotatedLayout.pptx");
    uno::Reference<chart::XChartDocument> xChartDoc = getChartDocFromDrawImpress(0, 5);
    uno::Reference<qa::XDumper> xDumper(xChartDoc, UNO_QUERY_THROW);
    OUString rDump = xDumper->dump();
    OUString rDump = getShapeDump(xChartDoc);
    OString aXmlDump = OUStringToOString(rDump, RTL_TEXTENCODING_UTF8);
    xmlDocUniquePtr pXmlDoc(xmlParseDoc(reinterpret_cast<const xmlChar*>(aXmlDump.getStr())));

diff --git a/chart2/source/controller/main/ChartController_Window.cxx b/chart2/source/controller/main/ChartController_Window.cxx
index 018419b..70ab543 100644
--- a/chart2/source/controller/main/ChartController_Window.cxx
+++ b/chart2/source/controller/main/ChartController_Window.cxx
@@ -1563,7 +1563,7 @@
        rtl::Reference< ChartModel > xChartModel = getChartModel();
        if(xChartModel.is())
        {
            OUString aDump = xChartModel->dump();
            OUString aDump = xChartModel->dump("shapes");
            SAL_WARN("chart2", aDump);
        }
    }
diff --git a/chart2/source/inc/dumpxmltostring.hxx b/chart2/source/inc/dumpxmltostring.hxx
new file mode 100644
index 0000000..d03f1ca
--- /dev/null
+++ b/chart2/source/inc/dumpxmltostring.hxx
@@ -0,0 +1,41 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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 <sal/config.h>

#include <libxml/tree.h>
#include <libxml/xmlwriter.h>

#include <new>

namespace chart
{
template <typename F> OUString dumpXmlToString(F f)
{
    auto const buf = xmlBufferCreate();
    if (buf == nullptr)
    {
        throw std::bad_alloc();
    }
    auto const writer = xmlNewTextWriterMemory(buf, 0);
    if (writer == nullptr)
    {
        throw std::bad_alloc();
    }
    f(writer);
    xmlFreeTextWriter(writer);
    OString s(reinterpret_cast<char const*>(xmlBufferContent(buf)), xmlBufferLength(buf));
    xmlBufferFree(buf);
    return OStringToOUString(s, RTL_TEXTENCODING_ISO_8859_1); //TODO
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/chart2/source/model/main/ChartModel.cxx b/chart2/source/model/main/ChartModel.cxx
index 781fc7c..0564f83 100644
--- a/chart2/source/model/main/ChartModel.cxx
+++ b/chart2/source/model/main/ChartModel.cxx
@@ -36,6 +36,7 @@
#include <PopupRequest.hxx>
#include <ModifyListenerHelper.hxx>
#include <Diagram.hxx>
#include <dumpxmltostring.hxx>

#include <com/sun/star/chart/ChartDataRowSource.hpp>
#include <com/sun/star/chart2/data/XPivotTableDataProvider.hpp>
@@ -1257,12 +1258,17 @@
}

//XDumper
OUString SAL_CALL ChartModel::dump()
OUString SAL_CALL ChartModel::dump(OUString const & kind)
{
    if (kind.isEmpty()) {
        return dumpXmlToString([this](auto writer) { return dumpAsXml(writer); });
    }

    // kind == "shapes":
    uno::Reference< qa::XDumper > xDumper(
            createInstance( CHART_VIEW_SERVICE_NAME ), uno::UNO_QUERY );
    if (xDumper.is())
        return xDumper->dump();
        return xDumper->dump(kind);

    return OUString();
}
diff --git a/chart2/source/view/main/ChartView.cxx b/chart2/source/view/main/ChartView.cxx
index 584dfdb..2de5611 100644
--- a/chart2/source/view/main/ChartView.cxx
+++ b/chart2/source/view/main/ChartView.cxx
@@ -56,6 +56,7 @@
#include <DateHelper.hxx>
#include <ExplicitCategoriesProvider.hxx>
#include <defines.hxx>
#include <dumpxmltostring.hxx>
#include <unonames.hxx>
#include <editeng/frmdiritem.hxx>
#include <editeng/eeitem.hxx>
@@ -1784,8 +1785,13 @@
    return aServiceNames;
}

OUString ChartView::dump()
OUString ChartView::dump(OUString const & kind)
{
    if (kind.isEmpty()) {
        return dumpXmlToString([this](auto writer) { return dumpAsXml(writer); });
    }

    // kind == "shapes":
#if HAVE_FEATURE_DESKTOP
    // Used for unit tests and in chartcontroller only, no need to drag in this when cross-compiling
    // for non-desktop
diff --git a/include/sfx2/xmldump.hxx b/include/sfx2/xmldump.hxx
deleted file mode 100644
index cd32d73..0000000
--- a/include/sfx2/xmldump.hxx
+++ /dev/null
@@ -1,29 +0,0 @@
/* -*- 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 <sal/types.h>

typedef struct _xmlTextWriter* xmlTextWriterPtr;

namespace sfx2
{
/// Implemented by objects that can be dumped to xml for debugging purposes.
class SAL_NO_VTABLE SAL_DLLPUBLIC_RTTI SAL_LOPLUGIN_ANNOTATE("crosscast") XmlDump
{
public:
    virtual void dumpAsXml(xmlTextWriterPtr pWriter) const = 0;

protected:
    ~XmlDump() noexcept = default;
};
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/offapi/com/sun/star/qa/XDumper.idl b/offapi/com/sun/star/qa/XDumper.idl
index f980ac0..0722b8b 100644
--- a/offapi/com/sun/star/qa/XDumper.idl
+++ b/offapi/com/sun/star/qa/XDumper.idl
@@ -24,10 +24,11 @@
{
    /**
     * dump the content into a string
     * @param kind  What kind of data to dump (for objects that know how to dump different data).
     * @since LibreOffice 3.6
     */

    string dump();
    string dump([in] string kind);
};

} ; // chart2
diff --git a/svtools/source/misc/embedhlp.cxx b/svtools/source/misc/embedhlp.cxx
index 053a308..d784b20 100644
--- a/svtools/source/misc/embedhlp.cxx
+++ b/svtools/source/misc/embedhlp.cxx
@@ -51,12 +51,12 @@
#include <com/sun/star/embed/XStateChangeListener.hpp>
#include <com/sun/star/embed/XLinkageSupport.hpp>
#include <com/sun/star/chart2/XDefaultSizeTransmitter.hpp>
#include <com/sun/star/qa/XDumper.hpp>
#include <embeddedobj/embeddedupdate.hxx>
#include <cppuhelper/implbase.hxx>
#include <vcl/svapp.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <tools/debug.hxx>
#include <sfx2/xmldump.hxx>
#include <memory>

using namespace com::sun::star;
@@ -287,10 +287,14 @@
        (void)xmlTextWriterStartElement(pWriter, BAD_CAST("mxObj"));
        (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("symbol"),
                                          BAD_CAST(typeid(*mxObj).name()));
        auto pComponent = dynamic_cast<sfx2::XmlDump*>(mxObj->getComponent().get());
        if (pComponent)
        css::uno::Reference<css::qa::XDumper> pComponent(
            mxObj->getComponent(), css::uno::UNO_QUERY);
        if (pComponent.is())
        {
            pComponent->dumpAsXml(pWriter);
            auto const s = pComponent->dump("");
            auto const s1 = OUStringToOString(s, RTL_TEXTENCODING_ISO_8859_1); //TODO
            (void)xmlTextWriterWriteRawLen(
                pWriter, reinterpret_cast<xmlChar const *>(s1.getStr()), s1.getLength());
        }
        (void)xmlTextWriterEndElement(pWriter);

diff --git a/sw/source/core/ole/ndole.cxx b/sw/source/core/ole/ndole.cxx
index a5210d2..831dcae 100644
--- a/sw/source/core/ole/ndole.cxx
+++ b/sw/source/core/ole/ndole.cxx
@@ -56,7 +56,6 @@
#include <atomic>
#include <deque>
#include <libxml/xmlwriter.h>
#include <sfx2/xmldump.hxx>
#include <osl/diagnose.h>
#include <flyfrm.hxx>