xref: /trunk/main/xmlsecurity/source/framework/elementcollector.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 #include "elementmark.hxx"
32 #include "elementcollector.hxx"
33 #include "buffernode.hxx"
34 #include <com/sun/star/xml/crypto/sax/ConstOfSecurityId.hpp>
35 
36 namespace cssu = com::sun::star::uno;
37 namespace cssxc = com::sun::star::xml::crypto;
38 
39 ElementCollector::ElementCollector(
40     sal_Int32 nSecurityId,
41     sal_Int32 nBufferId,
42     cssxc::sax::ElementMarkPriority nPriority,
43     bool bToModify,
44     const com::sun::star::uno::Reference<
45         com::sun::star::xml::crypto::sax::XReferenceResolvedListener >&
46         xReferenceResolvedListener)
47     :ElementMark(nSecurityId, nBufferId),
48      m_nPriority(nPriority),
49      m_bToModify(bToModify),
50      m_bAbleToNotify(false),
51      m_bNotified(false),
52      m_xReferenceResolvedListener(xReferenceResolvedListener)
53 /****** ElementCollector/ElementCollector *************************************
54  *
55  *   NAME
56  *  ElementCollector -- constructor method
57  *
58  *   SYNOPSIS
59  *  ElementCollector(nSecurityId, nBufferId, nPriority, bToModify
60  *                   xReferenceResolvedListener);
61  *
62  *   FUNCTION
63  *  construct an ElementCollector object.
64  *
65  *   INPUTS
66  *  nSecurityId -   represents which security entity the buffer node is
67  *          related with. Either a signature or an encryption is
68  *          a security entity.
69  *  nBufferId - the id of the element bufferred in the document
70  *          wrapper component. The document wrapper component
71  *          uses this id to search the particular bufferred
72  *          element.
73  *  nPriority - the priority value. ElementCollector with lower
74  *          priority value can't notify until all ElementCollectors
75  *          with higher priority value have notified.
76  *  bToModify - A flag representing whether this ElementCollector
77  *          notification will cause the modification of its working
78  *                  element.
79  *  xReferenceResolvedListener
80  *            - the listener that this ElementCollector notifies to.
81  *
82  *   RESULT
83  *  empty
84  *
85  *   HISTORY
86  *  05.01.2004 -    implemented
87  *
88  *   AUTHOR
89  *  Michael Mi
90  *  Email: michael.mi@sun.com
91  ******************************************************************************/
92 {
93     m_type = cssxc::sax::ElementMarkType_ELEMENTCOLLECTOR;
94 }
95 
96 /*
97 bool ElementCollector::isInternalNotificationSuppressed() const
98 {
99     return m_bInternalNotificationSuppressed;
100 }
101 */
102 
103 cssxc::sax::ElementMarkPriority ElementCollector::getPriority() const
104 {
105     return m_nPriority;
106 }
107 
108 bool ElementCollector::getModify() const
109 {
110     return m_bToModify;
111 }
112 
113 void ElementCollector::notifyListener()
114 /****** ElementCollector/notifyListener ***************************************
115  *
116  *   NAME
117  *  notifyListener -- enable the ability to notify the listener
118  *
119  *   SYNOPSIS
120  *  notifyListener();
121  *
122  *   FUNCTION
123  *  enable the ability to notify the listener and try to notify then.
124  *
125  *   INPUTS
126  *  empty
127  *
128  *   RESULT
129  *  empty
130  *
131  *   HISTORY
132  *  05.01.2004 -    implemented
133  *
134  *   AUTHOR
135  *  Michael Mi
136  *  Email: michael.mi@sun.com
137  ******************************************************************************/
138 {
139     m_bAbleToNotify = true;
140     doNotify();
141 }
142 
143 bool ElementCollector::isAbleToNotify() const
144 {
145     return m_bAbleToNotify;
146 }
147 
148 void ElementCollector::setReferenceResolvedListener(
149     const cssu::Reference< cssxc::sax::XReferenceResolvedListener >& xReferenceResolvedListener)
150 /****** ElementCollector/setReferenceResolvedListener *************************
151  *
152  *   NAME
153  *  setReferenceResolvedListener -- configures a listener for the buffer
154  *  node in this object
155  *
156  *   SYNOPSIS
157  *  setReferenceResolvedListener(xReferenceResolvedListener);
158  *
159  *   FUNCTION
160  *  configures a new listener and try to notify then.
161  *
162  *   INPUTS
163  *  xReferenceResolvedListener - the new listener
164  *
165  *   RESULT
166  *  empty
167  *
168  *   HISTORY
169  *  05.01.2004 -    implemented
170  *
171  *   AUTHOR
172  *  Michael Mi
173  *  Email: michael.mi@sun.com
174  ******************************************************************************/
175 {
176     m_xReferenceResolvedListener = xReferenceResolvedListener;
177     doNotify();
178 }
179 
180 void ElementCollector::setSecurityId(sal_Int32 nSecurityId)
181 /****** ElementCollector/setSecurityId ****************************************
182  *
183  *   NAME
184  *  setSecurityId -- configures the security Id of the buffer node
185  *
186  *   SYNOPSIS
187  *  setSecurityId(nSecurityId);
188  *
189  *   FUNCTION
190  *  configures the security Id and try to notify then
191  *
192  *   INPUTS
193  *  nSecurityId - the security Id
194  *
195  *   RESULT
196  *  empty
197  *
198  *   HISTORY
199  *  05.01.2004 -    implemented
200  *
201  *   AUTHOR
202  *  Michael Mi
203  *  Email: michael.mi@sun.com
204  ******************************************************************************/
205 {
206     m_nSecurityId = nSecurityId;
207     doNotify();
208 }
209 
210 void ElementCollector::doNotify()
211 /****** ElementCollector/doNotify *********************************************
212  *
213  *   NAME
214  *  doNotify -- tries to notify the listener
215  *
216  *   SYNOPSIS
217  *  doNotify();
218  *
219  *   FUNCTION
220  *  notifies the listener when all below conditions are satisfied:
221  *  the listener has not been notified;
222  *  the notify right is granted;
223  *  the listener has already been configured;
224  *  the security id has already been configure
225  *
226  *   INPUTS
227  *  empty
228  *
229  *   RESULT
230  *  empty
231  *
232  *   HISTORY
233  *  05.01.2004 -    implemented
234  *
235  *   AUTHOR
236  *  Michael Mi
237  *  Email: michael.mi@sun.com
238  ******************************************************************************/
239 {
240     if (!m_bNotified &&
241         m_bAbleToNotify &&
242         m_xReferenceResolvedListener.is() &&
243         m_nSecurityId != cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID)
244     {
245         m_bNotified = true;
246         m_xReferenceResolvedListener->referenceResolved(m_nBufferId);
247     }
248 }
249 
250 ElementCollector* ElementCollector::clone(
251     sal_Int32 nBufferId,
252     cssxc::sax::ElementMarkPriority nPriority ) const
253 /****** ElementCollector/clone ************************************************
254  *
255  *   NAME
256  *  clone -- duplicates this ElementCollector object
257  *
258  *   SYNOPSIS
259  *  cloned = clone(nBufferId, nPriority);
260  *
261  *   FUNCTION
262  *  duplicates this ElementCollector object with new buffer Id, priority.
263  *
264  *   INPUTS
265  *  nBufferId - the buffer node's Id
266  *  nPriority - the priority
267  *
268  *   RESULT
269  *  clone -     a new ElementCollector
270  *
271  *   HISTORY
272  *  05.01.2004 -    implemented
273  *
274  *   AUTHOR
275  *  Michael Mi
276  *  Email: michael.mi@sun.com
277  ******************************************************************************/
278 {
279     ElementCollector* pClonedOne
280         = new ElementCollector(m_nSecurityId,
281                        nBufferId, nPriority, m_bToModify,
282                        m_xReferenceResolvedListener);
283 
284     if (m_bAbleToNotify)
285     {
286         pClonedOne->notifyListener();
287     }
288 
289     if (m_pBufferNode != NULL)
290     {
291         m_pBufferNode->addElementCollector(pClonedOne);
292     }
293 
294     return pClonedOne;
295 }
296 
297