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 |
|
@version |
38 |
|
@since |
39 |
|
|
40 |
|
@Component |
41 |
|
@Named("officeimporter/table") |
42 |
|
@Singleton |
|
|
| 95.2% |
Uncovered Elements: 1 (21) |
Complexity: 4 |
Complexity Density: 0.27 |
|
43 |
|
public class TableFilter extends AbstractHTMLFilter |
44 |
|
{ |
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
|
45 |
66 |
@Override... |
46 |
|
public void filter(Document document, Map<String, String> cleaningParams) |
47 |
|
{ |
48 |
|
|
49 |
66 |
List<Element> tableCells = filterDescendants(document.getDocumentElement(), new String[] {TAG_TD, TAG_TH}); |
50 |
66 |
for (Element cell : tableCells) { |
51 |
12 |
List<Element> paragraphs = filterChildren(cell, TAG_P); |
52 |
12 |
if (paragraphs.size() == 1) { |
53 |
2 |
replaceWithChildren(paragraphs.get(0)); |
54 |
|
} |
55 |
|
} |
56 |
|
|
57 |
66 |
List<Element> emptyRows = |
58 |
|
filterDescendants(document.getDocumentElement(), new String[] {TAG_TR}, new ElementSelector() |
59 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
60 |
11 |
@Override... |
61 |
|
public boolean isSelected(Element element) |
62 |
|
{ |
63 |
11 |
return element.getChildNodes().getLength() == 0; |
64 |
|
} |
65 |
|
}); |
66 |
66 |
for (Element emptyRow : emptyRows) { |
67 |
1 |
emptyRow.getParentNode().removeChild(emptyRow); |
68 |
|
} |
69 |
|
|
70 |
66 |
List<Element> rows = filterDescendants(document.getDocumentElement(), new String[] {TAG_TR}); |
71 |
66 |
for (Element row : rows) { |
72 |
10 |
List<Element> childCells = filterDescendants(row, new String[] {TAG_TD}); |
73 |
10 |
if (hasAttribute(childCells, ATTRIBUTE_ROWSPAN, true)) { |
74 |
10 |
for (Element childCell : childCells) { |
75 |
9 |
childCell.removeAttribute(ATTRIBUTE_ROWSPAN); |
76 |
|
} |
77 |
|
} |
78 |
|
} |
79 |
|
} |
80 |
|
} |