1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.xml.internal.html.filter; |
21 |
|
|
22 |
|
import java.util.HashMap; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.w3c.dom.Document; |
29 |
|
import org.w3c.dom.Element; |
30 |
|
import org.w3c.dom.NodeList; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.xml.html.filter.AbstractHTMLFilter; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
@since |
39 |
|
|
40 |
|
@Component |
41 |
|
@Named("uniqueId") |
42 |
|
@Singleton |
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.36 |
|
43 |
|
public class UniqueIdFilter extends AbstractHTMLFilter |
44 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
45 |
1 |
@Override... |
46 |
|
public void filter(Document document, Map<String, String> cleaningParameters) |
47 |
|
{ |
48 |
1 |
Map<String, Integer> idCount = new HashMap<String, Integer>(); |
49 |
1 |
NodeList elements = document.getElementsByTagName("*"); |
50 |
7 |
for (int i = 0; i < elements.getLength(); i++) { |
51 |
6 |
Element element = (Element) elements.item(i); |
52 |
6 |
String id = element.getAttribute(ATTRIBUTE_ID); |
53 |
6 |
if (!"".equals(id)) { |
54 |
3 |
Integer count = idCount.get(id); |
55 |
3 |
if (count == null) { |
56 |
2 |
count = 0; |
57 |
|
} else { |
58 |
1 |
element.setAttribute(ATTRIBUTE_ID, id + count++); |
59 |
|
} |
60 |
3 |
idCount.put(id, count); |
61 |
|
} |
62 |
|
} |
63 |
|
} |
64 |
|
} |