23#include <oox/token/namespaces.hxx>
24#include <oox/token/tokens.hxx>
25#include <rtl/ustrbuf.hxx>
27#include <osl/diagnose.h>
28#include <com/sun/star/frame/XModel.hpp>
29#include <com/sun/star/lang/XServiceInfo.hpp>
47ContextHandler2Helper::ContextHandler2Helper(
bool bEnableTrimSpace, XmlFilterBase& rFilter ) :
50 mbEnableTrimSpace( bEnableTrimSpace ),
56ContextHandler2Helper::ContextHandler2Helper(
const ContextHandler2Helper& rParent ) :
57 mxContextStack( rParent.mxContextStack ),
58 mnRootStackSize( rParent.mxContextStack->
size() ),
59 mbEnableTrimSpace( rParent.mbEnableTrimSpace ),
64ContextHandler2Helper::~ContextHandler2Helper()
68sal_Int32 ContextHandler2Helper::getCurrentElementWithMce()
const
70 return mxContextStack->empty() ?
XML_ROOT_CONTEXT : mxContextStack->back().mnElement;
73sal_Int32 ContextHandler2Helper::getCurrentElement()
const
75 auto It = std::find_if(mxContextStack->rbegin(), mxContextStack->rend(),
76 [](
const ElementInfo& rItem) { return getNamespace(rItem.mnElement) != NMSP_mce; });
77 if (It != mxContextStack->rend())
82sal_Int32 ContextHandler2Helper::getParentElement( sal_Int32 nCountBack )
const
86 return (mxContextStack->size() ==
static_cast< size_t >( nCountBack )) ?
87 XML_ROOT_CONTEXT : (*mxContextStack)[ mxContextStack->size() - nCountBack - 1 ].mnElement;
90bool ContextHandler2Helper::isRootElement()
const
92 return mxContextStack->size() == mnRootStackSize + 1;
95Reference< XFastContextHandler > ContextHandler2Helper::implCreateChildContext(
96 sal_Int32 nElement,
const Reference< XFastAttributeList >& rxAttribs )
99 processCollectedChars();
100 ContextHandlerRef xContext = onCreateContext( nElement, AttributeList( rxAttribs ) );
104void ContextHandler2Helper::implStartElement( sal_Int32 nElement,
const Reference< XFastAttributeList >& rxAttribs )
106 AttributeList aAttribs( rxAttribs );
107 pushElementInfo( nElement ).mbTrimSpaces = aAttribs.getToken( XML_TOKEN( space ), XML_TOKEN_INVALID ) != XML_preserve;
108 onStartElement( aAttribs );
111void ContextHandler2Helper::implCharacters( std::u16string_view rChars )
114 if( !mxContextStack->empty() )
115 mxContextStack->back().maChars.append(rChars);
118void ContextHandler2Helper::implEndElement( sal_Int32 nElement )
120 OSL_ENSURE( getCurrentElementWithMce() == nElement,
"ContextHandler2Helper::implEndElement - context stack broken" );
121 if( !mxContextStack->empty() )
124 processCollectedChars();
130ContextHandlerRef ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
132 return onCreateRecordContext( nRecId, rStrm );
135void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
137 pushElementInfo( nRecId );
138 onStartRecord( rStrm );
141void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId )
143 OSL_ENSURE( getCurrentElementWithMce() == nRecId,
"ContextHandler2Helper::implEndRecord - context stack broken" );
144 if( !mxContextStack->empty() )
151ElementInfo& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement )
153 mxContextStack->emplace_back();
154 ElementInfo& rInfo = mxContextStack->back();
155 rInfo.mnElement = nElement;
159void ContextHandler2Helper::popElementInfo()
161 OSL_ENSURE( !mxContextStack->empty(),
"ContextHandler2Helper::popElementInfo - context stack broken" );
162 if( !mxContextStack->empty() )
163 mxContextStack->pop_back();
166void ContextHandler2Helper::processCollectedChars()
168 OSL_ENSURE( !mxContextStack->empty(),
"ContextHandler2Helper::processCollectedChars - no context info" );
169 if (mxContextStack->empty())
171 ElementInfo& rInfo = mxContextStack->back();
172 if( !rInfo.maChars.isEmpty() )
174 OUString aChars = rInfo.maChars.makeStringAndClear();
175 if( mbEnableTrimSpace && rInfo.mbTrimSpaces )
176 aChars = aChars.trim();
177 if( !aChars.isEmpty() )
178 onCharacters( aChars );
184 ContextHandler2Helper( rParent )
195 sal_Int32 nElement,
const Reference< XFastAttributeList >& rxAttribs )
197 if( getNamespace( nElement ) == NMSP_mce )
199 if( prepareMceContext( nElement,
AttributeList( rxAttribs ) ) )
204 return implCreateChildContext( nElement, rxAttribs );
208 sal_Int32 nElement,
const Reference< XFastAttributeList >& rxAttribs )
210 implStartElement( nElement, rxAttribs );
215 implCharacters( rChars );
220 implEndElement( nElement );
223bool ContextHandler2Helper::prepareMceContext( sal_Int32 nElement,
const AttributeList& rAttribs )
227 case MCE_TOKEN( AlternateContent ):
228 addMCEState( MCE_STATE::Started );
231 case MCE_TOKEN( Choice ):
233 if (isMCEStateEmpty() || getMCEState() != MCE_STATE::Started)
236 OUString aRequires = rAttribs.
getString( XML_Requires,
"none" );
242 std::vector<OUString> aSupportedNS =
251 Reference<XServiceInfo>
xModel(getDocFilter().getModel(), UNO_QUERY);
252 if (
xModel.is() &&
xModel->supportsService(
"com.sun.star.sheet.SpreadsheetDocument"))
255 auto it = std::find(aSupportedNS.begin(), aSupportedNS.end(),
"a14");
256 if (it != aSupportedNS.end())
258 aSupportedNS.erase(it);
262 if (std::find(aSupportedNS.begin(), aSupportedNS.end(), aRequires) != aSupportedNS.end())
263 setMCEState( MCE_STATE::FoundChoice ) ;
269 case MCE_TOKEN( Fallback ):
270 if( !isMCEStateEmpty() && getMCEState() == MCE_STATE::Started )
293 return implCreateRecordContext( nRecId,
rStrm );
298 implStartRecord( nRecId,
rStrm );
303 implEndRecord( nRecId );
Provides access to attribute values of an element.
OUString getStringDefaulted(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute, returns an empty string if attribute not present...
std::optional< OUString > getString(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute.
ContextHandler2(ContextHandler2Helper const &rParent)
virtual void onCharacters(const OUString &rChars) override
virtual void SAL_CALL characters(const OUString &rChars) final override
virtual void onEndElement() override
virtual ~ContextHandler2() override
virtual void onStartRecord(SequenceInputStream &rStrm) override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &rxAttribs) final override
virtual ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
virtual void onStartElement(const AttributeList &rAttribs) override
virtual ContextHandlerRef createRecordContext(sal_Int32 nRecId, SequenceInputStream &rStrm) override
virtual void startRecord(sal_Int32 nRecId, SequenceInputStream &rStrm) override
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &rxAttribs) final override
virtual void onEndRecord() override
virtual void SAL_CALL endFastElement(sal_Int32 nElement) final override
virtual void endRecord(sal_Int32 nRecId) override
virtual ContextHandlerRef onCreateRecordContext(sal_Int32 nRecId, SequenceInputStream &rStrm) override
if(aStr !=aBuf) UpdateName_Impl(m_xFollowLb.get()
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
std::shared_ptr< T > make_shared(Args &&... args)
const sal_Int32 XML_ROOT_CONTEXT
::rtl::Reference< ContextHandler > ContextHandlerRef
const PowerPointImport & mrFilter
Information about a processed element.
ElementInfo()
True = trims leading/trailing spaces from text data.
sal_Int32 mnElement
Collected element characters.
bool mbTrimSpaces
The element identifier.
Reference< XModel > xModel