1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <osl/diagnose.h>
28 #include <com/sun/star/frame/XModel.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::com::sun::star::xml::sax
;
37 /** Information about a processed element. */
40 OUStringBuffer maChars
; /// Collected element characters.
41 sal_Int32 mnElement
; /// The element identifier.
42 bool mbTrimSpaces
; /// True = trims leading/trailing spaces from text data.
44 explicit ElementInfo() : maChars( 0), mnElement( XML_TOKEN_INVALID
), mbTrimSpaces( false ) {}
47 ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace
, XmlFilterBase
& rFilter
) :
48 mxContextStack( std::make_shared
<ContextStack
>() ),
50 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
),
60 mrFilter(rParent
.mrFilter
)
64 ContextHandler2Helper::~ContextHandler2Helper()
68 sal_Int32
ContextHandler2Helper::getCurrentElementWithMce() const
70 return mxContextStack
->empty() ? XML_ROOT_CONTEXT
: mxContextStack
->back().mnElement
;
73 sal_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())
79 return XML_ROOT_CONTEXT
;
82 sal_Int32
ContextHandler2Helper::getParentElement( sal_Int32 nCountBack
) const
84 if( (nCountBack
< 0) || (mxContextStack
->size() < o3tl::make_unsigned( nCountBack
)) )
85 return XML_TOKEN_INVALID
;
86 return (mxContextStack
->size() == static_cast< size_t >( nCountBack
)) ?
87 XML_ROOT_CONTEXT
: (*mxContextStack
)[ mxContextStack
->size() - nCountBack
- 1 ].mnElement
;
90 bool ContextHandler2Helper::isRootElement() const
92 return mxContextStack
->size() == mnRootStackSize
+ 1;
95 Reference
< XFastContextHandler
> ContextHandler2Helper::implCreateChildContext(
96 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
98 // #i76091# process collected characters (calls onCharacters() if needed)
99 processCollectedChars();
100 ContextHandlerRef xContext
= onCreateContext( nElement
, AttributeList( rxAttribs
) );
104 void 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
);
111 void ContextHandler2Helper::implCharacters( std::u16string_view rChars
)
113 // #i76091# collect characters until new element starts or this element ends
114 if( !mxContextStack
->empty() )
115 mxContextStack
->back().maChars
.append(rChars
);
118 void ContextHandler2Helper::implEndElement( sal_Int32 nElement
)
120 OSL_ENSURE( getCurrentElementWithMce() == nElement
, "ContextHandler2Helper::implEndElement - context stack broken" );
121 if( !mxContextStack
->empty() )
123 // #i76091# process collected characters (calls onCharacters() if needed)
124 processCollectedChars();
130 ContextHandlerRef
ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
132 return onCreateRecordContext( nRecId
, rStrm
);
135 void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
137 pushElementInfo( nRecId
);
138 onStartRecord( rStrm
);
141 void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId
)
143 OSL_ENSURE( getCurrentElementWithMce() == nRecId
, "ContextHandler2Helper::implEndRecord - context stack broken" );
144 if( !mxContextStack
->empty() )
151 ElementInfo
& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement
)
153 mxContextStack
->emplace_back();
154 ElementInfo
& rInfo
= mxContextStack
->back();
155 rInfo
.mnElement
= nElement
;
159 void ContextHandler2Helper::popElementInfo()
161 OSL_ENSURE( !mxContextStack
->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
162 if( !mxContextStack
->empty() )
163 mxContextStack
->pop_back();
166 void 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
);
182 ContextHandler2::ContextHandler2( ContextHandler2Helper
const & rParent
) :
183 ContextHandler( dynamic_cast< ContextHandler
const & >( rParent
) ),
184 ContextHandler2Helper( rParent
)
188 ContextHandler2::~ContextHandler2()
192 // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
194 Reference
< XFastContextHandler
> SAL_CALL
ContextHandler2::createFastChildContext(
195 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
197 if( getNamespace( nElement
) == NMSP_mce
) // TODO for checking 'Ignorable'
199 if( prepareMceContext( nElement
, AttributeList( rxAttribs
) ) )
204 return implCreateChildContext( nElement
, rxAttribs
);
207 void SAL_CALL
ContextHandler2::startFastElement(
208 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
210 implStartElement( nElement
, rxAttribs
);
213 void SAL_CALL
ContextHandler2::characters( const OUString
& rChars
)
215 implCharacters( rChars
);
218 void SAL_CALL
ContextHandler2::endFastElement( sal_Int32 nElement
)
220 implEndElement( nElement
);
223 bool 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" );
238 // At this point we can't access namespaces as the correct xml filter
239 // is long gone. For now let's decide depending on a list of supported
240 // namespaces like we do in writerfilter
242 std::vector
<OUString
> aSupportedNS
=
244 "a14", // Impress needs this to import math formulas.
251 Reference
<XServiceInfo
> xModel(getDocFilter().getModel(), UNO_QUERY
);
252 if (xModel
.is() && xModel
->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
254 // No a14 for Calc documents, it would cause duplicated shapes as-is.
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
)
275 OUString str
= rAttribs
.getStringDefaulted( MCE_TOKEN( Ignorable
));
278 // Sequence< css::xml::FastAttribute > attrs = rAttribs.getFastAttributeList()->getFastAttributes();
279 // printf("MCE: %s\n", OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
280 // TODO: Check & Get the namespaces in "Ignorable"
281 // printf("NS: %d : %s\n", attrs.getLength(), OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
289 // oox.core.RecordContext interface -------------------------------------------
291 ContextHandlerRef
ContextHandler2::createRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
293 return implCreateRecordContext( nRecId
, rStrm
);
296 void ContextHandler2::startRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
298 implStartRecord( nRecId
, rStrm
);
301 void ContextHandler2::endRecord( sal_Int32 nRecId
)
303 implEndRecord( nRecId
);
306 // oox.core.ContextHandler2Helper interface -----------------------------------
308 ContextHandlerRef
ContextHandler2::onCreateContext( sal_Int32
, const AttributeList
& )
313 void ContextHandler2::onStartElement( const AttributeList
& )
317 void ContextHandler2::onCharacters( const OUString
& )
321 void ContextHandler2::onEndElement()
325 ContextHandlerRef
ContextHandler2::onCreateRecordContext( sal_Int32
, SequenceInputStream
& )
330 void ContextHandler2::onStartRecord( SequenceInputStream
& )
334 void ContextHandler2::onEndRecord()
338 } // namespace oox::core
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */