| 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 com.google.common.collect.Iterables; |
| 19 |
|
import com.google.common.collect.Lists; |
| 20 |
|
import java.io.IOException; |
| 21 |
|
import java.util.List; |
| 22 |
|
import javax.annotation.Nonnull; |
| 23 |
|
import javax.annotation.Nullable; |
| 24 |
|
import org.devacfr.maven.skins.reflow.JsoupUtils; |
| 25 |
|
import org.devacfr.maven.skins.reflow.snippet.ComponentToken.Tag; |
| 26 |
|
import org.jsoup.nodes.Comment; |
| 27 |
|
import org.jsoup.nodes.Document; |
| 28 |
|
import org.jsoup.nodes.Element; |
| 29 |
|
import org.jsoup.nodes.Node; |
| 30 |
|
import org.jsoup.nodes.TextNode; |
| 31 |
|
import org.slf4j.Logger; |
| 32 |
|
import org.slf4j.LoggerFactory; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
@author |
| 38 |
|
@version |
| 39 |
|
|
| |
|
| 86,2% |
Uncovered Elements: 12 (87) |
Complexity: 19 |
Complexity Density: 0,3 |
|
| 40 |
|
public abstract class Processor { |
| 41 |
|
|
| 42 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class); |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
protected final SnippetParser parser; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@param |
| 51 |
|
|
| 52 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 53 |
33 |
public Processor(final SnippetParser parser) {... |
| 54 |
33 |
this.parser = parser; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
@link |
| 59 |
|
|
| 60 |
|
@param |
| 61 |
|
|
| 62 |
|
|
| |
|
| 80,8% |
Uncovered Elements: 5 (26) |
Complexity: 7 |
Complexity Density: 0,32 |
|
| 63 |
87 |
public void parse(final ComponentToken token) {... |
| 64 |
87 |
if (LOGGER.isDebugEnabled()) { |
| 65 |
87 |
LOGGER.debug("Parse Token: {}", token); |
| 66 |
|
} |
| 67 |
87 |
SnippetContext snippetContext = parser.getSnippetContext(); |
| 68 |
87 |
switch (token.tag()) { |
| 69 |
9 |
case empty: |
| 70 |
9 |
snippetContext.render(createSnippetComponent(snippetContext, token, null)); |
| 71 |
9 |
break; |
| 72 |
|
|
| 73 |
38 |
case start: |
| 74 |
38 |
parser.push(token); |
| 75 |
38 |
parser.parse(); |
| 76 |
38 |
break; |
| 77 |
|
|
| 78 |
38 |
case end: |
| 79 |
38 |
final ComponentToken startToken = parser.pop(); |
| 80 |
38 |
if (!token.isCloseTagOf(startToken)) { |
| 81 |
0 |
throw new RuntimeException("start token " + startToken + " should be closed, but next token is " + token); |
| 82 |
|
} |
| 83 |
38 |
snippetContext.render(createSnippetComponent(snippetContext, startToken, token)); |
| 84 |
38 |
break; |
| 85 |
|
|
| 86 |
2 |
case html: |
| 87 |
2 |
snippetContext.render(createSnippetComponent(snippetContext, token, null)); |
| 88 |
2 |
break; |
| 89 |
|
|
| 90 |
0 |
default: |
| 91 |
0 |
throw new SnippetParseException("unknown token tag " + token.tag()); |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
@link |
| 97 |
|
|
| 98 |
|
@param |
| 99 |
|
|
| 100 |
|
@param |
| 101 |
|
|
| 102 |
|
@throws |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
protected abstract void appendChildrenToHtml(Node node, Appendable writer) throws IOException; |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
@param |
| 111 |
|
|
| 112 |
|
@param |
| 113 |
|
|
| 114 |
|
@return@link |
| 115 |
|
|
| |
|
| 85,7% |
Uncovered Elements: 7 (49) |
Complexity: 9 |
Complexity Density: 0,26 |
|
| 116 |
47 |
protected Element convertToHtml(@Nonnull final ComponentToken startToken, @Nullable final ComponentToken endToken) {... |
| 117 |
47 |
final Element startElement = startToken.getElement(); |
| 118 |
47 |
final Node previousElement = startElement.previousSibling(); |
| 119 |
47 |
Element endElement = null; |
| 120 |
47 |
if (endToken != null) { |
| 121 |
38 |
endElement = endToken.getElement(); |
| 122 |
|
} |
| 123 |
47 |
final Document doc = JsoupUtils.createHtmlDocument(""); |
| 124 |
47 |
final Element tmp = doc.body(); |
| 125 |
47 |
final Node parent = startElement.parentNode(); |
| 126 |
|
|
| 127 |
47 |
final StringBuilder html = new StringBuilder(ComponentResolver.convertElementToHtml(startElement)); |
| 128 |
47 |
final List<Node> nodesToRemove = Lists.newArrayList(); |
| 129 |
47 |
nodesToRemove.add(startElement); |
| 130 |
47 |
if (endElement != null) { |
| 131 |
38 |
boolean startCopy = false; |
| 132 |
|
|
| 133 |
38 |
for (final Node n : parent.childNodes()) { |
| 134 |
500 |
if (n.equals(endElement)) { |
| 135 |
38 |
nodesToRemove.add(n); |
| 136 |
38 |
break; |
| 137 |
|
} |
| 138 |
462 |
if (startCopy) { |
| 139 |
128 |
try { |
| 140 |
128 |
appendChildrenToHtml(n, html); |
| 141 |
|
} catch (final IOException e) { |
| 142 |
0 |
throw new RuntimeException(e.getMessage(), e); |
| 143 |
|
} |
| 144 |
128 |
nodesToRemove.add(n); |
| 145 |
|
} |
| 146 |
462 |
if (n.equals(startElement)) { |
| 147 |
38 |
startCopy = true; |
| 148 |
|
} |
| 149 |
|
} |
| 150 |
38 |
html.append(ComponentResolver.convertElementToHtml(endElement)); |
| 151 |
|
} |
| 152 |
47 |
tmp.html(html.toString()); |
| 153 |
47 |
final Element component = tmp.children().first(); |
| 154 |
47 |
final Element parentElement = startElement.parent(); |
| 155 |
|
|
| 156 |
47 |
if (previousElement != null) { |
| 157 |
47 |
previousElement.after(component); |
| 158 |
|
} else { |
| 159 |
0 |
if (parentElement.children().first() != null) { |
| 160 |
0 |
parentElement.children().first().before(component); |
| 161 |
|
} else { |
| 162 |
0 |
parentElement.children().add(component); |
| 163 |
|
} |
| 164 |
|
} |
| 165 |
47 |
nodesToRemove.forEach(Node::remove); |
| 166 |
47 |
return component; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
@link |
| 171 |
|
|
| 172 |
|
@param |
| 173 |
|
|
| 174 |
|
@param |
| 175 |
|
|
| 176 |
|
@param |
| 177 |
|
|
| 178 |
|
@return@link |
| 179 |
|
|
| 180 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
| 181 |
49 |
protected SnippetComponent<?> createSnippetComponent(final SnippetContext snippetContext,... |
| 182 |
|
final ComponentToken startToken, |
| 183 |
|
final ComponentToken endToken) { |
| 184 |
49 |
Element componentElement = null; |
| 185 |
49 |
if (Tag.html.equals(startToken.tag())) { |
| 186 |
2 |
componentElement = startToken.getElement(); |
| 187 |
|
} else { |
| 188 |
47 |
componentElement = convertToHtml(startToken, endToken); |
| 189 |
|
} |
| 190 |
49 |
return snippetContext.create(componentElement, startToken, endToken); |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
@author |
| 197 |
|
@version |
| 198 |
|
|
| |
|
| 87,5% |
Uncovered Elements: 3 (24) |
Complexity: 8 |
Complexity Density: 0,67 |
|
| 199 |
|
public static class WebComponentProcessor extends Processor { |
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
@param |
| 205 |
|
|
| 206 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 207 |
33 |
public WebComponentProcessor(final SnippetParser parser) {... |
| 208 |
33 |
super(parser); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
@inheritDoc |
| 213 |
|
|
| |
|
| 85,7% |
Uncovered Elements: 3 (21) |
Complexity: 7 |
Complexity Density: 0,64 |
|
| 214 |
128 |
@Override... |
| 215 |
|
protected void appendChildrenToHtml(final Node node, final Appendable writer) throws IOException { |
| 216 |
128 |
if (node instanceof Element) { |
| 217 |
34 |
final Element el = (Element) node; |
| 218 |
|
|
| 219 |
34 |
if ("div".equals(el.tagName()) && el.hasClass("source")) { |
| 220 |
4 |
writer.append(el.text()); |
| 221 |
|
} else { |
| 222 |
|
|
| 223 |
30 |
if (Iterables.tryFind(el.childNodes(), n -> n instanceof Comment).isPresent()) { |
| 224 |
0 |
writer.append(el.data()); |
| 225 |
|
} else { |
| 226 |
30 |
writer.append(el.outerHtml()); |
| 227 |
|
} |
| 228 |
|
} |
| 229 |
94 |
} else if (node instanceof Comment) { |
| 230 |
11 |
writer.append(((Comment) node).getData()); |
| 231 |
83 |
} else if (node instanceof TextNode) { |
| 232 |
83 |
writer.append(((TextNode) node).text()); |
| 233 |
|
} |
| 234 |
|
} |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
} |