@Test public void testGetBlocks() { BlockNavigator navigator = new BlockNavigator(); Assert.assertEquals(Arrays.asList(parentBlock, rootBlock), navigator.getBlocks(contextBlock, Block.Axes.ANCESTOR)); Assert.assertEquals(Arrays.asList(contextBlock, parentBlock, rootBlock), navigator.getBlocks(contextBlock, Block.Axes.ANCESTOR_OR_SELF)); Assert.assertEquals(Arrays.asList(contextBlockChild1, contextBlockChild2), navigator.getBlocks(contextBlock, Block.Axes.CHILD)); Assert.assertEquals(Arrays.asList(contextBlockChild1, contextBlockChild11, contextBlockChild12, contextBlockChild2, contextBlockChild21, contextBlockChild22), navigator.getBlocks(contextBlock, Block.Axes.DESCENDANT)); Assert.assertEquals(Arrays.asList(contextBlock, contextBlockChild1, contextBlockChild11, contextBlockChild12, contextBlockChild2, contextBlockChild21, contextBlockChild22), navigator.getBlocks(contextBlock, Block.Axes.DESCENDANT_OR_SELF)); Assert.assertEquals(Arrays.asList(followingBlock, followingBlockChild1, followingBlockChild2), navigator.getBlocks(contextBlock, Block.Axes.FOLLOWING)); Assert.assertEquals(Arrays.asList(followingBlock), navigator.getBlocks(contextBlock, Block.Axes.FOLLOWING_SIBLING)); Assert.assertEquals(Arrays.asList(parentBlock), navigator.getBlocks(contextBlock, Block.Axes.PARENT)); Assert.assertEquals(Arrays.asList(precedingBlock, precedingBlockChild1, precedingBlockChild2), navigator.getBlocks(contextBlock, Block.Axes.PRECEDING)); Assert.assertEquals(Arrays.asList(precedingBlock), navigator.getBlocks(contextBlock, Block.Axes.PRECEDING_SIBLING)); Assert.assertEquals(Arrays.asList(contextBlock), navigator.getBlocks(contextBlock, Block.Axes.SELF)); }
public <T extends Block> List<T> getBlocks(Block currentBlock, Axes currentAxes) { List<T> blocks = new ArrayList<T>(); Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: addBlock(block, blocks); break; case ANCESTOR_OR_SELF: addBlock(block, blocks); nextBlock = block.getParent(); break; case ANCESTOR: nextBlock = block.getParent(); axes = Axes.ANCESTOR_OR_SELF; break; case PARENT: nextBlock = block.getParent(); axes = Axes.SELF; break; case CHILD: if (!block.getChildren().isEmpty()) { nextBlock = block.getChildren().get(0); axes = Axes.FOLLOWING_SIBLING; addBlock(nextBlock, blocks); } break; case DESCENDANT_OR_SELF: addBlock(block, blocks); blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case DESCENDANT: blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); addBlock(nextBlock, blocks); break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { blocks = getBlocks(nextSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); addBlock(nextBlock, blocks); break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { blocks = getBlocks(previousSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; default: break; } block = nextBlock; } return blocks != null ? blocks : Collections.<T>emptyList(); }
BlockNavigator { public <T extends Block> List<T> getBlocks(Block currentBlock, Axes currentAxes) { List<T> blocks = new ArrayList<T>(); Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: addBlock(block, blocks); break; case ANCESTOR_OR_SELF: addBlock(block, blocks); nextBlock = block.getParent(); break; case ANCESTOR: nextBlock = block.getParent(); axes = Axes.ANCESTOR_OR_SELF; break; case PARENT: nextBlock = block.getParent(); axes = Axes.SELF; break; case CHILD: if (!block.getChildren().isEmpty()) { nextBlock = block.getChildren().get(0); axes = Axes.FOLLOWING_SIBLING; addBlock(nextBlock, blocks); } break; case DESCENDANT_OR_SELF: addBlock(block, blocks); blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case DESCENDANT: blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); addBlock(nextBlock, blocks); break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { blocks = getBlocks(nextSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); addBlock(nextBlock, blocks); break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { blocks = getBlocks(previousSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; default: break; } block = nextBlock; } return blocks != null ? blocks : Collections.<T>emptyList(); } }
BlockNavigator { public <T extends Block> List<T> getBlocks(Block currentBlock, Axes currentAxes) { List<T> blocks = new ArrayList<T>(); Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: addBlock(block, blocks); break; case ANCESTOR_OR_SELF: addBlock(block, blocks); nextBlock = block.getParent(); break; case ANCESTOR: nextBlock = block.getParent(); axes = Axes.ANCESTOR_OR_SELF; break; case PARENT: nextBlock = block.getParent(); axes = Axes.SELF; break; case CHILD: if (!block.getChildren().isEmpty()) { nextBlock = block.getChildren().get(0); axes = Axes.FOLLOWING_SIBLING; addBlock(nextBlock, blocks); } break; case DESCENDANT_OR_SELF: addBlock(block, blocks); blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case DESCENDANT: blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); addBlock(nextBlock, blocks); break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { blocks = getBlocks(nextSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); addBlock(nextBlock, blocks); break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { blocks = getBlocks(previousSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; default: break; } block = nextBlock; } return blocks != null ? blocks : Collections.<T>emptyList(); } BlockNavigator();  BlockNavigator(BlockMatcher matcher); }
BlockNavigator { public <T extends Block> List<T> getBlocks(Block currentBlock, Axes currentAxes) { List<T> blocks = new ArrayList<T>(); Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: addBlock(block, blocks); break; case ANCESTOR_OR_SELF: addBlock(block, blocks); nextBlock = block.getParent(); break; case ANCESTOR: nextBlock = block.getParent(); axes = Axes.ANCESTOR_OR_SELF; break; case PARENT: nextBlock = block.getParent(); axes = Axes.SELF; break; case CHILD: if (!block.getChildren().isEmpty()) { nextBlock = block.getChildren().get(0); axes = Axes.FOLLOWING_SIBLING; addBlock(nextBlock, blocks); } break; case DESCENDANT_OR_SELF: addBlock(block, blocks); blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case DESCENDANT: blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); addBlock(nextBlock, blocks); break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { blocks = getBlocks(nextSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); addBlock(nextBlock, blocks); break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { blocks = getBlocks(previousSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; default: break; } block = nextBlock; } return blocks != null ? blocks : Collections.<T>emptyList(); } BlockNavigator();  BlockNavigator(BlockMatcher matcher); List<T> getBlocks(Block currentBlock, Axes currentAxes); T getFirstBlock(Block currentBlock, Axes currentAxes); }
BlockNavigator { public <T extends Block> List<T> getBlocks(Block currentBlock, Axes currentAxes) { List<T> blocks = new ArrayList<T>(); Block block = currentBlock; Axes axes = currentAxes; while (block != null) { Block nextBlock = null; switch (axes) { case SELF: addBlock(block, blocks); break; case ANCESTOR_OR_SELF: addBlock(block, blocks); nextBlock = block.getParent(); break; case ANCESTOR: nextBlock = block.getParent(); axes = Axes.ANCESTOR_OR_SELF; break; case PARENT: nextBlock = block.getParent(); axes = Axes.SELF; break; case CHILD: if (!block.getChildren().isEmpty()) { nextBlock = block.getChildren().get(0); axes = Axes.FOLLOWING_SIBLING; addBlock(nextBlock, blocks); } break; case DESCENDANT_OR_SELF: addBlock(block, blocks); blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case DESCENDANT: blocks = getBlocks(block.getChildren(), Axes.DESCENDANT_OR_SELF, blocks); break; case FOLLOWING_SIBLING: nextBlock = block.getNextSibling(); addBlock(nextBlock, blocks); break; case FOLLOWING: for (Block nextSibling = block.getNextSibling(); nextSibling != null; nextSibling = nextSibling.getNextSibling()) { blocks = getBlocks(nextSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; case PRECEDING_SIBLING: nextBlock = block.getPreviousSibling(); addBlock(nextBlock, blocks); break; case PRECEDING: for (Block previousSibling = block.getPreviousSibling(); previousSibling != null; previousSibling = previousSibling.getPreviousSibling()) { blocks = getBlocks(previousSibling, Axes.DESCENDANT_OR_SELF, blocks); } break; default: break; } block = nextBlock; } return blocks != null ? blocks : Collections.<T>emptyList(); } BlockNavigator();  BlockNavigator(BlockMatcher matcher); List<T> getBlocks(Block currentBlock, Axes currentAxes); T getFirstBlock(Block currentBlock, Axes currentAxes);  }