xref: /AOO41X/main/comphelper/source/container/container.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_comphelper.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
33*cdf0e10cSrcweir #include <comphelper/container.hxx>
34*cdf0e10cSrcweir #include <osl/diagnose.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir //.........................................................................
37*cdf0e10cSrcweir namespace comphelper
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir //.........................................................................
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //==============================================================================
42*cdf0e10cSrcweir IndexAccessIterator::IndexAccessIterator(::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> xStartingPoint)
43*cdf0e10cSrcweir 	:m_xStartingPoint(xStartingPoint)
44*cdf0e10cSrcweir 	,m_xCurrentObject(NULL)
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir 	OSL_ENSURE(m_xStartingPoint.is(), "IndexAccessIterator::IndexAccessIterator : no starting point !");
47*cdf0e10cSrcweir }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir IndexAccessIterator::~IndexAccessIterator() {}
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //------------------------------------------------------------------------------
52*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> IndexAccessIterator::Next()
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir 	sal_Bool bCheckingStartingPoint = !m_xCurrentObject.is();
55*cdf0e10cSrcweir 		// ist die aktuelle Node der Anfangspunkt ?
56*cdf0e10cSrcweir 	sal_Bool bAlreadyCheckedCurrent = m_xCurrentObject.is();
57*cdf0e10cSrcweir 		// habe ich die aktuelle Node schon mal mittels ShouldHandleElement testen ?
58*cdf0e10cSrcweir 	if (!m_xCurrentObject.is())
59*cdf0e10cSrcweir 		m_xCurrentObject = m_xStartingPoint;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> xSearchLoop( m_xCurrentObject);
62*cdf0e10cSrcweir 	sal_Bool bHasMoreToSearch = sal_True;
63*cdf0e10cSrcweir 	sal_Bool bFoundSomething = sal_False;
64*cdf0e10cSrcweir 	while (!bFoundSomething && bHasMoreToSearch)
65*cdf0e10cSrcweir 	{
66*cdf0e10cSrcweir 		// pre-order-traversierung
67*cdf0e10cSrcweir 		if (!bAlreadyCheckedCurrent && ShouldHandleElement(xSearchLoop))
68*cdf0e10cSrcweir 		{
69*cdf0e10cSrcweir 			m_xCurrentObject = xSearchLoop;
70*cdf0e10cSrcweir 			bFoundSomething = sal_True;
71*cdf0e10cSrcweir 		}
72*cdf0e10cSrcweir 		else
73*cdf0e10cSrcweir 		{
74*cdf0e10cSrcweir 			// zuerst absteigen, wenn moeglich
75*cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess> xContainerAccess(xSearchLoop, ::com::sun::star::uno::UNO_QUERY);
76*cdf0e10cSrcweir 			if (xContainerAccess.is() && xContainerAccess->getCount() && ShouldStepInto(xContainerAccess))
77*cdf0e10cSrcweir 			{	// zum ersten Child
78*cdf0e10cSrcweir 				::com::sun::star::uno::Any aElement(xContainerAccess->getByIndex(0));
79*cdf0e10cSrcweir 				xSearchLoop = *(::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>*)aElement.getValue();
80*cdf0e10cSrcweir 				bCheckingStartingPoint = sal_False;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 				m_arrChildIndizies.push_back((sal_Int32)0);
83*cdf0e10cSrcweir 			}
84*cdf0e10cSrcweir 			else
85*cdf0e10cSrcweir 			{
86*cdf0e10cSrcweir 				// dann nach oben und nach rechts, wenn moeglich
87*cdf0e10cSrcweir 				while (m_arrChildIndizies.size() > 0)
88*cdf0e10cSrcweir 				{	// (mein Stack ist nich leer, also kann ich noch nach oben gehen)
89*cdf0e10cSrcweir 					::com::sun::star::uno::Reference< ::com::sun::star::container::XChild> xChild(xSearchLoop, ::com::sun::star::uno::UNO_QUERY);
90*cdf0e10cSrcweir 					OSL_ENSURE(xChild.is(), "IndexAccessIterator::Next : a content has no approriate interface !");
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 					::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> xParent( xChild->getParent());
93*cdf0e10cSrcweir 					xContainerAccess = ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>(xParent, ::com::sun::star::uno::UNO_QUERY);
94*cdf0e10cSrcweir 					OSL_ENSURE(xContainerAccess.is(), "IndexAccessIterator::Next : a content has an invalid parent !");
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 					// den Index, den SearchLoop in diesem Parent hatte, von meinem 'Stack'
97*cdf0e10cSrcweir 					sal_Int32 nOldSearchChildIndex = m_arrChildIndizies[m_arrChildIndizies.size() - 1];
98*cdf0e10cSrcweir 					m_arrChildIndizies.pop_back();
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 					if (nOldSearchChildIndex < xContainerAccess->getCount() - 1)
101*cdf0e10cSrcweir 					{	// auf dieser Ebene geht es noch nach rechts
102*cdf0e10cSrcweir 						++nOldSearchChildIndex;
103*cdf0e10cSrcweir 						// also das naechste Child
104*cdf0e10cSrcweir 						::com::sun::star::uno::Any aElement(xContainerAccess->getByIndex(nOldSearchChildIndex));
105*cdf0e10cSrcweir 						xSearchLoop = *(::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>*) aElement.getValue();
106*cdf0e10cSrcweir 						bCheckingStartingPoint = sal_False;
107*cdf0e10cSrcweir 						// und dessen Position auf den 'Stack'
108*cdf0e10cSrcweir 						m_arrChildIndizies.push_back((sal_Int32)nOldSearchChildIndex);
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 						break;
111*cdf0e10cSrcweir 					}
112*cdf0e10cSrcweir 					// hierher komme ich, wenn es auf der aktuellen Ebene nicht nach rechts geht, dann mache ich eine darueber weiter
113*cdf0e10cSrcweir 					xSearchLoop = xParent;
114*cdf0e10cSrcweir 					bCheckingStartingPoint = sal_False;
115*cdf0e10cSrcweir 				}
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 				if ((m_arrChildIndizies.size() == 0) && !bCheckingStartingPoint)
118*cdf0e10cSrcweir 				{	// das ist genau dann der Fall, wenn ich keinen rechten Nachbarn fuer irgendeinen der direkten Vorfahren des
119*cdf0e10cSrcweir 					// urspruenglichen xSearchLoop gefunden habe
120*cdf0e10cSrcweir 					bHasMoreToSearch = sal_False;
121*cdf0e10cSrcweir 				}
122*cdf0e10cSrcweir 			}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 			if (bHasMoreToSearch)
125*cdf0e10cSrcweir 			{	// ich habe in xSearchLoop jetzt ein Interface eines 'Knotens' meines 'Baumes', den ich noch abtesten kann
126*cdf0e10cSrcweir 				if (ShouldHandleElement(xSearchLoop))
127*cdf0e10cSrcweir 				{
128*cdf0e10cSrcweir 					m_xCurrentObject = xSearchLoop;
129*cdf0e10cSrcweir 					bFoundSomething = sal_True;
130*cdf0e10cSrcweir 				}
131*cdf0e10cSrcweir 				else
132*cdf0e10cSrcweir 					if (bCheckingStartingPoint)
133*cdf0e10cSrcweir 						// ich bin noch am Anfang, konnte nicht absteigen, und habe an diesem Anfang nix gefunden -> nix mehr zu tun
134*cdf0e10cSrcweir 						bHasMoreToSearch = sal_False;
135*cdf0e10cSrcweir 				bAlreadyCheckedCurrent = sal_True;
136*cdf0e10cSrcweir 			}
137*cdf0e10cSrcweir 		}
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	if (!bFoundSomething)
141*cdf0e10cSrcweir 	{
142*cdf0e10cSrcweir 		OSL_ENSURE(m_arrChildIndizies.size() == 0, "IndexAccessIterator::Next : items left on stack ! how this ?");
143*cdf0e10cSrcweir 		Invalidate();
144*cdf0e10cSrcweir 	}
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 	return m_xCurrentObject;
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir //.........................................................................
150*cdf0e10cSrcweir }	// namespace comphelper
151*cdf0e10cSrcweir //.........................................................................
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 
154