Skip to content

Package: FreeMarkerTemplate

FreeMarkerTemplate

nameinstructionbranchcomplexitylinemethod
FreeMarkerTemplate(String)
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
FreeMarkerTemplate(String, int, String, String)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
put(String, Object)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
run(File)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
writeFile()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
writeFileTo(String, String)
M: 62 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.ws.test.util;
12:
13:
14: import com.sun.xml.ws.test.SourcesCollector;
15: import freemarker.template.Configuration;
16: import freemarker.template.Template;
17: import freemarker.template.TemplateExceptionHandler;
18:
19: import java.io.File;
20: import java.io.FileWriter;
21: import java.util.HashMap;
22: import java.util.Map;
23: import java.util.Set;
24:
25: public class FreeMarkerTemplate {
26:
27: Map root = new HashMap();
28: String templateName;
29:
30: static Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
31: private static Map<String, Template> templates = new HashMap<>();
32:
33: static {
34: cfg.setClassForTemplateLoading(FreeMarkerTemplate.class, "/com/sun/xml/ws/test/freemarker");
35: cfg.setLocalizedLookup(false);
36: cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
37: }
38:
39: public FreeMarkerTemplate(String template) {
40: templateName = template;
41:• if (!templates.containsKey(templateName)) {
42: try {
43: templates.put(templateName, cfg.getTemplate(templateName));
44: } catch (Throwable t) {
45: throw new RuntimeException(t);
46: }
47: }
48: }
49:
50: public FreeMarkerTemplate(String id, int scriptOrder, String workdir, String templateName) {
51: this(templateName);
52:• root.put("serviceId", id != null ? id : "NULL");
53: root.put("stage", scriptOrder);
54: root.put("workdir", workdir);
55: }
56:
57: public void put(String key, Object value) {
58: root.put(key, value);
59: }
60:
61: public void run(File output) throws Exception {
62: try (FileWriter out = new FileWriter(output)) {
63: Template template = templates.get(templateName);
64: template.process(root, out);
65: out.flush();
66: } catch (Throwable t) {
67: throw t;
68: }
69: }
70:
71: public String writeFile() {
72: String workdir = (String) root.get("workdir");
73: String stage = "" + root.get("stage");
74: return writeFileTo(workdir, stage + "-" + templateName);
75: }
76:
77: public String writeFileTo(String dir, String filename) {
78: String fullFileName = dir + "/" + filename;
79: SourcesCollector.ensureDirectoryExists(new File(fullFileName).getParent());
80: System.out.println("\ngenerating file [" + fullFileName + "];\nparametersMap: [");
81: Set<Map.Entry> entryset = root.entrySet();
82:• for(Map.Entry entry : entryset) {
83: System.out.print(" ");
84: System.out.print(entry.getKey());
85: System.out.print(" : ");
86: System.out.println(entry.getValue());
87: }
88: System.out.println("]\n");
89:
90: File f = new File(fullFileName);
91: try {
92: run(f);
93: } catch (Exception t) {
94: t.printStackTrace(System.out);
95: }
96: return f.getAbsolutePath();
97: }
98:
99: }