1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.block

File ProtectedBlockFilter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
20
5
1
109
52
11
0.55
4
5
2.2

Classes

Class Line # Actions
ProtectedBlockFilter 40 20 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.block;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.xwiki.rendering.block.Block;
26    import org.xwiki.rendering.block.BlockFilter;
27    import org.xwiki.rendering.block.MacroMarkerBlock;
28   
29    /**
30    * Used to manipulate Blocks but by filtering out protected blocks.
31    * <p>
32    * Note: This API is a work in progress and currently a protected block is a code macro marker block. In the future we
33    * need to make this more generic and since we also need to review Transformations to make them more performant this
34    * class may go away which is why it's currently located in an internal package.
35    * </p>
36    *
37    * @version $Id: 0f0c31a283b8d0fb776ade43313f40fdac09098b $
38    * @since 2.6
39    */
 
40    public class ProtectedBlockFilter implements BlockFilter
41    {
 
42  802626 toggle @Override
43    public List<Block> filter(Block block)
44    {
45  802626 List<Block> blocks = new ArrayList<Block>();
46  802626 if (!isProtectedBlock(block)) {
47  802618 blocks.add(block);
48    }
49  802626 return blocks;
50    }
51   
52    /**
53    * @param blocks the blocks to filter
54    * @return the filtered blocks
55    */
 
56  809771 toggle public List<Block> filter(List<Block> blocks)
57    {
58  809771 List<Block> filteredBlocks = new ArrayList<Block>();
59  809771 for (Block block : blocks) {
60  802626 filteredBlocks.addAll(filter(block));
61    }
62  809770 return filteredBlocks;
63    }
64   
65    /**
66    * @param block the block to filter out
67    * @return the next sibling that is not a protected block or null if not found
68    */
 
69  802597 toggle public Block getNextSibling(Block block)
70    {
71  802597 Block sibling = block.getNextSibling();
72  802604 while (sibling != null && isProtectedBlock(sibling)) {
73  7 sibling = sibling.getNextSibling();
74    }
75  802597 return sibling;
76    }
77   
78    /**
79    * @param block the block to filter out
80    * @param blockClass the type of Blocks to look for
81    * @param recurse if true also search recursively children
82    * @param <T> the class of the Blocks to return
83    * @return the filtered blocks matching the passed Block class
84    */
 
85  3 toggle public <T extends Block> List<T> getChildrenByType(Block block, Class<T> blockClass, boolean recurse)
86    {
87  3 List<T> typedBlocks = new ArrayList<T>();
88  3 for (Block child : filter(block.getChildren())) {
89  22 if (blockClass.isAssignableFrom(child.getClass())) {
90  9 typedBlocks.add(blockClass.cast(child));
91    }
92  22 if (recurse && !child.getChildren().isEmpty()) {
93  1 typedBlocks.addAll(getChildrenByType(child, blockClass, true));
94    }
95    }
96   
97  3 return typedBlocks;
98    }
99   
100    /**
101    * @param block the block to test
102    * @return true if the passed block is a protected block or false otherwise
103    */
 
104  1564845 toggle private boolean isProtectedBlock(Block block)
105    {
106  1564845 return (block instanceof MacroMarkerBlock)
107    && "code".equals(((MacroMarkerBlock) block).getId());
108    }
109    }