sw content controls: add ODT import

Map

	<loext:content-control loext:showing-place-holder="...">

to com.sun.star.text.ContentControl and set its ShowingPlaceHolder
property based on the XML attribute.

This requires moving XMLImpSpanContext_Impl to txtparai.hxx, otherwise
XMLContentControlContext would have to be in txtparai.cxx, which is
already large.

Change-Id: I9a915868160ebcc0e98ded61bfab72f32864bd76
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132804
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Miklos Vajna 2022-04-11 11:27:13 +02:00
parent 431a0399bb
commit d1649e5d38
7 changed files with 260 additions and 37 deletions

View file

@ -372,6 +372,7 @@ $(eval $(call gb_Library_add_exception_objects,xo,\
xmloff/source/text/txtstyle \
xmloff/source/text/txtstyli \
xmloff/source/text/txtvfldi \
xmloff/source/text/xmlcontentcontrolcontext \
xmloff/source/xforms/SchemaContext \
xmloff/source/xforms/SchemaRestrictionContext \
xmloff/source/xforms/SchemaSimpleTypeContext \

View file

@ -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:showing-place-holder="true">test</loext:content-control></text:p>
</office:text>
</office:body>
</office:document>

View file

@ -419,6 +419,40 @@ CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testContentControlExport)
assertXPath(pXmlDoc, "//loext:content-control", "showing-place-holder", "true");
}
CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testContentControlImport)
{
// Given an ODF document with a content control:
OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "content-control.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;
// Without the accompanying fix in place, this failed with:
// - Expected: ContentControl
// - Actual : Text
// i.e. the content control was lost on import.
CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aPortionType);
uno::Reference<text::XTextContent> xContentControl;
xTextPortion->getPropertyValue("ContentControl") >>= xContentControl;
uno::Reference<text::XTextRange> xContentControlRange(xContentControl, uno::UNO_QUERY);
uno::Reference<text::XText> xText = xContentControlRange->getText();
uno::Reference<container::XEnumerationAccess> xContentEnumAccess(xText, uno::UNO_QUERY);
uno::Reference<container::XEnumeration> xContentEnum = xContentEnumAccess->createEnumeration();
uno::Reference<text::XTextRange> xContent(xContentEnum->nextElement(), uno::UNO_QUERY);
CPPUNIT_ASSERT_EQUAL(OUString("test"), xContent->getString());
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -60,6 +60,7 @@
#include "txtparaimphint.hxx"
#include "xmllinebreakcontext.hxx"
#include "xmlcontentcontrolcontext.hxx"
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
@ -288,43 +289,6 @@ XMLEndReferenceContext_Impl::XMLEndReferenceContext_Impl(
namespace {
class XMLImpSpanContext_Impl : public SvXMLImportContext
{
XMLHints_Impl& m_rHints;
XMLStyleHint_Impl *pHint;
bool& rIgnoreLeadingSpace;
sal_uInt8 nStarFontsConvFlags;
public:
XMLImpSpanContext_Impl(
SvXMLImport& rImport,
sal_Int32 nElement,
const Reference< xml::sax::XFastAttributeList > & xAttrList,
XMLHints_Impl& rHints,
bool& rIgnLeadSpace,
sal_uInt8 nSFConvFlags
);
static css::uno::Reference< css::xml::sax::XFastContextHandler > CreateSpanContext(
SvXMLImport& rImport,
sal_Int32 nElement,
const Reference< xml::sax::XFastAttributeList > & xAttrList,
XMLHints_Impl& rHints,
bool& rIgnLeadSpace,
sal_uInt8 nStarFontsConvFlags = 0
);
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
virtual void SAL_CALL endFastElement( sal_Int32 nElement ) override;
virtual void SAL_CALL characters( const OUString& rChars ) override;
};
class XMLImpHyperlinkContext_Impl : public SvXMLImportContext
{
XMLHints_Impl& m_rHints;
@ -1564,6 +1528,10 @@ css::uno::Reference< css::xml::sax::XFastContextHandler > XMLImpSpanContext_Impl
rHints, rIgnoreLeadingSpace );
break;
case XML_ELEMENT(LO_EXT, XML_CONTENT_CONTROL):
pContext = new XMLContentControlContext(rImport, nElement, rHints, rIgnoreLeadingSpace);
break;
default:
// none of the above? then it's probably a text field!
pContext = XMLTextFieldImportContext::CreateTextFieldImportContext(

View file

@ -102,4 +102,44 @@ public:
};
class XMLHints_Impl;
class XMLStyleHint_Impl;
class XMLImpSpanContext_Impl : public SvXMLImportContext
{
XMLHints_Impl& m_rHints;
XMLStyleHint_Impl *pHint;
bool& rIgnoreLeadingSpace;
sal_uInt8 nStarFontsConvFlags;
public:
XMLImpSpanContext_Impl(
SvXMLImport& rImport,
sal_Int32 nElement,
const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList,
XMLHints_Impl& rHints,
bool& rIgnLeadSpace,
sal_uInt8 nSFConvFlags
);
static css::uno::Reference< css::xml::sax::XFastContextHandler > CreateSpanContext(
SvXMLImport& rImport,
sal_Int32 nElement,
const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList,
XMLHints_Impl& rHints,
bool& rIgnLeadSpace,
sal_uInt8 nStarFontsConvFlags = 0
);
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
virtual void SAL_CALL endFastElement( sal_Int32 nElement ) override;
virtual void SAL_CALL characters( const OUString& rChars ) override;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -0,0 +1,117 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "xmlcontentcontrolcontext.hxx"
#include <com/sun/star/beans/XPropertySet.hpp>
#include <sax/tools/converter.hxx>
#include <xmloff/xmlimp.hxx>
#include <xmloff/xmlnamespace.hxx>
#include <xmloff/xmltoken.hxx>
#include "XMLTextMarkImportContext.hxx"
#include "txtparai.hxx"
using namespace com::sun::star;
using namespace xmloff::token;
XMLContentControlContext::XMLContentControlContext(SvXMLImport& rImport, sal_Int32 /*nElement*/,
XMLHints_Impl& rHints, bool& rIgnoreLeadingSpace)
: SvXMLImportContext(rImport)
, m_rHints(rHints)
, m_rIgnoreLeadingSpace(rIgnoreLeadingSpace)
, m_xStart(GetImport().GetTextImport()->GetCursorAsRange()->getStart())
{
}
void XMLContentControlContext::startFastElement(
sal_Int32 /*nElement*/, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
{
for (auto& rIter : sax_fastparser::castToFastAttributeList(xAttrList))
{
bool bTmp = false;
switch (rIter.getToken())
{
case XML_ELEMENT(LO_EXT, XML_SHOWING_PLACE_HOLDER):
{
if (sax::Converter::convertBool(bTmp, rIter.toView()))
{
m_bShowingPlaceHolder = bTmp;
}
break;
}
default:
XMLOFF_WARN_UNKNOWN("xmloff", rIter);
}
}
}
void XMLContentControlContext::endFastElement(sal_Int32)
{
if (!m_xStart.is())
{
SAL_WARN("xmloff.text", "XMLContentControlContext::endFastElement: no m_xStart");
return;
}
uno::Reference<text::XTextRange> xEndRange
= GetImport().GetTextImport()->GetCursorAsRange()->getStart();
// Create range for insertion.
uno::Reference<text::XTextCursor> xInsertionCursor
= GetImport().GetTextImport()->GetText()->createTextCursorByRange(xEndRange);
xInsertionCursor->gotoRange(m_xStart, /*bExpand=*/true);
uno::Reference<text::XTextContent> xContentControl
= XMLTextMarkImportContext::CreateAndInsertMark(
GetImport(), "com.sun.star.text.ContentControl", OUString(), xInsertionCursor);
if (!xContentControl.is())
{
SAL_WARN("xmloff.text", "cannot insert content control");
return;
}
uno::Reference<beans::XPropertySet> xPropertySet(xContentControl, uno::UNO_QUERY);
if (!xPropertySet.is())
{
return;
}
if (m_bShowingPlaceHolder)
{
xPropertySet->setPropertyValue("ShowingPlaceHolder", uno::makeAny(m_bShowingPlaceHolder));
}
}
css::uno::Reference<css::xml::sax::XFastContextHandler>
XMLContentControlContext::createFastChildContext(
sal_Int32 nElement, const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
{
return XMLImpSpanContext_Impl::CreateSpanContext(GetImport(), nElement, xAttrList, m_rHints,
m_rIgnoreLeadingSpace);
}
void XMLContentControlContext::characters(const OUString& rChars)
{
GetImport().GetTextImport()->InsertString(rChars, m_rIgnoreLeadingSpace);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View file

@ -0,0 +1,55 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#pragma once
#include <xmloff/xmlictxt.hxx>
#include <com/sun/star/text/XTextContent.hpp>
class XMLHints_Impl;
/// Imports <loext:content-control>.
class XMLContentControlContext : public SvXMLImportContext
{
XMLHints_Impl& m_rHints;
bool& m_rIgnoreLeadingSpace;
css::uno::Reference<css::text::XTextRange> m_xStart;
bool m_bShowingPlaceHolder = false;
public:
XMLContentControlContext(SvXMLImport& rImport, sal_Int32 nElement, XMLHints_Impl& rHints,
bool& rIgnoreLeadingSpace);
void SAL_CALL startFastElement(
sal_Int32 nElement,
const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttrList) override;
void SAL_CALL endFastElement(sal_Int32 nElement) override;
css::uno::Reference<css::xml::sax::XFastContextHandler> SAL_CALL createFastChildContext(
sal_Int32 nElement,
const css::uno::Reference<css::xml::sax::XFastAttributeList>& rAttrList) override;
void SAL_CALL characters(const OUString& rChars) override;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */