Kaydet (Commit) 3583f7a1 authored tarafından Miklos Vajna's avatar Miklos Vajna

tdf#121203 DOCX import: fix loss of free-form text in date control

Date SDT from DOCX is imported as date control since commit
3ec2d26d (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: 's avatarMiklos Vajna <vmiklos@collabora.com>
üst 630b5db9
......@@ -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
......
......@@ -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
......
......@@ -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;
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment