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