1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.officeimporter.internal.filter; |
21 |
|
|
22 |
|
import java.util.List; |
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.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.xml.html.filter.AbstractHTMLFilter; |
32 |
|
import org.xwiki.xml.html.filter.ElementSelector; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
43 |
|
@Component |
44 |
|
@Named("officeimporter/redundancy") |
45 |
|
@Singleton |
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 4 |
Complexity Density: 0.36 |
|
46 |
|
public class RedundancyFilter extends AbstractHTMLFilter |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
private static final String[] FILTERED_IF_NO_ATTRIBUTES_TAGS = new String[] {TAG_SPAN, TAG_DIV}; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private static final String[] FILTERED_IF_NO_CONTENT_TAGS = new String[] { |
57 |
|
TAG_EM, TAG_STRONG, TAG_DFN, TAG_CODE, TAG_SAMP, TAG_KBD, TAG_VAR, TAG_CITE, TAG_ABBR, |
58 |
|
TAG_ACRONYM, TAG_ADDRESS, TAG_BLOCKQUOTE, TAG_Q, TAG_PRE, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6}; |
59 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
60 |
66 |
@Override... |
61 |
|
public void filter(Document document, Map<String, String> cleaningParams) |
62 |
|
{ |
63 |
66 |
List<Element> elementsWithNoAttributes = |
64 |
|
filterDescendants(document.getDocumentElement(), FILTERED_IF_NO_ATTRIBUTES_TAGS, new ElementSelector() |
65 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
66 |
33 |
@Override... |
67 |
|
public boolean isSelected(Element element) |
68 |
|
{ |
69 |
33 |
return !element.hasAttributes(); |
70 |
|
} |
71 |
|
}); |
72 |
66 |
for (Element element : elementsWithNoAttributes) { |
73 |
33 |
replaceWithChildren(element); |
74 |
|
} |
75 |
66 |
List<Element> elementsWithNoContent = |
76 |
|
filterDescendants(document.getDocumentElement(), FILTERED_IF_NO_CONTENT_TAGS, new ElementSelector() |
77 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
68 |
@Override... |
79 |
|
public boolean isSelected(Element element) |
80 |
|
{ |
81 |
68 |
return element.getTextContent().trim().equals(""); |
82 |
|
} |
83 |
|
}); |
84 |
66 |
for (Element element : elementsWithNoContent) { |
85 |
20 |
String textContent = element.getTextContent(); |
86 |
20 |
if (textContent.equals("")) { |
87 |
20 |
element.getParentNode().removeChild(element); |
88 |
|
} else { |
89 |
0 |
element.setTextContent(textContent.replaceAll(" ", " ")); |
90 |
|
} |
91 |
|
} |
92 |
|
} |
93 |
|
} |