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/helper/attributelist.hxx>
22 #include <oox/token/namespaces.hxx>
23 #include <oox/token/tokens.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <osl/diagnose.h>
30 using namespace ::com::sun::star::uno
;
31 using namespace ::com::sun::star::xml::sax
;
33 /** Information about a processed element. */
36 OUStringBuffer maChars
; /// Collected element characters.
37 sal_Int32 mnElement
; /// The element identifier.
38 bool mbTrimSpaces
; /// True = trims leading/trailing spaces from text data.
40 explicit ElementInfo() : maChars( 0), mnElement( XML_TOKEN_INVALID
), mbTrimSpaces( false ) {}
43 ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace
) :
44 mxContextStack( new ContextStack
),
46 mbEnableTrimSpace( bEnableTrimSpace
)
48 pushElementInfo( XML_ROOT_CONTEXT
);
51 ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper
& rParent
) :
52 mxContextStack( rParent
.mxContextStack
),
53 mnRootStackSize( rParent
.mxContextStack
->size() ),
54 mbEnableTrimSpace( rParent
.mbEnableTrimSpace
)
58 ContextHandler2Helper::~ContextHandler2Helper()
62 sal_Int32
ContextHandler2Helper::getCurrentElementWithMce() const
64 return mxContextStack
->empty() ? XML_ROOT_CONTEXT
: mxContextStack
->back().mnElement
;
67 sal_Int32
ContextHandler2Helper::getCurrentElement() const
69 auto It
= std::find_if(mxContextStack
->rbegin(), mxContextStack
->rend(),
70 [](const ElementInfo
& rItem
) { return getNamespace(rItem
.mnElement
) != NMSP_mce
; });
71 if (It
!= mxContextStack
->rend())
73 return XML_ROOT_CONTEXT
;
76 sal_Int32
ContextHandler2Helper::getParentElement( sal_Int32 nCountBack
) const
78 if( (nCountBack
< 0) || (mxContextStack
->size() < static_cast< size_t >( nCountBack
)) )
79 return XML_TOKEN_INVALID
;
80 return (mxContextStack
->size() == static_cast< size_t >( nCountBack
)) ?
81 XML_ROOT_CONTEXT
: (*mxContextStack
)[ mxContextStack
->size() - nCountBack
- 1 ].mnElement
;
84 bool ContextHandler2Helper::isRootElement() const
86 return mxContextStack
->size() == mnRootStackSize
+ 1;
89 Reference
< XFastContextHandler
> ContextHandler2Helper::implCreateChildContext(
90 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
92 // #i76091# process collected characters (calls onCharacters() if needed)
93 processCollectedChars();
94 ContextHandlerRef xContext
= onCreateContext( nElement
, AttributeList( rxAttribs
) );
95 return Reference
< XFastContextHandler
>( xContext
.get() );
98 void ContextHandler2Helper::implStartElement( sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
100 AttributeList
aAttribs( rxAttribs
);
101 pushElementInfo( nElement
).mbTrimSpaces
= aAttribs
.getToken( XML_TOKEN( space
), XML_TOKEN_INVALID
) != XML_preserve
;
102 onStartElement( aAttribs
);
105 void ContextHandler2Helper::implCharacters( const OUString
& rChars
)
107 // #i76091# collect characters until new element starts or this element ends
108 if( !mxContextStack
->empty() )
109 mxContextStack
->back().maChars
.append(rChars
);
112 void ContextHandler2Helper::implEndElement( sal_Int32 nElement
)
114 OSL_ENSURE( getCurrentElementWithMce() == nElement
, "ContextHandler2Helper::implEndElement - context stack broken" );
115 if( !mxContextStack
->empty() )
117 // #i76091# process collected characters (calls onCharacters() if needed)
118 processCollectedChars();
124 ContextHandlerRef
ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
126 return onCreateRecordContext( nRecId
, rStrm
);
129 void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
131 pushElementInfo( nRecId
);
132 onStartRecord( rStrm
);
135 void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId
)
137 OSL_ENSURE( getCurrentElementWithMce() == nRecId
, "ContextHandler2Helper::implEndRecord - context stack broken" );
138 if( !mxContextStack
->empty() )
145 ElementInfo
& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement
)
147 mxContextStack
->emplace_back();
148 ElementInfo
& rInfo
= mxContextStack
->back();
149 rInfo
.mnElement
= nElement
;
153 void ContextHandler2Helper::popElementInfo()
155 OSL_ENSURE( !mxContextStack
->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
156 if( !mxContextStack
->empty() )
157 mxContextStack
->pop_back();
160 void ContextHandler2Helper::processCollectedChars()
162 OSL_ENSURE( !mxContextStack
->empty(), "ContextHandler2Helper::processCollectedChars - no context info" );
163 if (mxContextStack
->empty())
165 ElementInfo
& rInfo
= mxContextStack
->back();
166 if( !rInfo
.maChars
.isEmpty() )
168 OUString aChars
= rInfo
.maChars
.makeStringAndClear();
169 if( mbEnableTrimSpace
&& rInfo
.mbTrimSpaces
)
170 aChars
= aChars
.trim();
171 if( !aChars
.isEmpty() )
172 onCharacters( aChars
);
176 ContextHandler2::ContextHandler2( ContextHandler2Helper
const & rParent
) :
177 ContextHandler( dynamic_cast< ContextHandler
const & >( rParent
) ),
178 ContextHandler2Helper( rParent
)
182 ContextHandler2::~ContextHandler2()
186 // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
188 Reference
< XFastContextHandler
> SAL_CALL
ContextHandler2::createFastChildContext(
189 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
191 return implCreateChildContext( nElement
, rxAttribs
);
194 void SAL_CALL
ContextHandler2::startFastElement(
195 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
197 implStartElement( nElement
, rxAttribs
);
200 void SAL_CALL
ContextHandler2::characters( const OUString
& rChars
)
202 implCharacters( rChars
);
205 void SAL_CALL
ContextHandler2::endFastElement( sal_Int32 nElement
)
207 implEndElement( nElement
);
210 // oox.core.RecordContext interface -------------------------------------------
212 ContextHandlerRef
ContextHandler2::createRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
214 return implCreateRecordContext( nRecId
, rStrm
);
217 void ContextHandler2::startRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
219 implStartRecord( nRecId
, rStrm
);
222 void ContextHandler2::endRecord( sal_Int32 nRecId
)
224 implEndRecord( nRecId
);
227 // oox.core.ContextHandler2Helper interface -----------------------------------
229 ContextHandlerRef
ContextHandler2::onCreateContext( sal_Int32
, const AttributeList
& )
234 void ContextHandler2::onStartElement( const AttributeList
& )
238 void ContextHandler2::onCharacters( const OUString
& )
242 void ContextHandler2::onEndElement()
246 ContextHandlerRef
ContextHandler2::onCreateRecordContext( sal_Int32
, SequenceInputStream
& )
251 void ContextHandler2::onStartRecord( SequenceInputStream
& )
255 void ContextHandler2::onEndRecord()
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */