| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.htmlcleaner; |
| 21 |
|
|
| 22 |
|
import java.util.Iterator; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Map; |
| 25 |
|
import java.util.regex.Matcher; |
| 26 |
|
import java.util.regex.Pattern; |
| 27 |
|
|
| 28 |
|
import javax.xml.parsers.DocumentBuilder; |
| 29 |
|
import javax.xml.parsers.ParserConfigurationException; |
| 30 |
|
|
| 31 |
|
import org.apache.commons.lang3.StringEscapeUtils; |
| 32 |
|
import org.apache.commons.lang3.StringUtils; |
| 33 |
|
import org.w3c.dom.Comment; |
| 34 |
|
import org.w3c.dom.DOMImplementation; |
| 35 |
|
import org.w3c.dom.Document; |
| 36 |
|
import org.w3c.dom.DocumentType; |
| 37 |
|
import org.w3c.dom.Element; |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@version |
| 55 |
|
@since |
| 56 |
|
|
| |
|
| 73.8% |
Uncovered Elements: 34 (130) |
Complexity: 27 |
Complexity Density: 0.31 |
|
| 57 |
|
public class XWikiDOMSerializer |
| 58 |
|
{ |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
private static final Pattern CDATA_PATTERN = Pattern.compile("<!\\[CDATA\\[.*(\\]\\]>|<!\\[CDATA\\[)", |
| 63 |
|
Pattern.DOTALL); |
| 64 |
|
|
| 65 |
|
private static final String CSS_COMMENT_START = "/*"; |
| 66 |
|
|
| 67 |
|
private static final String CSS_COMMENT_END = "*/"; |
| 68 |
|
|
| 69 |
|
private static final String JS_COMMENT = "//"; |
| 70 |
|
|
| 71 |
|
private static final String NEW_LINE = "\n"; |
| 72 |
|
|
| 73 |
|
private static final String SCRIPT_TAG_NAME = "script"; |
| 74 |
|
|
| 75 |
|
private static final String STYLE_TAG_NAME = "style"; |
| 76 |
|
|
| 77 |
|
private static final String HTML_TAG_NAME = "html"; |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
private CleanerProperties props; |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
private boolean escapeXml; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
@param |
| 91 |
|
@param |
| 92 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 93 |
4986 |
public XWikiDOMSerializer(CleanerProperties props, boolean escapeXml)... |
| 94 |
|
{ |
| 95 |
4986 |
this.props = props; |
| 96 |
4986 |
this.escapeXml = escapeXml; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
@param@link |
| 101 |
|
|
| 102 |
|
@param |
| 103 |
|
@return |
| 104 |
|
@throws |
| 105 |
|
|
| |
|
| 47.5% |
Uncovered Elements: 21 (40) |
Complexity: 7 |
Complexity Density: 0.25 |
|
| 106 |
4986 |
public Document createDOM(DocumentBuilder documentDocumentBuilder, TagNode rootNode)... |
| 107 |
|
throws ParserConfigurationException |
| 108 |
|
{ |
| 109 |
4986 |
DOMImplementation impl = documentDocumentBuilder.getDOMImplementation(); |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
4986 |
Document document; |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
4986 |
if (rootNode.getDocType() != null) { |
| 122 |
4986 |
String qualifiedName = rootNode.getDocType().getPart1(); |
| 123 |
4986 |
String publicId = rootNode.getDocType().getPublicId(); |
| 124 |
4986 |
String systemId = rootNode.getDocType().getSystemId(); |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
4986 |
if (qualifiedName == null) { |
| 130 |
0 |
qualifiedName = HTML_TAG_NAME; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
4986 |
DocumentType documentType = impl.createDocumentType(qualifiedName, publicId, systemId); |
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
4986 |
if (qualifiedName.equals("HTML")) { |
| 140 |
0 |
qualifiedName = HTML_TAG_NAME; |
| 141 |
|
} |
| 142 |
4986 |
document = impl.createDocument(rootNode.getNamespaceURIOnPath(""), qualifiedName, documentType); |
| 143 |
|
} else { |
| 144 |
0 |
document = documentDocumentBuilder.newDocument(); |
| 145 |
0 |
Element rootElement = document.createElement(rootNode.getName()); |
| 146 |
0 |
document.appendChild(rootElement); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
4986 |
Map<String, String> attributes = rootNode.getAttributes(); |
| 153 |
4986 |
Iterator<Map.Entry<String, String>> entryIterator = attributes.entrySet().iterator(); |
| 154 |
4986 |
while (entryIterator.hasNext()) { |
| 155 |
0 |
Map.Entry<String, String> entry = entryIterator.next(); |
| 156 |
0 |
String attrName = entry.getKey(); |
| 157 |
0 |
String attrValue = entry.getValue(); |
| 158 |
0 |
if (escapeXml) { |
| 159 |
0 |
attrValue = Utils.escapeXml(attrValue, props, true); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
0 |
document.getDocumentElement().setAttribute(attrName, attrValue); |
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
0 |
if (attrName.equalsIgnoreCase("id")) { |
| 168 |
0 |
document.getDocumentElement().setIdAttribute(attrName, true); |
| 169 |
|
} |
| 170 |
|
} |
| 171 |
|
|
| 172 |
4986 |
createSubnodes(document, document.getDocumentElement(), rootNode.getAllChildren()); |
| 173 |
|
|
| 174 |
4986 |
return document; |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
@param |
| 181 |
|
@param |
| 182 |
|
@param |
| 183 |
|
@param |
| 184 |
|
|
| |
|
| 92.3% |
Uncovered Elements: 2 (26) |
Complexity: 8 |
Complexity Density: 0.5 |
|
| 185 |
92443 |
private void flushContent(Document document, Element element, StringBuffer bufferedContent, Object item)... |
| 186 |
|
{ |
| 187 |
92443 |
if (bufferedContent.length() > 0 && !(item instanceof ContentNode)) { |
| 188 |
|
|
| 189 |
25499 |
boolean specialCase = this.props.isUseCdataForScriptAndStyle() && isScriptOrStyle(element); |
| 190 |
25499 |
String content = bufferedContent.toString(); |
| 191 |
|
|
| 192 |
25499 |
if (this.escapeXml && !specialCase) { |
| 193 |
0 |
content = Utils.escapeXml(content, this.props, true); |
| 194 |
25499 |
} else if (specialCase) { |
| 195 |
194 |
content = processCDATABlocks(content); |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
25499 |
if (specialCase) { |
| 201 |
194 |
if (SCRIPT_TAG_NAME.equalsIgnoreCase(element.getNodeName())) { |
| 202 |
|
|
| 203 |
188 |
element.appendChild(document.createTextNode(JS_COMMENT)); |
| 204 |
188 |
element.appendChild(document.createCDATASection(NEW_LINE + content + NEW_LINE + JS_COMMENT)); |
| 205 |
|
} else { |
| 206 |
|
|
| 207 |
6 |
element.appendChild(document.createTextNode(CSS_COMMENT_START)); |
| 208 |
6 |
element.appendChild(document.createCDATASection(CSS_COMMENT_END + StringUtils.chomp(content) |
| 209 |
|
+ NEW_LINE + CSS_COMMENT_START)); |
| 210 |
6 |
element.appendChild(document.createTextNode(CSS_COMMENT_END)); |
| 211 |
|
} |
| 212 |
|
} else { |
| 213 |
25305 |
element.appendChild(document.createTextNode(content)); |
| 214 |
|
} |
| 215 |
|
|
| 216 |
25499 |
bufferedContent.setLength(0); |
| 217 |
|
} |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
@param |
| 224 |
|
@return |
| 225 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 5 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 226 |
194 |
private String processCDATABlocks(String content)... |
| 227 |
|
{ |
| 228 |
194 |
StringBuffer result = new StringBuffer(); |
| 229 |
194 |
Matcher matcher = CDATA_PATTERN.matcher(content); |
| 230 |
194 |
int cursor = 0; |
| 231 |
194 |
while (matcher.find()) { |
| 232 |
0 |
result.append(StringEscapeUtils.unescapeHtml4(content.substring(cursor, matcher.start()))); |
| 233 |
0 |
result.append(content.substring(matcher.start() + 9, matcher.end() - matcher.group(1).length())); |
| 234 |
0 |
cursor = matcher.end() - matcher.group(1).length() + 3; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
194 |
if (cursor < content.length()) { |
| 238 |
194 |
result.append(StringEscapeUtils.unescapeHtml4(content.substring(cursor))); |
| 239 |
|
} |
| 240 |
|
|
| 241 |
194 |
String contentResult = result.toString().replace("<![CDATA[", "").replace("]]>", ""); |
| 242 |
|
|
| 243 |
194 |
return contentResult; |
| 244 |
|
} |
| 245 |
|
|
| 246 |
|
|
| 247 |
|
@param |
| 248 |
|
@return |
| 249 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 250 |
25499 |
protected boolean isScriptOrStyle(Element element)... |
| 251 |
|
{ |
| 252 |
25499 |
String tagName = element.getNodeName(); |
| 253 |
25499 |
return SCRIPT_TAG_NAME.equalsIgnoreCase(tagName) || STYLE_TAG_NAME.equalsIgnoreCase(tagName); |
| 254 |
|
} |
| 255 |
|
|
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
@param |
| 260 |
|
@param |
| 261 |
|
@param |
| 262 |
|
|
| |
|
| 84.6% |
Uncovered Elements: 6 (39) |
Complexity: 7 |
Complexity Density: 0.26 |
|
| 263 |
35754 |
private void createSubnodes(Document document, Element element, List<? extends BaseToken> tagChildren)... |
| 264 |
|
{ |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
35754 |
StringBuffer bufferedContent = new StringBuffer(); |
| 283 |
|
|
| 284 |
35754 |
if (tagChildren != null) { |
| 285 |
35754 |
for (Object item : tagChildren) { |
| 286 |
|
|
| 287 |
56689 |
flushContent(document, element, bufferedContent, item); |
| 288 |
|
|
| 289 |
56689 |
if (item instanceof CommentNode) { |
| 290 |
42 |
CommentNode commentToken = (CommentNode) item; |
| 291 |
42 |
Comment comment = document.createComment(commentToken.getContent()); |
| 292 |
42 |
element.appendChild(comment); |
| 293 |
56647 |
} else if (item instanceof ContentNode) { |
| 294 |
25874 |
ContentNode contentToken = (ContentNode) item; |
| 295 |
25874 |
bufferedContent.append(contentToken.getContent()); |
| 296 |
30773 |
} else if (item instanceof TagNode) { |
| 297 |
30768 |
TagNode subTagNode = (TagNode) item; |
| 298 |
30768 |
Element subelement = document.createElement(subTagNode.getName()); |
| 299 |
30768 |
Map<String, String> attributes = subTagNode.getAttributes(); |
| 300 |
30768 |
for (Map.Entry<String, String> entry : attributes.entrySet()) { |
| 301 |
31126 |
String attrName = entry.getKey(); |
| 302 |
31126 |
String attrValue = entry.getValue(); |
| 303 |
31126 |
if (this.escapeXml) { |
| 304 |
0 |
attrValue = Utils.escapeXml(attrValue, this.props, true); |
| 305 |
|
} |
| 306 |
31126 |
subelement.setAttribute(attrName, attrValue); |
| 307 |
|
} |
| 308 |
|
|
| 309 |
|
|
| 310 |
30768 |
createSubnodes(document, subelement, subTagNode.getAllChildren()); |
| 311 |
|
|
| 312 |
30768 |
element.appendChild(subelement); |
| 313 |
5 |
} else if (item instanceof List<?>) { |
| 314 |
0 |
@SuppressWarnings("unchecked") |
| 315 |
|
List<BaseToken> sublist = (List<BaseToken>) item; |
| 316 |
0 |
createSubnodes(document, element, sublist); |
| 317 |
|
} |
| 318 |
|
} |
| 319 |
35754 |
flushContent(document, element, bufferedContent, null); |
| 320 |
|
} |
| 321 |
|
} |
| 322 |
|
} |