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