tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / oox / source / core / contexthandler2.cxx
blob5eb7252041251276063d191904222012804ed0a2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <oox/core/contexthandler2.hxx>
21 #include <oox/core/xmlfilterbase.hxx>
22 #include <oox/helper/attributelist.hxx>
23 #include <oox/token/namespaces.hxx>
24 #include <oox/token/tokens.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <o3tl/string_view.hxx>
28 #include <osl/diagnose.h>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
32 namespace oox::core {
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::xml::sax;
38 /** Information about a processed element. */
39 struct ElementInfo
41 OUStringBuffer maChars; /// Collected element characters.
42 sal_Int32 mnElement; /// The element identifier.
43 bool mbTrimSpaces; /// True = trims leading/trailing spaces from text data.
45 explicit ElementInfo() : maChars( 0), mnElement( XML_TOKEN_INVALID ), mbTrimSpaces( false ) {}
48 ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace ) :
49 mxContextStack( std::make_shared<ContextStack>() ),
50 mnRootStackSize( 0 ),
51 mbEnableTrimSpace( bEnableTrimSpace )
53 pushElementInfo( XML_ROOT_CONTEXT );
56 ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper& rParent ) :
57 mxContextStack( rParent.mxContextStack ),
58 mnRootStackSize( rParent.mxContextStack->size() ),
59 mbEnableTrimSpace( rParent.mbEnableTrimSpace )
63 ContextHandler2Helper::~ContextHandler2Helper()
67 sal_Int32 ContextHandler2Helper::getCurrentElementWithMce() const
69 return mxContextStack->empty() ? XML_ROOT_CONTEXT : mxContextStack->back().mnElement;
72 sal_Int32 ContextHandler2Helper::getCurrentElement() const
74 auto It = std::find_if(mxContextStack->rbegin(), mxContextStack->rend(),
75 [](const ElementInfo& rItem) { return getNamespace(rItem.mnElement) != NMSP_mce; });
76 if (It != mxContextStack->rend())
77 return It->mnElement;
78 return XML_ROOT_CONTEXT;
81 sal_Int32 ContextHandler2Helper::getParentElement( sal_Int32 nCountBack ) const
83 if( (nCountBack < 0) || (mxContextStack->size() < o3tl::make_unsigned( nCountBack )) )
84 return XML_TOKEN_INVALID;
85 return (mxContextStack->size() == static_cast< size_t >( nCountBack )) ?
86 XML_ROOT_CONTEXT : (*mxContextStack)[ mxContextStack->size() - nCountBack - 1 ].mnElement;
89 bool ContextHandler2Helper::isRootElement() const
91 return mxContextStack->size() == mnRootStackSize + 1;
94 Reference< XFastContextHandler > ContextHandler2Helper::implCreateChildContext(
95 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
97 // #i76091# process collected characters (calls onCharacters() if needed)
98 processCollectedChars();
99 ContextHandlerRef xContext = onCreateContext( nElement, AttributeList( rxAttribs ) );
100 return xContext;
103 void ContextHandler2Helper::implStartElement( sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
105 AttributeList aAttribs( rxAttribs );
106 pushElementInfo( nElement ).mbTrimSpaces = aAttribs.getToken( XML_TOKEN( space ), XML_TOKEN_INVALID ) != XML_preserve;
107 onStartElement( aAttribs );
110 void ContextHandler2Helper::implCharacters( std::u16string_view rChars )
112 // #i76091# collect characters until new element starts or this element ends
113 if( !mxContextStack->empty() )
114 mxContextStack->back().maChars.append(rChars);
117 void ContextHandler2Helper::implEndElement( sal_Int32 nElement )
119 OSL_ENSURE( getCurrentElementWithMce() == nElement, "ContextHandler2Helper::implEndElement - context stack broken" );
120 if( !mxContextStack->empty() )
122 // #i76091# process collected characters (calls onCharacters() if needed)
123 processCollectedChars();
124 onEndElement();
125 popElementInfo();
129 ContextHandlerRef ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
131 return onCreateRecordContext( nRecId, rStrm );
134 void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
136 pushElementInfo( nRecId );
137 onStartRecord( rStrm );
140 void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId )
142 OSL_ENSURE( getCurrentElementWithMce() == nRecId, "ContextHandler2Helper::implEndRecord - context stack broken" );
143 if( !mxContextStack->empty() )
145 onEndRecord();
146 popElementInfo();
150 ElementInfo& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement )
152 mxContextStack->emplace_back();
153 ElementInfo& rInfo = mxContextStack->back();
154 rInfo.mnElement = nElement;
155 return rInfo;
158 void ContextHandler2Helper::popElementInfo()
160 OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
161 if( !mxContextStack->empty() )
162 mxContextStack->pop_back();
165 void ContextHandler2Helper::processCollectedChars()
167 OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::processCollectedChars - no context info" );
168 if (mxContextStack->empty())
169 return;
170 ElementInfo& rInfo = mxContextStack->back();
171 if( !rInfo.maChars.isEmpty() )
173 OUString aChars = rInfo.maChars.makeStringAndClear();
174 if( mbEnableTrimSpace && rInfo.mbTrimSpaces )
175 aChars = aChars.trim();
176 if( !aChars.isEmpty() )
177 onCharacters( aChars );
181 ContextHandler2::ContextHandler2( ContextHandler2Helper const & rParent ) :
182 ContextHandler( dynamic_cast< ContextHandler const & >( rParent ) ),
183 ContextHandler2Helper( rParent )
187 ContextHandler2::~ContextHandler2()
191 // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
193 Reference< XFastContextHandler > SAL_CALL ContextHandler2::createFastChildContext(
194 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
196 if( getNamespace( nElement ) == NMSP_mce ) // TODO for checking 'Ignorable'
198 if( prepareMceContext( nElement, AttributeList( rxAttribs ) ) )
199 return this;
200 return nullptr;
203 return implCreateChildContext( nElement, rxAttribs );
206 void SAL_CALL ContextHandler2::startFastElement(
207 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
209 implStartElement( nElement, rxAttribs );
212 void SAL_CALL ContextHandler2::characters( const OUString& rChars )
214 implCharacters( rChars );
217 void SAL_CALL ContextHandler2::endFastElement( sal_Int32 nElement )
219 implEndElement( nElement );
222 bool ContextHandler2Helper::prepareMceContext( sal_Int32 nElement, const AttributeList& rAttribs )
224 switch( nElement )
226 case MCE_TOKEN( AlternateContent ):
227 addMCEState( MCE_STATE::Started );
228 break;
230 case MCE_TOKEN( Choice ):
231 if (!isMCEStateEmpty() && getMCEState() == MCE_STATE::Started)
233 OUString aRequires = rAttribs.getStringDefaulted(XML_Requires);
235 // At this point we can't access namespaces as the correct xml filter
236 // is long gone. For now let's decide depending on a list of supported
237 // namespaces like we do in writerfilter
239 static constexpr std::u16string_view aSupportedNS[] =
241 // u"a14", // We do not currently support inline formulas and other a14 stuff
242 u"p14",
243 u"p15",
244 u"x12ac",
245 u"v"
248 for (size_t pos = 0; pos != std::u16string_view::npos;)
250 // 'Requires' is a space-separated list
251 auto ns = o3tl::getToken(aRequires, u' ', pos);
252 if (!ns.empty() && std::find(std::begin(aSupportedNS), std::end(aSupportedNS), ns) == std::end(aSupportedNS))
253 return false;
256 setMCEState( MCE_STATE::FoundChoice ) ;
257 break;
259 return false;
261 case MCE_TOKEN( Fallback ):
262 if( !isMCEStateEmpty() && getMCEState() == MCE_STATE::Started )
263 break;
264 return false;
265 default:
267 OUString str = rAttribs.getStringDefaulted( MCE_TOKEN( Ignorable ));
268 if( !str.isEmpty() )
270 // Sequence< css::xml::FastAttribute > attrs = rAttribs.getFastAttributeList()->getFastAttributes();
271 // printf("MCE: %s\n", OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
272 // TODO: Check & Get the namespaces in "Ignorable"
273 // printf("NS: %d : %s\n", attrs.getLength(), OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
276 return false;
278 return true;
281 // oox.core.RecordContext interface -------------------------------------------
283 ContextHandlerRef ContextHandler2::createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
285 return implCreateRecordContext( nRecId, rStrm );
288 void ContextHandler2::startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
290 implStartRecord( nRecId, rStrm );
293 void ContextHandler2::endRecord( sal_Int32 nRecId )
295 implEndRecord( nRecId );
298 // oox.core.ContextHandler2Helper interface -----------------------------------
300 ContextHandlerRef ContextHandler2::onCreateContext( sal_Int32, const AttributeList& )
302 return nullptr;
305 void ContextHandler2::onStartElement( const AttributeList& )
309 void ContextHandler2::onCharacters( const OUString& )
313 void ContextHandler2::onEndElement()
317 ContextHandlerRef ContextHandler2::onCreateRecordContext( sal_Int32, SequenceInputStream& )
319 return nullptr;
322 void ContextHandler2::onStartRecord( SequenceInputStream& )
326 void ContextHandler2::onEndRecord()
330 } // namespace oox::core
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */