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