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 : #include <osl/diagnose.h>
23 :
24 : namespace oox {
25 : namespace core {
26 :
27 : using namespace ::com::sun::star::uno;
28 : using namespace ::com::sun::star::xml::sax;
29 :
30 : /** Information about a processed element. */
31 438569 : struct ElementInfo
32 : {
33 : OUStringBuffer maChars; /// Collected element characters.
34 : sal_Int32 mnElement; /// The element identifier.
35 : bool mbTrimSpaces; /// True = trims leading/trailing spaces from text data.
36 :
37 350303 : inline explicit ElementInfo() : maChars( 0), mnElement( XML_TOKEN_INVALID ), mbTrimSpaces( false ) {}
38 : };
39 :
40 8136 : ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace ) :
41 8136 : mxContextStack( new ContextStack ),
42 : mnRootStackSize( 0 ),
43 16272 : mbEnableTrimSpace( bEnableTrimSpace )
44 : {
45 8136 : pushElementInfo( XML_ROOT_CONTEXT );
46 8136 : }
47 :
48 246767 : ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper& rParent ) :
49 : mxContextStack( rParent.mxContextStack ),
50 246767 : mnRootStackSize( rParent.mxContextStack->size() ),
51 493534 : mbEnableTrimSpace( rParent.mbEnableTrimSpace )
52 : {
53 246767 : }
54 :
55 254903 : ContextHandler2Helper::~ContextHandler2Helper()
56 : {
57 254903 : }
58 :
59 0 : sal_Int32 ContextHandler2Helper::getCurrentElementWithMce() const
60 : {
61 0 : return mxContextStack->empty() ? XML_ROOT_CONTEXT : mxContextStack->back().mnElement;
62 : }
63 :
64 216794 : sal_Int32 ContextHandler2Helper::getCurrentElement() const
65 : {
66 650912 : for ( ContextStack::reverse_iterator It = mxContextStack->rbegin();
67 433935 : It != mxContextStack->rend(); ++It )
68 216970 : if( getNamespace( It->mnElement ) != NMSP_mce )
69 216790 : return It->mnElement;
70 0 : return XML_ROOT_CONTEXT;
71 : }
72 :
73 3537 : sal_Int32 ContextHandler2Helper::getParentElement( sal_Int32 nCountBack ) const
74 : {
75 3537 : if( (nCountBack < 0) || (mxContextStack->size() < static_cast< size_t >( nCountBack )) )
76 0 : return XML_TOKEN_INVALID;
77 3537 : return (mxContextStack->size() == static_cast< size_t >( nCountBack )) ?
78 3537 : XML_ROOT_CONTEXT : (*mxContextStack)[ mxContextStack->size() - nCountBack - 1 ].mnElement;
79 : }
80 :
81 14420 : bool ContextHandler2Helper::isRootElement() const
82 : {
83 14420 : return mxContextStack->size() == mnRootStackSize + 1;
84 : }
85 :
86 547628 : Reference< XFastContextHandler > ContextHandler2Helper::implCreateChildContext(
87 : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
88 : {
89 : // #i76091# process collected characters (calls onCharacters() if needed)
90 547628 : processCollectedChars();
91 547629 : ContextHandlerRef xContext = onCreateContext( nElement, AttributeList( rxAttribs ) );
92 547626 : return Reference< XFastContextHandler >( xContext.get() );
93 : }
94 :
95 342050 : void ContextHandler2Helper::implStartElement( sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
96 : {
97 342050 : AttributeList aAttribs( rxAttribs );
98 342049 : pushElementInfo( nElement ).mbTrimSpaces = aAttribs.getToken( XML_TOKEN( space ), XML_TOKEN_INVALID ) != XML_preserve;
99 342049 : onStartElement( aAttribs );
100 342052 : }
101 :
102 35266 : void ContextHandler2Helper::implCharacters( const OUString& rChars )
103 : {
104 : // #i76091# collect characters until new element starts or this element ends
105 35266 : if( !mxContextStack->empty() )
106 35266 : mxContextStack->back().maChars.append(rChars);
107 35266 : }
108 :
109 342050 : void ContextHandler2Helper::implEndElement( sal_Int32 nElement )
110 : {
111 : (void)nElement; // prevent "unused parameter" warning in product build
112 : OSL_ENSURE( getCurrentElementWithMce() == nElement, "ContextHandler2Helper::implEndElement - context stack broken" );
113 342050 : if( !mxContextStack->empty() )
114 : {
115 : // #i76091# process collected characters (calls onCharacters() if needed)
116 342050 : processCollectedChars();
117 342052 : onEndElement();
118 342051 : popElementInfo();
119 : }
120 342049 : }
121 :
122 311 : ContextHandlerRef ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
123 : {
124 311 : return onCreateRecordContext( nRecId, rStrm );
125 : }
126 :
127 118 : void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
128 : {
129 118 : pushElementInfo( nRecId );
130 118 : onStartRecord( rStrm );
131 118 : }
132 :
133 118 : void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId )
134 : {
135 : (void)nRecId; // prevent "unused parameter" warning in product build
136 : OSL_ENSURE( getCurrentElementWithMce() == nRecId, "ContextHandler2Helper::implEndRecord - context stack broken" );
137 118 : if( !mxContextStack->empty() )
138 : {
139 118 : onEndRecord();
140 118 : popElementInfo();
141 : }
142 118 : }
143 :
144 350303 : ElementInfo& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement )
145 : {
146 350303 : mxContextStack->resize( mxContextStack->size() + 1 );
147 350303 : ElementInfo& rInfo = mxContextStack->back();
148 350306 : rInfo.mnElement = nElement;
149 350306 : return rInfo;
150 : }
151 :
152 342169 : void ContextHandler2Helper::popElementInfo()
153 : {
154 : OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
155 342169 : if( !mxContextStack->empty() )
156 342169 : mxContextStack->pop_back();
157 342167 : }
158 :
159 889676 : void ContextHandler2Helper::processCollectedChars()
160 : {
161 : OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::processCollectedChars - no context info" );
162 889676 : if (mxContextStack->empty())
163 889693 : return;
164 889665 : ElementInfo& rInfo = mxContextStack->back();
165 889663 : if( !rInfo.maChars.isEmpty() )
166 : {
167 35173 : OUString aChars = rInfo.maChars.makeStringAndClear();
168 35173 : if( mbEnableTrimSpace && rInfo.mbTrimSpaces )
169 22856 : aChars = aChars.trim();
170 35174 : if( !aChars.isEmpty() )
171 20539 : onCharacters( aChars );
172 : }
173 : }
174 :
175 246062 : ContextHandler2::ContextHandler2( ContextHandler2Helper& rParent ) :
176 246062 : ContextHandler( dynamic_cast< ContextHandler& >( rParent ) ),
177 246062 : ContextHandler2Helper( rParent )
178 : {
179 246062 : }
180 :
181 246183 : ContextHandler2::~ContextHandler2()
182 : {
183 246183 : }
184 :
185 : // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
186 :
187 490123 : Reference< XFastContextHandler > SAL_CALL ContextHandler2::createFastChildContext(
188 : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException, std::exception )
189 : {
190 490123 : return implCreateChildContext( nElement, rxAttribs );
191 : }
192 :
193 327555 : void SAL_CALL ContextHandler2::startFastElement(
194 : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException, std::exception )
195 : {
196 327555 : implStartElement( nElement, rxAttribs );
197 327557 : }
198 :
199 33911 : void SAL_CALL ContextHandler2::characters( const OUString& rChars ) throw( SAXException, RuntimeException, std::exception )
200 : {
201 33911 : implCharacters( rChars );
202 33911 : }
203 :
204 327557 : void SAL_CALL ContextHandler2::endFastElement( sal_Int32 nElement ) throw( SAXException, RuntimeException, std::exception )
205 : {
206 327557 : implEndElement( nElement );
207 327556 : }
208 :
209 : // oox.core.RecordContext interface -------------------------------------------
210 :
211 179 : ContextHandlerRef ContextHandler2::createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
212 : {
213 179 : return implCreateRecordContext( nRecId, rStrm );
214 : }
215 :
216 40 : void ContextHandler2::startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
217 : {
218 40 : implStartRecord( nRecId, rStrm );
219 40 : }
220 :
221 40 : void ContextHandler2::endRecord( sal_Int32 nRecId )
222 : {
223 40 : implEndRecord( nRecId );
224 40 : }
225 :
226 : // oox.core.ContextHandler2Helper interface -----------------------------------
227 :
228 0 : ContextHandlerRef ContextHandler2::onCreateContext( sal_Int32, const AttributeList& )
229 : {
230 0 : return 0;
231 : }
232 :
233 260279 : void ContextHandler2::onStartElement( const AttributeList& )
234 : {
235 260279 : }
236 :
237 3019 : void ContextHandler2::onCharacters( const OUString& )
238 : {
239 3019 : }
240 :
241 297794 : void ContextHandler2::onEndElement()
242 : {
243 297794 : }
244 :
245 0 : ContextHandlerRef ContextHandler2::onCreateRecordContext( sal_Int32, SequenceInputStream& )
246 : {
247 0 : return 0;
248 : }
249 :
250 40 : void ContextHandler2::onStartRecord( SequenceInputStream& )
251 : {
252 40 : }
253 :
254 40 : void ContextHandler2::onEndRecord()
255 : {
256 40 : }
257 :
258 : } // namespace core
259 : } // namespace oox
260 :
261 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|