| 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.Arrays; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Map; |
| 25 |
|
|
| 26 |
|
import javax.inject.Named; |
| 27 |
|
import javax.inject.Singleton; |
| 28 |
|
|
| 29 |
|
import org.w3c.dom.Document; |
| 30 |
|
import org.w3c.dom.Element; |
| 31 |
|
import org.w3c.dom.Node; |
| 32 |
|
import org.xwiki.component.annotation.Component; |
| 33 |
|
import org.xwiki.xml.html.filter.AbstractHTMLFilter; |
| 34 |
|
import org.xwiki.xml.html.filter.ElementSelector; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
@version |
| 40 |
|
@since |
| 41 |
|
|
| 42 |
|
@Component |
| 43 |
|
@Named("officeimporter/linebreak") |
| 44 |
|
@Singleton |
| |
|
| 100% |
Uncovered Elements: 0 (44) |
Complexity: 19 |
Complexity Density: 0.7 |
|
| 45 |
|
public class LineBreakFilter extends AbstractHTMLFilter |
| 46 |
|
{ |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
private static final String[] BLOCK_ELEMENT_TAGS = |
| 51 |
|
new String[] {TAG_P, TAG_UL, TAG_OL, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TAG_TABLE}; |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 56 |
1 |
static {... |
| 57 |
1 |
Arrays.sort(BLOCK_ELEMENT_TAGS); |
| 58 |
|
} |
| 59 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 60 |
66 |
@Override... |
| 61 |
|
public void filter(Document document, Map<String, String> cleaningParams) |
| 62 |
|
{ |
| 63 |
66 |
List<Element> lineBreaksToReplace = |
| 64 |
|
filterDescendants(document.getDocumentElement(), new String[] {TAG_BR}, new ElementSelector() |
| 65 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 66 |
65 |
@Override... |
| 67 |
|
public boolean isSelected(Element element) |
| 68 |
|
{ |
| 69 |
65 |
Node prev = findPreviousNode(element); |
| 70 |
65 |
Node next = findNextNode(element); |
| 71 |
65 |
return !(null == prev && null == next) && (isBlockElement(prev) || isBlockElement(next)); |
| 72 |
|
} |
| 73 |
|
}); |
| 74 |
66 |
for (Element lineBreak : lineBreaksToReplace) { |
| 75 |
60 |
Node parent = lineBreak.getParentNode(); |
| 76 |
60 |
Element element = document.createElement(TAG_DIV); |
| 77 |
60 |
element.setAttribute(ATTRIBUTE_CLASS, "wikimodel-emptyline"); |
| 78 |
60 |
parent.insertBefore(element, lineBreak); |
| 79 |
60 |
parent.removeChild(lineBreak); |
| 80 |
|
} |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
@param |
| 88 |
|
@return |
| 89 |
|
|
| 90 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 5 |
Complexity Density: 1.25 |
|
| 91 |
65 |
private Node findPreviousNode(Element element)... |
| 92 |
|
{ |
| 93 |
65 |
Node prev = element.getPreviousSibling(); |
| 94 |
98 |
while (prev != null && (isLineBreak(prev) || isEmptyTextNode(prev) || isCommentNode(prev))) { |
| 95 |
33 |
prev = prev.getPreviousSibling(); |
| 96 |
|
} |
| 97 |
65 |
return prev; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
@param |
| 104 |
|
@return |
| 105 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 5 |
Complexity Density: 1.25 |
|
| 106 |
65 |
private Node findNextNode(Element element)... |
| 107 |
|
{ |
| 108 |
65 |
Node next = element.getNextSibling(); |
| 109 |
98 |
while (next != null && (isLineBreak(next) || isEmptyTextNode(next) || isCommentNode(next))) { |
| 110 |
33 |
next = next.getNextSibling(); |
| 111 |
|
} |
| 112 |
65 |
return next; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
@param |
| 119 |
|
@return |
| 120 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 121 |
80 |
private boolean isBlockElement(Node node)... |
| 122 |
|
{ |
| 123 |
80 |
boolean isBlockElement = false; |
| 124 |
80 |
if (null != node) { |
| 125 |
60 |
for (String blockElement : BLOCK_ELEMENT_TAGS) { |
| 126 |
600 |
isBlockElement = blockElement.equals(node.getNodeName()) ? true : isBlockElement; |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
80 |
return isBlockElement; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
@param@link |
| 136 |
|
@return |
| 137 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
80 |
private boolean isEmptyTextNode(Node node)... |
| 139 |
|
{ |
| 140 |
80 |
return null != node && node.getNodeType() == Node.TEXT_NODE && node.getTextContent().trim().equals(""); |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
@param@link |
| 147 |
|
@return |
| 148 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 149 |
80 |
private boolean isCommentNode(Node node)... |
| 150 |
|
{ |
| 151 |
80 |
return null != node && node.getNodeType() == Node.COMMENT_NODE; |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
@param@link |
| 158 |
|
@return |
| 159 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 160 |
146 |
private boolean isLineBreak(Node node)... |
| 161 |
|
{ |
| 162 |
146 |
return null != node && node.getNodeName().equals(TAG_BR); |
| 163 |
|
} |
| 164 |
|
} |