Kaydet (Commit) b2385830 authored tarafından Jacobo Aragunde Pérez's avatar Jacobo Aragunde Pérez

ooxml: export date controls properly

Export date controls as ooxml Standard Document Tags (SDT) instead
of replacing them with plain text.

SDT date controls contain the date in ISO format as an attribute of
<date> tag, a custom date format that can be specified by the user
in the <dateFormat> tag and the date formatted in that custom format
in the <w:sdtContent> tag.

The unit test testFormControl from ooxmlexport suite was removed,
it only checked if the date control was exported as text and it
obviously fails now. A new test that checks the values of the
exported control was written instead.

A pair of date format functions were added to datetimeutils.hxx.

TODO: to avoid supporting all the posible custom formats that can be
specified in the <dateFormat> tag, it is forced to dd/mm/yyyy on
export.

Change-Id: I9d1b6f840ee9e133831fdb04ad399fe31bcb2063
üst 4dcad28a
......@@ -15,6 +15,12 @@
// This function converts a 'DateTime' object to an 'OString' object
TOOLS_DLLPUBLIC OString DateTimeToOString( const DateTime& rDateTime );
// This function converts a 'Date' object to an 'OString' object in ISO-8601 representation
TOOLS_DLLPUBLIC OString DateToOString( const Date& rDate );
// This function converts a 'Date' object to an 'OString' object in DD/MM/YYYY format
TOOLS_DLLPUBLIC OString DateToDDMMYYYYOString( const Date& rDate );
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -15,6 +15,7 @@
#include <com/sun/star/drawing/FillStyle.hpp>
#include <com/sun/star/drawing/LineJoint.hpp>
#include <com/sun/star/drawing/LineStyle.hpp>
#include <com/sun/star/drawing/XControlShape.hpp>
#include <com/sun/star/awt/Gradient.hpp>
#include <com/sun/star/style/TabStop.hpp>
#include <com/sun/star/view/XViewSettingsSupplier.hpp>
......@@ -2046,14 +2047,6 @@ DECLARE_OOXMLEXPORT_TEST(testAutofit, "autofit.docx")
CPPUNIT_ASSERT_EQUAL(false, bool(getProperty<sal_Bool>(getShape(2), "FrameIsAutomaticHeight")));
}
DECLARE_OOXMLEXPORT_TEST(testFormControl, "form-control.docx")
{
if (!m_bExported)
return;
// "[Date]" was missing.
getParagraph(1, "Foo [Date] bar.");
}
DECLARE_OOXMLEXPORT_TEST(testTrackChangesDeletedParagraphMark, "testTrackChangesDeletedParagraphMark.docx")
{
xmlDocPtr pXmlDoc = parseExport("word/document.xml");
......@@ -2963,6 +2956,23 @@ DECLARE_OOXMLEXPORT_TEST(testGenericTextField, "Unsupportedtextfields.docx")
CPPUNIT_ASSERT(contents.match("PRINTDATE \\* MERGEFORMAT"));
}
DECLARE_OOXMLEXPORT_TEST(testDateControl, "date-control.docx")
{
// check XML
xmlDocPtr pXmlDoc = parseExport("word/document.xml");
if (!pXmlDoc)
return;
assertXPath(pXmlDoc, "/w:document/w:body/w:p/w:sdt/w:sdtPr/w:date", "fullDate", "2014-03-05T00:00:00Z");
assertXPathContent(pXmlDoc, "/w:document/w:body/w:p/w:sdt/w:sdtContent/w:r/w:t", "05/03/2014");
// check imported control
uno::Reference<drawing::XControlShape> xControl(getShape(1), uno::UNO_QUERY);
util::Date aDate = getProperty<util::Date>(xControl->getControl(), "Date");
CPPUNIT_ASSERT_EQUAL(5, sal_Int32(aDate.Day));
CPPUNIT_ASSERT_EQUAL(3, sal_Int32(aDate.Month));
CPPUNIT_ASSERT_EQUAL(2014, sal_Int32(aDate.Year));
}
#endif
CPPUNIT_PLUGIN_IMPLEMENT();
......
......@@ -3597,11 +3597,59 @@ void DocxAttributeOutput::WritePostponedFormControl(const SdrObject* pObject)
uno::Reference<lang::XServiceInfo> xInfo(xControlModel, uno::UNO_QUERY);
if (xInfo->supportsService("com.sun.star.form.component.DateField"))
{
// gather component properties
uno::Reference<beans::XPropertySet> xPropertySet(xControlModel, uno::UNO_QUERY);
OUString aHelpText = xPropertySet->getPropertyValue("HelpText").get<OUString>();
OString sDate;
OUString aContentText;
bool bHasDate = false;
css::util::Date aUNODate;
if (xPropertySet->getPropertyValue("Date") >>= aUNODate)
{
bHasDate = true;
Date aDate(aUNODate.Day, aUNODate.Month, aUNODate.Year);
sDate = DateToOString(aDate);
aContentText = OUString::createFromAscii(DateToDDMMYYYYOString(aDate).getStr());
}
else
aContentText = xPropertySet->getPropertyValue("HelpText").get<OUString>();
// output component
m_pSerializer->startElementNS(XML_w, XML_sdt, FSEND);
m_pSerializer->startElementNS(XML_w, XML_sdtPr, FSEND);
if (bHasDate)
m_pSerializer->startElementNS(XML_w, XML_date,
FSNS( XML_w, XML_fullDate ), sDate.getStr(),
FSEND);
else
m_pSerializer->startElementNS(XML_w, XML_date, FSEND);
m_pSerializer->singleElementNS(XML_w, XML_dateFormat,
FSNS(XML_w, XML_val), "dd/MM/yyyy", //TODO: hardwired
FSEND);
m_pSerializer->singleElementNS(XML_w, XML_lid,
FSNS(XML_w, XML_val), "en-US", //TODO: hardwired
FSEND);
m_pSerializer->singleElementNS(XML_w, XML_storeMappedDataAs,
FSNS(XML_w, XML_val), "dateTime",
FSEND);
m_pSerializer->singleElementNS(XML_w, XML_calendar,
FSNS(XML_w, XML_val), "gregorian",
FSEND);
m_pSerializer->endElementNS(XML_w, XML_date);
m_pSerializer->endElementNS(XML_w, XML_sdtPr);
m_pSerializer->startElementNS(XML_w, XML_sdtContent, FSEND);
m_pSerializer->startElementNS(XML_w, XML_r, FSEND);
RunText(aHelpText);
RunText(aContentText);
m_pSerializer->endElementNS(XML_w, XML_r);
m_pSerializer->endElementNS(XML_w, XML_sdtContent);
m_pSerializer->endElementNS(XML_w, XML_sdt);
}
}
}
......
......@@ -54,3 +54,23 @@ OString DateTimeToOString( const DateTime& rDateTime )
return aBuffer.makeStringAndClear();
}
OString DateToOString( const Date& rDate )
{
Time aTime( Time::EMPTY );
return DateTimeToOString( DateTime( rDate, aTime ) );
}
OString DateToDDMMYYYYOString( const Date& rDate )
{
OStringBuffer aBuffer( 25 );
lcl_AppendTwoDigits( aBuffer, rDate.GetDay() );
aBuffer.append( '/' );
lcl_AppendTwoDigits( aBuffer, rDate.GetMonth() );
aBuffer.append( '/' );
aBuffer.append( sal_Int32( rDate.GetYear() ) );
return aBuffer.makeStringAndClear();
}
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