xref: /trunk/main/xmlsecurity/source/framework/saxeventkeeperimpl.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 buffered 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 efficient 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 buffered 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 running.
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     virtual void SAL_CALL removeElementCollector( sal_Int32 id );
244     virtual sal_Int32 SAL_CALL addBlocker(  );
245     virtual void SAL_CALL removeBlocker( sal_Int32 id );
246     virtual sal_Bool SAL_CALL isBlocking(  );
247     virtual com::sun::star::uno::Reference<
248         com::sun::star::xml::wrapper::XXMLElementWrapper > SAL_CALL
249         getElement( sal_Int32 id );
250     virtual void SAL_CALL setElement(
251         sal_Int32 id,
252         const com::sun::star::uno::Reference<
253             com::sun::star::xml::wrapper::XXMLElementWrapper >&
254             aElement );
255     virtual com::sun::star::uno::Reference<
256         com::sun::star::xml::sax::XDocumentHandler > SAL_CALL
257         setNextHandler( const com::sun::star::uno::Reference<
258             com::sun::star::xml::sax::XDocumentHandler >& xNewHandler );
259     virtual rtl::OUString SAL_CALL printBufferNodeTree();
260     virtual com::sun::star::uno::Reference<
261         com::sun::star::xml::wrapper::XXMLElementWrapper > SAL_CALL
262         getCurrentBlockingNode();
263 
264     /* XSecuritySAXEventKeeper */
265     virtual sal_Int32 SAL_CALL addSecurityElementCollector(
266         com::sun::star::xml::crypto::sax::ElementMarkPriority priority,
267         sal_Bool modifyElement );
268     virtual sal_Int32 SAL_CALL cloneElementCollector(
269         sal_Int32 referenceId,
270         com::sun::star::xml::crypto::sax::ElementMarkPriority priority );
271     virtual void SAL_CALL setSecurityId( sal_Int32 id, sal_Int32 securityId );
272 
273     /* XReferenceResolvedBroadcaster */
274     virtual void SAL_CALL addReferenceResolvedListener(
275         sal_Int32 referenceId,
276         const com::sun::star::uno::Reference<
277             com::sun::star::xml::crypto::sax::XReferenceResolvedListener >&
278             listener );
279     virtual void SAL_CALL removeReferenceResolvedListener(
280         sal_Int32 referenceId,
281         const com::sun::star::uno::Reference<
282             com::sun::star::xml::crypto::sax::XReferenceResolvedListener >&
283             listener );
284 
285     /* XSAXEventKeeperStatusChangeBroadcaster */
286     virtual void SAL_CALL addSAXEventKeeperStatusChangeListener(
287         const com::sun::star::uno::Reference<
288             com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener >&
289             listener );
290     virtual void SAL_CALL removeSAXEventKeeperStatusChangeListener(
291         const com::sun::star::uno::Reference<
292             com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener >&
293             listener );
294 
295     /* XDocumentHandler */
296     virtual void SAL_CALL startDocument(  );
297     virtual void SAL_CALL endDocument(  );
298     virtual void SAL_CALL startElement(
299         const rtl::OUString& aName,
300         const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >&
301         xAttribs );
302     virtual void SAL_CALL endElement( const rtl::OUString& aName );
303     virtual void SAL_CALL characters( const rtl::OUString& aChars );
304     virtual void SAL_CALL ignorableWhitespace( const rtl::OUString& aWhitespaces );
305     virtual void SAL_CALL processingInstruction(
306         const rtl::OUString& aTarget, const rtl::OUString& aData );
307     virtual void SAL_CALL setDocumentLocator(
308         const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator );
309 
310     /* XInitialization */
311     virtual void SAL_CALL initialize(
312         const com::sun::star::uno::Sequence< com::sun::star::uno::Any >& aArguments );
313 
314     /* XServiceInfo */
315     virtual rtl::OUString SAL_CALL getImplementationName(  );
316     virtual sal_Bool SAL_CALL supportsService( const rtl::OUString& ServiceName );
317     virtual com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames(  );
318 };
319 
320 rtl::OUString SAXEventKeeperImpl_getImplementationName();
321 
322 sal_Bool SAL_CALL SAXEventKeeperImpl_supportsService( const rtl::OUString& ServiceName );
323 
324 com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL SAXEventKeeperImpl_getSupportedServiceNames(  );
325 
326 com::sun::star::uno::Reference< com::sun::star::uno::XInterface >
327 SAL_CALL SAXEventKeeperImpl_createInstance( const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > & rSMgr);
328 
329 #endif
330