bump product version to 4.1.6.2
[LibreOffice.git] / include / oox / core / contexthandler2.hxx
blobf1a0c7db38b2981c9dc7f47dd61b0bc7b20bbdd1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef OOX_CORE_CONTEXTHANDLER2_HXX
21 #define OOX_CORE_CONTEXTHANDLER2_HXX
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"
30 namespace oox {
31 namespace core {
33 // ============================================================================
35 const sal_Int32 XML_ROOT_CONTEXT = SAL_MAX_INT32;
37 // ============================================================================
39 struct ElementInfo;
41 /** Helper class that provides a context stack.
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.
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.
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.
64 class OOX_DLLPUBLIC ContextHandler2Helper
66 public:
67 explicit ContextHandler2Helper( bool bEnableTrimSpace );
68 explicit ContextHandler2Helper( const ContextHandler2Helper& rParent );
69 virtual ~ContextHandler2Helper();
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;
75 // interface --------------------------------------------------------------
77 /** Will be called to create a context handler for the passed element.
79 Usually 'this' can be returned to improve performance by reusing the
80 same instance to process several elements. Used by OOXML import only.
82 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) = 0;
84 /** Will be called when a new element has been started.
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.
90 The current element identifier can be accessed with getCurrentElement()
91 or isCurrentElement(). Used by OOXML import only.
93 virtual void onStartElement( const AttributeList& rAttribs ) = 0;
95 /** Will be called before a new child element starts, or if the current
96 element is about to be left.
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.
103 The current element identifier can be accessed with getCurrentElement()
104 or isCurrentElement(). Used by OOXML import only.
106 virtual void onCharacters( const OUString& rChars ) = 0;
108 /** Will be called when the current element is about to be left.
110 The current element identifier can be accessed with getCurrentElement()
111 or isCurrentElement(). Used by OOXML import only.
113 virtual void onEndElement() = 0;
115 /** Will be called to create a context handler for the passed record.
117 Usually 'this' can be returned to improve performance by reusing the
118 same instance to process several records. Used by BIFF import only.
120 virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ) = 0;
122 /** Will be called when a new record block in a binary stream has been
123 started.
125 The current record identifier can be accessed with getCurrentElement()
126 or isCurrentElement(). Used by BIFF import only.
128 virtual void onStartRecord( SequenceInputStream& rStrm ) = 0;
130 /** Will be called when the current record block is about to be left.
132 The current record identifier can be accessed with getCurrentElement()
133 or isCurrentElement(). Used by BIFF import only.
135 virtual void onEndRecord() = 0;
137 // helpers ----------------------------------------------------------------
139 /** Returns the identifier of the currently processed element. Ignores MCE elements in stack */
140 sal_Int32 getCurrentElement() const;
142 /** Returns the identifier of the currently processed element - Including MCE root elements */
143 sal_Int32 getCurrentElementWithMce() const;
145 /** Returns true, if nElement contains the identifier of the currently
146 processed element. */
147 inline bool isCurrentElement( sal_Int32 nElement ) const
148 { return getCurrentElement() == nElement; }
150 /** Returns true, if either nElement1 or nElement2 contain the identifier
151 of the currently processed element. */
152 inline bool isCurrentElement( sal_Int32 nElement1, sal_Int32 nElement2 ) const
153 { return isCurrentElement( nElement1 ) || isCurrentElement( nElement2 ); }
155 /** Returns the identifier of the specified parent element. */
156 sal_Int32 getParentElement( sal_Int32 nCountBack = 1 ) const;
158 /** Returns true, if nElement contains the identifier of the specified
159 parent element. */
160 inline sal_Int32 isParentElement( sal_Int32 nElement, sal_Int32 nCountBack = 1 ) const
161 { return getParentElement( nCountBack ) == nElement; }
163 /** Returns true, if the element currently processed is the root element of
164 the context or fragment handler. */
165 bool isRootElement() const;
167 // implementation ---------------------------------------------------------
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 );
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 );
181 /** Must be called from characters() in derived classes. */
182 void implCharacters( const OUString& rChars );
184 /** Must be called from endFastElement() in derived classes. */
185 void implEndElement( sal_Int32 nElement );
187 /** Must be called from createRecordContext() in derived classes. */
188 ContextHandlerRef implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm );
190 /** Must be called from startRecord() in derived classes. */
191 void implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm );
193 /** Must be called from endRecord() in derived classes. */
194 void implEndRecord( sal_Int32 nRecId );
196 private:
197 ContextHandler2Helper& operator=( const ContextHandler2Helper& );
199 ElementInfo& pushElementInfo( sal_Int32 nElement );
200 void popElementInfo();
201 void processCollectedChars();
203 private:
204 typedef ::std::vector< ElementInfo > ContextStack;
205 typedef ::boost::shared_ptr< ContextStack > ContextStackRef;
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().
212 // ============================================================================
214 class OOX_DLLPUBLIC ContextHandler2 : public ContextHandler, public ContextHandler2Helper
216 public:
217 explicit ContextHandler2( ContextHandler2Helper& rParent );
218 virtual ~ContextHandler2();
220 // resolve ambiguity from base classes
221 virtual void SAL_CALL acquire() throw() { ContextHandler::acquire(); }
222 virtual void SAL_CALL release() throw() { ContextHandler::release(); }
224 // com.sun.star.xml.sax.XFastContextHandler interface ---------------------
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 );
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 );
239 virtual void SAL_CALL characters( const OUString& rChars )
240 throw( ::com::sun::star::xml::sax::SAXException,
241 ::com::sun::star::uno::RuntimeException );
243 virtual void SAL_CALL endFastElement( sal_Int32 nElement )
244 throw( ::com::sun::star::xml::sax::SAXException,
245 ::com::sun::star::uno::RuntimeException );
247 // oox.core.ContextHandler interface --------------------------------------
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 );
253 // oox.core.ContextHandler2Helper interface -------------------------------
255 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs );
256 virtual void onStartElement( const AttributeList& rAttribs );
257 virtual void onCharacters( const OUString& rChars );
258 virtual void onEndElement();
260 virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm );
261 virtual void onStartRecord( SequenceInputStream& rStrm );
262 virtual void onEndRecord();
265 // ============================================================================
267 } // namespace core
268 } // namespace oox
270 #endif
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */