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 INCLUDED_OOX_CORE_CONTEXTHANDLER2_HXX
21 : #define INCLUDED_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 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 0 : bool isCurrentElement( sal_Int32 nElement ) const
148 0 : { return getCurrentElement() == nElement; }
149 :
150 : /** Returns true, if either nElement1 or nElement2 contain the identifier
151 : of the currently processed element. */
152 0 : 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 : bool 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 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 :
210 : protected:
211 : bool mbEnableTrimSpace; ///< True = trim whitespace in characters().
212 : };
213 :
214 :
215 :
216 0 : class OOX_DLLPUBLIC ContextHandler2 : public ContextHandler, public ContextHandler2Helper
217 : {
218 : public:
219 : explicit ContextHandler2( ContextHandler2Helper& rParent );
220 : virtual ~ContextHandler2();
221 :
222 : // resolve ambiguity from base classes
223 0 : virtual void SAL_CALL acquire() throw() SAL_OVERRIDE { ContextHandler::acquire(); }
224 0 : virtual void SAL_CALL release() throw() SAL_OVERRIDE { ContextHandler::release(); }
225 :
226 : // com.sun.star.xml.sax.XFastContextHandler interface ---------------------
227 :
228 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL
229 : createFastChildContext(
230 : sal_Int32 nElement,
231 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs )
232 : throw( ::com::sun::star::xml::sax::SAXException,
233 : ::com::sun::star::uno::RuntimeException, std::exception ) SAL_FINAL SAL_OVERRIDE;
234 :
235 : virtual void SAL_CALL startFastElement(
236 : sal_Int32 nElement,
237 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rxAttribs )
238 : throw( ::com::sun::star::xml::sax::SAXException,
239 : ::com::sun::star::uno::RuntimeException, std::exception ) SAL_FINAL SAL_OVERRIDE;
240 :
241 : virtual void SAL_CALL characters( const OUString& rChars )
242 : throw( ::com::sun::star::xml::sax::SAXException,
243 : ::com::sun::star::uno::RuntimeException, std::exception ) SAL_FINAL SAL_OVERRIDE;
244 :
245 : virtual void SAL_CALL endFastElement( sal_Int32 nElement )
246 : throw( ::com::sun::star::xml::sax::SAXException,
247 : ::com::sun::star::uno::RuntimeException, std::exception ) SAL_FINAL SAL_OVERRIDE;
248 :
249 : // oox.core.ContextHandler interface --------------------------------------
250 :
251 : virtual ContextHandlerRef createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ) SAL_OVERRIDE;
252 : virtual void startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm ) SAL_OVERRIDE;
253 : virtual void endRecord( sal_Int32 nRecId ) SAL_OVERRIDE;
254 :
255 : // oox.core.ContextHandler2Helper interface -------------------------------
256 :
257 : virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) SAL_OVERRIDE;
258 : virtual void onStartElement( const AttributeList& rAttribs ) SAL_OVERRIDE;
259 : virtual void onCharacters( const OUString& rChars ) SAL_OVERRIDE;
260 : virtual void onEndElement() SAL_OVERRIDE;
261 :
262 : virtual ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ) SAL_OVERRIDE;
263 : virtual void onStartRecord( SequenceInputStream& rStrm ) SAL_OVERRIDE;
264 : virtual void onEndRecord() SAL_OVERRIDE;
265 : };
266 :
267 :
268 :
269 : } // namespace core
270 : } // namespace oox
271 :
272 : #endif
273 :
274 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|