Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "oox/core/contexthandler2.hxx"
21 : : #include <rtl/ustrbuf.hxx>
22 : :
23 : : namespace oox {
24 : : namespace core {
25 : :
26 : : // ============================================================================
27 : :
28 : : using namespace ::com::sun::star::uno;
29 : : using namespace ::com::sun::star::xml::sax;
30 : :
31 : : using ::rtl::OUString;
32 : : using ::rtl::OUStringBuffer;
33 : :
34 : : // ============================================================================
35 : :
36 : : /** Information about a processed element. */
37 : 6306 : struct ElementInfo
38 : : {
39 : : OUStringBuffer maChars; /// Collected element characters.
40 : : sal_Int32 mnElement; /// The element identifier.
41 : : bool mbTrimSpaces; /// True = trims leading/trailing spaces from text data.
42 : :
43 : 4230 : inline explicit ElementInfo() : mnElement( XML_TOKEN_INVALID ), mbTrimSpaces( false ) {}
44 : : ElementInfo( sal_Int32 nElement ) : mnElement( nElement ), mbTrimSpaces(false) {}
45 : : };
46 : :
47 : : // ============================================================================
48 : :
49 : 234 : ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace ) :
50 [ + - ]: 234 : mxContextStack( new ContextStack ),
51 : : mnRootStackSize( 0 ),
52 : 468 : mbEnableTrimSpace( bEnableTrimSpace )
53 : : {
54 [ + - ]: 234 : pushElementInfo( XML_ROOT_CONTEXT );
55 : 234 : }
56 : :
57 : 996 : ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper& rParent ) :
58 : : mxContextStack( rParent.mxContextStack ),
59 : 996 : mnRootStackSize( rParent.mxContextStack->size() ),
60 : 996 : mbEnableTrimSpace( rParent.mbEnableTrimSpace )
61 : : {
62 : 996 : }
63 : :
64 : 1230 : ContextHandler2Helper::~ContextHandler2Helper()
65 : : {
66 [ - + ]: 1230 : }
67 : :
68 : 0 : sal_Int32 ContextHandler2Helper::getCurrentElementWithMce() const
69 : : {
70 [ # # ]: 0 : return mxContextStack->empty() ? XML_ROOT_CONTEXT : mxContextStack->back().mnElement;
71 : : }
72 : :
73 : 7173 : sal_Int32 ContextHandler2Helper::getCurrentElement() const
74 : : {
75 [ + - + - ]: 14430 : for ( ContextStack::reverse_iterator It = mxContextStack->rbegin();
[ + - ]
76 : 7215 : It != mxContextStack->rend(); ++It )
77 [ + - ][ + + ]: 7215 : if( getNamespace( It->mnElement ) != NMSP_mce )
78 [ + - ]: 7173 : return It->mnElement;
79 : 7173 : return XML_ROOT_CONTEXT;
80 : : }
81 : :
82 : 132 : sal_Int32 ContextHandler2Helper::getParentElement( sal_Int32 nCountBack ) const
83 : : {
84 [ + - ][ - + ]: 132 : if( (nCountBack < 0) || (mxContextStack->size() < static_cast< size_t >( nCountBack )) )
[ - + ]
85 : 0 : return XML_TOKEN_INVALID;
86 : 132 : return (mxContextStack->size() == static_cast< size_t >( nCountBack )) ?
87 [ + - ]: 132 : XML_ROOT_CONTEXT : (*mxContextStack)[ mxContextStack->size() - nCountBack - 1 ].mnElement;
88 : : }
89 : :
90 : 378 : bool ContextHandler2Helper::isRootElement() const
91 : : {
92 : 378 : return mxContextStack->size() == mnRootStackSize + 1;
93 : : }
94 : :
95 : 5637 : Reference< XFastContextHandler > ContextHandler2Helper::implCreateChildContext(
96 : : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
97 : : {
98 : : // #i76091# process collected characters (calls onCharacters() if needed)
99 [ + - ]: 5637 : processCollectedChars();
100 [ + - ][ + - ]: 5637 : ContextHandlerRef xContext = onCreateContext( nElement, AttributeList( rxAttribs ) );
[ + - ]
101 [ + + ][ + - ]: 5637 : return Reference< XFastContextHandler >( xContext.get() );
102 : : }
103 : :
104 : 3996 : void ContextHandler2Helper::implStartElement( sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
105 : : {
106 [ + - ]: 3996 : AttributeList aAttribs( rxAttribs );
107 [ + - ][ + - ]: 3996 : pushElementInfo( nElement ).mbTrimSpaces = aAttribs.getToken( XML_TOKEN( space ), XML_TOKEN_INVALID ) != XML_preserve;
108 [ + - ][ + - ]: 3996 : onStartElement( aAttribs );
109 : 3996 : }
110 : :
111 : 1986 : void ContextHandler2Helper::implCharacters( const OUString& rChars )
112 : : {
113 : : // #i76091# collect characters until new element starts or this element ends
114 [ + - ]: 1986 : if( !mxContextStack->empty() )
115 : 1986 : mxContextStack->back().maChars.append(rChars);
116 : 1986 : }
117 : :
118 : 3996 : void ContextHandler2Helper::implEndElement( sal_Int32 nElement )
119 : : {
120 : : (void)nElement; // prevent "unused parameter" warning in product build
121 : : OSL_ENSURE( getCurrentElementWithMce() == nElement, "ContextHandler2Helper::implEndElement - context stack broken" );
122 [ + - ]: 3996 : if( !mxContextStack->empty() )
123 : : {
124 : : // #i76091# process collected characters (calls onCharacters() if needed)
125 : 3996 : processCollectedChars();
126 : 3996 : onEndElement();
127 : 3996 : popElementInfo();
128 : : }
129 : 3996 : }
130 : :
131 : 0 : ContextHandlerRef ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
132 : : {
133 : 0 : return onCreateRecordContext( nRecId, rStrm );
134 : : }
135 : :
136 : 0 : void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
137 : : {
138 : 0 : pushElementInfo( nRecId );
139 : 0 : onStartRecord( rStrm );
140 : 0 : }
141 : :
142 : 0 : void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId )
143 : : {
144 : : (void)nRecId; // prevent "unused parameter" warning in product build
145 : : OSL_ENSURE( getCurrentElementWithMce() == nRecId, "ContextHandler2Helper::implEndRecord - context stack broken" );
146 [ # # ]: 0 : if( !mxContextStack->empty() )
147 : : {
148 : 0 : onEndRecord();
149 : 0 : popElementInfo();
150 : : }
151 : 0 : }
152 : :
153 : 4230 : ElementInfo& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement )
154 : : {
155 : 4230 : mxContextStack->resize( mxContextStack->size() + 1 );
156 : 4230 : ElementInfo& rInfo = mxContextStack->back();
157 : 4230 : rInfo.mnElement = nElement;
158 : 4230 : return rInfo;
159 : : }
160 : :
161 : 3996 : void ContextHandler2Helper::popElementInfo()
162 : : {
163 : : OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
164 [ + - ]: 3996 : if( !mxContextStack->empty() )
165 : 3996 : mxContextStack->pop_back();
166 : 3996 : }
167 : :
168 : 9633 : void ContextHandler2Helper::processCollectedChars()
169 : : {
170 : : OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::processCollectedChars - no context info" );
171 : 9633 : ElementInfo& rInfo = mxContextStack->back();
172 [ + + ]: 9633 : if( rInfo.maChars.getLength() > 0 )
173 : : {
174 [ + - ]: 1161 : OUString aChars = rInfo.maChars.makeStringAndClear();
175 [ + + ][ + + ]: 1161 : if( mbEnableTrimSpace && rInfo.mbTrimSpaces )
176 : 819 : aChars = aChars.trim();
177 [ + + ]: 1161 : if( !aChars.isEmpty() )
178 [ + - ]: 1161 : onCharacters( aChars );
179 : : }
180 : 9633 : }
181 : :
182 : : // ============================================================================
183 : :
184 : 969 : ContextHandler2::ContextHandler2( ContextHandler2Helper& rParent ) :
185 : 969 : ContextHandler( dynamic_cast< ContextHandler& >( rParent ) ),
186 [ + - ][ + - ]: 969 : ContextHandler2Helper( rParent )
187 : : {
188 : 969 : }
189 : :
190 [ + - ]: 978 : ContextHandler2::~ContextHandler2()
191 : : {
192 [ - + ]: 978 : }
193 : :
194 : : // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
195 : :
196 : 2235 : Reference< XFastContextHandler > SAL_CALL ContextHandler2::createFastChildContext(
197 : : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException )
198 : : {
199 : 2235 : return implCreateChildContext( nElement, rxAttribs );
200 : : }
201 : :
202 : 2562 : void SAL_CALL ContextHandler2::startFastElement(
203 : : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException )
204 : : {
205 : 2562 : implStartElement( nElement, rxAttribs );
206 : 2562 : }
207 : :
208 : 1155 : void SAL_CALL ContextHandler2::characters( const OUString& rChars ) throw( SAXException, RuntimeException )
209 : : {
210 : 1155 : implCharacters( rChars );
211 : 1155 : }
212 : :
213 : 2562 : void SAL_CALL ContextHandler2::endFastElement( sal_Int32 nElement ) throw( SAXException, RuntimeException )
214 : : {
215 : 2562 : implEndElement( nElement );
216 : 2562 : }
217 : :
218 : : // oox.core.RecordContext interface -------------------------------------------
219 : :
220 : 0 : ContextHandlerRef ContextHandler2::createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
221 : : {
222 : 0 : return implCreateRecordContext( nRecId, rStrm );
223 : : }
224 : :
225 : 0 : void ContextHandler2::startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
226 : : {
227 : 0 : implStartRecord( nRecId, rStrm );
228 : 0 : }
229 : :
230 : 0 : void ContextHandler2::endRecord( sal_Int32 nRecId )
231 : : {
232 : 0 : implEndRecord( nRecId );
233 : 0 : }
234 : :
235 : : // oox.core.ContextHandler2Helper interface -----------------------------------
236 : :
237 : 0 : ContextHandlerRef ContextHandler2::onCreateContext( sal_Int32, const AttributeList& )
238 : : {
239 : 0 : return 0;
240 : : }
241 : :
242 : 1941 : void ContextHandler2::onStartElement( const AttributeList& )
243 : : {
244 : 1941 : }
245 : :
246 : 192 : void ContextHandler2::onCharacters( const OUString& )
247 : : {
248 : 192 : }
249 : :
250 : 1221 : void ContextHandler2::onEndElement()
251 : : {
252 : 1221 : }
253 : :
254 : 0 : ContextHandlerRef ContextHandler2::onCreateRecordContext( sal_Int32, SequenceInputStream& )
255 : : {
256 : 0 : return 0;
257 : : }
258 : :
259 : 0 : void ContextHandler2::onStartRecord( SequenceInputStream& )
260 : : {
261 : 0 : }
262 : :
263 : 0 : void ContextHandler2::onEndRecord()
264 : : {
265 : 0 : }
266 : :
267 : : // ============================================================================
268 : :
269 : : } // namespace core
270 : : } // namespace oox
271 : :
272 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|