From b3c6511659c58bcc75a9742e0ae0cc436b6f6825 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 26 May 2022 08:25:50 +0200 Subject: sw content controls, date: add ODT filter Map the Date, DateFormat and DateLanguage UNO properties to: And do the opposite on import. Change-Id: I16d3f755d77ed20380d877ba65dfe6d063f2bec2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134977 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- xmloff/qa/unit/data/content-control-date.fodt | 8 +++ xmloff/qa/unit/text.cxx | 75 +++++++++++++++++++++++++ xmloff/source/core/xmltoken.cxx | 2 + xmloff/source/text/txtparae.cxx | 23 ++++++++ xmloff/source/text/xmlcontentcontrolcontext.cxx | 31 ++++++++++ xmloff/source/text/xmlcontentcontrolcontext.hxx | 3 + xmloff/source/token/tokens.txt | 2 + 7 files changed, 144 insertions(+) create mode 100644 xmloff/qa/unit/data/content-control-date.fodt (limited to 'xmloff') diff --git a/xmloff/qa/unit/data/content-control-date.fodt b/xmloff/qa/unit/data/content-control-date.fodt new file mode 100644 index 000000000000..dd3749a02e99 --- /dev/null +++ b/xmloff/qa/unit/data/content-control-date.fodt @@ -0,0 +1,8 @@ + + + + + choose a date + + + diff --git a/xmloff/qa/unit/text.cxx b/xmloff/qa/unit/text.cxx index 75dc7e87e008..303f31c4ca40 100644 --- a/xmloff/qa/unit/text.cxx +++ b/xmloff/qa/unit/text.cxx @@ -715,6 +715,81 @@ CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testPictureContentControlImport) CPPUNIT_ASSERT(bPicture); } +CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testDateContentControlExport) +{ + // Given a document with a date content control around a text portion: + getComponent() = loadFromDesktop("private:factory/swriter"); + uno::Reference xMSF(getComponent(), uno::UNO_QUERY); + uno::Reference xTextDocument(getComponent(), uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + xText->insertString(xCursor, "choose a date", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + uno::Reference xContentControl( + xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY); + uno::Reference xContentControlProps(xContentControl, uno::UNO_QUERY); + xContentControlProps->setPropertyValue("Date", uno::Any(true)); + xContentControlProps->setPropertyValue("DateFormat", uno::Any(OUString("YYYY-MM-DD"))); + xContentControlProps->setPropertyValue("DateLanguage", uno::Any(OUString("en-US"))); + xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true); + + // When exporting to ODT: + uno::Reference xStorable(getComponent(), uno::UNO_QUERY); + uno::Sequence aStoreProps = comphelper::InitPropertySequence({ + { "FilterName", uno::Any(OUString("writer8")) }, + }); + utl::TempFile aTempFile; + aTempFile.EnableKillingFile(); + xStorable->storeToURL(aTempFile.GetURL(), aStoreProps); + validate(aTempFile.GetFileName(), test::ODF); + + // Then make sure the expected markup is used: + std::unique_ptr pStream = parseExportStream(aTempFile, "content.xml"); + // Without the accompanying fix in place, this test would have failed with: + // - XPath '//loext:content-control' no attribute 'date' exist + xmlDocUniquePtr pXmlDoc = parseXmlStream(pStream.get()); + assertXPath(pXmlDoc, "//loext:content-control", "date", "true"); + assertXPath(pXmlDoc, "//loext:content-control", "date-format", "YYYY-MM-DD"); + assertXPath(pXmlDoc, "//loext:content-control", "date-rfc-language-tag", "en-US"); +} + +CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testDateContentControlImport) +{ + // Given an ODF document with a date content control: + OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "content-control-date.fodt"; + + // When loading that document: + getComponent() = loadFromDesktop(aURL); + + // Then make sure that the content control is not lost on import: + uno::Reference xTextDocument(getComponent(), uno::UNO_QUERY); + uno::Reference xParagraphsAccess(xTextDocument->getText(), + uno::UNO_QUERY); + uno::Reference xParagraphs = xParagraphsAccess->createEnumeration(); + uno::Reference xParagraph(xParagraphs->nextElement(), + uno::UNO_QUERY); + uno::Reference xPortions = xParagraph->createEnumeration(); + uno::Reference xTextPortion(xPortions->nextElement(), uno::UNO_QUERY); + OUString aPortionType; + xTextPortion->getPropertyValue("TextPortionType") >>= aPortionType; + CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aPortionType); + uno::Reference xContentControl; + xTextPortion->getPropertyValue("ContentControl") >>= xContentControl; + uno::Reference xContentControlProps(xContentControl, uno::UNO_QUERY); + bool bDate{}; + xContentControlProps->getPropertyValue("Date") >>= bDate; + // Without the accompanying fix in place, this test would have failed, the content control was + // imported as a default rich text one. + CPPUNIT_ASSERT(bDate); + OUString aDateFormat; + xContentControlProps->getPropertyValue("DateFormat") >>= aDateFormat; + CPPUNIT_ASSERT_EQUAL(OUString("YYYY-MM-DD"), aDateFormat); + OUString aDateLanguage; + xContentControlProps->getPropertyValue("DateLanguage") >>= aDateLanguage; + CPPUNIT_ASSERT_EQUAL(OUString("en-US"), aDateLanguage); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/core/xmltoken.cxx b/xmloff/source/core/xmltoken.cxx index e863867a7c78..949178c67f09 100644 --- a/xmloff/source/core/xmltoken.cxx +++ b/xmloff/source/core/xmltoken.cxx @@ -3498,6 +3498,8 @@ namespace xmloff::token { TOKEN("unchecked-state", XML_UNCHECKED_STATE), TOKEN("display-text", XML_DISPLAY_TEXT), TOKEN("picture", XML_PICTURE), + TOKEN("date-format", XML_DATE_FORMAT), + TOKEN("date-rfc-language-tag", XML_DATE_RFC_LANGUAGE_TAG), #if OSL_DEBUG_LEVEL > 0 diff --git a/xmloff/source/text/txtparae.cxx b/xmloff/source/text/txtparae.cxx index e8b930ea6663..4c4537db064a 100644 --- a/xmloff/source/text/txtparae.cxx +++ b/xmloff/source/text/txtparae.cxx @@ -3943,6 +3943,29 @@ void XMLTextParagraphExport::ExportContentControl( GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_PICTURE, aBuffer.makeStringAndClear()); } + + bool bDate = false; + xPropertySet->getPropertyValue("Date") >>= bDate; + if (bDate) + { + OUStringBuffer aBuffer; + sax::Converter::convertBool(aBuffer, bDate); + GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_DATE, aBuffer.makeStringAndClear()); + } + + OUString aDateFormat; + xPropertySet->getPropertyValue("DateFormat") >>= aDateFormat; + if (!aDateFormat.isEmpty()) + { + GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_DATE_FORMAT, aDateFormat); + } + + OUString aDateLanguage; + xPropertySet->getPropertyValue("DateLanguage") >>= aDateLanguage; + if (!aDateLanguage.isEmpty()) + { + GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_DATE_RFC_LANGUAGE_TAG, aDateLanguage); + } } SvXMLElementExport aElem(GetExport(), bExport, XML_NAMESPACE_LO_EXT, XML_CONTENT_CONTROL, false, diff --git a/xmloff/source/text/xmlcontentcontrolcontext.cxx b/xmloff/source/text/xmlcontentcontrolcontext.cxx index 202b74553e18..dbe6b824504c 100644 --- a/xmloff/source/text/xmlcontentcontrolcontext.cxx +++ b/xmloff/source/text/xmlcontentcontrolcontext.cxx @@ -94,6 +94,24 @@ void XMLContentControlContext::startFastElement( } break; } + case XML_ELEMENT(LO_EXT, XML_DATE): + { + if (sax::Converter::convertBool(bTmp, rIter.toView())) + { + m_bDate = bTmp; + } + break; + } + case XML_ELEMENT(LO_EXT, XML_DATE_FORMAT): + { + m_aDateFormat = rIter.toString(); + break; + } + case XML_ELEMENT(LO_EXT, XML_DATE_RFC_LANGUAGE_TAG): + { + m_aDateLanguage = rIter.toString(); + break; + } default: XMLOFF_WARN_UNKNOWN("xmloff", rIter); } @@ -162,6 +180,19 @@ void XMLContentControlContext::endFastElement(sal_Int32) { xPropertySet->setPropertyValue("Picture", uno::Any(m_bPicture)); } + + if (m_bDate) + { + xPropertySet->setPropertyValue("Date", uno::Any(m_bDate)); + } + if (!m_aDateFormat.isEmpty()) + { + xPropertySet->setPropertyValue("DateFormat", uno::Any(m_aDateFormat)); + } + if (!m_aDateLanguage.isEmpty()) + { + xPropertySet->setPropertyValue("DateLanguage", uno::Any(m_aDateLanguage)); + } } css::uno::Reference diff --git a/xmloff/source/text/xmlcontentcontrolcontext.hxx b/xmloff/source/text/xmlcontentcontrolcontext.hxx index d3874d31257e..623ef97e8df3 100644 --- a/xmloff/source/text/xmlcontentcontrolcontext.hxx +++ b/xmloff/source/text/xmlcontentcontrolcontext.hxx @@ -44,6 +44,9 @@ class XMLContentControlContext : public SvXMLImportContext OUString m_aUncheckedState; std::vector m_aListItems; bool m_bPicture = false; + bool m_bDate = false; + OUString m_aDateFormat; + OUString m_aDateLanguage; public: XMLContentControlContext(SvXMLImport& rImport, sal_Int32 nElement, XMLHints_Impl& rHints, diff --git a/xmloff/source/token/tokens.txt b/xmloff/source/token/tokens.txt index 7917dfd75f4c..04b8b62de5d3 100644 --- a/xmloff/source/token/tokens.txt +++ b/xmloff/source/token/tokens.txt @@ -3241,4 +3241,6 @@ checked-state unchecked-state display-text picture +date-format +date-rfc-language-tag TOKEN_END_DUMMY -- cgit