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