From 90e1698dab3d286c10136719aa85fee2f56ad9fd Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Mon, 23 Jan 2023 16:38:03 +0100 Subject: tdf#153090 xmloff: ODF import/export of source style on ToX For Table of Figures/Objects/Tables. Make this similar to DOCX: only one source style is possible, so omit the intermediate text:index-source-styles element. Also a boolean attribute like text:use-index-source-styles appears as unnecessary complexity, so try to do without. Change-Id: I7cb599c7ec1261ddedc196a25c3eafe35f35fbe9 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146013 Tested-by: Jenkins Reviewed-by: Michael Stahl --- .../text/XMLIndexAlphabeticalSourceContext.cxx | 2 +- .../text/XMLIndexBibliographySourceContext.cxx | 5 ++-- .../text/XMLIndexIllustrationSourceContext.cxx | 4 ++-- xmloff/source/text/XMLIndexObjectSourceContext.cxx | 5 ++-- xmloff/source/text/XMLIndexSourceBaseContext.cxx | 23 ++++++++++++++---- xmloff/source/text/XMLIndexSourceBaseContext.hxx | 16 +++++++++++-- xmloff/source/text/XMLIndexTOCSourceContext.cxx | 2 +- xmloff/source/text/XMLIndexTOCStylesContext.cxx | 28 +++++++++++++++++----- xmloff/source/text/XMLIndexTableSourceContext.cxx | 3 +-- xmloff/source/text/XMLIndexUserSourceContext.cxx | 5 ++-- xmloff/source/text/XMLSectionExport.cxx | 16 +++++++++++++ 11 files changed, 81 insertions(+), 28 deletions(-) (limited to 'xmloff') diff --git a/xmloff/source/text/XMLIndexAlphabeticalSourceContext.cxx b/xmloff/source/text/XMLIndexAlphabeticalSourceContext.cxx index df1385d95b0e..d3210421f525 100644 --- a/xmloff/source/text/XMLIndexAlphabeticalSourceContext.cxx +++ b/xmloff/source/text/XMLIndexAlphabeticalSourceContext.cxx @@ -45,7 +45,7 @@ using namespace ::xmloff::token; XMLIndexAlphabeticalSourceContext::XMLIndexAlphabeticalSourceContext( SvXMLImport& rImport, Reference & rPropSet) -: XMLIndexSourceBaseContext(rImport, rPropSet, false) +: XMLIndexSourceBaseContext(rImport, rPropSet, UseStyles::None) , bMainEntryStyleNameOK(false) , bSeparators(false) , bCombineEntries(true) diff --git a/xmloff/source/text/XMLIndexBibliographySourceContext.cxx b/xmloff/source/text/XMLIndexBibliographySourceContext.cxx index 0ec5563895f6..226e5b10dc3a 100644 --- a/xmloff/source/text/XMLIndexBibliographySourceContext.cxx +++ b/xmloff/source/text/XMLIndexBibliographySourceContext.cxx @@ -38,9 +38,8 @@ using ::com::sun::star::xml::sax::XAttributeList; XMLIndexBibliographySourceContext::XMLIndexBibliographySourceContext( SvXMLImport& rImport, - Reference & rPropSet) : - XMLIndexSourceBaseContext(rImport, - rPropSet, false) + Reference & rPropSet) + : XMLIndexSourceBaseContext(rImport, rPropSet, UseStyles::None) { } diff --git a/xmloff/source/text/XMLIndexIllustrationSourceContext.cxx b/xmloff/source/text/XMLIndexIllustrationSourceContext.cxx index 780d821bc883..74f2ed9332c6 100644 --- a/xmloff/source/text/XMLIndexIllustrationSourceContext.cxx +++ b/xmloff/source/text/XMLIndexIllustrationSourceContext.cxx @@ -37,8 +37,8 @@ using ::xmloff::token::XML_TOKEN_INVALID; XMLIndexIllustrationSourceContext::XMLIndexIllustrationSourceContext( SvXMLImport& rImport, - Reference & rPropSet) : - XMLIndexTableSourceContext(rImport, rPropSet) + Reference & rPropSet) + : XMLIndexTableSourceContext(rImport, rPropSet) { } diff --git a/xmloff/source/text/XMLIndexObjectSourceContext.cxx b/xmloff/source/text/XMLIndexObjectSourceContext.cxx index d35f3f025149..847ec680b0cd 100644 --- a/xmloff/source/text/XMLIndexObjectSourceContext.cxx +++ b/xmloff/source/text/XMLIndexObjectSourceContext.cxx @@ -43,9 +43,8 @@ using namespace ::xmloff::token; XMLIndexObjectSourceContext::XMLIndexObjectSourceContext( SvXMLImport& rImport, - Reference & rPropSet) : - XMLIndexSourceBaseContext(rImport, - rPropSet, false), + Reference & rPropSet) + : XMLIndexSourceBaseContext(rImport, rPropSet, UseStyles::Single), bUseCalc(false), bUseChart(false), bUseDraw(false), diff --git a/xmloff/source/text/XMLIndexSourceBaseContext.cxx b/xmloff/source/text/XMLIndexSourceBaseContext.cxx index 77de7bf4cac9..242e95012781 100644 --- a/xmloff/source/text/XMLIndexSourceBaseContext.cxx +++ b/xmloff/source/text/XMLIndexSourceBaseContext.cxx @@ -42,9 +42,9 @@ using ::com::sun::star::xml::sax::XAttributeList; XMLIndexSourceBaseContext::XMLIndexSourceBaseContext( SvXMLImport& rImport, Reference & rPropSet, - bool bLevelFormats) + UseStyles const eUseStyles) : SvXMLImportContext(rImport) -, bUseLevelFormats(bLevelFormats) +, m_UseStyles(eUseStyles) , bChapterIndex(false) , bRelativeTabs(true) , rIndexPropertySet(rPropSet) @@ -100,7 +100,7 @@ void XMLIndexSourceBaseContext::endFastElement(sal_Int32 ) css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexSourceBaseContext::createFastChildContext( sal_Int32 nElement, - const css::uno::Reference< css::xml::sax::XFastAttributeList >& ) + const css::uno::Reference& xAttrList) { SvXMLImportContextRef xContext; @@ -109,12 +109,25 @@ css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexSourceBaseCont xContext = new XMLIndexTitleTemplateContext(GetImport(), rIndexPropertySet); } - else if ( bUseLevelFormats && - nElement == XML_ELEMENT(TEXT, XML_INDEX_SOURCE_STYLES) ) + else if (m_UseStyles == UseStyles::Level + && nElement == XML_ELEMENT(TEXT, XML_INDEX_SOURCE_STYLES)) { xContext = new XMLIndexTOCStylesContext(GetImport(), rIndexPropertySet); } + else if (m_UseStyles == UseStyles::Single + && (nElement == XML_ELEMENT(LO_EXT, XML_INDEX_SOURCE_STYLE) + || nElement == XML_ELEMENT(TEXT, XML_INDEX_SOURCE_STYLE))) + { + OUString const styleName(xmloff::GetIndexSourceStyleName(xAttrList)); + if (!styleName.isEmpty()) + { + OUString const convertedStyleName(GetImport().GetStyleDisplayName( + XmlStyleFamily::TEXT_PARAGRAPH, styleName)); + rIndexPropertySet->setPropertyValue("CreateFromParagraphStyle", css::uno::Any(convertedStyleName)); + } + } + // else: unknown namespace -> ignore return xContext; diff --git a/xmloff/source/text/XMLIndexSourceBaseContext.hxx b/xmloff/source/text/XMLIndexSourceBaseContext.hxx index 17d287e9a882..b927954e5f83 100644 --- a/xmloff/source/text/XMLIndexSourceBaseContext.hxx +++ b/xmloff/source/text/XMLIndexSourceBaseContext.hxx @@ -35,7 +35,11 @@ namespace com::sun::star { */ class XMLIndexSourceBaseContext : public SvXMLImportContext { - bool bUseLevelFormats; +protected: + enum class UseStyles { None, Level, Single }; + +private: + UseStyles m_UseStyles; bool bChapterIndex; /// chapter-wise or document index? bool bRelativeTabs; /// tab stops relative to margin or indent? @@ -50,7 +54,7 @@ public: XMLIndexSourceBaseContext( SvXMLImport& rImport, css::uno::Reference & rPropSet, - bool bLevelFormats); + UseStyles eUseStyles); virtual ~XMLIndexSourceBaseContext() override; @@ -69,4 +73,12 @@ protected: const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; }; + +namespace xmloff { + +OUString GetIndexSourceStyleName( + css::uno::Reference const& xAttrList); + +} // namespace xmloff + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/text/XMLIndexTOCSourceContext.cxx b/xmloff/source/text/XMLIndexTOCSourceContext.cxx index 45899e4543d2..bea5ff58ad4c 100644 --- a/xmloff/source/text/XMLIndexTOCSourceContext.cxx +++ b/xmloff/source/text/XMLIndexTOCSourceContext.cxx @@ -40,7 +40,7 @@ using ::com::sun::star::xml::sax::XAttributeList; XMLIndexTOCSourceContext::XMLIndexTOCSourceContext( SvXMLImport& rImport, Reference & rPropSet) -: XMLIndexSourceBaseContext(rImport, rPropSet, true) +: XMLIndexSourceBaseContext(rImport, rPropSet, UseStyles::Level) // use all chapters by default , nOutlineLevel(rImport.GetTextImport()->GetChapterNumbering()->getCount()) , bUseOutline(true) diff --git a/xmloff/source/text/XMLIndexTOCStylesContext.cxx b/xmloff/source/text/XMLIndexTOCStylesContext.cxx index 5331b687a9f9..c3e2c5b4ec31 100644 --- a/xmloff/source/text/XMLIndexTOCStylesContext.cxx +++ b/xmloff/source/text/XMLIndexTOCStylesContext.cxx @@ -18,6 +18,8 @@ */ #include "XMLIndexTOCStylesContext.hxx" + +#include "XMLIndexSourceBaseContext.hxx" #include #include #include @@ -103,6 +105,23 @@ void XMLIndexTOCStylesContext::endFastElement(sal_Int32 ) xIndexReplace->replaceByIndex(nOutlineLevel, Any(aStyleNamesSequence)); } +namespace xmloff { + +OUString GetIndexSourceStyleName( + css::uno::Reference const& xAttrList) +{ + for (auto& rIter : sax_fastparser::castToFastAttributeList(xAttrList)) + { + if (rIter.getToken() == XML_ELEMENT(TEXT, XML_STYLE_NAME)) + { + return rIter.toString(); + } + } + return OUString(); +} + +} // namespace xmloff + css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexTOCStylesContext::createFastChildContext( sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) @@ -111,13 +130,10 @@ css::uno::Reference< css::xml::sax::XFastContextHandler > XMLIndexTOCStylesConte if ( nElement == XML_ELEMENT(TEXT, XML_INDEX_SOURCE_STYLE) ) { // find text:style-name attribute and record in aStyleNames - for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) ) + OUString const styleName(xmloff::GetIndexSourceStyleName(xAttrList)); + if (!styleName.isEmpty()) { - if ( aIter.getToken() == XML_ELEMENT(TEXT, XML_STYLE_NAME) ) - { - aStyleNames.push_back(aIter.toString()); - break; - } + aStyleNames.emplace_back(styleName); } } diff --git a/xmloff/source/text/XMLIndexTableSourceContext.cxx b/xmloff/source/text/XMLIndexTableSourceContext.cxx index 480baf0cee16..3a7f8a5a779b 100644 --- a/xmloff/source/text/XMLIndexTableSourceContext.cxx +++ b/xmloff/source/text/XMLIndexTableSourceContext.cxx @@ -45,8 +45,7 @@ using ::com::sun::star::xml::sax::XAttributeList; XMLIndexTableSourceContext::XMLIndexTableSourceContext( SvXMLImport& rImport, Reference & rPropSet) - : XMLIndexSourceBaseContext(rImport, - rPropSet, false) + : XMLIndexSourceBaseContext(rImport, rPropSet, UseStyles::Single) , nDisplayFormat(0) , bSequenceOK(false) , bDisplayFormatOK(false) diff --git a/xmloff/source/text/XMLIndexUserSourceContext.cxx b/xmloff/source/text/XMLIndexUserSourceContext.cxx index 392e8b72bc5f..ebaf6ee23e4e 100644 --- a/xmloff/source/text/XMLIndexUserSourceContext.cxx +++ b/xmloff/source/text/XMLIndexUserSourceContext.cxx @@ -37,9 +37,8 @@ using namespace ::xmloff::token; XMLIndexUserSourceContext::XMLIndexUserSourceContext( SvXMLImport& rImport, - Reference & rPropSet) : - XMLIndexSourceBaseContext(rImport, - rPropSet, true), + Reference & rPropSet) + : XMLIndexSourceBaseContext(rImport, rPropSet, UseStyles::Level), bUseObjects(false), bUseGraphic(false), bUseMarks(false), diff --git a/xmloff/source/text/XMLSectionExport.cxx b/xmloff/source/text/XMLSectionExport.cxx index d97e30c0c6fa..f76c4f2815f1 100644 --- a/xmloff/source/text/XMLSectionExport.cxx +++ b/xmloff/source/text/XMLSectionExport.cxx @@ -830,6 +830,22 @@ void XMLSectionExport::ExportBaseIndexSource( aAny >>= xLevelParagraphStyles; ExportLevelParagraphStyles(xLevelParagraphStyles); } + else if (TEXT_SECTION_TYPE_ILLUSTRATION == eType + || TEXT_SECTION_TYPE_OBJECT == eType + || TEXT_SECTION_TYPE_TABLE == eType) + { + Any const any(rPropertySet->getPropertyValue("CreateFromParagraphStyle")); + if (any.hasValue() && + (rExport.getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED)) + { + OUString const styleName(any.get()); + GetExport().AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME, + GetExport().EncodeStyleName(styleName)); + + SvXMLElementExport const e(GetExport(), + XML_NAMESPACE_LO_EXT, XML_INDEX_SOURCE_STYLE, true, false); + } + } } -- cgit