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