[Libreoffice-commits] core.git: sw/qa writerfilter/source

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Tue Nov 13 08:03:04 UTC 2018


 sw/qa/extras/ooxmlimport/data/tdf121203.docx |binary
 sw/qa/extras/ooxmlimport/ooxmlimport2.cxx    |    8 +++++
 writerfilter/source/dmapper/DomainMapper.cxx |    2 -
 writerfilter/source/dmapper/SdtHelper.cxx    |   41 +++++++++++++++++++++++----
 writerfilter/source/dmapper/SdtHelper.hxx    |    4 ++
 5 files changed, 49 insertions(+), 6 deletions(-)

New commits:
commit 3583f7a1256c901199574c8373443038e28813f0
Author:     Miklos Vajna <vmiklos at collabora.com>
AuthorDate: Mon Nov 12 23:01:12 2018 +0100
Commit:     Miklos Vajna <vmiklos at collabora.com>
CommitDate: Tue Nov 13 09:02:40 2018 +0100

    tdf#121203 DOCX import: fix loss of free-form text in date control
    
    Date SDT from DOCX is imported as date control since commit
    3ec2d26dc2017ac4a27483febfc63328632f352d (bnc#779630 initial DOCX import
    of w:sdt's w:date, 2013-04-30).
    
    One detail I missed there is our date control is strict: it doesn't
    allow free-form text. However, DOCX date SDT has an optional ISO date,
    but the actual value can be free-form text. This means that importing
    free-form text without an ISO date is lost on import.
    
    Fix the data loss by restricting the creation of the date control: only
    do this if we recognize the date format or in case we have an ISO date.
    Otherwise just show the free-form text to avoid data loss.
    
    Change-Id: I8125bdc749954a6a1c496de74b6682744adb7680
    Reviewed-on: https://gerrit.libreoffice.org/63311
    Tested-by: Jenkins
    Reviewed-by: Miklos Vajna <vmiklos at collabora.com>

diff --git a/sw/qa/extras/ooxmlimport/data/tdf121203.docx b/sw/qa/extras/ooxmlimport/data/tdf121203.docx
new file mode 100644
index 000000000000..5aa3b2ed7474
Binary files /dev/null and b/sw/qa/extras/ooxmlimport/data/tdf121203.docx differ
diff --git a/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx b/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx
index 6fe7784cd062..743ed626fe8b 100644
--- a/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx
+++ b/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx
@@ -36,6 +36,14 @@ DECLARE_OOXMLIMPORT_TEST(testTdf108545_embeddedDocxIcon, "tdf108545_embeddedDocx
     CPPUNIT_ASSERT_EQUAL(embed::Aspects::MSOLE_ICON, xSupplier->getAspect());
 }
 
+DECLARE_OOXMLIMPORT_TEST(testTdf121203, "tdf121203.docx")
+{
+    // Make sure that the date SDT's content is imported as plain text, as it
+    // has no ISO date, so we have no idea how to represent that with our date
+    // control.
+    CPPUNIT_ASSERT_EQUAL(OUString("17-Oct-2018 09:00"), getRun(getParagraph(1), 1)->getString());
+}
+
 DECLARE_OOXMLIMPORT_TEST(testTdf109053, "tdf109053.docx")
 {
     // Table was imported into a text frame which led to a one page document
diff --git a/writerfilter/source/dmapper/DomainMapper.cxx b/writerfilter/source/dmapper/DomainMapper.cxx
index 16158a46fabf..5e9bed49044a 100644
--- a/writerfilter/source/dmapper/DomainMapper.cxx
+++ b/writerfilter/source/dmapper/DomainMapper.cxx
@@ -3202,7 +3202,7 @@ void DomainMapper::lcl_utext(const sal_uInt8 * data_, size_t len)
         }
     }
     // Form controls are not allowed in headers / footers; see sw::DocumentContentOperationsManager::InsertDrawObj()
