| 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 javax.annotation.Nonnull; |
| 21 |
|
import org.apache.commons.lang3.builder.ToStringBuilder; |
| 22 |
|
import org.apache.velocity.context.Context; |
| 23 |
|
import org.devacfr.maven.skins.reflow.JsoupUtils; |
| 24 |
|
import org.jsoup.nodes.Document; |
| 25 |
|
import org.jsoup.nodes.Element; |
| 26 |
|
import org.jsoup.select.Elements; |
| 27 |
|
import org.slf4j.Logger; |
| 28 |
|
import org.slf4j.LoggerFactory; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
@author |
| 32 |
|
@version |
| 33 |
|
@param |
| 34 |
|
|
| 35 |
|
|
| |
|
| 78,7% |
Uncovered Elements: 10 (47) |
Complexity: 12 |
Complexity Density: 0,36 |
|
| 36 |
|
public class SnippetComponent<T extends SnippetComponent<T>> extends Component<T> { |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
@author |
| 42 |
|
@version |
| 43 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 44 |
|
public enum Type { |
| 45 |
|
|
| 46 |
|
webComponent, |
| 47 |
|
|
| 48 |
|
shortcode |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
private static Logger LOGGER = LoggerFactory.getLogger(SnippetComponent.class); |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
private final Type type; |
| 55 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 56 |
53 |
@Nonnull... |
| 57 |
|
public static SnippetComponent<?> createSnippet(@Nonnull final Element element, |
| 58 |
|
final Component<?> parent, |
| 59 |
|
final Type type) { |
| 60 |
53 |
requireNonNull(element); |
| 61 |
53 |
return new SnippetComponent<>(element, type).withParent(parent); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@param |
| 68 |
|
|
| 69 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 70 |
53 |
public SnippetComponent(@Nonnull final Element element, final Type type) {... |
| 71 |
53 |
super(element); |
| 72 |
53 |
this.type = requireNonNull(type); |
| 73 |
53 |
this.addAttributes(element.attributes()); |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 79 |
2 |
@Nonnull... |
| 80 |
|
public Type getType() { |
| 81 |
2 |
return type; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
@link |
| 86 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 87 |
0 |
@Override... |
| 88 |
|
protected @Nonnull SnippetComponent<?> getRootParent() { |
| 89 |
0 |
return this; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
@link |
| 94 |
|
|
| 95 |
|
@param |
| 96 |
|
|
| 97 |
|
|
| |
|
| 75,8% |
Uncovered Elements: 8 (33) |
Complexity: 7 |
Complexity Density: 0,28 |
|
| 98 |
52 |
public void render(final SnippetContext context) {... |
| 99 |
52 |
final Context renderContext = context.getConfig().getVelocityContext(); |
| 100 |
52 |
final Element element = getElement(); |
| 101 |
52 |
if (element == null) { |
| 102 |
0 |
LOGGER.warn("Element is null for snippet component: {}, file:{}", this, context.getConfig().getFileId()); |
| 103 |
0 |
return; |
| 104 |
|
} |
| 105 |
52 |
try { |
| 106 |
52 |
final String html = context.renderComponent(this, renderContext); |
| 107 |
|
|
| 108 |
52 |
final Document doc = JsoupUtils.createHtmlDocument(html); |
| 109 |
52 |
final Elements root = doc.body().children(); |
| 110 |
52 |
if (root.isEmpty()) { |
| 111 |
0 |
element.remove(); |
| 112 |
0 |
return; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
52 |
if (root.size() > 1 && root.first().tagName().equals("pre")) { |
| 116 |
1 |
final Element div = new Element("div"); |
| 117 |
1 |
root.forEach((e) -> div.appendChild(e)); |
| 118 |
1 |
element.replaceWith(div); |
| 119 |
|
} else { |
| 120 |
51 |
for (final Element el : root) { |
| 121 |
|
|
| 122 |
51 |
if (context.getParser().hasIncludedSnippetComponent(el)) { |
| 123 |
1 |
final SnippetParser parser = context.createChildParser(); |
| 124 |
1 |
final Element childDoc = parser.parse(context.getConfig(), el.outerHtml()).document(); |
| 125 |
1 |
childDoc.children().forEach((e) -> element.before(e)); |
| 126 |
|
} else { |
| 127 |
50 |
element.before(el); |
| 128 |
|
} |
| 129 |
|
} |
| 130 |
51 |
element.remove(); |
| 131 |
|
} |
| 132 |
|
} catch (final Exception e) { |
| 133 |
0 |
LOGGER.error("Rendering Snippet component failed:{}, file:{}", this, context.getConfig().getFileId()); |
| 134 |
0 |
throw new RuntimeException(e.getMessage(), e); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
91 |
@Override... |
| 139 |
|
public String toString() { |
| 140 |
91 |
return ToStringBuilder.reflectionToString(this); |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
} |