/** Iterating the Tree like the following
If there are (unvisited) children -> go down
If there are no (unvisited) children, but unvistsiblings -> go up and right
*/
private Expression getNextExpression() {
Expression nextExpression = null;
// the current expression might be null if the desired type of expression was never found in the tree
if (mCurrentExpression != null) {
// if all tree is desired, or root, or if it is not element expression
if (!mOnlyChildren || mAncestorsAndCurrent.size() == 1 || !(mAncestorsAndCurrent.peek().mExp instanceof ElementExp)) {
List<Expression> children = (List<Expression>) mCurrentExpression.visit(mVisitor);
// see if we can go DOWN the tree
if (children.size() > 0) {
Expression nextExpCandidate = children.get(0);
// DO NOT expand elements which occur more than one time in the ancestors hierarchy (i.e.
// since we compute the last element: Do not expand it, if it also occurs before)
if (isNoKnownElement(nextExpCandidate)) {
// GO DOWN - Proceed with first child
nextExpression = nextExpCandidate;
mAncestorsAndCurrent.push(new UniqueAncestor(nextExpression, 0));
}
}
}
// if you could not get depper, but you can go up
// if there was no first child for the next expression and still some parent not being the root
while (nextExpression == null && mAncestorsAndCurrent.size() > 1) {
// go one up the stack
UniqueAncestor uniqueAncestor = mAncestorsAndCurrent.pop();
// get the new parent
Expression parent = mAncestorsAndCurrent.peek().mExp;
// to get the siblings
List<Expression> siblings = (List<Expression>) parent.visit(mVisitor);
// get the unvisted sibling index
final int nextSiblingIndex = uniqueAncestor.mSiblingIndex + 1;
if (nextSiblingIndex < siblings.size()) {
Expression nextExpCandidate = siblings.get(nextSiblingIndex);
// DO NOT expand elements which occur more than one time in the ancestors hierarchy (i.e.
// since we compute the last element: Do not expand it, if it also occurs before)
if (isNoKnownElement(nextExpCandidate)) {
// GO RIGHT - Add next sibling to the stack
nextExpression = nextExpCandidate;