-    else if (!m_pImpl->m_pSdtHelper->getDateFormat().isEmpty() && !IsInHeaderFooter())
+    else if (m_pImpl->m_pSdtHelper->validateDateFormat() && !IsInHeaderFooter())
     {
         /*
          * Here we assume w:sdt only contains a single text token. We need to
diff --git a/writerfilter/source/dmapper/SdtHelper.cxx b/writerfilter/source/dmapper/SdtHelper.cxx
index f8d0200a1074..cccebf1d260c 100644
--- a/writerfilter/source/dmapper/SdtHelper.cxx
+++ b/writerfilter/source/dmapper/SdtHelper.cxx
@@ -24,6 +24,26 @@
 #include "DomainMapper_Impl.hxx"
 #include "StyleSheetTable.hxx"
 
+namespace
+{
+/// Maps OOXML <w:dateFormat> values to UNO date format values.
+sal_Int16 getUNODateFormat(const OUString& rDateFormat)
+{
+    // See com/sun/star/awt/UnoControlDateFieldModel.idl, DateFormat; sadly
+    // there are no constants.
+    sal_Int16 nDateFormat = -1;
+
+    if (rDateFormat == "M/d/yyyy" || rDateFormat == "M.d.yyyy")
+        // MMDDYYYY
+        nDateFormat = 8;
+    else if (rDateFormat == "dd/MM/yyyy")
+        // DDMMYYYY
+        nDateFormat = 7;
+
+    return nDateFormat;
+}
+}
+
 namespace writerfilter
 {
 namespace dmapper
@@ -92,6 +112,14 @@ void SdtHelper::createDropDownControl()
     m_aDropDownItems.clear();
 }
 
+bool SdtHelper::validateDateFormat()
+{
+    bool bRet = !m_sDate.isEmpty() || getUNODateFormat(m_sDateFormat.toString()) != -1;
+    if (!bRet)
+        m_sDateFormat.setLength(0);
+    return bRet;
+}
+
 void SdtHelper::createDateControl(OUString const& rContentText, const beans::PropertyValue& rCharFormat)
 {
     uno::Reference<awt::XControlModel> xControlModel;
@@ -114,14 +142,17 @@ void SdtHelper::createDateControl(OUString const& rContentText, const beans::Pro
     xPropertySet->setPropertyValue("Dropdown", uno::makeAny(true));
 
     // See com/sun/star/awt/UnoControlDateFieldModel.idl, DateFormat; sadly there are no constants
-    sal_Int16 nDateFormat = 0;
     OUString sDateFormat = m_sDateFormat.makeStringAndClear();
-    if (sDateFormat == "M/d/yyyy" || sDateFormat == "M.d.yyyy")
-        // Approximate with MM.dd.yyy
-        nDateFormat = 8;
-    else
+    sal_Int16 nDateFormat = getUNODateFormat(sDateFormat);
+    if (nDateFormat == -1)
+    {
         // Set default format, so at least the date picker is created.
         SAL_WARN("writerfilter", "unhandled w:dateFormat value");
+        if (m_sDate.isEmpty())
+            return;
+        else
+            nDateFormat = 0;
+    }
     xPropertySet->setPropertyValue("DateFormat", uno::makeAny(nDateFormat));
 
     util::Date aDate;
diff --git a/writerfilter/source/dmapper/SdtHelper.hxx b/writerfilter/source/dmapper/SdtHelper.hxx
index 48fa71d73c41..91f005f9d544 100644
--- a/writerfilter/source/dmapper/SdtHelper.hxx
+++ b/writerfilter/source/dmapper/SdtHelper.hxx
@@ -87,6 +87,10 @@ public:
     {
         return m_sDateFormat;
     }
+
+    /// Decides if we have enough information to create a date control.
+    bool validateDateFormat();
+
     OUStringBuffer& getLocale()
     {
         return m_sLocale;


More information about the Libreoffice-commits mailing list