1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.refactoring.internal.splitter; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.ListIterator; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.apache.commons.lang3.StringUtils; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.refactoring.WikiDocument; |
33 |
|
import org.xwiki.refactoring.splitter.DocumentSplitter; |
34 |
|
import org.xwiki.refactoring.splitter.criterion.SplittingCriterion; |
35 |
|
import org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion; |
36 |
|
import org.xwiki.rendering.block.Block; |
37 |
|
import org.xwiki.rendering.block.Block.Axes; |
38 |
|
import org.xwiki.rendering.block.BlockFilter; |
39 |
|
import org.xwiki.rendering.block.HeaderBlock; |
40 |
|
import org.xwiki.rendering.block.IdBlock; |
41 |
|
import org.xwiki.rendering.block.LinkBlock; |
42 |
|
import org.xwiki.rendering.block.NewLineBlock; |
43 |
|
import org.xwiki.rendering.block.SectionBlock; |
44 |
|
import org.xwiki.rendering.block.SpaceBlock; |
45 |
|
import org.xwiki.rendering.block.SpecialSymbolBlock; |
46 |
|
import org.xwiki.rendering.block.WordBlock; |
47 |
|
import org.xwiki.rendering.block.XDOM; |
48 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
49 |
|
import org.xwiki.rendering.listener.reference.DocumentResourceReference; |
50 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
51 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
52 |
|
|
53 |
|
|
54 |
|
@link |
55 |
|
|
56 |
|
@version |
57 |
|
@since |
58 |
|
|
59 |
|
@Component |
60 |
|
@Singleton |
|
|
| 90% |
Uncovered Elements: 8 (80) |
Complexity: 24 |
Complexity Density: 0.45 |
|
61 |
|
public class DefaultDocumentSplitter implements DocumentSplitter |
62 |
|
{ |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private static final String ANCHOR_PARAMETER = "anchor"; |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
68 |
3 |
@Override... |
69 |
|
public List<WikiDocument> split(WikiDocument rootDoc, SplittingCriterion splittingCriterion, |
70 |
|
NamingCriterion namingCriterion) |
71 |
|
{ |
72 |
3 |
List<WikiDocument> result = new ArrayList<WikiDocument>(); |
73 |
|
|
74 |
3 |
result.add(rootDoc); |
75 |
|
|
76 |
3 |
split(rootDoc, rootDoc.getXdom().getChildren(), 1, result, splittingCriterion, namingCriterion); |
77 |
3 |
updateAnchors(result); |
78 |
3 |
return result; |
79 |
|
} |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
@param@link |
85 |
|
@param |
86 |
|
@param |
87 |
|
@param |
88 |
|
@param@link |
89 |
|
@param@link |
90 |
|
|
|
|
| 91.3% |
Uncovered Elements: 2 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
91 |
8 |
private void split(WikiDocument parentDoc, List<Block> children, int depth, List<WikiDocument> result,... |
92 |
|
SplittingCriterion splittingCriterion, NamingCriterion namingCriterion) |
93 |
|
{ |
94 |
8 |
ListIterator<Block> it = children.listIterator(); |
95 |
27 |
while (it.hasNext()) { |
96 |
19 |
Block block = it.next(); |
97 |
19 |
if (splittingCriterion.shouldSplit(block, depth)) { |
98 |
|
|
99 |
8 |
XDOM xdom = new XDOM(block.getChildren()); |
100 |
8 |
String newDocumentName = namingCriterion.getDocumentName(xdom); |
101 |
8 |
WikiDocument newDoc = new WikiDocument(newDocumentName, xdom, parentDoc); |
102 |
8 |
result.add(newDoc); |
103 |
|
|
104 |
8 |
it.remove(); |
105 |
|
|
106 |
8 |
it.add(new NewLineBlock()); |
107 |
8 |
it.add(createLink(block, newDocumentName)); |
108 |
|
|
109 |
8 |
if (splittingCriterion.shouldIterate(block, depth)) { |
110 |
5 |
split(newDoc, newDoc.getXdom().getChildren(), depth + 1, result, splittingCriterion, |
111 |
|
namingCriterion); |
112 |
|
} |
113 |
11 |
} else if (splittingCriterion.shouldIterate(block, depth)) { |
114 |
0 |
split(parentDoc, block.getChildren(), depth + 1, result, splittingCriterion, namingCriterion); |
115 |
|
} |
116 |
|
} |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
@link |
121 |
|
|
122 |
|
@param@link |
123 |
|
@param |
124 |
|
@return@link |
125 |
|
|
|
|
| 50% |
Uncovered Elements: 6 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
126 |
8 |
private LinkBlock createLink(Block block, String target)... |
127 |
|
{ |
128 |
8 |
Block firstBlock = block.getChildren().get(0); |
129 |
8 |
if (firstBlock instanceof HeaderBlock) { |
130 |
8 |
DocumentResourceReference reference = new DocumentResourceReference(target); |
131 |
|
|
132 |
8 |
Block clonedHeaderBlock = firstBlock.clone(new BlockFilter() |
133 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 4 |
Complexity Density: 1 |
|
134 |
9 |
@Override... |
135 |
|
public List<Block> filter(Block block) |
136 |
|
{ |
137 |
9 |
List<Block> blocks = new ArrayList<Block>(); |
138 |
9 |
if (block instanceof WordBlock || block instanceof SpaceBlock |
139 |
|
|| block instanceof SpecialSymbolBlock) { |
140 |
8 |
blocks.add(block); |
141 |
|
} |
142 |
9 |
return blocks; |
143 |
|
} |
144 |
|
}); |
145 |
8 |
return new LinkBlock(clonedHeaderBlock.getChildren(), reference, false); |
146 |
0 |
} else if (firstBlock instanceof SectionBlock) { |
147 |
0 |
return createLink(firstBlock, target); |
148 |
|
} else { |
149 |
0 |
throw new IllegalArgumentException( |
150 |
|
"A SectionBlock should either begin with a HeaderBlock or another SectionBlock."); |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
161 |
3 |
private void updateAnchors(List<WikiDocument> documents)... |
162 |
|
{ |
163 |
|
|
164 |
3 |
Map<String, String> fragments = collectDocumentFragments(documents); |
165 |
|
|
166 |
|
|
167 |
3 |
for (WikiDocument document : documents) { |
168 |
11 |
updateAnchors(document, fragments); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
@param |
174 |
|
@param@link |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 9 |
Complexity Density: 0.69 |
|
176 |
11 |
private void updateAnchors(WikiDocument document, Map<String, String> fragments)... |
177 |
|
{ |
178 |
11 |
for (LinkBlock linkBlock : document.getXdom().<LinkBlock> getBlocks(new ClassBlockMatcher(LinkBlock.class), |
179 |
|
Axes.DESCENDANT)) { |
180 |
10 |
ResourceReference reference = linkBlock.getReference(); |
181 |
10 |
ResourceType resoureceType = reference.getType(); |
182 |
10 |
String fragment = null; |
183 |
10 |
if ((ResourceType.DOCUMENT.equals(resoureceType) || ResourceType.SPACE.equals(resoureceType)) |
184 |
|
&& StringUtils.isEmpty(reference.getReference())) { |
185 |
1 |
fragment = reference.getParameter(ANCHOR_PARAMETER); |
186 |
9 |
} else if (StringUtils.startsWith(reference.getReference(), "#") |
187 |
|
&& (ResourceType.PATH.equals(resoureceType) || ResourceType.URL.equals(resoureceType))) { |
188 |
1 |
fragment = reference.getReference().substring(1); |
189 |
|
} |
190 |
|
|
191 |
10 |
String targetDocument = fragments.get(fragment); |
192 |
10 |
if (targetDocument != null && !targetDocument.equals(document.getFullName())) { |
193 |
|
|
194 |
2 |
reference.setType(ResourceType.DOCUMENT); |
195 |
2 |
reference.setReference(targetDocument); |
196 |
2 |
reference.setParameter(ANCHOR_PARAMETER, fragment); |
197 |
|
} |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
|
@link |
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
@return |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
208 |
3 |
private Map<String, String> collectDocumentFragments(List<WikiDocument> documents)... |
209 |
|
{ |
210 |
3 |
Map<String, String> fragments = new HashMap<String, String>(); |
211 |
3 |
for (WikiDocument document : documents) { |
212 |
11 |
for (IdBlock idBlock : document.getXdom().<IdBlock> getBlocks(new ClassBlockMatcher(IdBlock.class), |
213 |
|
Axes.DESCENDANT)) { |
214 |
1 |
fragments.put(idBlock.getName(), document.getFullName()); |
215 |
|
} |
216 |
|
} |
217 |
3 |
return fragments; |
218 |
|
} |
219 |
|
} |