writerfilter: fix assert importing fdo77404-1.docx

The problem is that StyleSheetTable::ApplyStyleSheets() inserts a
SwNumRule with name "WW8Num1" and then ListDef::CreateNumberingRule()
also wants to insert a SwNumRule with name "WW8Num1" but gets an
exception instead, leaving ListDef::m_xNumRules null, and then
finishParagraph thinks it's numbered but there's no ListId.

Try to avoid collisions of the generated names in
ListDef::GetStyleName(), by checking what styles actually exist in the
document (which works better in the Insert->File case), and
on the assumption that the initialising call always happens before
the using calls.

(regression from 7992bd73a2307edce96a145e954f8e4c3ab9f57d)

Change-Id: I91c98aa897c12778fb214e9690da0bae99550b93
Reviewed-on: https://gerrit.libreoffice.org/79312
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@cib.de>
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.cxx b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
index 192b5ab..81acdf5 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.cxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
@@ -1237,9 +1237,10 @@
    {
        bool bNumberingFromBaseStyle = false;
        sal_Int32 nListId = pEntry ? lcl_getListId(pEntry, GetStyleSheetTable(), bNumberingFromBaseStyle) : -1;
        if (nListId >= 0 && !pParaContext->isSet(PROP_NUMBERING_STYLE_NAME))
        auto const pList(GetListTable()->GetList(nListId));
        if (pList && nListId >= 0 && !pParaContext->isSet(PROP_NUMBERING_STYLE_NAME))
        {
            pParaContext->Insert( PROP_NUMBERING_STYLE_NAME, uno::makeAny( ListDef::GetStyleName( nListId ) ), false);
            pParaContext->Insert( PROP_NUMBERING_STYLE_NAME, uno::makeAny( pList->GetStyleName(nListId) ), false);
            isNumberingViaStyle = true;

            // Indent properties from the paragraph style have priority
@@ -6075,7 +6076,12 @@
            *pListLevel = pStyleSheetProperties->GetListLevel();

        // So we are in a paragraph style and it has numbering. Look up the relevant numbering rules.
        OUString aListName = ListDef::GetStyleName(nListId);
        auto const pList(GetListTable()->GetList(nListId));
        OUString aListName;
        if (pList)
        {
            aListName = pList->GetStyleName(nListId);
        }
        uno::Reference< style::XStyleFamiliesSupplier > xStylesSupplier(GetTextDocument(), uno::UNO_QUERY_THROW);
        uno::Reference< container::XNameAccess > xStyleFamilies = xStylesSupplier->getStyleFamilies();
        uno::Reference<container::XNameAccess> xNumberingStyles;
@@ -6190,7 +6196,9 @@
        if (nNumberingLevel < 0) // It seems it's valid to omit numbering level, and in that case it means zero.
            nNumberingLevel = 0;

        const OUString aListName = ListDef::GetStyleName(nListId);
        auto const pList(GetListTable()->GetList(nListId));
        assert(pList);
        const OUString aListName = pList->GetStyleName(nListId);
        const uno::Reference< style::XStyleFamiliesSupplier > xStylesSupplier(GetTextDocument(), uno::UNO_QUERY_THROW);
        const uno::Reference< container::XNameAccess > xStyleFamilies = xStylesSupplier->getStyleFamilies();
        uno::Reference<container::XNameAccess> xNumberingStyles;
diff --git a/writerfilter/source/dmapper/NumberingManager.cxx b/writerfilter/source/dmapper/NumberingManager.cxx
index 1b8a8b2..8f7d05e 100644
--- a/writerfilter/source/dmapper/NumberingManager.cxx
+++ b/writerfilter/source/dmapper/NumberingManager.cxx
@@ -447,12 +447,27 @@
{
}

OUString ListDef::GetStyleName( sal_Int32 nId )
OUString ListDef::GetStyleName(sal_Int32 const nId,
    uno::Reference<container::XNameContainer> const& xStyles)
{
    OUString sStyleName( "WWNum" );
    sStyleName += OUString::number( nId );
    if (xStyles.is())
    {
        OUString sStyleName( "WWNum" );
        sStyleName += OUString::number( nId );

    return sStyleName;
        while (xStyles.is() && xStyles->hasByName(sStyleName)) // unique
        {
            sStyleName += "a";
        }

        m_StyleName = sStyleName;
    }
    else
    {
// fails in rtftok test        assert(!m_StyleName.isEmpty()); // must be inited first
    }

    return m_StyleName;
}

uno::Sequence<uno::Sequence<beans::PropertyValue>> ListDef::GetMergedPropertyValues()
@@ -519,7 +534,7 @@
                xFactory->createInstance("com.sun.star.style.NumberingStyle"),
                uno::UNO_QUERY_THROW );

            OUString sStyleName = GetStyleName( GetId( ) );
            OUString sStyleName = GetStyleName(GetId(), xStyles);

            xStyles->insertByName( sStyleName, makeAny( xStyle ) );

diff --git a/writerfilter/source/dmapper/NumberingManager.hxx b/writerfilter/source/dmapper/NumberingManager.hxx
index c484b7b..0ba356f 100644
--- a/writerfilter/source/dmapper/NumberingManager.hxx
+++ b/writerfilter/source/dmapper/NumberingManager.hxx
@@ -170,6 +170,9 @@
    // Cache for the UNO numbering rules
    css::uno::Reference< css::container::XIndexReplace > m_xNumRules;

    /// mapped list style name
    OUString m_StyleName;

public:
    typedef tools::SvRef< ListDef > Pointer;

@@ -181,7 +184,9 @@
    const AbstractListDef::Pointer& GetAbstractDefinition( ) { return m_pAbstractDef; };

    // Mapping functions
    static OUString GetStyleName( sal_Int32 nId );
    OUString GetStyleName(sal_Int32 nId,
            css::uno::Reference<css::container::XNameContainer> const& xStyles
                = css::uno::Reference<css::container::XNameContainer>());

    css::uno::Sequence< css::uno::Sequence<css::beans::PropertyValue> > GetMergedPropertyValues();