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 : #ifndef OOX_CORE_CONTEXTHANDLER2_HXX
21 : #define OOX_CORE_CONTEXTHANDLER2_HXX
22 :
23 : #include <vector>
24 : #include <boost/shared_ptr.hpp>
25 : #include "oox/helper/attributelist.hxx"
26 : #include "oox/helper/binaryinputstream.hxx"
27 : #include "oox/core/contexthandler.hxx"
28 : #include "oox/dllapi.h"
29 :
30 : namespace oox {
31 : namespace core {
32 :
33 : // ============================================================================
34 :
35 : const sal_Int32 XML_ROOT_CONTEXT = SAL_MAX_INT32;
36 :
37 : // ============================================================================
38 :
39 : struct ElementInfo;
40 :
41 : /** Helper class that provides a context stack.
42 :
43 : Fragment handlers and context handlers derived from this helper class will
44 : track the identifiers of the visited elements in a stack. The idea is to
45 : use the same instance of a fragment handler or context handler to process
46 : several nested elements in an XML stream. For that, the abstract function
47 : onCreateContext() has to return 'this' for the passed element.
48 :
49 : Derived classes have to implement the createFastChildContext(),
50 : startFastElement(), characters(), and endFastElement() functions from the
51 : com.sun.star.xml.sax.XFastContextHandler interface by simply forwarding
52 : them to the respective implCreateChildContext(), implStartElement(),
53 : implCharacters(), and implEndElement() functions of this helper. This is
54 : implemented already in the classes ContextHandler2 and FragmentHandler2.
55 : The new abstract functions have to be implemented according to the elements
56 : to be processed.
57 :
58 : Similarly, for binary import, derived classes have to forward the
59 : createRecordContext(), startRecord(), and endRecord() functions from the
60 : ContextHandler class to the implCreateRecordContext(), implStartRecord(),
61 : and implEndRecord() functions of this helper. Again, this is implemented
62 : already in the classes ContextHandler2 and FragmentHandler2.
63 : */
64 : class OOX_DLLPUBLIC ContextHandler2Helper
65 : {
66 : public:
67 : explicit ContextHandler2Helper( bool bEnableTrimSpace );
68 : explicit ContextHandler2Helper( const ContextHandler2Helper& rParent );
69 : virtual ~ContextHandler2Helper();
70 :
71 : // allow instances to be stored in ::rtl::Reference
72 : virtual void SAL_CALL acquire() throw() = 0;
73 : virtual void SAL_CALL release() throw() = 0;
74 :
75 : // interface --------------------------------------------------------------
76 :
77 : /** Will be called to create a context handler for the passed element.
78 :
79 : Usually 'this' can be returned to improve performance by reusing the
80 : same instance to process several elements. Used by OOXML import only.
81 : */
82 : virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) = 0;
83 :
84 : /** Will be called when a new element has been started.
85 :
86 : This function is called at the context handler returned from
87 : onCreateContext(), or, for root elements of an XML stream, at the
88 : fragment handler itself.
89 :
90 : The current element identifier can be accessed with getCurrentElement()
91 : or isCurrentElement(). Used by OOXML import only.
92 : */
93 : virtual void onStartElement( const AttributeList& rAttribs ) = 0;
94 :
95 : /** Will be called before a new child element starts, or if the current
96 : element is about to be left.
97 :
98 : This helper function collects all text fragments received by the
99 : characters() function (such as encoded characters which are passed in
100 : separate calls to the characters() function), and passes the
101 : concatenated and trimmed string.
102 :
103 : The current element identifier can be accessed with getCurrentElement()
104 : or isCurrentElement(). Used by OOXML import only.
105 : */
106 : virtual void onCharacters( const ::rtl::OUString& rChars ) = 0;
107 :
108 : /** Will be called when the current element is about to be left.
109 :
110 : The current element identifier can be accessed with getCurrentElement()
111 : or isCurrentElement(). Used by OOXML import only.
112 : */
113 : virtual void onEndElement() = 0;
114 :
115 : /** Will be called to create a context handler for the passed record.
116 :
117 : Usually 'this' can be returned to improve performance by reusing the
118 : same instance to process several records. Used by BIFF import only.
119 : */
120 : virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ) = 0;
121 :
122 : /** Will be called when a new record block in a binary stream has been
123 : started.
124 :
125 : The current record identifier can be accessed with getCurrentElement()
126 : or isCurrentElement(). Used by BIFF import only.
127 : */
128 : virtual void onStartRecord( SequenceInputStream& rStrm ) = 0;
129 :
130 : /** Will be called when the current record block is about to be left.
131 :
132 : The current record identifier can be accessed with getCurrentElement()
133 : or isCurrentElement(). Used by BIFF import only.
134 : */
135 : virtual void onEndRecord() = 0;
136 :
137 : // helpers ----------------------------------------------------------------
138 :
139 : /** Returns the identifier of the currently processed element. Ignores MCE elements in stack */
140 : sal_Int32 getCurrentElement() const;
141 :
142 : /** Returns the identifier of the currently processed element - Including MCE root elements */
143 : sal_Int32 getCurrentElementWithMce() const;
144 :
145 : /** Returns true, if nElement contains the identifier of the currently
146 : processed element. */
147 135 : inline bool isCurrentElement( sal_Int32 nElement ) const
148 135 : { return getCurrentElement() == nElement; }
149 :
150 : /** Returns true, if either nElement1 or nElement2 contain the identifier
151 : of the currently processed element. */
152 0 : inline bool isCurrentElement( sal_Int32 nElement1, sal_Int32 nElement2 ) const
153 0 : { return isCurrentElement( nElement1 ) || isCurrentElement( nElement2 ); }
154 :
155 : /** Returns the identifier of the specified parent element. */
156 : sal_Int32 getParentElement( sal_Int32 nCountBack = 1 ) const;
157 :
158 : /** Returns true, if nElement contains the identifier of the specified
159 : parent element. */
160 0 : inline sal_Int32 isParentElement( sal_Int32 nElement, sal_Int32 nCountBack = 1 ) const
161 0 : { return getParentElement( nCountBack ) == nElement; }
162 :
163 : /** Returns true, if the element currently processed is the root element of
164 : the context or fragment handler. */
165 : bool isRootElement() const;
166 :
167 : // implementation ---------------------------------------------------------
168 :
169 : protected:
170 : /** Must be called from createFastChildContext() in derived classes. */
171 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler >
172 : implCreateChildContext(
173 : sal_Int32 nElement,
174 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs );
175 :
176 : /** Must be called from startFastElement() in derived classes. */
177 : void implStartElement(
178 : sal_Int32 nElement,
179 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs );
180 :
181 : /** Must be called from characters() in derived classes. */
182 : void implCharacters( const ::rtl::OUString& rChars );
183 :
184 : /** Must be called from endFastElement() in derived classes. */
185 : void implEndElement( sal_Int32 nElement );
186 :
187 : /** Must be called from createRecordContext() in derived classes. */
188 : ContextHandlerRef implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm );
189 :
190 : /** Must be called from startRecord() in derived classes. */
191 : void implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm );
192 :
193 : /** Must be called from endRecord() in derived classes. */
194 : void implEndRecord( sal_Int32 nRecId );
195 :
196 : private:
197 : ContextHandler2Helper& operator=( const ContextHandler2Helper& );
198 :
199 : ElementInfo& pushElementInfo( sal_Int32 nElement );
200 : void popElementInfo();
201 : void processCollectedChars();
202 :
203 : private:
204 : typedef ::std::vector< ElementInfo > ContextStack;
205 : typedef ::boost::shared_ptr< ContextStack > ContextStackRef;
206 :
207 : ContextStackRef mxContextStack; ///< Stack of all processed elements.
208 : size_t mnRootStackSize; ///< Stack size on construction time.
209 : bool mbEnableTrimSpace; ///< True = trim whitespace in characters().
210 : };
211 :
212 : // ============================================================================
213 :
214 44 : class OOX_DLLPUBLIC ContextHandler2 : public ContextHandler, public ContextHandler2Helper
215 : {
216 : public:
217 : explicit ContextHandler2( ContextHandler2Helper& rParent );
218 : virtual ~ContextHandler2();
219 :
220 : // resolve ambiguity from base classes
221 12392 : virtual void SAL_CALL acquire() throw() { ContextHandler::acquire(); }
222 12392 : virtual void SAL_CALL release() throw() { ContextHandler::release(); }
223 :
224 : // com.sun.star.xml.sax.XFastContextHandler interface ---------------------
225 :
226 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL
227 : createFastChildContext(
228 : sal_Int32 nElement,
229 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs )
230 : throw( ::com::sun::star::xml::sax::SAXException,
231 : ::com::sun::star::uno::RuntimeException );
232 :
233 : virtual void SAL_CALL startFastElement(
234 : sal_Int32 nElement,
235 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs )
236 : throw( ::com::sun::star::xml::sax::SAXException,
237 : ::com::sun::star::uno::RuntimeException );
238 :
239 : virtual void SAL_CALL characters( const ::rtl::OUString& rChars )
240 : throw( ::com::sun::star::xml::sax::SAXException,
241 : ::com::sun::star::uno::RuntimeException );
242 :
243 : virtual void SAL_CALL endFastElement( sal_Int32 nElement )
244 : throw( ::com::sun::star::xml::sax::SAXException,
245 : ::com::sun::star::uno::RuntimeException );
246 :
247 : // oox.core.ContextHandler interface --------------------------------------
248 :
249 : virtual ContextHandlerRef createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm );
250 : virtual void startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm );
251 : virtual void endRecord( sal_Int32 nRecId );
252 :
253 : // oox.core.ContextHandler2Helper interface -------------------------------
254 :
255 : virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs );
256 : virtual void onStartElement( const AttributeList& rAttribs );
257 : virtual void onCharacters( const ::rtl::OUString& rChars );
258 : virtual void onEndElement();
259 :
260 : virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm );
261 : virtual void onStartRecord( SequenceInputStream& rStrm );
262 : virtual void onEndRecord();
263 : };
264 :
265 : // ============================================================================
266 :
267 : } // namespace core
268 : } // namespace oox
269 :
270 : #endif
271 :
272 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|