| 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 java.io.StringWriter; |
| 21 |
|
import java.io.Writer; |
| 22 |
|
import java.util.UUID; |
| 23 |
|
import java.util.function.Consumer; |
| 24 |
|
import javax.annotation.Nonnull; |
| 25 |
|
import javax.annotation.Nullable; |
| 26 |
|
import org.apache.commons.io.IOUtils; |
| 27 |
|
import org.apache.velocity.VelocityContext; |
| 28 |
|
import org.apache.velocity.app.Velocity; |
| 29 |
|
import org.apache.velocity.context.Context; |
| 30 |
|
import org.apache.velocity.runtime.RuntimeConstants; |
| 31 |
|
import org.apache.velocity.runtime.RuntimeSingleton; |
| 32 |
|
import org.apache.velocity.tools.Scope; |
| 33 |
|
import org.apache.velocity.tools.ToolManager; |
| 34 |
|
import org.apache.velocity.tools.config.EasyFactoryConfiguration; |
| 35 |
|
import org.apache.velocity.tools.generic.ClassTool; |
| 36 |
|
import org.apache.velocity.tools.generic.ComparisonDateTool; |
| 37 |
|
import org.apache.velocity.tools.generic.ContextTool; |
| 38 |
|
import org.apache.velocity.tools.generic.DisplayTool; |
| 39 |
|
import org.apache.velocity.tools.generic.EscapeTool; |
| 40 |
|
import org.apache.velocity.tools.generic.FieldTool; |
| 41 |
|
import org.apache.velocity.tools.generic.LinkTool; |
| 42 |
|
import org.apache.velocity.tools.generic.LoopTool; |
| 43 |
|
import org.apache.velocity.tools.generic.MathTool; |
| 44 |
|
import org.apache.velocity.tools.generic.NumberTool; |
| 45 |
|
import org.apache.velocity.tools.generic.RenderTool; |
| 46 |
|
import org.apache.velocity.tools.generic.ResourceTool; |
| 47 |
|
import org.apache.velocity.tools.generic.XmlTool; |
| 48 |
|
import org.devacfr.maven.skins.reflow.HtmlTool; |
| 49 |
|
import org.devacfr.maven.skins.reflow.ISkinConfig; |
| 50 |
|
import org.devacfr.maven.skins.reflow.JsoupUtils; |
| 51 |
|
import org.devacfr.maven.skins.reflow.URITool; |
| 52 |
|
import org.devacfr.maven.skins.reflow.snippet.SnippetComponent.Type; |
| 53 |
|
import org.jsoup.nodes.Element; |
| 54 |
|
import org.jsoup.nodes.Node; |
| 55 |
|
import org.jsoup.nodes.TextNode; |
| 56 |
|
import org.slf4j.Logger; |
| 57 |
|
import org.slf4j.LoggerFactory; |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
@author |
| 61 |
|
@version |
| 62 |
|
|
| |
|
| 95,1% |
Uncovered Elements: 6 (122) |
Complexity: 33 |
Complexity Density: 0,42 |
|
| 63 |
|
public class SnippetContext { |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(SnippetContext.class); |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
private String htmlSource; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private ISkinConfig config; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
private final SnippetParser parser; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@param |
| 81 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 82 |
68 |
public SnippetContext(@Nonnull final SnippetParser parser) {... |
| 83 |
68 |
this.parser = requireNonNull(parser); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 89 |
34 |
public void reset() {... |
| 90 |
34 |
this.htmlSource = null; |
| 91 |
34 |
this.config = null; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
@return |
| 98 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 99 |
0 |
public String generateSnippetIdentifier() {... |
| 100 |
0 |
return "snippet-placement-" + UUID.randomUUID().toString(); |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
@return |
| 107 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 108 |
104 |
public SnippetParser getParser() {... |
| 109 |
104 |
return this.parser; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
@return |
| 116 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 117 |
108 |
public ISkinConfig getConfig() {... |
| 118 |
108 |
return config; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
@return |
| 125 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 126 |
2 |
public SnippetParser createChildParser() {... |
| 127 |
2 |
final SnippetParser parser = new SnippetParser(); |
| 128 |
2 |
return parser; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
@param |
| 135 |
|
|
| 136 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 137 |
36 |
public void setConfig(final ISkinConfig config) {... |
| 138 |
36 |
this.config = config; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
@param |
| 145 |
|
|
| 146 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 147 |
68 |
void setHtmlSource(final String htmlSource) {... |
| 148 |
68 |
this.htmlSource = htmlSource; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
@return |
| 155 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 156 |
34 |
public String html() {... |
| 157 |
34 |
return htmlSource; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
@return |
| 164 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 165 |
2 |
public Element document() {... |
| 166 |
2 |
return JsoupUtils.createHtmlDocument(html()); |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
@param |
| 173 |
|
|
| 174 |
|
@param |
| 175 |
|
|
| 176 |
|
@param |
| 177 |
|
|
| 178 |
|
@return@link |
| 179 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0,22 |
|
| 180 |
102 |
@Nonnull... |
| 181 |
|
public Component<?> create(@Nonnull final Element element, |
| 182 |
|
@Nonnull final ComponentToken startToken, |
| 183 |
|
@Nullable final ComponentToken endToken) { |
| 184 |
102 |
requireNonNull(element); |
| 185 |
102 |
requireNonNull(startToken); |
| 186 |
102 |
Component<?> component = null; |
| 187 |
102 |
if (parser.isSnippet(startToken.name())) { |
| 188 |
|
|
| 189 |
100 |
component = SnippetComponent.createSnippet(element, null, startToken.type()); |
| 190 |
100 |
recurciveCreateComponent(element, component); |
| 191 |
|
} else { |
| 192 |
|
|
| 193 |
2 |
component = Component.createComponent(element, null); |
| 194 |
2 |
recurciveCreateComponent(element, component); |
| 195 |
|
} |
| 196 |
102 |
return component; |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
@param |
| 203 |
|
|
| 204 |
|
@param |
| 205 |
|
|
| 206 |
|
@return@link |
| 207 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
| 208 |
418 |
@Nonnull... |
| 209 |
|
public Component<?> create(@Nonnull final Element element, final Component<?> parent) { |
| 210 |
418 |
requireNonNull(element); |
| 211 |
418 |
Component<?> component = null; |
| 212 |
418 |
if (parser.isSnippet(element.tagName())) { |
| 213 |
6 |
component = SnippetComponent.createSnippet(element, parent, Type.webComponent); |
| 214 |
|
} else { |
| 215 |
412 |
component = Component.createComponent(element, parent); |
| 216 |
|
} |
| 217 |
418 |
recurciveCreateComponent(element, component); |
| 218 |
418 |
return component; |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
@param |
| 225 |
|
|
| 226 |
|
@param |
| 227 |
|
|
| 228 |
|
|
| |
|
| 94,4% |
Uncovered Elements: 1 (18) |
Complexity: 7 |
Complexity Density: 0,7 |
|
| 229 |
520 |
private void recurciveCreateComponent(@Nonnull final Node element, final Component<?> parent) {... |
| 230 |
520 |
element.childNodes().forEach(child -> { |
| 231 |
1228 |
Component<?> component = null; |
| 232 |
|
|
| 233 |
1228 |
if (child instanceof TextNode && ((TextNode) child).text().trim().length() > 0) { |
| 234 |
252 |
component = Component.createComponent(child, parent); |
| 235 |
976 |
} else if ("p".equals(child.nodeName()) && child.outerHtml().length() == 7) { |
| 236 |
|
|
| 237 |
976 |
} else if (child instanceof Element) { |
| 238 |
416 |
final Element el = (Element) child; |
| 239 |
416 |
component = create(el, parent); |
| 240 |
|
} |
| 241 |
1228 |
if (component != null) { |
| 242 |
668 |
parent.addChild(component); |
| 243 |
|
} |
| 244 |
|
}); |
| 245 |
|
} |
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
@param |
| 251 |
|
|
| 252 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
| 253 |
102 |
protected void render(final Component<?> component) {... |
| 254 |
102 |
traverseTee(component, c -> { |
| 255 |
668 |
if (c instanceof SnippetComponent) { |
| 256 |
4 |
((SnippetComponent<?>) c).render(this); |
| 257 |
|
} |
| 258 |
|
}); |
| 259 |
102 |
if (component instanceof SnippetComponent) { |
| 260 |
100 |
((SnippetComponent<?>) component).render(this); |
| 261 |
|
} |
| 262 |
|
} |
| 263 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 264 |
770 |
private void traverseTee(final Component<?> component, final Consumer<Component<?>> consumer) {... |
| 265 |
770 |
final Consumer<Component<?>> traverse = c -> traverseTee(c, consumer); |
| 266 |
770 |
component.getChildren().forEach(consumer.andThen(traverse)); |
| 267 |
|
} |
| 268 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
|
| 269 |
106 |
protected String renderComponent(final SnippetComponent<?> component, final Context context) {... |
| 270 |
106 |
final StringWriter writer = new StringWriter(); |
| 271 |
106 |
try { |
| 272 |
106 |
mergeTemplate(component, context, writer); |
| 273 |
106 |
return writer.toString(); |
| 274 |
|
} finally { |
| 275 |
106 |
IOUtils.closeQuietly(writer); |
| 276 |
|
} |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
|
@param |
| 283 |
|
|
| 284 |
|
@param |
| 285 |
|
|
| 286 |
|
@param |
| 287 |
|
|
| 288 |
|
|
| |
|
| 89,3% |
Uncovered Elements: 3 (28) |
Complexity: 5 |
Complexity Density: 0,25 |
|
| 289 |
106 |
protected void mergeTemplate(final SnippetComponent<?> component, final Context contextParent, final Writer writer) {... |
| 290 |
106 |
boolean found = false; |
| 291 |
106 |
for (final String path : this.parser.getSnippetPaths()) { |
| 292 |
292 |
final String filePath = path + '/' + component.getName() + ".vm"; |
| 293 |
292 |
if (Velocity.resourceExists(filePath)) { |
| 294 |
106 |
found = true; |
| 295 |
106 |
final Context context = createVelocityContext(contextParent); |
| 296 |
106 |
context.put("snippet", component); |
| 297 |
106 |
context.put("snippetPath", filePath); |
| 298 |
106 |
context.put("config", this.config); |
| 299 |
106 |
if (this.config.getContext() != null) { |
| 300 |
38 |
context.put("pageContext", this.config.getContext()); |
| 301 |
38 |
context.put("pageType", this.config.getContext().getType()); |
| 302 |
|
} |
| 303 |
106 |
context.put("velocity", Velocity.class); |
| 304 |
106 |
context.put("site", this.config.getSiteModel()); |
| 305 |
|
|
| 306 |
106 |
Velocity.mergeTemplate("META-INF/skin/snippets/_snippet.vm", |
| 307 |
|
RuntimeSingleton.getString(RuntimeConstants.INPUT_ENCODING, RuntimeConstants.ENCODING_DEFAULT), |
| 308 |
|
context, |
| 309 |
|
writer); |
| 310 |
106 |
break; |
| 311 |
|
} else { |
| 312 |
186 |
if (LOGGER.isDebugEnabled()) { |
| 313 |
186 |
LOGGER.debug("Template for component '{}' not found in path:{} ", component, filePath); |
| 314 |
|
} |
| 315 |
|
} |
| 316 |
|
} |
| 317 |
106 |
if (!found) { |
| 318 |
0 |
LOGGER.warn("The snippet '{}' template doesn't exist", component.getName()); |
| 319 |
|
} |
| 320 |
|
} |
| 321 |
|
|
| 322 |
|
|
| 323 |
|
|
| 324 |
|
|
| 325 |
|
@param |
| 326 |
|
|
| 327 |
|
@return |
| 328 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 329 |
106 |
protected Context createVelocityContext(final Context contextParent) {... |
| 330 |
106 |
final VelocityContext context = new VelocityContext(contextParent); |
| 331 |
106 |
return context; |
| 332 |
|
} |
| 333 |
|
|
| 334 |
|
|
| 335 |
|
|
| 336 |
|
|
| 337 |
|
@return |
| 338 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
|
| 339 |
34 |
protected static ToolManager createToolManaged() {... |
| 340 |
|
|
| 341 |
34 |
final EasyFactoryConfiguration config = new EasyFactoryConfiguration(false); |
| 342 |
34 |
config.property("safeMode", Boolean.FALSE); |
| 343 |
34 |
config.toolbox(Scope.REQUEST) |
| 344 |
|
.tool(ContextTool.class) |
| 345 |
|
.tool(LinkTool.class) |
| 346 |
|
.tool(LoopTool.class) |
| 347 |
|
.tool(RenderTool.class); |
| 348 |
34 |
config.toolbox(Scope.APPLICATION) |
| 349 |
|
.tool(ClassTool.class) |
| 350 |
|
.tool(ComparisonDateTool.class) |
| 351 |
|
.tool(DisplayTool.class) |
| 352 |
|
.tool(EscapeTool.class) |
| 353 |
|
.tool(FieldTool.class) |
| 354 |
|
.tool(MathTool.class) |
| 355 |
|
.tool(NumberTool.class) |
| 356 |
|
.tool(ResourceTool.class) |
| 357 |
|
.tool(XmlTool.class) |
| 358 |
|
.tool(URITool.class) |
| 359 |
|
.tool(HtmlTool.class); |
| 360 |
|
|
| 361 |
34 |
final ToolManager manager = new ToolManager(false, false); |
| 362 |
34 |
manager.configure(config); |
| 363 |
34 |
return manager; |
| 364 |
|
} |
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
|
| |
|
| 33,3% |
Uncovered Elements: 6 (9) |
Complexity: 4 |
Complexity Density: 0,8 |
|
| 369 |
|
public static class SnippetResource { |
| 370 |
|
|
| 371 |
|
|
| 372 |
|
private final String name; |
| 373 |
|
|
| 374 |
|
|
| 375 |
|
private final String path; |
| 376 |
|
|
| 377 |
|
|
| 378 |
|
|
| 379 |
|
|
| 380 |
|
@param |
| 381 |
|
|
| 382 |
|
@param |
| 383 |
|
|
| 384 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 385 |
4776 |
public SnippetResource(final String name, final String path) {... |
| 386 |
4776 |
this.name = name; |
| 387 |
4776 |
this.path = path; |
| 388 |
|
} |
| 389 |
|
|
| 390 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 391 |
0 |
public String getName() {... |
| 392 |
0 |
return name; |
| 393 |
|
} |
| 394 |
|
|
| 395 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 396 |
0 |
public String getPath() {... |
| 397 |
0 |
return path; |
| 398 |
|
} |
| 399 |
|
|
| 400 |
|
|
| 401 |
|
@inheritDoc |
| 402 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 403 |
0 |
@Override... |
| 404 |
|
public String toString() { |
| 405 |
0 |
return "SnippetResource [name=" + name + ", path=" + path + "]"; |
| 406 |
|
} |
| 407 |
|
} |
| 408 |
|
|
| 409 |
|
} |