1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.xml.html; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.Writer; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.List; |
26 |
|
import java.util.regex.Matcher; |
27 |
|
import java.util.regex.Pattern; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.jdom.DocType; |
31 |
|
import org.jdom.Element; |
32 |
|
import org.jdom.input.DOMBuilder; |
33 |
|
import org.jdom.output.Format; |
34 |
|
import org.jdom.output.XMLOutputter; |
35 |
|
import org.w3c.dom.Document; |
36 |
|
import org.w3c.dom.Node; |
37 |
|
import org.w3c.dom.NodeList; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
45 |
|
|
|
|
| 91.9% |
Uncovered Elements: 5 (62) |
Complexity: 15 |
Complexity Density: 0.41 |
|
46 |
|
public final class HTMLUtils |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private static final List<String> OMIT_ELEMENT_EXPANDING_SET = Arrays.asList( |
54 |
|
"area", "base", "br", "col", "hr", "img", "input", "link", "meta", "param"); |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
|
|
| 81% |
Uncovered Elements: 12 (63) |
Complexity: 15 |
Complexity Density: 0.37 |
|
71 |
|
public static class XWikiXMLOutputter extends XMLOutputter |
72 |
|
{ |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private static final Pattern ENTITY = Pattern.compile("&[a-z]+;|&#[0-9a-zA-Z]+;"); |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private static final String AMPERSAND = "&"; |
82 |
|
|
83 |
|
private static final String[] REPLACE_ELEMENTS_SEARCH = new String[] { "<", ">" }; |
84 |
|
|
85 |
|
private static final String[] REPLACE_ELEMENTS_RESULT = new String[] { "<", ">" }; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private boolean omitDocType; |
91 |
|
|
92 |
|
|
93 |
|
@param@link |
94 |
|
@param |
95 |
|
@see |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
97 |
4789 |
public XWikiXMLOutputter(Format format, boolean omitDocType)... |
98 |
|
{ |
99 |
4789 |
super(format); |
100 |
4789 |
this.omitDocType = omitDocType; |
101 |
|
} |
102 |
|
|
|
|
| 47.8% |
Uncovered Elements: 12 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
103 |
56444 |
@Override... |
104 |
|
public String escapeElementEntities(String text) |
105 |
|
{ |
106 |
56444 |
if (text.length() == 0) { |
107 |
331 |
return text; |
108 |
|
} |
109 |
|
|
110 |
56113 |
String result; |
111 |
56113 |
int pos1 = text.indexOf("<![CDATA["); |
112 |
56113 |
if (pos1 > -1) { |
113 |
0 |
int pos2 = text.indexOf("]]>", pos1 + 9); |
114 |
0 |
if (pos2 + 3 == text.length()) { |
115 |
0 |
return text; |
116 |
|
} |
117 |
0 |
result = escapeElementEntities(text.substring(0, pos1)); |
118 |
0 |
if (pos2 + 3 == text.length()) { |
119 |
0 |
result = result + text.substring(pos1); |
120 |
|
} else { |
121 |
0 |
result = result + text.substring(pos1, pos2 + 3) + escapeElementEntities(text.substring(pos2 + 3)); |
122 |
|
} |
123 |
|
} else { |
124 |
56113 |
result = escapeAmpersand(text); |
125 |
56113 |
StringUtils.replaceEach(text, REPLACE_ELEMENTS_SEARCH, REPLACE_ELEMENTS_RESULT); |
126 |
|
} |
127 |
|
|
128 |
56113 |
return result; |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
131 |
31117 |
@Override... |
132 |
|
public String escapeAttributeEntities(String text) |
133 |
|
{ |
134 |
31117 |
String result = escapeElementEntities(text); |
135 |
|
|
136 |
|
|
137 |
31117 |
result = StringUtils.replace(result, "\"", """); |
138 |
|
|
139 |
31117 |
return result; |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@param |
146 |
|
@return |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
148 |
56113 |
private String escapeAmpersand(String text)... |
149 |
|
{ |
150 |
56113 |
StringBuilder buffer = new StringBuilder(text); |
151 |
|
|
152 |
56113 |
int pos = buffer.indexOf(AMPERSAND); |
153 |
62464 |
while (pos > -1 && pos < buffer.length()) { |
154 |
|
|
155 |
6351 |
Matcher matcher = ENTITY.matcher(buffer.substring(pos)); |
156 |
6351 |
if (matcher.lookingAt()) { |
157 |
|
|
158 |
2951 |
pos = pos + matcher.end() - matcher.start(); |
159 |
|
} else { |
160 |
|
|
161 |
3400 |
buffer.replace(pos, pos + 1, "&"); |
162 |
3400 |
pos += 5; |
163 |
|
} |
164 |
6351 |
pos = buffer.indexOf(AMPERSAND, pos); |
165 |
|
} |
166 |
56113 |
return buffer.toString(); |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
169 |
4788 |
@Override... |
170 |
|
protected void printDocType(Writer out, DocType docType) throws IOException |
171 |
|
{ |
172 |
4788 |
if (!this.omitDocType) { |
173 |
128 |
super.printDocType(out, docType); |
174 |
|
} |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
177 |
25576 |
@Override... |
178 |
|
protected void printElement(Writer out, Element element, int level, NamespaceStack namespaces) |
179 |
|
throws IOException |
180 |
|
{ |
181 |
|
|
182 |
25576 |
boolean currentFormatPolicy = currentFormat.getExpandEmptyElements(); |
183 |
25576 |
try { |
184 |
25576 |
String elementName = element.getName(); |
185 |
25576 |
for (String name : OMIT_ELEMENT_EXPANDING_SET) { |
186 |
247047 |
if (name.equals(elementName)) { |
187 |
|
|
188 |
2602 |
currentFormat.setExpandEmptyElements(false); |
189 |
2602 |
break; |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
25576 |
super.printElement(out, element, level, namespaces); |
195 |
|
|
196 |
|
} finally { |
197 |
|
|
198 |
25576 |
currentFormat.setExpandEmptyElements(currentFormatPolicy); |
199 |
|
} |
200 |
|
} |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
206 |
0 |
private HTMLUtils()... |
207 |
|
{ |
208 |
|
|
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
@param |
213 |
|
@return |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
129 |
public static String toString(Document document)... |
216 |
|
{ |
217 |
129 |
return HTMLUtils.toString(document, false, false); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
@param |
222 |
|
@param |
223 |
|
@param |
224 |
|
@return |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
226 |
4789 |
public static String toString(Document document, boolean omitDeclaration, boolean omitDoctype)... |
227 |
|
{ |
228 |
|
|
229 |
|
|
230 |
4789 |
DOMBuilder builder = new DOMBuilder(); |
231 |
4789 |
org.jdom.Document jdomDoc = builder.build(document); |
232 |
|
|
233 |
4789 |
Format format = Format.getRawFormat(); |
234 |
|
|
235 |
|
|
236 |
4789 |
format.setLineSeparator("\n"); |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
4789 |
format.setExpandEmptyElements(true); |
241 |
|
|
242 |
4789 |
format.setOmitDeclaration(omitDeclaration); |
243 |
|
|
244 |
4789 |
XMLOutputter outputter = new XWikiXMLOutputter(format, omitDoctype); |
245 |
4789 |
String result = outputter.outputString(jdomDoc); |
246 |
|
|
247 |
4789 |
return result; |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
@param |
256 |
|
|
|
|
| 93.8% |
Uncovered Elements: 2 (32) |
Complexity: 8 |
Complexity Density: 0.44 |
|
257 |
4660 |
public static void stripHTMLEnvelope(Document document)... |
258 |
|
{ |
259 |
4660 |
org.w3c.dom.Element root = document.getDocumentElement(); |
260 |
4660 |
if (root.getNodeName().equalsIgnoreCase(HTMLConstants.TAG_HTML)) { |
261 |
|
|
262 |
4660 |
Node bodyNode = null; |
263 |
4660 |
Node headNode = null; |
264 |
4660 |
NodeList nodes = root.getChildNodes(); |
265 |
13978 |
for (int i = 0; i < nodes.getLength(); i++) { |
266 |
9318 |
Node node = nodes.item(i); |
267 |
9318 |
if (node.getNodeName().equalsIgnoreCase(HTMLConstants.TAG_HEAD)) { |
268 |
4659 |
headNode = node; |
269 |
4659 |
} else if (node.getNodeName().equalsIgnoreCase(HTMLConstants.TAG_BODY)) { |
270 |
4659 |
bodyNode = node; |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
4660 |
if (headNode != null) { |
275 |
4659 |
root.removeChild(headNode); |
276 |
|
} |
277 |
|
|
278 |
4660 |
if (bodyNode != null) { |
279 |
|
|
280 |
4659 |
NodeList bodyChildrenNodes = bodyNode.getChildNodes(); |
281 |
10124 |
while (bodyChildrenNodes.getLength() > 0) { |
282 |
5465 |
root.insertBefore(bodyChildrenNodes.item(0), null); |
283 |
|
} |
284 |
4659 |
root.removeChild(bodyNode); |
285 |
|
} |
286 |
|
} |
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
@param |
293 |
|
@param |
294 |
|
@param |
295 |
|
|
|
|
| 86.7% |
Uncovered Elements: 2 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
296 |
3981 |
public static void stripFirstElementInside(Document document, String parentTagName, String elementTagName)... |
297 |
|
{ |
298 |
3981 |
NodeList parentNodes = document.getElementsByTagName(parentTagName); |
299 |
3981 |
if (parentNodes.getLength() > 0) { |
300 |
3981 |
Node parentNode = parentNodes.item(0); |
301 |
|
|
302 |
3981 |
Node pNode = parentNode.getFirstChild(); |
303 |
3981 |
if (elementTagName.equalsIgnoreCase(pNode.getNodeName())) { |
304 |
|
|
305 |
3981 |
NodeList pChildrenNodes = pNode.getChildNodes(); |
306 |
7971 |
while (pChildrenNodes.getLength() > 0) { |
307 |
3990 |
parentNode.insertBefore(pChildrenNodes.item(0), null); |
308 |
|
} |
309 |
3981 |
parentNode.removeChild(pNode); |
310 |
|
} |
311 |
|
} |
312 |
|
} |
313 |
|
} |