1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SAXEVENTKEEPERIMPL_HXX
29 #define _SAXEVENTKEEPERIMPL_HXX
30 
31 #include <com/sun/star/xml/crypto/sax/XSecuritySAXEventKeeper.hpp>
32 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
33 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
34 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeListener.hpp>
35 #include <com/sun/star/xml/csax/XCompressedDocumentHandler.hpp>
36 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
37 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
38 #include <com/sun/star/lang/XInitialization.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <cppuhelper/implbase6.hxx>
41 
42 #include "buffernode.hxx"
43 #include "elementmark.hxx"
44 #include "elementcollector.hxx"
45 
46 #ifndef INCLUDED_VECTOR
47 #include <vector>
48 #define INCLUDED_VECTOR
49 #endif
50 
51 class SAXEventKeeperImpl : public cppu::WeakImplHelper6
52 <
53 	com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper,
54 	com::sun::star::xml::crypto::sax::XReferenceResolvedBroadcaster,
55 	com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeBroadcaster,
56 	com::sun::star::xml::sax::XDocumentHandler,
57 	com::sun::star::lang::XInitialization,
58 	com::sun::star::lang::XServiceInfo
59 >
60 /****** SAXEventKeeperImpl.hxx/CLASS SAXEventKeeperImpl ***********************
61  *
62  *   NAME
63  *	SAXEventKeeperImpl -- SAX events buffer controller
64  *
65  *   FUNCTION
66  *	Controls SAX events to be bufferred, and controls bufferred SAX events
67  *	to be released.
68  *
69  *   HISTORY
70  *	05.01.2004 -	Interface supported: XSecuritySAXEventKeeper,
71  *	            	XReferenceResolvedBroadcaster,
72  *	            	XSAXEventKeeperStatusChangeBroadcaster,
73  *	            	XDocumentHandler, XInitialization, XServiceInfo
74  *
75  *   AUTHOR
76  *	Michael Mi
77  *	Email: michael.mi@sun.com
78  ******************************************************************************/
79 {
80 private:
81 	/*
82 	 * the XMLDocumentWrapper component which maintains all bufferred SAX
83 	 * in DOM format.
84 	 */
85 	com::sun::star::uno::Reference<
86 		com::sun::star::xml::wrapper::XXMLDocumentWrapper >
87 		m_xXMLDocument;
88 
89 	/*
90 	 * the document handler provided by the XMLDocumentWrapper component.
91 	 */
92 	com::sun::star::uno::Reference<
93 		com::sun::star::xml::sax::XDocumentHandler > m_xDocumentHandler;
94 
95 	/*
96 	 * the compressed document handler provided by the XMLDocumentWrapper
97 	 * component, the handler has more effient method definition that the
98 	 * normal document handler.
99 	 */
100 	com::sun::star::uno::Reference<
101 		com::sun::star::xml::csax::XCompressedDocumentHandler >
102 		m_xCompressedDocumentHandler;
103 
104 	/*
105 	 * a listener which receives this SAXEventKeeper's status change
106 	 * notification.
107 	 * Based on the status changes, the listener can decide whether the
108 	 * SAXEventKeeper should chain on/chain off the SAX chain, or whether
109 	 * the SAXEventKeeper is useless any long.
110 	 */
111 	com::sun::star::uno::Reference<
112 		com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener >
113 		m_xSAXEventKeeperStatusChangeListener;
114 
115 	/*
116 	 * the root node of the BufferNode tree.
117 	 * the BufferNode tree is used to keep track of all bufferred elements,
118 	 * it has the same structure with the document which maintains those
119 	 * elements physically.
120 	 */
121 	BufferNode*  m_pRootBufferNode;
122 
123 	/*
124 	 * the current active BufferNode.
125 	 * this is used to keep track the current location in the BufferNode tree,
126 	 * the next generated BufferNode will become a child BufferNode of it.
127 	 */
128 	BufferNode*  m_pCurrentBufferNode;
129 
130 	/*
131 	 * the next Id for a coming ElementMark.
132 	 * the variable is increased by 1 when an new ElementMark is generated,
133 	 * in this way, we can promise the Id of any ElementMark is unique.
134 	 */
135 	sal_Int32    m_nNextElementMarkId;
136 
137 	/*
138 	 * maintains a collection of all ElementMarks.
139 	 */
140 	std::vector< const ElementMark* > m_vElementMarkBuffers;
141 
142 	/*
143 	 * maintains a list of new ElementCollectors that will be created
144 	 * on the element represented by the next incoming startElement SAX
145 	 * event.
146 	 * The reason that such the m_vNewElementCollectors is necessary
147 	 * is: when an ElementCollector is asked to create, it can't be
148 	 * created completely at once, because the BufferNode it will be
149 	 * working on has not been created until the next startElement
150 	 * SAX event comes.
151 	 */
152 	std::vector< const ElementCollector* > m_vNewElementCollectors;
153 
154 	/*
155 	 * maintains the new Blocker that will be created
156 	 * on the element represented by the next incoming startElement SAX
157 	 * event.
158 	 */
159 	ElementMark* m_pNewBlocker;
160 
161 	/*
162 	 * the document handler to which all received SAX events will be
163 	 * forwarded.
164 	 */
165 	com::sun::star::uno::Reference<
166 		com::sun::star::xml::sax::XDocumentHandler > m_xNextHandler;
167 
168 	/*
169 	 * the current BufferNode which prevents the SAX events to be
170 	 * forwarded to the m_xNextHandler.
171 	 */
172 	BufferNode*  m_pCurrentBlockingBufferNode;
173 
174 	/*
175 	 * maintains a list of ElementMark that has been asked to release.
176 	 * Because during processing a request of releasing an ElementMark,
177 	 * another releasing ElementMark request can be invoked. To avoid
178 	 * reentering the same method, a such request only add that ElementMark
179 	 * into this ElementMark list, then all ElementMarks will be processed in
180 	 * order.
181 	 */
182 	std::vector< sal_Int32 > m_vReleasedElementMarkBuffers;
183 
184 	/*
185 	 * a flag to indicate whether the ElementMark releasing process is runing.
186 	 * When a releasing request comes, the assigned ElementMark is added to
187 	 * the m_vReleasedElementMarkBuffers first, then this flag is checked.
188 	 * If the ElementMark releasing process is not running, then call that
189 	 * method.
190 	 */
191 	bool     m_bIsReleasing;
192 
193 	/*
194 	 * a flag to indicate whether it is the "Forwarding" mode now.
195 	 * A "Forwarding" mode means that all received SAX events are from the
196 	 * XMLDocumentWrapper component, instead of up-stream component in the
197 	 * SAX chain.
198 	 * The difference between "Forwarding" mode and normal mode is that:
199 	 * no SAX events need to be transferred to the XMLDocumentWrapper component
200 	 * again even if a buffer request happens.
201 	 */
202 	bool     m_bIsForwarding;
203 
204 	void setCurrentBufferNode(BufferNode* pBufferNode);
205 
206 	BufferNode* addNewElementMarkBuffers();
207 
208 	ElementMark* findElementMarkBuffer(sal_Int32 nId) const;
209 
210 	void removeElementMarkBuffer(sal_Int32 nId);
211 
212 	rtl::OUString printBufferNode(
213 		BufferNode* pBufferNode, sal_Int32 nIndent) const;
214 
215 	com::sun::star::uno::Sequence< com::sun::star::uno::Reference<
216 		com::sun::star::xml::wrapper::XXMLElementWrapper > >
217 		collectChildWorkingElement(BufferNode* pBufferNode) const;
218 
219 	void smashBufferNode(
220 		BufferNode* pBufferNode, bool bClearRoot) const;
221 
222 	BufferNode* findNextBlockingBufferNode(
223 		BufferNode* pStartBufferNode) const;
224 
225 	void diffuse(BufferNode* pBufferNode) const;
226 
227 	void releaseElementMarkBuffer();
228 
229 	void markElementMarkBuffer(sal_Int32 nId);
230 
231 	sal_Int32 createElementCollector(
232 		sal_Int32 nSecurityId,
233 		com::sun::star::xml::crypto::sax::ElementMarkPriority nPriority,
234 		bool bModifyElement,
235 		const com::sun::star::uno::Reference<
236 			com::sun::star::xml::crypto::sax::XReferenceResolvedListener>&
237 			xReferenceResolvedListener);
238 
239 	sal_Int32 createBlocker(sal_Int32 nSecurityId);
240 
241 public:
242 	SAXEventKeeperImpl();
243 	virtual ~SAXEventKeeperImpl();
244 
245 	/* XSAXEventKeeper */
246 	virtual sal_Int32 SAL_CALL addElementCollector(  )
247 		throw (com::sun::star::uno::RuntimeException);
248 	virtual void SAL_CALL removeElementCollector( sal_Int32 id )
249 		throw (com::sun::star::uno::RuntimeException);
250 	virtual sal_Int32 SAL_CALL addBlocker(  )
251 		throw (com::sun::star::uno::RuntimeException);
252 	virtual void SAL_CALL removeBlocker( sal_Int32 id )
253 		throw (com::sun::star::uno::RuntimeException);
254 	virtual sal_Bool SAL_CALL isBlocking(  )
255 		throw (com::sun::star::uno::RuntimeException);
256 	virtual com::sun::star::uno::Reference<
257 		com::sun::star::xml::wrapper::XXMLElementWrapper > SAL_CALL
258 		getElement( sal_Int32 id )
259 		throw (com::sun::star::uno::RuntimeException);
260 	virtual void SAL_CALL setElement(
261 		sal_Int32 id,
262 		const com::sun::star::uno::Reference<
263 			com::sun::star::xml::wrapper::XXMLElementWrapper >&
264 			aElement )
265 		throw (com::sun::star::uno::RuntimeException);
266 	virtual com::sun::star::uno::Reference<
267 		com::sun::star::xml::sax::XDocumentHandler > SAL_CALL
268 		setNextHandler( const com::sun::star::uno::Reference<
269 			com::sun::star::xml::sax::XDocumentHandler >& xNewHandler )
270 		throw (com::sun::star::uno::RuntimeException);
271 	virtual rtl::OUString SAL_CALL printBufferNodeTree()
272 		throw (com::sun::star::uno::RuntimeException);
273 	virtual com::sun::star::uno::Reference<
274 		com::sun::star::xml::wrapper::XXMLElementWrapper > SAL_CALL
275 		getCurrentBlockingNode()
276 		throw (com::sun::star::uno::RuntimeException);
277 
278 	/* XSecuritySAXEventKeeper */
279 	virtual sal_Int32 SAL_CALL addSecurityElementCollector(
280 		com::sun::star::xml::crypto::sax::ElementMarkPriority priority,
281 		sal_Bool modifyElement )
282 		throw (com::sun::star::uno::RuntimeException);
283 	virtual sal_Int32 SAL_CALL cloneElementCollector(
284 		sal_Int32 referenceId,
285 		com::sun::star::xml::crypto::sax::ElementMarkPriority priority )
286 		throw (com::sun::star::uno::RuntimeException);
287 	virtual void SAL_CALL setSecurityId( sal_Int32 id, sal_Int32 securityId )
288 		throw (com::sun::star::uno::RuntimeException);
289 
290 	/* XReferenceResolvedBroadcaster */
291 	virtual void SAL_CALL addReferenceResolvedListener(
292 		sal_Int32 referenceId,
293 		const com::sun::star::uno::Reference<
294 			com::sun::star::xml::crypto::sax::XReferenceResolvedListener >&
295 			listener )
296 		throw (com::sun::star::uno::RuntimeException);
297 	virtual void SAL_CALL removeReferenceResolvedListener(
298 		sal_Int32 referenceId,
299 		const com::sun::star::uno::Reference<
300 			com::sun::star::xml::crypto::sax::XReferenceResolvedListener >&
301 			listener )
302 		throw (com::sun::star::uno::RuntimeException);
303 
304 	/* XSAXEventKeeperStatusChangeBroadcaster */
305 	virtual void SAL_CALL addSAXEventKeeperStatusChangeListener(
306 		const com::sun::star::uno::Reference<
307 			com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener >&
308 			listener )
309 		throw (com::sun::star::uno::RuntimeException);
310 	virtual void SAL_CALL removeSAXEventKeeperStatusChangeListener(
311 		const com::sun::star::uno::Reference<
312 			com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener >&
313 			listener )
314 		throw (com::sun::star::uno::RuntimeException);
315 
316 	/* XDocumentHandler */
317 	virtual void SAL_CALL startDocument(  )
318 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
319 	virtual void SAL_CALL endDocument(  )
320 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
321 	virtual void SAL_CALL startElement(
322 		const rtl::OUString& aName,
323 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >&
324 		xAttribs )
325 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
326 	virtual void SAL_CALL endElement( const rtl::OUString& aName )
327 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
328 	virtual void SAL_CALL characters( const rtl::OUString& aChars )
329 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
330 	virtual void SAL_CALL ignorableWhitespace( const rtl::OUString& aWhitespaces )
331 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
332 	virtual void SAL_CALL processingInstruction(
333 		const rtl::OUString& aTarget, const rtl::OUString& aData )
334 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
335 	virtual void SAL_CALL setDocumentLocator(
336 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator )
337 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException);
338 
339 	/* XInitialization */
340 	virtual void SAL_CALL initialize(
341 		const com::sun::star::uno::Sequence< com::sun::star::uno::Any >& aArguments )
342 		throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException);
343 
344 	/* XServiceInfo */
345 	virtual rtl::OUString SAL_CALL getImplementationName(  )
346 		throw (com::sun::star::uno::RuntimeException);
347 	virtual sal_Bool SAL_CALL supportsService( const rtl::OUString& ServiceName )
348 		throw (com::sun::star::uno::RuntimeException);
349 	virtual com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames(  )
350 		throw (com::sun::star::uno::RuntimeException);
351 };
352 
353 rtl::OUString SAXEventKeeperImpl_getImplementationName()
354 	throw ( com::sun::star::uno::RuntimeException );
355 
356 sal_Bool SAL_CALL SAXEventKeeperImpl_supportsService( const rtl::OUString& ServiceName )
357 	throw ( com::sun::star::uno::RuntimeException );
358 
359 com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL SAXEventKeeperImpl_getSupportedServiceNames(  )
360 	throw ( com::sun::star::uno::RuntimeException );
361 
362 com::sun::star::uno::Reference< com::sun::star::uno::XInterface >
363 SAL_CALL SAXEventKeeperImpl_createInstance( const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > & rSMgr)
364 	throw ( com::sun::star::uno::Exception );
365 
366 #endif
367 
368 
369 
370