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 8054 : 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 6178 : inline explicit ElementInfo() : mnElement( XML_TOKEN_INVALID ), mbTrimSpaces( false ) {}
44 : ElementInfo( sal_Int32 nElement ) : mnElement( nElement ), mbTrimSpaces(false) {}
45 : };
46 :
47 : // ============================================================================
48 :
49 216 : ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace ) :
50 216 : mxContextStack( new ContextStack ),
51 : mnRootStackSize( 0 ),
52 432 : mbEnableTrimSpace( bEnableTrimSpace )
53 : {
54 216 : pushElementInfo( XML_ROOT_CONTEXT );
55 216 : }
56 :
57 1174 : ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper& rParent ) :
58 : mxContextStack( rParent.mxContextStack ),
59 1174 : mnRootStackSize( rParent.mxContextStack->size() ),
60 2348 : mbEnableTrimSpace( rParent.mbEnableTrimSpace )
61 : {
62 1174 : }
63 :
64 1390 : ContextHandler2Helper::~ContextHandler2Helper()
65 : {
66 1390 : }
67 :
68 0 : sal_Int32 ContextHandler2Helper::getCurrentElementWithMce() const
69 : {
70 0 : return mxContextStack->empty() ? XML_ROOT_CONTEXT : mxContextStack->back().mnElement;
71 : }
72 :
73 12722 : sal_Int32 ContextHandler2Helper::getCurrentElement() const
74 : {
75 38250 : for ( ContextStack::reverse_iterator It = mxContextStack->rbegin();
76 25500 : It != mxContextStack->rend(); ++It )
77 12750 : if( getNamespace( It->mnElement ) != NMSP_mce )
78 12722 : return It->mnElement;
79 0 : return XML_ROOT_CONTEXT;
80 : }
81 :
82 148 : sal_Int32 ContextHandler2Helper::getParentElement( sal_Int32 nCountBack ) const
83 : {
84 148 : if( (nCountBack < 0) || (mxContextStack->size() < static_cast< size_t >( nCountBack )) )
85 0 : return XML_TOKEN_INVALID;
86 148 : return (mxContextStack->size() == static_cast< size_t >( nCountBack )) ?
87 148 : XML_ROOT_CONTEXT : (*mxContextStack)[ mxContextStack->size() - nCountBack - 1 ].mnElement;
88 : }
89 :
90 380 : bool ContextHandler2Helper::isRootElement() const
91 : {
92 380 : return mxContextStack->size() == mnRootStackSize + 1;
93 : }
94 :
95 8218 : Reference< XFastContextHandler > ContextHandler2Helper::implCreateChildContext(
96 : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
97 : {
98 : // #i76091# process collected characters (calls onCharacters() if needed)
99 8218 : processCollectedChars();
100 8218 : ContextHandlerRef xContext = onCreateContext( nElement, AttributeList( rxAttribs ) );
101 8218 : return Reference< XFastContextHandler >( xContext.get() );
102 : }
103 :
104 5962 : void ContextHandler2Helper::implStartElement( sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
105 : {
106 5962 : AttributeList aAttribs( rxAttribs );
107 5962 : pushElementInfo( nElement ).mbTrimSpaces = aAttribs.getToken( XML_TOKEN( space ), XML_TOKEN_INVALID ) != XML_preserve;
108 5962 : onStartElement( aAttribs );
109 5962 : }
110 :
111 2636 : void ContextHandler2Helper::implCharacters( const OUString& rChars )
112 : {
113 : // #i76091# collect characters until new element starts or this element ends
114 2636 : if( !mxContextStack->empty() )
115 2636 : mxContextStack->back().maChars.append(rChars);
116 2636 : }
117 :
118 5962 : 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 5962 : if( !mxContextStack->empty() )
123 : {
124 : // #i76091# process collected characters (calls onCharacters() if needed)
125 5962 : processCollectedChars();
126 5962 : onEndElement();
127 5962 : popElementInfo();
128 : }
129 5962 : }
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 6178 : ElementInfo& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement )
154 : {
155 6178 : mxContextStack->resize( mxContextStack->size() + 1 );
156 6178 : ElementInfo& rInfo = mxContextStack->back();
157 6178 : rInfo.mnElement = nElement;
158 6178 : return rInfo;
159 : }
160 :
161 5962 : void ContextHandler2Helper::popElementInfo()
162 : {
163 : OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
164 5962 : if( !mxContextStack->empty() )
165 5962 : mxContextStack->pop_back();
166 5962 : }
167 :
168 14180 : void ContextHandler2Helper::processCollectedChars()
169 : {
170 : OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::processCollectedChars - no context info" );
171 14180 : ElementInfo& rInfo = mxContextStack->back();
172 14180 : if( rInfo.maChars.getLength() > 0 )
173 : {
174 1996 : OUString aChars = rInfo.maChars.makeStringAndClear();
175 1996 : if( mbEnableTrimSpace && rInfo.mbTrimSpaces )
176 1734 : aChars = aChars.trim();
177 1996 : if( !aChars.isEmpty() )
178 1994 : onCharacters( aChars );
179 : }
180 14180 : }
181 :
182 : // ============================================================================
183 :
184 1042 : ContextHandler2::ContextHandler2( ContextHandler2Helper& rParent ) :
185 1042 : ContextHandler( dynamic_cast< ContextHandler& >( rParent ) ),
186 1042 : ContextHandler2Helper( rParent )
187 : {
188 1042 : }
189 :
190 1130 : ContextHandler2::~ContextHandler2()
191 : {
192 1130 : }
193 :
194 : // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
195 :
196 4842 : Reference< XFastContextHandler > SAL_CALL ContextHandler2::createFastChildContext(
197 : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException )
198 : {
199 4842 : return implCreateChildContext( nElement, rxAttribs );
200 : }
201 :
202 4760 : void SAL_CALL ContextHandler2::startFastElement(
203 : sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException )
204 : {
205 4760 : implStartElement( nElement, rxAttribs );
206 4760 : }
207 :
208 2006 : void SAL_CALL ContextHandler2::characters( const OUString& rChars ) throw( SAXException, RuntimeException )
209 : {
210 2006 : implCharacters( rChars );
211 2006 : }
212 :
213 4760 : void SAL_CALL ContextHandler2::endFastElement( sal_Int32 nElement ) throw( SAXException, RuntimeException )
214 : {
215 4760 : implEndElement( nElement );
216 4760 : }
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 4080 : void ContextHandler2::onStartElement( const AttributeList& )
243 : {
244 4080 : }
245 :
246 148 : void ContextHandler2::onCharacters( const OUString& )
247 : {
248 148 : }
249 :
250 1374 : void ContextHandler2::onEndElement()
251 : {
252 1374 : }
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: */
|