| 1 |
|
package org.europa.together.application; |
| 2 |
|
|
| 3 |
|
import java.io.StringWriter; |
| 4 |
|
import java.util.Map; |
| 5 |
|
import org.apache.velocity.Template; |
| 6 |
|
import org.apache.velocity.VelocityContext; |
| 7 |
|
import org.apache.velocity.app.Velocity; |
| 8 |
|
import org.apache.velocity.app.VelocityEngine; |
| 9 |
|
import org.apache.velocity.runtime.RuntimeConstants; |
| 10 |
|
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; |
| 11 |
|
import org.apache.velocity.runtime.resource.loader.StringResourceLoader; |
| 12 |
|
import org.apache.velocity.runtime.resource.util.StringResourceRepository; |
| 13 |
|
import org.europa.together.business.Logger; |
| 14 |
|
import org.europa.together.domain.LogLevel; |
| 15 |
|
import org.springframework.stereotype.Repository; |
| 16 |
|
import org.europa.together.business.TemplateRenderer; |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
@Repository |
| |
|
| 100% |
Uncovered Elements: 0 (32) |
Complexity: 6 |
Complexity Density: 0.24 |
|
| 22 |
|
public class VelocityRenderer implements TemplateRenderer { |
| 23 |
|
|
| 24 |
|
private static final long serialVersionUID = 3L; |
| 25 |
|
private static final Logger LOGGER = new LogbackLogger(VelocityRenderer.class); |
| 26 |
|
|
| 27 |
|
private VelocityEngine engine = new VelocityEngine(); |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 32 |
8 |
public VelocityRenderer() {... |
| 33 |
8 |
LOGGER.log("instance class", LogLevel.INFO); |
| 34 |
|
} |
| 35 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 36 |
2 |
@Override... |
| 37 |
|
public String loadContentByClasspathResource(final String resourcePath, |
| 38 |
|
final String template, |
| 39 |
|
final Map<String, String> properties) { |
| 40 |
2 |
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); |
| 41 |
2 |
engine.setProperty("classpath.resource.loader.class", |
| 42 |
|
ClasspathResourceLoader.class.getName()); |
| 43 |
2 |
engine.init(); |
| 44 |
2 |
return processContent(resourcePath + template, properties); |
| 45 |
|
} |
| 46 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 47 |
3 |
@Override... |
| 48 |
|
public String loadContentByFileResource(final String resourcePath, |
| 49 |
|
final String template, |
| 50 |
|
final Map<String, String> properties) { |
| 51 |
3 |
engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, resourcePath); |
| 52 |
3 |
engine.init(); |
| 53 |
3 |
return processContent(template, properties); |
| 54 |
|
} |
| 55 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 56 |
3 |
@Override... |
| 57 |
|
public String loadContentByStringResource(final String resource, |
| 58 |
|
final Map<String, String> properties) { |
| 59 |
3 |
engine.setProperty(Velocity.RESOURCE_LOADER, "string"); |
| 60 |
3 |
engine.addProperty("string.resource.loader.class", |
| 61 |
|
StringResourceLoader.class.getName()); |
| 62 |
3 |
engine.addProperty("string.resource.loader.repository.static", "false"); |
| 63 |
3 |
engine.init(); |
| 64 |
3 |
StringResourceRepository templateRepository |
| 65 |
|
= (StringResourceRepository) engine |
| 66 |
|
.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT); |
| 67 |
3 |
templateRepository.putStringResource("stringResource", resource); |
| 68 |
3 |
return processContent("stringResource", properties); |
| 69 |
|
} |
| 70 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 71 |
8 |
private String processContent(final String resource,... |
| 72 |
|
final Map<String, String> properties) { |
| 73 |
8 |
StringWriter writer = new StringWriter(); |
| 74 |
8 |
Template template = engine.getTemplate(resource); |
| 75 |
6 |
writer.flush(); |
| 76 |
6 |
VelocityContext context = new VelocityContext(); |
| 77 |
6 |
if (properties != null) { |
| 78 |
5 |
for (Map.Entry<String, String> entry : properties.entrySet()) { |
| 79 |
7 |
context.put(entry.getKey(), entry.getValue()); |
| 80 |
|
} |
| 81 |
|
} else { |
| 82 |
1 |
context.put("__init__", ""); |
| 83 |
|
} |
| 84 |
6 |
template.merge(context, writer); |
| 85 |
6 |
return writer.toString(); |
| 86 |
|
} |
| 87 |
|
} |