1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "elementmark.hxx"
32*cdf0e10cSrcweir #include "elementcollector.hxx"
33*cdf0e10cSrcweir #include "buffernode.hxx"
34*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/ConstOfSecurityId.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
37*cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir ElementCollector::ElementCollector(
40*cdf0e10cSrcweir 	sal_Int32 nSecurityId,
41*cdf0e10cSrcweir 	sal_Int32 nBufferId,
42*cdf0e10cSrcweir 	cssxc::sax::ElementMarkPriority nPriority,
43*cdf0e10cSrcweir 	bool bToModify,
44*cdf0e10cSrcweir 	const com::sun::star::uno::Reference<
45*cdf0e10cSrcweir 		com::sun::star::xml::crypto::sax::XReferenceResolvedListener >&
46*cdf0e10cSrcweir 		xReferenceResolvedListener)
47*cdf0e10cSrcweir 	:ElementMark(nSecurityId, nBufferId),
48*cdf0e10cSrcweir 	 m_nPriority(nPriority),
49*cdf0e10cSrcweir 	 m_bToModify(bToModify),
50*cdf0e10cSrcweir  	 m_bAbleToNotify(false),
51*cdf0e10cSrcweir  	 m_bNotified(false),
52*cdf0e10cSrcweir 	 m_xReferenceResolvedListener(xReferenceResolvedListener)
53*cdf0e10cSrcweir /****** ElementCollector/ElementCollector *************************************
54*cdf0e10cSrcweir  *
55*cdf0e10cSrcweir  *   NAME
56*cdf0e10cSrcweir  *	ElementCollector -- constructor method
57*cdf0e10cSrcweir  *
58*cdf0e10cSrcweir  *   SYNOPSIS
59*cdf0e10cSrcweir  *	ElementCollector(nSecurityId, nBufferId, nPriority, bToModify
60*cdf0e10cSrcweir  *	                 xReferenceResolvedListener);
61*cdf0e10cSrcweir  *
62*cdf0e10cSrcweir  *   FUNCTION
63*cdf0e10cSrcweir  *	construct an ElementCollector object.
64*cdf0e10cSrcweir  *
65*cdf0e10cSrcweir  *   INPUTS
66*cdf0e10cSrcweir  *	nSecurityId - 	represents which security entity the buffer node is
67*cdf0e10cSrcweir  *			related with. Either a signature or an encryption is
68*cdf0e10cSrcweir  *			a security entity.
69*cdf0e10cSrcweir  *	nBufferId -	the id of the element bufferred in the document
70*cdf0e10cSrcweir  *			wrapper component. The document wrapper component
71*cdf0e10cSrcweir  *			uses this id to search the particular bufferred
72*cdf0e10cSrcweir  *			element.
73*cdf0e10cSrcweir  *	nPriority -	the priority value. ElementCollector with lower
74*cdf0e10cSrcweir  *			priority value can't notify until all ElementCollectors
75*cdf0e10cSrcweir  *			with higher priority value have notified.
76*cdf0e10cSrcweir  *	bToModify -	A flag representing whether this ElementCollector
77*cdf0e10cSrcweir  *			notification will cause the modification of its working
78*cdf0e10cSrcweir  *	                element.
79*cdf0e10cSrcweir  *	xReferenceResolvedListener
80*cdf0e10cSrcweir  *	          -	the listener that this ElementCollector notifies to.
81*cdf0e10cSrcweir  *
82*cdf0e10cSrcweir  *   RESULT
83*cdf0e10cSrcweir  *	empty
84*cdf0e10cSrcweir  *
85*cdf0e10cSrcweir  *   HISTORY
86*cdf0e10cSrcweir  *	05.01.2004 -	implemented
87*cdf0e10cSrcweir  *
88*cdf0e10cSrcweir  *   AUTHOR
89*cdf0e10cSrcweir  *	Michael Mi
90*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
91*cdf0e10cSrcweir  ******************************************************************************/
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir 	m_type = cssxc::sax::ElementMarkType_ELEMENTCOLLECTOR;
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir /*
97*cdf0e10cSrcweir bool ElementCollector::isInternalNotificationSuppressed() const
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir 	return m_bInternalNotificationSuppressed;
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir */
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir cssxc::sax::ElementMarkPriority ElementCollector::getPriority() const
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir 	return m_nPriority;
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir bool ElementCollector::getModify() const
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	return m_bToModify;
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir void ElementCollector::notifyListener()
114*cdf0e10cSrcweir /****** ElementCollector/notifyListener ***************************************
115*cdf0e10cSrcweir  *
116*cdf0e10cSrcweir  *   NAME
117*cdf0e10cSrcweir  *	notifyListener -- enable the ability to notify the listener
118*cdf0e10cSrcweir  *
119*cdf0e10cSrcweir  *   SYNOPSIS
120*cdf0e10cSrcweir  *	notifyListener();
121*cdf0e10cSrcweir  *
122*cdf0e10cSrcweir  *   FUNCTION
123*cdf0e10cSrcweir  *	enable the ability to notify the listener and try to notify then.
124*cdf0e10cSrcweir  *
125*cdf0e10cSrcweir  *   INPUTS
126*cdf0e10cSrcweir  *	empty
127*cdf0e10cSrcweir  *
128*cdf0e10cSrcweir  *   RESULT
129*cdf0e10cSrcweir  *	empty
130*cdf0e10cSrcweir  *
131*cdf0e10cSrcweir  *   HISTORY
132*cdf0e10cSrcweir  *	05.01.2004 -	implemented
133*cdf0e10cSrcweir  *
134*cdf0e10cSrcweir  *   AUTHOR
135*cdf0e10cSrcweir  *	Michael Mi
136*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
137*cdf0e10cSrcweir  ******************************************************************************/
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 	m_bAbleToNotify = true;
140*cdf0e10cSrcweir 	doNotify();
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir bool ElementCollector::isAbleToNotify() const
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	return m_bAbleToNotify;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir void ElementCollector::setReferenceResolvedListener(
149*cdf0e10cSrcweir 	const cssu::Reference< cssxc::sax::XReferenceResolvedListener >& xReferenceResolvedListener)
150*cdf0e10cSrcweir /****** ElementCollector/setReferenceResolvedListener *************************
151*cdf0e10cSrcweir  *
152*cdf0e10cSrcweir  *   NAME
153*cdf0e10cSrcweir  *	setReferenceResolvedListener -- configures a listener for the buffer
154*cdf0e10cSrcweir  *	node in this object
155*cdf0e10cSrcweir  *
156*cdf0e10cSrcweir  *   SYNOPSIS
157*cdf0e10cSrcweir  *	setReferenceResolvedListener(xReferenceResolvedListener);
158*cdf0e10cSrcweir  *
159*cdf0e10cSrcweir  *   FUNCTION
160*cdf0e10cSrcweir  *	configures a new listener and try to notify then.
161*cdf0e10cSrcweir  *
162*cdf0e10cSrcweir  *   INPUTS
163*cdf0e10cSrcweir  *	xReferenceResolvedListener - the new listener
164*cdf0e10cSrcweir  *
165*cdf0e10cSrcweir  *   RESULT
166*cdf0e10cSrcweir  *	empty
167*cdf0e10cSrcweir  *
168*cdf0e10cSrcweir  *   HISTORY
169*cdf0e10cSrcweir  *	05.01.2004 -	implemented
170*cdf0e10cSrcweir  *
171*cdf0e10cSrcweir  *   AUTHOR
172*cdf0e10cSrcweir  *	Michael Mi
173*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
174*cdf0e10cSrcweir  ******************************************************************************/
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir 	m_xReferenceResolvedListener = xReferenceResolvedListener;
177*cdf0e10cSrcweir 	doNotify();
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir void ElementCollector::setSecurityId(sal_Int32 nSecurityId)
181*cdf0e10cSrcweir /****** ElementCollector/setSecurityId ****************************************
182*cdf0e10cSrcweir  *
183*cdf0e10cSrcweir  *   NAME
184*cdf0e10cSrcweir  *	setSecurityId -- configures the security Id of the buffer node
185*cdf0e10cSrcweir  *
186*cdf0e10cSrcweir  *   SYNOPSIS
187*cdf0e10cSrcweir  *	setSecurityId(nSecurityId);
188*cdf0e10cSrcweir  *
189*cdf0e10cSrcweir  *   FUNCTION
190*cdf0e10cSrcweir  *	configures the security Id and try to notify then
191*cdf0e10cSrcweir  *
192*cdf0e10cSrcweir  *   INPUTS
193*cdf0e10cSrcweir  *	nSecurityId - the security Id
194*cdf0e10cSrcweir  *
195*cdf0e10cSrcweir  *   RESULT
196*cdf0e10cSrcweir  *	empty
197*cdf0e10cSrcweir  *
198*cdf0e10cSrcweir  *   HISTORY
199*cdf0e10cSrcweir  *	05.01.2004 -	implemented
200*cdf0e10cSrcweir  *
201*cdf0e10cSrcweir  *   AUTHOR
202*cdf0e10cSrcweir  *	Michael Mi
203*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
204*cdf0e10cSrcweir  ******************************************************************************/
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir 	m_nSecurityId = nSecurityId;
207*cdf0e10cSrcweir 	doNotify();
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir void ElementCollector::doNotify()
211*cdf0e10cSrcweir /****** ElementCollector/doNotify *********************************************
212*cdf0e10cSrcweir  *
213*cdf0e10cSrcweir  *   NAME
214*cdf0e10cSrcweir  *	doNotify -- tries to notify the listener
215*cdf0e10cSrcweir  *
216*cdf0e10cSrcweir  *   SYNOPSIS
217*cdf0e10cSrcweir  *	doNotify();
218*cdf0e10cSrcweir  *
219*cdf0e10cSrcweir  *   FUNCTION
220*cdf0e10cSrcweir  *	notifies the listener when all below conditions are satisfied:
221*cdf0e10cSrcweir  *	the listener has not been notified;
222*cdf0e10cSrcweir  *	the notify right is granted;
223*cdf0e10cSrcweir  *	the listener has already been configured;
224*cdf0e10cSrcweir  *	the security id has already been configure
225*cdf0e10cSrcweir  *
226*cdf0e10cSrcweir  *   INPUTS
227*cdf0e10cSrcweir  *	empty
228*cdf0e10cSrcweir  *
229*cdf0e10cSrcweir  *   RESULT
230*cdf0e10cSrcweir  *	empty
231*cdf0e10cSrcweir  *
232*cdf0e10cSrcweir  *   HISTORY
233*cdf0e10cSrcweir  *	05.01.2004 -	implemented
234*cdf0e10cSrcweir  *
235*cdf0e10cSrcweir  *   AUTHOR
236*cdf0e10cSrcweir  *	Michael Mi
237*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
238*cdf0e10cSrcweir  ******************************************************************************/
239*cdf0e10cSrcweir {
240*cdf0e10cSrcweir 	if (!m_bNotified &&
241*cdf0e10cSrcweir 	    m_bAbleToNotify &&
242*cdf0e10cSrcweir 	    m_xReferenceResolvedListener.is() &&
243*cdf0e10cSrcweir 	    m_nSecurityId != cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID)
244*cdf0e10cSrcweir 	{
245*cdf0e10cSrcweir 		m_bNotified = true;
246*cdf0e10cSrcweir 		m_xReferenceResolvedListener->referenceResolved(m_nBufferId);
247*cdf0e10cSrcweir 	}
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir ElementCollector* ElementCollector::clone(
251*cdf0e10cSrcweir 	sal_Int32 nBufferId,
252*cdf0e10cSrcweir 	cssxc::sax::ElementMarkPriority nPriority ) const
253*cdf0e10cSrcweir /****** ElementCollector/clone ************************************************
254*cdf0e10cSrcweir  *
255*cdf0e10cSrcweir  *   NAME
256*cdf0e10cSrcweir  *	clone -- duplicates this ElementCollector object
257*cdf0e10cSrcweir  *
258*cdf0e10cSrcweir  *   SYNOPSIS
259*cdf0e10cSrcweir  *	cloned = clone(nBufferId, nPriority);
260*cdf0e10cSrcweir  *
261*cdf0e10cSrcweir  *   FUNCTION
262*cdf0e10cSrcweir  *	duplicates this ElementCollector object with new buffer Id, priority.
263*cdf0e10cSrcweir  *
264*cdf0e10cSrcweir  *   INPUTS
265*cdf0e10cSrcweir  *	nBufferId -	the buffer node's Id
266*cdf0e10cSrcweir  *	nPriority -	the priority
267*cdf0e10cSrcweir  *
268*cdf0e10cSrcweir  *   RESULT
269*cdf0e10cSrcweir  *	clone - 	a new ElementCollector
270*cdf0e10cSrcweir  *
271*cdf0e10cSrcweir  *   HISTORY
272*cdf0e10cSrcweir  *	05.01.2004 -	implemented
273*cdf0e10cSrcweir  *
274*cdf0e10cSrcweir  *   AUTHOR
275*cdf0e10cSrcweir  *	Michael Mi
276*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
277*cdf0e10cSrcweir  ******************************************************************************/
278*cdf0e10cSrcweir {
279*cdf0e10cSrcweir 	ElementCollector* pClonedOne
280*cdf0e10cSrcweir 		= new ElementCollector(m_nSecurityId,
281*cdf0e10cSrcweir 				       nBufferId, nPriority, m_bToModify,
282*cdf0e10cSrcweir 				       m_xReferenceResolvedListener);
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir 	if (m_bAbleToNotify)
285*cdf0e10cSrcweir 	{
286*cdf0e10cSrcweir 		pClonedOne->notifyListener();
287*cdf0e10cSrcweir 	}
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 	if (m_pBufferNode != NULL)
290*cdf0e10cSrcweir 	{
291*cdf0e10cSrcweir 		m_pBufferNode->addElementCollector(pClonedOne);
292*cdf0e10cSrcweir 	}
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	return pClonedOne;
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297