| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.devacfr.maven.skins.reflow.snippet; |
| 17 |
|
|
| 18 |
|
import static java.util.Objects.requireNonNull; |
| 19 |
|
|
| 20 |
|
import com.google.common.base.MoreObjects; |
| 21 |
|
import com.google.common.base.Strings; |
| 22 |
|
import com.google.common.collect.Maps; |
| 23 |
|
import com.google.common.collect.Sets; |
| 24 |
|
import java.util.Map; |
| 25 |
|
import java.util.Set; |
| 26 |
|
import java.util.stream.Collectors; |
| 27 |
|
import javax.annotation.Nonnull; |
| 28 |
|
import javax.annotation.Nullable; |
| 29 |
|
import org.jsoup.nodes.Attribute; |
| 30 |
|
import org.jsoup.nodes.Attributes; |
| 31 |
|
import org.jsoup.nodes.Element; |
| 32 |
|
import org.jsoup.nodes.Node; |
| 33 |
|
import org.jsoup.nodes.TextNode; |
| 34 |
|
import org.jsoup.parser.Tag; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
@author |
| 40 |
|
@version |
| 41 |
|
@param |
| 42 |
|
|
| 43 |
|
|
| |
|
| 81,6% |
Uncovered Elements: 21 (114) |
Complexity: 38 |
Complexity Density: 0,6 |
|
| 44 |
|
public class Component<T extends Component<T>> { |
| 45 |
|
|
| 46 |
|
private final static Set<String> knownTags = Sets.newHashSet("svg"); |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
private final Map<String, String> attributes = Maps.newHashMap(); |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
private Component<?> parent; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private final Components children = new Components(); |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
private final Map<String, Components> childrenMap = Maps.newHashMap(); |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
private final Node node; |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
@param |
| 65 |
|
|
| 66 |
|
@param |
| 67 |
|
|
| 68 |
|
@return |
| 69 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 70 |
317 |
public static Component<?> createComponent(@Nonnull final Node node, final Component<?> parent) {... |
| 71 |
317 |
return new Component<>(node).withParent(parent).addAttributes(node.attributes()); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@param |
| 76 |
|
|
| 77 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 78 |
370 |
protected Component(@Nonnull final Node node) {... |
| 79 |
370 |
this.node = requireNonNull(node); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
@return |
| 84 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 85 |
1049 |
public String getName() {... |
| 86 |
1049 |
return node.nodeName(); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
@return |
| 91 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 92 |
577 |
public boolean isHtmlTag() {... |
| 93 |
577 |
return node instanceof TextNode || Tag.isKnownTag(node.nodeName()) || knownTags.contains(node.nodeName()); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
@return |
| 98 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
| 99 |
321 |
public String getHtml() {... |
| 100 |
321 |
if (!isHtmlTag()) { |
| 101 |
124 |
if (children.isEmpty()) { |
| 102 |
12 |
return ""; |
| 103 |
|
} else { |
| 104 |
112 |
return children.html(); |
| 105 |
|
} |
| 106 |
|
} else { |
| 107 |
197 |
return this.node.outerHtml(); |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
@return |
| 113 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 114 |
15 |
public String getOwnHtml() {... |
| 115 |
15 |
if (!isHtmlTag()) { |
| 116 |
3 |
return null; |
| 117 |
|
} else { |
| 118 |
12 |
return this.node.outerHtml(); |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
|
| 123 |
|
@return |
| 124 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 125 |
9 |
public Component<?> getParent() {... |
| 126 |
9 |
return parent; |
| 127 |
|
} |
| 128 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 129 |
52 |
@Nullable protected Element getElement() {... |
| 130 |
52 |
if (this.node instanceof Element) { |
| 131 |
52 |
return (Element) this.node; |
| 132 |
|
} |
| 133 |
0 |
return null; |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
@param |
| 138 |
|
|
| 139 |
|
@return |
| 140 |
|
|
| |
|
| 87,5% |
Uncovered Elements: 3 (24) |
Complexity: 7 |
Complexity Density: 0,5 |
|
| 141 |
307 |
public Object get(@Nonnull final String name) {... |
| 142 |
307 |
requireNonNull(name); |
| 143 |
307 |
String key = name.toLowerCase(); |
| 144 |
|
|
| 145 |
307 |
if (this.attributes.containsKey(key)) { |
| 146 |
98 |
return this.attributes.get(key); |
| 147 |
|
} else { |
| 148 |
|
|
| 149 |
|
|
| 150 |
209 |
if (key.endsWith("s") && !this.childrenMap.containsKey(key)) { |
| 151 |
9 |
key = key.substring(0, key.length() - 1); |
| 152 |
9 |
if (this.childrenMap.containsKey(key)) { |
| 153 |
9 |
return this.childrenMap.get(key); |
| 154 |
|
} |
| 155 |
200 |
} else if (this.childrenMap.containsKey(key)) { |
| 156 |
148 |
final Components value = this.childrenMap.get(key); |
| 157 |
|
|
| 158 |
148 |
if (value.size() > 1) { |
| 159 |
0 |
return value; |
| 160 |
|
} |
| 161 |
148 |
return value.first(); |
| 162 |
|
} |
| 163 |
|
} |
| 164 |
52 |
return null; |
| 165 |
|
} |
| 166 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 167 |
269 |
public Map<String, String> getAttrs() {... |
| 168 |
269 |
return this.attributes; |
| 169 |
|
} |
| 170 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 171 |
1 |
public String getAttribute(String name) {... |
| 172 |
1 |
if (!this.attributes.containsKey(name)) { |
| 173 |
0 |
return null; |
| 174 |
|
} |
| 175 |
1 |
return this.attributes.get(name); |
| 176 |
|
} |
| 177 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 178 |
22 |
public boolean hasAttribute(String name) {... |
| 179 |
22 |
return this.attributes.containsKey(name); |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
|
| 183 |
|
@return |
| 184 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 185 |
15 |
public Map<String, String> getAriaAttributes() {... |
| 186 |
15 |
return this.attributes.entrySet() |
| 187 |
|
.stream() |
| 188 |
|
.filter(e -> e.getKey().startsWith("aria-")) |
| 189 |
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
|
| 193 |
|
@return |
| 194 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 195 |
15 |
public Map<String, String> getDataAttributes() {... |
| 196 |
15 |
return this.attributes.entrySet() |
| 197 |
|
.stream() |
| 198 |
|
.filter(e -> e.getKey().startsWith("data-")) |
| 199 |
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 200 |
|
} |
| 201 |
|
|
| 202 |
|
|
| 203 |
|
@return |
| 204 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 205 |
382 |
public Components getChildren() {... |
| 206 |
382 |
return children; |
| 207 |
|
} |
| 208 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 209 |
0 |
public Components getChildren(final String name) {... |
| 210 |
0 |
final String key = requireNonNull(name).toLowerCase(); |
| 211 |
0 |
if (this.childrenMap.containsKey(key)) { |
| 212 |
0 |
return this.childrenMap.get(key); |
| 213 |
|
} |
| 214 |
0 |
return Components.empty(); |
| 215 |
|
} |
| 216 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0,22 |
|
| 217 |
319 |
public T addChild(final Component<?> component) {... |
| 218 |
319 |
final String key = component.getName(); |
| 219 |
319 |
Components value = null; |
| 220 |
319 |
if (!this.childrenMap.containsKey(key)) { |
| 221 |
284 |
value = new Components(); |
| 222 |
284 |
this.childrenMap.put(key, value); |
| 223 |
|
} else { |
| 224 |
35 |
value = this.childrenMap.get(key); |
| 225 |
|
} |
| 226 |
319 |
value.add(component); |
| 227 |
319 |
this.children.add(component); |
| 228 |
319 |
return self(); |
| 229 |
|
} |
| 230 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 231 |
370 |
protected T withParent(final Component<?> parent) {... |
| 232 |
370 |
this.parent = parent; |
| 233 |
370 |
return self(); |
| 234 |
|
} |
| 235 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 236 |
0 |
@Nonnull... |
| 237 |
|
protected SnippetComponent<?> getRootParent() { |
| 238 |
0 |
Component<?> parent = this.parent; |
| 239 |
0 |
while (!(parent instanceof SnippetComponent<?>)) { |
| 240 |
0 |
parent = parent.parent; |
| 241 |
|
} |
| 242 |
0 |
return (SnippetComponent<?>) parent; |
| 243 |
|
} |
| 244 |
|
|
| 245 |
|
|
| 246 |
|
@param |
| 247 |
|
|
| 248 |
|
@return |
| 249 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 250 |
370 |
protected T addAttributes(@Nonnull final Attributes attrs) {... |
| 251 |
370 |
attrs.asList().stream().forEach(this::addAttribute); |
| 252 |
370 |
return self(); |
| 253 |
|
} |
| 254 |
|
|
| 255 |
|
|
| 256 |
|
@param |
| 257 |
|
|
| 258 |
|
@return |
| 259 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 260 |
398 |
protected T addAttribute(@Nonnull final Attribute attr) {... |
| 261 |
398 |
this.attributes.put(requireNonNull(attr.getKey()).toLowerCase(), |
| 262 |
398 |
Strings.isNullOrEmpty(attr.getValue()) ? "true" : attr.getValue()); |
| 263 |
398 |
return self(); |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
@return |
| 268 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 269 |
1457 |
@SuppressWarnings("unchecked")... |
| 270 |
|
protected T self() { |
| 271 |
1457 |
return (T) this; |
| 272 |
|
} |
| 273 |
|
|
| 274 |
|
|
| 275 |
|
@inheritDoc |
| 276 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 277 |
232 |
@Override... |
| 278 |
|
public String toString() { |
| 279 |
232 |
return MoreObjects.toStringHelper(this) |
| 280 |
|
.add("name", this.getName()) |
| 281 |
|
.add("isHtmlTag", this.isHtmlTag()) |
| 282 |
|
.add("attributes", this.attributes) |
| 283 |
|
.add("children", this.children.stream().map((cpt) -> cpt.getName()).collect(Collectors.toList())) |
| 284 |
|
.toString(); |
| 285 |
|
} |
| 286 |
|
} |