| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| HeadingLevelSplittingCriterion | 34 | 9 | 0% | 4 | 0 |
| 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.refactoring.splitter.criterion; | |
| 21 | ||
| 22 | import java.util.Arrays; | |
| 23 | ||
| 24 | import org.xwiki.rendering.block.Block; | |
| 25 | import org.xwiki.rendering.block.HeaderBlock; | |
| 26 | import org.xwiki.rendering.block.SectionBlock; | |
| 27 | ||
| 28 | /** | |
| 29 | * A {@link SplittingCriterion} based on the heading levels present in an xwiki document. | |
| 30 | * | |
| 31 | * @version $Id: fd2e13e199969aa237ebc3a9c2befab2e3e74b7d $ | |
| 32 | * @since 1.9M1 | |
| 33 | */ | |
| 34 | public class HeadingLevelSplittingCriterion implements SplittingCriterion | |
| 35 | { | |
| 36 | /** | |
| 37 | * Array of heading levels (ascending) used for splitting. | |
| 38 | */ | |
| 39 | private int[] headingLevels; | |
| 40 | ||
| 41 | /** | |
| 42 | * Constructs a new {@link HeadingLevelSplittingCriterion}. | |
| 43 | * | |
| 44 | * @param headingLevels sorted array of heading levels (ascending) | |
| 45 | */ | |
| 46 | 4 | public HeadingLevelSplittingCriterion(int[] headingLevels) |
| 47 | { | |
| 48 | 4 | this.headingLevels = headingLevels; |
| 49 | 4 | Arrays.sort(this.headingLevels); |
| 50 | } | |
| 51 | ||
| 52 | 14 | @Override |
| 53 | public boolean shouldIterate(Block block, int depth) | |
| 54 | { | |
| 55 | 14 | return (block instanceof SectionBlock && depth < headingLevels[headingLevels.length - 1]); |
| 56 | } | |
| 57 | ||
| 58 | 16 | @Override |
| 59 | public boolean shouldSplit(Block block, int depth) | |
| 60 | { | |
| 61 | 16 | boolean shouldSplit = false; |
| 62 | 16 | if (block instanceof SectionBlock) { |
| 63 | 8 | SectionBlock section = (SectionBlock) block; |
| 64 | 8 | Block firstChild = section.getChildren().get(0); |
| 65 | 8 | shouldSplit = (firstChild instanceof HeaderBlock) && (Arrays.binarySearch(headingLevels, depth) >= 0); |
| 66 | } | |
| 67 | 16 | return shouldSplit; |
| 68 | } | |
| 69 | } |