private void treeWalker ( Node node , int level , List < PathQueryMatcher > queryMatchers , List < Node > collector ) { MatchType matchType = queryMatchers . get ( level ) . match ( level , node ) ; if ( matchType == MatchType . NOT_A_MATCH ) { // no reason to scan deeper //noinspection UnnecessaryReturnStatement return ; } else if ( matchType == MatchType . NODE_MATCH ) { // we have a match if ( level == queryMatchers . size ( ) - 1 ) { // full path match collector . add ( node ) ; } else if ( node instanceof Element ) { // scan deeper Element element = ( Element ) node ; List < Node > childElements = element . getChildElements ( ) ; for ( Node childElement : childElements ) { treeWalker ( childElement , level + 1 , queryMatchers , collector ) ; } } } else { throw new PathQueryException ( "Unknown MatchType: " + matchType ) ; } }
Recursive BFS tree walker