1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <com/sun/star/xml/crypto/sax/XSecuritySAXEventKeeper.hpp>
23 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
24 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
25 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
26 #include <com/sun/star/lang/XInitialization.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <xmlsecuritydllapi.h>
29 #include <cppuhelper/implbase.hxx>
35 class ElementCollector
;
36 namespace com::sun::star::xml::crypto::sax
{ class XSAXEventKeeperStatusChangeListener
; }
37 namespace com::sun::star::xml::csax
{ class XCompressedDocumentHandler
; }
38 namespace com::sun::star::xml::wrapper
{ class XXMLDocumentWrapper
; }
41 class SAXEventKeeperImpl final
: public cppu::WeakImplHelper
43 css::xml::crypto::sax::XSecuritySAXEventKeeper
,
44 css::xml::crypto::sax::XReferenceResolvedBroadcaster
,
45 css::xml::crypto::sax::XSAXEventKeeperStatusChangeBroadcaster
,
46 css::xml::sax::XDocumentHandler
,
47 css::lang::XInitialization
,
48 css::lang::XServiceInfo
50 /****** SAXEventKeeperImpl.hxx/CLASS SAXEventKeeperImpl ***********************
53 * SAXEventKeeperImpl -- SAX events buffer controller
56 * Controls SAX events to be buffered, and controls buffered SAX events
58 ******************************************************************************/
62 * the XMLDocumentWrapper component which maintains all buffered SAX
65 css::uno::Reference
< css::xml::wrapper::XXMLDocumentWrapper
>
69 * the document handler provided by the XMLDocumentWrapper component.
71 css::uno::Reference
< css::xml::sax::XDocumentHandler
> m_xDocumentHandler
;
74 * the compressed document handler provided by the XMLDocumentWrapper
75 * component, the handler has more efficient method definition that the
76 * normal document handler.
78 css::uno::Reference
< css::xml::csax::XCompressedDocumentHandler
>
79 m_xCompressedDocumentHandler
;
82 * a listener which receives this SAXEventKeeper's status change
84 * Based on the status changes, the listener can decide whether the
85 * SAXEventKeeper should chain on/chain off the SAX chain, or whether
86 * the SAXEventKeeper is useless any long.
88 css::uno::Reference
< css::xml::crypto::sax::XSAXEventKeeperStatusChangeListener
>
89 m_xSAXEventKeeperStatusChangeListener
;
92 * the root node of the BufferNode tree.
93 * the BufferNode tree is used to keep track of all buffered elements,
94 * it has the same structure with the document which maintains those
95 * elements physically.
97 std::unique_ptr
<BufferNode
> m_pRootBufferNode
;
100 * the current active BufferNode.
101 * this is used to keep track the current location in the BufferNode tree,
102 * the next generated BufferNode will become a child BufferNode of it.
104 BufferNode
* m_pCurrentBufferNode
;
107 * the next Id for a coming ElementMark.
108 * the variable is increased by 1 when a new ElementMark is generated,
109 * in this way, we can promise the Id of any ElementMark is unique.
111 sal_Int32 m_nNextElementMarkId
;
114 * maintains a collection of all ElementMarks.
116 std::vector
<std::unique_ptr
<const ElementMark
>> m_vElementMarkBuffers
;
119 * maintains a list of new ElementCollectors that will be created
120 * on the element represented by the next incoming startElement SAX
122 * The reason that such the m_vNewElementCollectors is necessary
123 * is: when an ElementCollector is asked to create, it can't be
124 * created completely at once, because the BufferNode it will be
125 * working on has not been created until the next startElement
128 std::vector
< const ElementCollector
* > m_vNewElementCollectors
;
131 * maintains the new Blocker that will be created
132 * on the element represented by the next incoming startElement SAX
135 ElementMark
* m_pNewBlocker
;
138 * the document handler to which all received SAX events will be
141 css::uno::Reference
< css::xml::sax::XDocumentHandler
> m_xNextHandler
;
144 * the current BufferNode which prevents the SAX events to be
145 * forwarded to the m_xNextHandler.
147 BufferNode
* m_pCurrentBlockingBufferNode
;
150 * maintains a list of ElementMark that has been asked to release.
151 * Because during processing a request of releasing an ElementMark,
152 * another releasing ElementMark request can be invoked. To avoid
153 * reentering the same method, a such request only add that ElementMark
154 * into this ElementMark list, then all ElementMarks will be processed in
157 std::vector
< sal_Int32
> m_vReleasedElementMarkBuffers
;
160 * a flag to indicate whether the ElementMark releasing process is running.
161 * When a releasing request comes, the assigned ElementMark is added to
162 * the m_vReleasedElementMarkBuffers first, then this flag is checked.
163 * If the ElementMark releasing process is not running, then call that
169 * a flag to indicate whether it is the "Forwarding" mode now.
170 * A "Forwarding" mode means that all received SAX events are from the
171 * XMLDocumentWrapper component, instead of up-stream component in the
173 * The difference between "Forwarding" mode and normal mode is that:
174 * no SAX events need to be transferred to the XMLDocumentWrapper component
175 * again even if a buffer request happens.
177 bool m_bIsForwarding
;
179 void setCurrentBufferNode(BufferNode
* pBufferNode
);
181 BufferNode
* addNewElementMarkBuffers();
183 ElementMark
* findElementMarkBuffer(sal_Int32 nId
) const;
185 void removeElementMarkBuffer(sal_Int32 nId
);
187 OUString
printBufferNode(
188 BufferNode
const * pBufferNode
, sal_Int32 nIndent
) const;
190 static css::uno::Sequence
< css::uno::Reference
< css::xml::wrapper::XXMLElementWrapper
> >
191 collectChildWorkingElement(BufferNode
const * pBufferNode
);
193 void smashBufferNode(
194 BufferNode
* pBufferNode
, bool bClearRoot
) const;
196 static BufferNode
* findNextBlockingBufferNode(
197 BufferNode
* pStartBufferNode
);
199 static void diffuse(BufferNode
* pBufferNode
);
201 void releaseElementMarkBuffer();
203 void markElementMarkBuffer(sal_Int32 nId
);
205 sal_Int32
createElementCollector(
206 css::xml::crypto::sax::ElementMarkPriority nPriority
,
208 const css::uno::Reference
< css::xml::crypto::sax::XReferenceResolvedListener
>& xReferenceResolvedListener
);
210 sal_Int32
createBlocker();
213 SAXEventKeeperImpl();
214 virtual ~SAXEventKeeperImpl() override
;
216 SAXEventKeeperImpl(const SAXEventKeeperImpl
&) = delete;
217 SAXEventKeeperImpl
& operator=(const SAXEventKeeperImpl
&) = delete;
219 /* XSAXEventKeeper */
220 virtual sal_Int32 SAL_CALL
addElementCollector( ) override
;
221 virtual void SAL_CALL
removeElementCollector( sal_Int32 id
) override
;
222 virtual sal_Int32 SAL_CALL
addBlocker( ) override
;
223 virtual void SAL_CALL
removeBlocker( sal_Int32 id
) override
;
224 virtual sal_Bool SAL_CALL
isBlocking( ) override
;
225 virtual css::uno::Reference
< css::xml::wrapper::XXMLElementWrapper
> SAL_CALL
226 getElement( sal_Int32 id
) override
;
227 virtual void SAL_CALL
setElement(
229 const css::uno::Reference
< css::xml::wrapper::XXMLElementWrapper
>& aElement
) override
;
230 virtual css::uno::Reference
<
231 css::xml::sax::XDocumentHandler
> SAL_CALL
232 setNextHandler( const css::uno::Reference
<
233 css::xml::sax::XDocumentHandler
>& xNewHandler
) override
;
234 virtual OUString SAL_CALL
printBufferNodeTree() override
;
235 virtual css::uno::Reference
< css::xml::wrapper::XXMLElementWrapper
> SAL_CALL
236 getCurrentBlockingNode() override
;
238 /* XSecuritySAXEventKeeper */
239 virtual sal_Int32 SAL_CALL
addSecurityElementCollector(
240 css::xml::crypto::sax::ElementMarkPriority priority
,
241 sal_Bool modifyElement
) override
;
242 virtual void SAL_CALL
setSecurityId( sal_Int32 id
, sal_Int32 securityId
) override
;
244 /* XReferenceResolvedBroadcaster */
245 virtual void SAL_CALL
addReferenceResolvedListener(
246 sal_Int32 referenceId
,
247 const css::uno::Reference
< css::xml::crypto::sax::XReferenceResolvedListener
>& listener
) override
;
248 virtual void SAL_CALL
removeReferenceResolvedListener(
249 sal_Int32 referenceId
,
250 const css::uno::Reference
< css::xml::crypto::sax::XReferenceResolvedListener
>& listener
) override
;
252 /* XSAXEventKeeperStatusChangeBroadcaster */
253 virtual void SAL_CALL
addSAXEventKeeperStatusChangeListener(
254 const css::uno::Reference
< css::xml::crypto::sax::XSAXEventKeeperStatusChangeListener
>& listener
) override
;
255 virtual void SAL_CALL
removeSAXEventKeeperStatusChangeListener(
256 const css::uno::Reference
< css::xml::crypto::sax::XSAXEventKeeperStatusChangeListener
>& listener
) override
;
258 /* XDocumentHandler */
259 virtual void SAL_CALL
startDocument( ) override
;
260 virtual void SAL_CALL
endDocument( ) override
;
261 virtual void SAL_CALL
startElement(
262 const OUString
& aName
,
263 const css::uno::Reference
< css::xml::sax::XAttributeList
>&
265 virtual void SAL_CALL
endElement( const OUString
& aName
) override
;
266 virtual void SAL_CALL
characters( const OUString
& aChars
) override
;
267 virtual void SAL_CALL
ignorableWhitespace( const OUString
& aWhitespaces
) override
;
268 virtual void SAL_CALL
processingInstruction(
269 const OUString
& aTarget
, const OUString
& aData
) override
;
270 virtual void SAL_CALL
setDocumentLocator(
271 const css::uno::Reference
< css::xml::sax::XLocator
>& xLocator
) override
;
273 /* XInitialization */
274 virtual void SAL_CALL
initialize(
275 const css::uno::Sequence
< css::uno::Any
>& aArguments
) override
;
278 virtual OUString SAL_CALL
getImplementationName( ) override
;
279 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
280 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
;
283 /// @throws css::uno::RuntimeException
284 OUString
SAXEventKeeperImpl_getImplementationName();
286 /// @throws css::uno::RuntimeException
287 css::uno::Sequence
< OUString
> SAXEventKeeperImpl_getSupportedServiceNames( );
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */