summaryrefslogtreecommitdiff
path: root/sw
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.com>2020-01-30 17:56:53 +0100
committerMiklos Vajna <vmiklos@collabora.com>2020-02-04 09:51:49 +0100
commite40fd1a115e6aa40b203c37387566400e074f8a4 (patch)
tree592085efb99b09ed5e2405638a4eb4c7671feca4 /sw
parenta8070d38c81c64e9e9cff0ea28fdcffc2763c365 (diff)
DOCX export: write document variables
This means that in case a user field is exported to DOCX and the user updates the field, the result will be still correct, not empty. (cherry picked from commit ae5f469d3893de73850ccc369dbf426a4acd8f15) Conflicts: sw/qa/extras/ooxmlexport/ooxmlexport14.cxx sw/source/filter/ww8/docxexport.cxx Change-Id: I2b52292c70aa6f597f92af95e16c773839247efa Reviewed-on: https://gerrit.libreoffice.org/c/core/+/87875 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Diffstat (limited to 'sw')
-rw-r--r--sw/qa/extras/ooxmlexport/ooxmlexport13.cxx7
-rw-r--r--sw/source/filter/ww8/docxexport.cxx55
-rw-r--r--sw/source/filter/ww8/docxexport.hxx3
3 files changed, 65 insertions, 0 deletions
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport13.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport13.cxx
index 32e7ad309664..2026124121f4 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport13.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport13.cxx
@@ -439,6 +439,7 @@ CPPUNIT_TEST_FIXTURE(SwModelTestBase, testUserField)
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString("Office Open XML Text");
xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
+ validate(maTempFile.GetFileName(), test::OOXML);
mbExported = true;
xmlDocPtr pXmlDoc = parseExport("word/document.xml");
CPPUNIT_ASSERT(pXmlDoc);
@@ -447,6 +448,12 @@ CPPUNIT_TEST_FIXTURE(SwModelTestBase, testUserField)
// exported as <w:t>User Field foo = bar</w:t>.
assertXPathContent(pXmlDoc, "//w:p/w:r[2]/w:instrText", " DOCVARIABLE foo ");
assertXPathContent(pXmlDoc, "//w:p/w:r[4]/w:t", "bar");
+
+ // Make sure that not only the variables, but also their values are written.
+ pXmlDoc = parseExport("word/settings.xml");
+ CPPUNIT_ASSERT(pXmlDoc);
+ assertXPath(pXmlDoc, "//w:docVars/w:docVar", "name", "foo");
+ assertXPath(pXmlDoc, "//w:docVars/w:docVar", "val", "bar");
}
CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/filter/ww8/docxexport.cxx b/sw/source/filter/ww8/docxexport.cxx
index 0c1e9e5fc378..0956b563a15f 100644
--- a/sw/source/filter/ww8/docxexport.cxx
+++ b/sw/source/filter/ww8/docxexport.cxx
@@ -33,6 +33,7 @@
#include <com/sun/star/xml/sax/XSAXSerializable.hpp>
#include <com/sun/star/xml/sax/Writer.hpp>
#include <com/sun/star/awt/XControlModel.hpp>
+#include <com/sun/star/text/XTextFieldsSupplier.hpp>
#include <oox/token/namespaces.hxx>
#include <oox/token/tokens.hxx>
@@ -912,6 +913,58 @@ void DocxExport::WriteProperties( )
m_pFilter->exportDocumentProperties( xDocProps, bSecurityOptOpenReadOnly );
}
+void DocxExport::WriteDocVars(const sax_fastparser::FSHelperPtr& pFS)
+{
+ SwDocShell* pDocShell = m_pDoc->GetDocShell();
+ if (!pDocShell)
+ {
+ return;
+ }
+
+ uno::Reference<text::XTextFieldsSupplier> xModel(pDocShell->GetModel(), uno::UNO_QUERY);
+ uno::Reference<container::XNameAccess> xTextFieldMasters = xModel->getTextFieldMasters();
+ uno::Sequence<rtl::OUString> aMasterNames = xTextFieldMasters->getElementNames();
+ if (!aMasterNames.hasElements())
+ {
+ return;
+ }
+
+ // Only write docVars if there will be at least a single docVar.
+ bool bStarted = false;
+ const OUStringLiteral aPrefix("com.sun.star.text.fieldmaster.User.");
+ for (const auto& rMasterName : aMasterNames)
+ {
+ if (!rMasterName.startsWith(aPrefix))
+ {
+ // Not a user field.
+ continue;
+ }
+
+ uno::Reference<beans::XPropertySet> xField;
+ xTextFieldMasters->getByName(rMasterName) >>= xField;
+ if (!xField.is())
+ {
+ continue;
+ }
+
+ OUString aKey = rMasterName.copy(aPrefix.getLength());
+ OUString aValue;
+ xField->getPropertyValue("Content") >>= aValue;
+ if (!bStarted)
+ {
+ bStarted = true;
+ pFS->startElementNS(XML_w, XML_docVars, FSEND);
+ }
+ pFS->singleElementNS(XML_w, XML_docVar, FSNS(XML_w, XML_name), aKey.toUtf8(),
+ FSNS(XML_w, XML_val), aValue.toUtf8(), FSEND);
+ }
+
+ if (bStarted)
+ {
+ pFS->endElementNS(XML_w, XML_docVars);
+ }
+}
+
void DocxExport::WriteSettings()
{
SwViewShell *pViewShell(m_pDoc->getIDocumentLayoutAccess().GetCurrentViewShell());
@@ -1129,6 +1182,8 @@ void DocxExport::WriteSettings()
}
}
+ WriteDocVars(pFS);
+
// Protect form
// Section-specific write protection
if (! hasProtectionProperties)
diff --git a/sw/source/filter/ww8/docxexport.hxx b/sw/source/filter/ww8/docxexport.hxx
index d0a8296eac30..4241cc194d74 100644
--- a/sw/source/filter/ww8/docxexport.hxx
+++ b/sw/source/filter/ww8/docxexport.hxx
@@ -244,6 +244,9 @@ private:
/// Write word/settings.xml
void WriteSettings();
+ /// Writes the <w:docVars> part of settings.xml
+ void WriteDocVars(const sax_fastparser::FSHelperPtr& pFS);
+
/// Write word/theme/theme1.xml
void WriteTheme();