@Test public void testGetFirstBlock() { BlockNavigator navigator = new BlockNavigator(); Assert.assertSame(parentBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR)); Assert.assertSame(contextBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR_OR_SELF)); Assert.assertSame(contextBlockChild1, navigator.getFirstBlock(contextBlock, Block.Axes.CHILD)); Assert.assertSame(contextBlockChild1, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT)); Assert.assertSame(contextBlock, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT_OR_SELF)); Assert.assertSame(followingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING)); Assert.assertSame(followingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING_SIBLING)); Assert.assertSame(parentBlock, navigator.getFirstBlock(contextBlock, Block.Axes.PARENT)); Assert.assertSame(precedingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING)); Assert.assertSame(precedingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING_SIBLING)); Assert.assertSame(contextBlock, navigator.getFirstBlock(contextBlock, Block.Axes.SELF)); navigator = new BlockNavigator(new SameBlockMatcher(rootBlock)); Assert.assertSame(rootBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR_OR_SELF)); Assert.assertSame(rootBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR)); navigator = new BlockNavigator(new SameBlockMatcher(contextBlockChild22)); Assert.assertSame(contextBlockChild22, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT)); Assert.assertSame(contextBlockChild22, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT_OR_SELF)); navigator = new BlockNavigator(new SameBlockMatcher(followingBlockChild2)); Assert.assertSame(followingBlockChild2, navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING)); navigator = new BlockNavigator(new SameBlockMatcher(contextBlockChild2)); Assert.assertSame(contextBlockChild2, navigator.getFirstBlock(contextBlock, Block.Axes.CHILD)); navigator = new BlockNavigator(new SameBlockMatcher(precedingBlockChild2)); Assert.assertSame(precedingBlockChild2, navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING)); navigator = new BlockNavigator(new SameBlockMatcher(new WordBlock("unexistingBlock"))); Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING_SIBLING)); Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.PARENT)); Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING_SIBLING)); Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.SELF)); }
public <T extends Block> T getFirstBlock(Block currentBlock, Axes currentAxes) { Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: if (this.matcher.match(block)) { return (T) block; } break; case ANCESTOR_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case ANCESTOR: case PARENT: axes = axes == Axes.PARENT ? Axes.SELF : Axes.ANCESTOR_OR_SELF; nextBlock = block.getParent(); break; case CHILD: List<Block> children = block.getChildren(); if (!children.isEmpty()) { nextBlock = children.get(0); axes = Axes.FOLLOWING_SIBLING; if (this.matcher.match(nextBlock)) { return (T) nextBlock; } } break; case DESCENDANT_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case DESCENDANT: for (Block child : block.getChildren()) { Block matchedBlock = getFirstBlock(child, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { Block matchedBlock = getFirstBlock(nextSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { Block matchedBlock = getFirstBlock(previousSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; default: break; } block = nextBlock; } return (T) block; }
BlockNavigator { public <T extends Block> T getFirstBlock(Block currentBlock, Axes currentAxes) { Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: if (this.matcher.match(block)) { return (T) block; } break; case ANCESTOR_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case ANCESTOR: case PARENT: axes = axes == Axes.PARENT ? Axes.SELF : Axes.ANCESTOR_OR_SELF; nextBlock = block.getParent(); break; case CHILD: List<Block> children = block.getChildren(); if (!children.isEmpty()) { nextBlock = children.get(0); axes = Axes.FOLLOWING_SIBLING; if (this.matcher.match(nextBlock)) { return (T) nextBlock; } } break; case DESCENDANT_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case DESCENDANT: for (Block child : block.getChildren()) { Block matchedBlock = getFirstBlock(child, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { Block matchedBlock = getFirstBlock(nextSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { Block matchedBlock = getFirstBlock(previousSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; default: break; } block = nextBlock; } return (T) block; } }
BlockNavigator { public <T extends Block> T getFirstBlock(Block currentBlock, Axes currentAxes) { Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: if (this.matcher.match(block)) { return (T) block; } break; case ANCESTOR_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case ANCESTOR: case PARENT: axes = axes == Axes.PARENT ? Axes.SELF : Axes.ANCESTOR_OR_SELF; nextBlock = block.getParent(); break; case CHILD: List<Block> children = block.getChildren(); if (!children.isEmpty()) { nextBlock = children.get(0); axes = Axes.FOLLOWING_SIBLING; if (this.matcher.match(nextBlock)) { return (T) nextBlock; } } break; case DESCENDANT_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case DESCENDANT: for (Block child : block.getChildren()) { Block matchedBlock = getFirstBlock(child, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { Block matchedBlock = getFirstBlock(nextSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { Block matchedBlock = getFirstBlock(previousSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; default: break; } block = nextBlock; } return (T) block; } BlockNavigator();  BlockNavigator(BlockMatcher matcher); }
BlockNavigator { public <T extends Block> T getFirstBlock(Block currentBlock, Axes currentAxes) { Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: if (this.matcher.match(block)) { return (T) block; } break; case ANCESTOR_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case ANCESTOR: case PARENT: axes = axes == Axes.PARENT ? Axes.SELF : Axes.ANCESTOR_OR_SELF; nextBlock = block.getParent(); break; case CHILD: List<Block> children = block.getChildren(); if (!children.isEmpty()) { nextBlock = children.get(0); axes = Axes.FOLLOWING_SIBLING; if (this.matcher.match(nextBlock)) { return (T) nextBlock; } } break; case DESCENDANT_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case DESCENDANT: for (Block child : block.getChildren()) { Block matchedBlock = getFirstBlock(child, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { Block matchedBlock = getFirstBlock(nextSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { Block matchedBlock = getFirstBlock(previousSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; default: break; } block = nextBlock; } return (T) block; } BlockNavigator();  BlockNavigator(BlockMatcher matcher); List<T> getBlocks(Block currentBlock, Axes currentAxes); T getFirstBlock(Block currentBlock, Axes currentAxes); }
BlockNavigator { public <T extends Block> T getFirstBlock(Block currentBlock, Axes currentAxes) { Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: if (this.matcher.match(block)) { return (T) block; } break; case ANCESTOR_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case ANCESTOR: case PARENT: axes = axes == Axes.PARENT ? Axes.SELF : Axes.ANCESTOR_OR_SELF; nextBlock = block.getParent(); break; case CHILD: List<Block> children = block.getChildren(); if (!children.isEmpty()) { nextBlock = children.get(0); axes = Axes.FOLLOWING_SIBLING; if (this.matcher.match(nextBlock)) { return (T) nextBlock; } } break; case DESCENDANT_OR_SELF: if (this.matcher.match(block)) { return (T) block; } case DESCENDANT: for (Block child : block.getChildren()) { Block matchedBlock = getFirstBlock(child, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { Block matchedBlock = getFirstBlock(nextSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); if (nextBlock != null && this.matcher.match(nextBlock)) { return (T) nextBlock; } break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { Block matchedBlock = getFirstBlock(previousSibling, Axes.DESCENDANT_OR_SELF); if (matchedBlock != null) { return (T) matchedBlock; } } break; default: break; } block = nextBlock; } return (T) block; } BlockNavigator();  BlockNavigator(BlockMatcher matcher); List<T> getBlocks(Block currentBlock, Axes currentAxes); T getFirstBlock(Block currentBlock, Axes currentAxes);  }