1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.refactoring.splitter.criterion.naming; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
26 |
|
import org.xwiki.rendering.block.Block; |
27 |
|
import org.xwiki.rendering.block.BlockFilter; |
28 |
|
import org.xwiki.rendering.block.HeaderBlock; |
29 |
|
import org.xwiki.rendering.block.SpaceBlock; |
30 |
|
import org.xwiki.rendering.block.SpecialSymbolBlock; |
31 |
|
import org.xwiki.rendering.block.WordBlock; |
32 |
|
import org.xwiki.rendering.block.XDOM; |
33 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
34 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
35 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
36 |
|
|
37 |
|
|
38 |
|
@link |
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 91.8% |
Uncovered Elements: 5 (61) |
Complexity: 18 |
Complexity Density: 0.46 |
|
43 |
|
public class HeadingNameNamingCriterion implements NamingCriterion |
44 |
|
{ |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
private BlockRenderer plainSyntaxRenderer; |
49 |
|
|
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
private DocumentAccessBridge docBridge; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@link |
58 |
|
|
59 |
|
private NamingCriterion mainPageNameAndNumberingNamingCriterion; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private List<String> documentNames; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private String basePageName; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private String spaceName; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
private boolean prependBasePageName; |
80 |
|
|
81 |
|
|
82 |
|
@link |
83 |
|
|
84 |
|
@param |
85 |
|
@param@link |
86 |
|
@param |
87 |
|
@param |
88 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
89 |
2 |
public HeadingNameNamingCriterion(String baseDocumentName, DocumentAccessBridge docBridge,... |
90 |
|
BlockRenderer plainSyntaxRenderer, boolean prependBasePageName) |
91 |
|
{ |
92 |
2 |
this.mainPageNameAndNumberingNamingCriterion = new PageIndexNamingCriterion(baseDocumentName, docBridge); |
93 |
2 |
this.docBridge = docBridge; |
94 |
2 |
this.plainSyntaxRenderer = plainSyntaxRenderer; |
95 |
2 |
this.documentNames = new ArrayList<String>(); |
96 |
2 |
int dot = baseDocumentName.lastIndexOf('.'); |
97 |
2 |
this.spaceName = (dot != -1) ? baseDocumentName.substring(0, dot) : "Main"; |
98 |
2 |
this.basePageName = baseDocumentName.substring(dot + 1); |
99 |
2 |
this.prependBasePageName = prependBasePageName; |
100 |
|
} |
101 |
|
|
|
|
| 92.1% |
Uncovered Elements: 3 (38) |
Complexity: 11 |
Complexity Density: 0.46 |
|
102 |
11 |
@Override... |
103 |
|
public String getDocumentName(XDOM newDoc) |
104 |
|
{ |
105 |
11 |
String documentName = null; |
106 |
11 |
String prefix = spaceName + "."; |
107 |
11 |
if (newDoc.getChildren().size() > 0) { |
108 |
11 |
Block firstChild = newDoc.getChildren().get(0); |
109 |
11 |
if (firstChild instanceof HeaderBlock) { |
110 |
|
|
111 |
10 |
Block clonedHeaderBlock = firstChild.clone(new BlockFilter() |
112 |
|
{ |
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 4 |
Complexity Density: 1 |
|
113 |
24 |
public List<Block> filter(Block block)... |
114 |
|
{ |
115 |
24 |
List<Block> blocks = new ArrayList<Block>(); |
116 |
24 |
if (block instanceof WordBlock || block instanceof SpaceBlock |
117 |
|
|| block instanceof SpecialSymbolBlock) { |
118 |
24 |
blocks.add(block); |
119 |
|
} |
120 |
24 |
return blocks; |
121 |
|
} |
122 |
|
}); |
123 |
10 |
XDOM xdom = new XDOM(clonedHeaderBlock.getChildren()); |
124 |
|
|
125 |
10 |
WikiPrinter printer = new DefaultWikiPrinter(); |
126 |
10 |
this.plainSyntaxRenderer.render(xdom, printer); |
127 |
|
|
128 |
10 |
documentName = cleanPageName(printer.toString()); |
129 |
|
} |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
11 |
if (null == documentName || documentName.equals("")) { |
134 |
2 |
documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc); |
135 |
9 |
} else if (prependBasePageName) { |
136 |
0 |
documentName = prefix + basePageName + INDEX_SEPERATOR + documentName; |
137 |
|
} else { |
138 |
9 |
documentName = prefix + documentName; |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
11 |
int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255; |
143 |
11 |
if (documentName.length() > maxWidth) { |
144 |
1 |
documentName = documentName.substring(0, maxWidth); |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
11 |
String newDocumentName = documentName; |
149 |
11 |
int localIndex = 0; |
150 |
12 |
while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) { |
151 |
|
|
152 |
1 |
newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
11 |
documentNames.add(newDocumentName); |
157 |
|
|
158 |
11 |
return newDocumentName; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
@param |
165 |
|
@return |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
167 |
10 |
private String cleanPageName(String originalName)... |
168 |
|
{ |
169 |
|
|
170 |
10 |
String replaced = originalName.trim().replaceAll("[\\.:]", "-"); |
171 |
|
|
172 |
10 |
replaced = replaced.replaceAll("[@?#~/]", ""); |
173 |
10 |
return replaced; |
174 |
|
|
175 |
|
} |
176 |
|
} |