fdo#76279 Invalid Citation on RoundTrip

Reviewed on:
	https://gerrit.libreoffice.org/9898

Change-Id: Idfe39761180ae1eeb0066ae3aac13de6ac6df0b9
diff --git a/sw/inc/authfld.hxx b/sw/inc/authfld.hxx
index e2b006b..6e3926b 100644
--- a/sw/inc/authfld.hxx
+++ b/sw/inc/authfld.hxx
@@ -153,6 +153,9 @@ public:
    /// For internal use only, in general continue using ExpandField() instead.
    OUString ConditionalExpand(ToxAuthorityField eField) const;

    //To handle Citation
    OUString ExpandCitation(ToxAuthorityField eField) const;

    SwAuthorityField(SwAuthorityFieldType* pType, const OUString& rFieldContents);
    SwAuthorityField(SwAuthorityFieldType* pType, sal_IntPtr nHandle);
    virtual ~SwAuthorityField();
diff --git a/sw/inc/fldbas.hxx b/sw/inc/fldbas.hxx
index 0d1724f..f6893e8 100644
--- a/sw/inc/fldbas.hxx
+++ b/sw/inc/fldbas.hxx
@@ -319,6 +319,8 @@ public:
      */
    OUString            ExpandField(bool const bCached, ToxAuthorityField eField = AUTH_FIELD_IDENTIFIER) const;

    OUString            ExpandCitation(ToxAuthorityField eField = AUTH_FIELD_IDENTIFIER) const;

    /// @return name or content.
    virtual OUString    GetFieldName() const;

diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport.cxx
index 4c48e95..76c3fb9 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport.cxx
@@ -3000,14 +3000,8 @@ DECLARE_OOXMLEXPORT_TEST(testCitation,"FDO74775.docx")
    xmlDocPtr pXmlDoc = parseExport();
    if (!pXmlDoc)
        return;
    xmlXPathObjectPtr pXmlObj = getXPathNode(pXmlDoc,"/w:document/w:body/w:sdt/w:sdtContent/w:p[1]/w:r[3]/w:instrText");
    xmlNodeSetPtr pXmlNodes = pXmlObj->nodesetval;
    CPPUNIT_ASSERT(pXmlNodes != 0);
    CPPUNIT_ASSERT(pXmlNodes->nodeNr > 0);
    xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
    OUString contents = OUString::createFromAscii((const char*)((pXmlNode->children[0]).content));
    CPPUNIT_ASSERT(contents.match(" CITATION [Kra06]"));
    xmlXPathFreeObject(pXmlObj);
    assertXPathContent(pXmlDoc, "/w:document/w:body/w:sdt/w:sdtContent/w:p[1]/w:r[3]/w:instrText", " CITATION Kra06 \\l 1033 ");
    assertXPathContent(pXmlDoc, "/w:document/w:body/w:sdt/w:sdtContent/w:p[1]/w:r[5]/w:t", "(Kramer & Chen, 2006)");
}

DECLARE_OOXMLEXPORT_TEST(testFdo76016, "fdo76016.docx")
diff --git a/sw/source/core/fields/authfld.cxx b/sw/source/core/fields/authfld.cxx
index 4cfaf6a..2eff387 100644
--- a/sw/source/core/fields/authfld.cxx
+++ b/sw/source/core/fields/authfld.cxx
@@ -569,6 +569,28 @@ OUString SwAuthorityField::ConditionalExpand(ToxAuthorityField eField) const
    return sRet;
}

OUString SwAuthorityField::ExpandCitation(ToxAuthorityField eField) const
{
    SwAuthorityFieldType* pAuthType = (SwAuthorityFieldType*)GetTyp();
    OUString sRet;

    if( pAuthType->IsSequence() )
    {
       if(!pAuthType->GetDoc()->IsExpFldsLocked())
           m_nTempSequencePos = pAuthType->GetSequencePos( m_nHandle );
       if( m_nTempSequencePos >= 0 )
           sRet += OUString::number( m_nTempSequencePos );
    }
    else
    {
        const SwAuthEntry* pEntry = pAuthType->GetEntryByHandle(m_nHandle);
        //TODO: Expand to: identifier, number sequence, ...
        if(pEntry)
            sRet += pEntry->GetAuthorField(eField);
    }
    return sRet;
}

SwField* SwAuthorityField::Copy() const
{
    SwAuthorityFieldType* pAuthType = (SwAuthorityFieldType*)GetTyp();
diff --git a/sw/source/core/fields/fldbas.cxx b/sw/source/core/fields/fldbas.cxx
index 90600e2..0827a7f 100644
--- a/sw/source/core/fields/fldbas.cxx
+++ b/sw/source/core/fields/fldbas.cxx
@@ -405,6 +405,12 @@ OUString SwField::ExpandField(bool const bCached, ToxAuthorityField eField) cons
    return Expand();
}

OUString SwField::ExpandCitation(ToxAuthorityField eField) const
{
    const SwAuthorityField* pAuthorityField = static_cast<const SwAuthorityField*>(this);
        return (pAuthorityField ? pAuthorityField->ExpandCitation(eField) : OUString());
}

SwField * SwField::CopyField() const
{
    SwField *const pNew = Copy();
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx
index 168fa9d..b6d64af 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -1430,7 +1430,7 @@ void DocxAttributeOutput::EndField_Impl( FieldInfos& rInfos )
        OUString sExpand;
        if(rInfos.eType == ww::eCITATION)
        {
            sExpand = rInfos.pField->ExpandField( false , AUTH_FIELD_TITLE);
            sExpand = rInfos.pField->ExpandCitation(AUTH_FIELD_TITLE);
        }
        else
        {
diff --git a/sw/source/filter/ww8/ww8atr.cxx b/sw/source/filter/ww8/ww8atr.cxx
index 83fd5d8..44c433c 100644
--- a/sw/source/filter/ww8/ww8atr.cxx
+++ b/sw/source/filter/ww8/ww8atr.cxx
@@ -2923,9 +2923,8 @@ void AttributeOutputBase::TextField( const SwFmtFld& rField )
        break;
    case RES_AUTHORITY:
    {
        const OUString sStr = " CITATION "
                + lcl_GetExpandedField(*pFld);
        GetExport().OutputField( pFld, ww::eCITATION, sStr );
        OUString sRet(pFld->ExpandCitation(AUTH_FIELD_IDENTIFIER));
        GetExport().OutputField( pFld, ww::eCITATION, sRet );
    }
    break;
    case RES_POSTITFLD:
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.cxx b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
index edd30e5..e74383e 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.cxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
@@ -3801,12 +3801,12 @@ void DomainMapper_Impl::CloseFieldCommand()
                                  OUString::createFromAscii(aIt->second.cFieldServiceName));
                                  uno::Reference< beans::XPropertySet > xTC(xFieldInterface,
                                  uno::UNO_QUERY_THROW);

                        if( !sFirstParam.isEmpty()){
                        OUString sCmd(pContext->GetCommand());//sCmd is the entire instrText inclusing the index e.g. CITATION Kra06 \l 1033
                        if( !sCmd.isEmpty()){
                            uno::Sequence<com::sun::star::beans::PropertyValue> aValues(1);
                            com::sun::star::beans::PropertyValue propertyVal;
                            propertyVal.Name = "Identifier";
                            propertyVal.Value = uno::makeAny(sFirstParam);
                            propertyVal.Value = uno::makeAny(sCmd);
                            aValues[0] = propertyVal;
                                    xTC->setPropertyValue("Fields",
                                            uno::makeAny(aValues));