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.ArrayList; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.w3c.dom.Document; |
31 |
|
import org.w3c.dom.Element; |
32 |
|
import org.w3c.dom.NamedNodeMap; |
33 |
|
import org.w3c.dom.Node; |
34 |
|
import org.w3c.dom.NodeList; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.xml.html.filter.AbstractHTMLFilter; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
@version |
42 |
|
@since |
43 |
|
|
44 |
|
@Component |
45 |
|
@Named("officeimporter/style") |
46 |
|
@Singleton |
|
|
| 89.1% |
Uncovered Elements: 5 (46) |
Complexity: 12 |
Complexity Density: 0.44 |
|
47 |
|
public class StyleFilter extends AbstractHTMLFilter |
48 |
|
{ |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
private static final String ATTRIBUTE_SEPARATOR = "|"; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private Map<String, String> attributeMappingsStrict; |
60 |
|
|
61 |
|
|
62 |
|
@link |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
64 |
20 |
public StyleFilter()... |
65 |
|
{ |
66 |
20 |
this.attributeMappingsStrict = new HashMap<String, String>(); |
67 |
20 |
this.attributeMappingsStrict.put(TAG_A, "|href|name|"); |
68 |
20 |
this.attributeMappingsStrict.put(TAG_IMG, "|alt|src|height|width|"); |
69 |
20 |
this.attributeMappingsStrict.put(TAG_TD, "|colspan|rowspan|"); |
70 |
20 |
this.attributeMappingsStrict.put(TAG_TH, "|colspan|"); |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
73 |
66 |
@Override... |
74 |
|
public void filter(Document document, Map<String, String> cleaningParams) |
75 |
|
{ |
76 |
66 |
String mode = cleaningParams.get("filterStyles"); |
77 |
66 |
if (null != mode && mode.equals("strict")) { |
78 |
35 |
filter(document.getDocumentElement(), this.attributeMappingsStrict); |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@param |
86 |
|
@param |
87 |
|
|
|
|
| 84.8% |
Uncovered Elements: 5 (33) |
Complexity: 8 |
Complexity Density: 0.42 |
|
88 |
378 |
private void filter(Node node, Map<String, String> attributeMappings)... |
89 |
|
{ |
90 |
378 |
if (node instanceof Element) { |
91 |
268 |
Element element = (Element) node; |
92 |
268 |
String allowedAttributes = attributeMappings.get(element.getNodeName().toLowerCase()); |
93 |
268 |
NamedNodeMap currentAttributes = element.getAttributes(); |
94 |
268 |
if (null == allowedAttributes) { |
95 |
|
|
96 |
257 |
while (currentAttributes.getLength() > 0) { |
97 |
0 |
currentAttributes.removeNamedItem(currentAttributes.item(0).getNodeName()); |
98 |
|
} |
99 |
|
} else { |
100 |
|
|
101 |
11 |
List<String> attributesToBeRemoved = new ArrayList<String>(); |
102 |
14 |
for (int i = 0; i < currentAttributes.getLength(); i++) { |
103 |
3 |
String attributeName = currentAttributes.item(i).getNodeName(); |
104 |
3 |
String pattern = ATTRIBUTE_SEPARATOR + attributeName.toLowerCase() + ATTRIBUTE_SEPARATOR; |
105 |
3 |
if (allowedAttributes.indexOf(pattern) == -1) { |
106 |
0 |
attributesToBeRemoved.add(attributeName); |
107 |
|
} |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
11 |
for (String attribute : attributesToBeRemoved) { |
112 |
0 |
currentAttributes.removeNamedItem(attribute); |
113 |
|
} |
114 |
|
} |
115 |
268 |
if (node.hasChildNodes()) { |
116 |
232 |
NodeList children = node.getChildNodes(); |
117 |
575 |
for (int i = 0; i < children.getLength(); i++) { |
118 |
343 |
filter(children.item(i), attributeMappings); |
119 |
|
} |
120 |
|
} |
121 |
|
} |
122 |
|
} |
123 |
|
} |