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 <rtl/ustrbuf.hxx>
26 // ============================================================================
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::xml::sax
;
32 // ============================================================================
34 /** Information about a processed element. */
37 OUStringBuffer maChars
; /// Collected element characters.
38 sal_Int32 mnElement
; /// The element identifier.
39 bool mbTrimSpaces
; /// True = trims leading/trailing spaces from text data.
41 inline explicit ElementInfo() : maChars( 0), mnElement( XML_TOKEN_INVALID
), mbTrimSpaces( false ) {}
42 ElementInfo( sal_Int32 nElement
) : maChars( 0 ), mnElement( nElement
), mbTrimSpaces(false) {}
45 // ============================================================================
47 ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace
) :
48 mxContextStack( new ContextStack
),
50 mbEnableTrimSpace( bEnableTrimSpace
)
52 pushElementInfo( XML_ROOT_CONTEXT
);
55 ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper
& rParent
) :
56 mxContextStack( rParent
.mxContextStack
),
57 mnRootStackSize( rParent
.mxContextStack
->size() ),
58 mbEnableTrimSpace( rParent
.mbEnableTrimSpace
)
62 ContextHandler2Helper::~ContextHandler2Helper()
66 sal_Int32
ContextHandler2Helper::getCurrentElementWithMce() const
68 return mxContextStack
->empty() ? XML_ROOT_CONTEXT
: mxContextStack
->back().mnElement
;
71 sal_Int32
ContextHandler2Helper::getCurrentElement() const
73 for ( ContextStack::reverse_iterator It
= mxContextStack
->rbegin();
74 It
!= mxContextStack
->rend(); ++It
)
75 if( getNamespace( It
->mnElement
) != NMSP_mce
)
77 return XML_ROOT_CONTEXT
;
80 sal_Int32
ContextHandler2Helper::getParentElement( sal_Int32 nCountBack
) const
82 if( (nCountBack
< 0) || (mxContextStack
->size() < static_cast< size_t >( nCountBack
)) )
83 return XML_TOKEN_INVALID
;
84 return (mxContextStack
->size() == static_cast< size_t >( nCountBack
)) ?
85 XML_ROOT_CONTEXT
: (*mxContextStack
)[ mxContextStack
->size() - nCountBack
- 1 ].mnElement
;
88 bool ContextHandler2Helper::isRootElement() const
90 return mxContextStack
->size() == mnRootStackSize
+ 1;
93 Reference
< XFastContextHandler
> ContextHandler2Helper::implCreateChildContext(
94 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
96 // #i76091# process collected characters (calls onCharacters() if needed)
97 processCollectedChars();
98 ContextHandlerRef xContext
= onCreateContext( nElement
, AttributeList( rxAttribs
) );
99 return Reference
< XFastContextHandler
>( xContext
.get() );
102 void ContextHandler2Helper::implStartElement( sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
104 AttributeList
aAttribs( rxAttribs
);
105 pushElementInfo( nElement
).mbTrimSpaces
= aAttribs
.getToken( XML_TOKEN( space
), XML_TOKEN_INVALID
) != XML_preserve
;
106 onStartElement( aAttribs
);
109 void ContextHandler2Helper::implCharacters( const OUString
& rChars
)
111 // #i76091# collect characters until new element starts or this element ends
112 if( !mxContextStack
->empty() )
113 mxContextStack
->back().maChars
.append(rChars
);
116 void ContextHandler2Helper::implEndElement( sal_Int32 nElement
)
118 (void)nElement
; // prevent "unused parameter" warning in product build
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();
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 (void)nRecId
; // prevent "unused parameter" warning in product build
143 OSL_ENSURE( getCurrentElementWithMce() == nRecId
, "ContextHandler2Helper::implEndRecord - context stack broken" );
144 if( !mxContextStack
->empty() )
151 ElementInfo
& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement
)
153 mxContextStack
->resize( mxContextStack
->size() + 1 );
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 ElementInfo
& rInfo
= mxContextStack
->back();
170 if( !rInfo
.maChars
.isEmpty() )
172 OUString aChars
= rInfo
.maChars
.makeStringAndClear();
173 if( mbEnableTrimSpace
&& rInfo
.mbTrimSpaces
)
174 aChars
= aChars
.trim();
175 if( !aChars
.isEmpty() )
176 onCharacters( aChars
);
180 // ============================================================================
182 ContextHandler2::ContextHandler2( ContextHandler2Helper
& rParent
) :
183 ContextHandler( dynamic_cast< ContextHandler
& >( 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
) throw( SAXException
, RuntimeException
)
197 return implCreateChildContext( nElement
, rxAttribs
);
200 void SAL_CALL
ContextHandler2::startFastElement(
201 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
) throw( SAXException
, RuntimeException
)
203 implStartElement( nElement
, rxAttribs
);
206 void SAL_CALL
ContextHandler2::characters( const OUString
& rChars
) throw( SAXException
, RuntimeException
)
208 implCharacters( rChars
);
211 void SAL_CALL
ContextHandler2::endFastElement( sal_Int32 nElement
) throw( SAXException
, RuntimeException
)
213 implEndElement( nElement
);
216 // oox.core.RecordContext interface -------------------------------------------
218 ContextHandlerRef
ContextHandler2::createRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
220 return implCreateRecordContext( nRecId
, rStrm
);
223 void ContextHandler2::startRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
225 implStartRecord( nRecId
, rStrm
);
228 void ContextHandler2::endRecord( sal_Int32 nRecId
)
230 implEndRecord( nRecId
);
233 // oox.core.ContextHandler2Helper interface -----------------------------------
235 ContextHandlerRef
ContextHandler2::onCreateContext( sal_Int32
, const AttributeList
& )
240 void ContextHandler2::onStartElement( const AttributeList
& )
244 void ContextHandler2::onCharacters( const OUString
& )
248 void ContextHandler2::onEndElement()
252 ContextHandlerRef
ContextHandler2::onCreateRecordContext( sal_Int32
, SequenceInputStream
& )
257 void ContextHandler2::onStartRecord( SequenceInputStream
& )
261 void ContextHandler2::onEndRecord()
265 // ============================================================================
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */