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>
22 #include <osl/diagnose.h>
27 using namespace ::com::sun::star::uno
;
28 using namespace ::com::sun::star::xml::sax
;
30 /** Information about a processed element. */
33 OUStringBuffer maChars
; /// Collected element characters.
34 sal_Int32 mnElement
; /// The element identifier.
35 bool mbTrimSpaces
; /// True = trims leading/trailing spaces from text data.
37 inline explicit ElementInfo() : maChars( 0), mnElement( XML_TOKEN_INVALID
), mbTrimSpaces( false ) {}
40 ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace
) :
41 mxContextStack( new ContextStack
),
43 mbEnableTrimSpace( bEnableTrimSpace
)
45 pushElementInfo( XML_ROOT_CONTEXT
);
48 ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper
& rParent
) :
49 mxContextStack( rParent
.mxContextStack
),
50 mnRootStackSize( rParent
.mxContextStack
->size() ),
51 mbEnableTrimSpace( rParent
.mbEnableTrimSpace
)
55 ContextHandler2Helper::~ContextHandler2Helper()
59 sal_Int32
ContextHandler2Helper::getCurrentElementWithMce() const
61 return mxContextStack
->empty() ? XML_ROOT_CONTEXT
: mxContextStack
->back().mnElement
;
64 sal_Int32
ContextHandler2Helper::getCurrentElement() const
66 for ( ContextStack::reverse_iterator It
= mxContextStack
->rbegin();
67 It
!= mxContextStack
->rend(); ++It
)
68 if( getNamespace( It
->mnElement
) != NMSP_mce
)
70 return XML_ROOT_CONTEXT
;
73 sal_Int32
ContextHandler2Helper::getParentElement( sal_Int32 nCountBack
) const
75 if( (nCountBack
< 0) || (mxContextStack
->size() < static_cast< size_t >( nCountBack
)) )
76 return XML_TOKEN_INVALID
;
77 return (mxContextStack
->size() == static_cast< size_t >( nCountBack
)) ?
78 XML_ROOT_CONTEXT
: (*mxContextStack
)[ mxContextStack
->size() - nCountBack
- 1 ].mnElement
;
81 bool ContextHandler2Helper::isRootElement() const
83 return mxContextStack
->size() == mnRootStackSize
+ 1;
86 Reference
< XFastContextHandler
> ContextHandler2Helper::implCreateChildContext(
87 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
89 // #i76091# process collected characters (calls onCharacters() if needed)
90 processCollectedChars();
91 ContextHandlerRef xContext
= onCreateContext( nElement
, AttributeList( rxAttribs
) );
92 return Reference
< XFastContextHandler
>( xContext
.get() );
95 void ContextHandler2Helper::implStartElement( sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
)
97 AttributeList
aAttribs( rxAttribs
);
98 pushElementInfo( nElement
).mbTrimSpaces
= aAttribs
.getToken( XML_TOKEN( space
), XML_TOKEN_INVALID
) != XML_preserve
;
99 onStartElement( aAttribs
);
102 void ContextHandler2Helper::implCharacters( const OUString
& rChars
)
104 // #i76091# collect characters until new element starts or this element ends
105 if( !mxContextStack
->empty() )
106 mxContextStack
->back().maChars
.append(rChars
);
109 void ContextHandler2Helper::implEndElement( sal_Int32 nElement
)
111 (void)nElement
; // prevent "unused parameter" warning in product build
112 OSL_ENSURE( getCurrentElementWithMce() == nElement
, "ContextHandler2Helper::implEndElement - context stack broken" );
113 if( !mxContextStack
->empty() )
115 // #i76091# process collected characters (calls onCharacters() if needed)
116 processCollectedChars();
122 ContextHandlerRef
ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
124 return onCreateRecordContext( nRecId
, rStrm
);
127 void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
129 pushElementInfo( nRecId
);
130 onStartRecord( rStrm
);
133 void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId
)
135 (void)nRecId
; // prevent "unused parameter" warning in product build
136 OSL_ENSURE( getCurrentElementWithMce() == nRecId
, "ContextHandler2Helper::implEndRecord - context stack broken" );
137 if( !mxContextStack
->empty() )
144 ElementInfo
& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement
)
146 mxContextStack
->resize( mxContextStack
->size() + 1 );
147 ElementInfo
& rInfo
= mxContextStack
->back();
148 rInfo
.mnElement
= nElement
;
152 void ContextHandler2Helper::popElementInfo()
154 OSL_ENSURE( !mxContextStack
->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
155 if( !mxContextStack
->empty() )
156 mxContextStack
->pop_back();
159 void ContextHandler2Helper::processCollectedChars()
161 OSL_ENSURE( !mxContextStack
->empty(), "ContextHandler2Helper::processCollectedChars - no context info" );
162 if (mxContextStack
->empty())
164 ElementInfo
& rInfo
= mxContextStack
->back();
165 if( !rInfo
.maChars
.isEmpty() )
167 OUString aChars
= rInfo
.maChars
.makeStringAndClear();
168 if( mbEnableTrimSpace
&& rInfo
.mbTrimSpaces
)
169 aChars
= aChars
.trim();
170 if( !aChars
.isEmpty() )
171 onCharacters( aChars
);
175 ContextHandler2::ContextHandler2( ContextHandler2Helper
& rParent
) :
176 ContextHandler( dynamic_cast< ContextHandler
& >( rParent
) ),
177 ContextHandler2Helper( rParent
)
181 ContextHandler2::~ContextHandler2()
185 // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
187 Reference
< XFastContextHandler
> SAL_CALL
ContextHandler2::createFastChildContext(
188 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
) throw( SAXException
, RuntimeException
, std::exception
)
190 return implCreateChildContext( nElement
, rxAttribs
);
193 void SAL_CALL
ContextHandler2::startFastElement(
194 sal_Int32 nElement
, const Reference
< XFastAttributeList
>& rxAttribs
) throw( SAXException
, RuntimeException
, std::exception
)
196 implStartElement( nElement
, rxAttribs
);
199 void SAL_CALL
ContextHandler2::characters( const OUString
& rChars
) throw( SAXException
, RuntimeException
, std::exception
)
201 implCharacters( rChars
);
204 void SAL_CALL
ContextHandler2::endFastElement( sal_Int32 nElement
) throw( SAXException
, RuntimeException
, std::exception
)
206 implEndElement( nElement
);
209 // oox.core.RecordContext interface -------------------------------------------
211 ContextHandlerRef
ContextHandler2::createRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
213 return implCreateRecordContext( nRecId
, rStrm
);
216 void ContextHandler2::startRecord( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
218 implStartRecord( nRecId
, rStrm
);
221 void ContextHandler2::endRecord( sal_Int32 nRecId
)
223 implEndRecord( nRecId
);
226 // oox.core.ContextHandler2Helper interface -----------------------------------
228 ContextHandlerRef
ContextHandler2::onCreateContext( sal_Int32
, const AttributeList
& )
233 void ContextHandler2::onStartElement( const AttributeList
& )
237 void ContextHandler2::onCharacters( const OUString
& )
241 void ContextHandler2::onEndElement()
245 ContextHandlerRef
ContextHandler2::onCreateRecordContext( sal_Int32
, SequenceInputStream
& )
250 void ContextHandler2::onStartRecord( SequenceInputStream
& )
254 void ContextHandler2::onEndRecord()
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */