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 .
20 #include <com/sun/star/uno/XInterface.hpp>
21 #include <com/sun/star/container/XIndexAccess.hpp>
22 #include <com/sun/star/container/XChild.hpp>
23 #include <comphelper/container.hxx>
24 #include <o3tl/any.hxx>
25 #include <osl/diagnose.h>
32 IndexAccessIterator::IndexAccessIterator(css::uno::Reference
< css::uno::XInterface
> const & xStartingPoint
)
33 :m_xStartingPoint(xStartingPoint
)
35 OSL_ENSURE(m_xStartingPoint
.is(), "IndexAccessIterator::IndexAccessIterator : no starting point !");
38 IndexAccessIterator::~IndexAccessIterator() {}
41 css::uno::Reference
< css::uno::XInterface
> const & IndexAccessIterator::Next()
43 bool bCheckingStartingPoint
= !m_xCurrentObject
.is();
44 // Is the current node the starting point?
45 bool bAlreadyCheckedCurrent
= m_xCurrentObject
.is();
46 // Have I already tested the current node through ShouldHandleElement?
47 if (!m_xCurrentObject
.is())
48 m_xCurrentObject
= m_xStartingPoint
;
50 css::uno::Reference
< css::uno::XInterface
> xSearchLoop( m_xCurrentObject
);
51 bool bHasMoreToSearch
= true;
52 bool bFoundSomething
= false;
53 while (!bFoundSomething
&& bHasMoreToSearch
)
56 if (!bAlreadyCheckedCurrent
&& ShouldHandleElement(xSearchLoop
))
58 m_xCurrentObject
= xSearchLoop
;
59 bFoundSomething
= true;
63 // First, check to see if there's a match below
64 css::uno::Reference
< css::container::XIndexAccess
> xContainerAccess(xSearchLoop
, css::uno::UNO_QUERY
);
65 if (xContainerAccess
.is() && xContainerAccess
->getCount() && ShouldStepInto(xContainerAccess
))
67 css::uno::Any
aElement(xContainerAccess
->getByIndex(0));
68 xSearchLoop
= *o3tl::doAccess
<css::uno::Reference
<css::uno::XInterface
>>(aElement
);
69 bCheckingStartingPoint
= false;
71 m_arrChildIndizies
.push_back(sal_Int32(0));
74 { // otherwise, look above and to the right, if possible
75 while (!m_arrChildIndizies
.empty())
76 { // If the list isn't empty and there's nothing above
77 css::uno::Reference
< css::container::XChild
> xChild(xSearchLoop
, css::uno::UNO_QUERY
);
78 OSL_ENSURE(xChild
.is(), "IndexAccessIterator::Next : a content has no appropriate interface !");
80 css::uno::Reference
< css::uno::XInterface
> xParent( xChild
->getParent());
81 xContainerAccess
.set(xParent
, css::uno::UNO_QUERY
);
82 OSL_ENSURE(xContainerAccess
.is(), "IndexAccessIterator::Next : a content has an invalid parent !");
84 // Remove the index that SearchLoop had within this parent from my stack
85 sal_Int32 nOldSearchChildIndex
= m_arrChildIndizies
[m_arrChildIndizies
.size() - 1];
86 m_arrChildIndizies
.pop_back();
88 if (nOldSearchChildIndex
< xContainerAccess
->getCount() - 1)
89 { // Move to the right in this row
90 ++nOldSearchChildIndex
;
91 // and check the next child
92 css::uno::Any
aElement(xContainerAccess
->getByIndex(nOldSearchChildIndex
));
93 xSearchLoop
= *o3tl::doAccess
<css::uno::Reference
<css::uno::XInterface
>>(aElement
);
94 bCheckingStartingPoint
= false;
95 // and update its position in the list.
96 m_arrChildIndizies
.push_back(nOldSearchChildIndex
);
100 // Finally, if there's nothing more to do in this row (to the right), we'll move on to the next row.
101 xSearchLoop
= xParent
;
102 bCheckingStartingPoint
= false;
105 if (m_arrChildIndizies
.empty() && !bCheckingStartingPoint
)
106 { //This is the case if there is nothing to the right in the original search loop
107 bHasMoreToSearch
= false;
111 if (bHasMoreToSearch
)
112 { // If there is still a node in the tree which can be tested
113 if (ShouldHandleElement(xSearchLoop
))
115 m_xCurrentObject
= xSearchLoop
;
116 bFoundSomething
= true;
119 if (bCheckingStartingPoint
)
120 bHasMoreToSearch
= false;
121 bAlreadyCheckedCurrent
= true;
126 if (!bFoundSomething
)
128 OSL_ENSURE(m_arrChildIndizies
.empty(), "IndexAccessIterator::Next : items left on stack ! how this ?");
132 return m_xCurrentObject
;
136 } // namespace comphelper
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */