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