diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2022-05-26 08:25:50 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-05-26 14:06:20 +0200 |
commit | b3c6511659c58bcc75a9742e0ae0cc436b6f6825 (patch) | |
tree | ef019e6ae4c30833ce5642f2d4056528aa9105ca /xmloff | |
parent | 20deeb799a5298188eb967e3ba677b830c5a5d3c (diff) |
sw content controls, date: add ODT filter
Map the Date, DateFormat and DateLanguage UNO properties to:
<loext:content-control loext:date="..." loext:date-format="..." loext:date-rfc-language-tag="...">
And do the opposite on import.
Change-Id: I16d3f755d77ed20380d877ba65dfe6d063f2bec2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134977
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
Diffstat (limited to 'xmloff')
-rw-r--r-- | xmloff/qa/unit/data/content-control-date.fodt | 8 | ||||
-rw-r--r-- | xmloff/qa/unit/text.cxx | 75 | ||||
-rw-r--r-- | xmloff/source/core/xmltoken.cxx | 2 | ||||
-rw-r--r-- | xmloff/source/text/txtparae.cxx | 23 | ||||
-rw-r--r-- | xmloff/source/text/xmlcontentcontrolcontext.cxx | 31 | ||||
-rw-r--r-- | xmloff/source/text/xmlcontentcontrolcontext.hxx | 3 | ||||
-rw-r--r-- | xmloff/source/token/tokens.txt | 2 |
7 files changed, 144 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<office:document xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text"> + <office:body> + <office:text> + <text:p><loext:content-control loext:date="true" loext:date-format="YYYY-MM-DD" loext:date-rfc-language-tag="en-US">choose a date</loext:content-control></text:p> + </office:text> + </office:body> +</office:document> 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<lang::XMultiServiceFactory> xMSF(getComponent(), uno::UNO_QUERY); + uno::Reference<text::XTextDocument> xTextDocument(getComponent(), uno::UNO_QUERY); + uno::Reference<text::XText> xText = xTextDocument->getText(); + uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor(); + xText->insertString(xCursor, "choose a date", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + uno::Reference<text::XTextContent> xContentControl( + xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY); + uno::Reference<beans::XPropertySet> 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<frame::XStorable> xStorable(getComponent(), uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> 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<SvStream> 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<text::XTextDocument> xTextDocument(getComponent(), uno::UNO_QUERY); + uno::Reference<container::XEnumerationAccess> xParagraphsAccess(xTextDocument->getText(), + uno::UNO_QUERY); + uno::Reference<container::XEnumeration> xParagraphs = xParagraphsAccess->createEnumeration(); + uno::Reference<container::XEnumerationAccess> xParagraph(xParagraphs->nextElement(), + uno::UNO_QUERY); + uno::Reference<container::XEnumeration> xPortions = xParagraph->createEnumeration(); + uno::Reference<beans::XPropertySet> xTextPortion(xPortions->nextElement(), uno::UNO_QUERY); + OUString aPortionType; + xTextPortion->getPropertyValue("TextPortionType") >>= aPortionType; + CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aPortionType); + uno::Reference<text::XTextContent> xContentControl; + xTextPortion->getPropertyValue("ContentControl") >>= xContentControl; + uno::Reference<beans::XPropertySet> 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<css::xml::sax::XFastContextHandler> 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<css::beans::PropertyValues> 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 |