-
Notifications
You must be signed in to change notification settings - Fork 128
/
FreeMarkerUtil.java
150 lines (136 loc) · 5.41 KB
/
FreeMarkerUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.jboss.windup.reporting.freemarker;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.services.Imported;
import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.Variables;
import org.jboss.windup.config.operation.Iteration;
import org.jboss.windup.graph.model.WindupVertexFrame;
import org.jboss.windup.util.Util;
import org.jboss.windup.util.exception.WindupException;
import freemarker.template.SimpleScalar;
import freemarker.template.SimpleSequence;
/**
* This class contains several useful utility functions that can be used for rendering a freemarker template within Windup.
*
*/
public class FreeMarkerUtil
{
/**
* Gets the default configuration for Freemarker within Windup.
*/
public static Configuration getDefaultFreemarkerConfiguration()
{
freemarker.template.Configuration configuration = new freemarker.template.Configuration(Configuration.VERSION_2_3_26);
DefaultObjectWrapperBuilder objectWrapperBuilder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_26);
objectWrapperBuilder.setUseAdaptersForContainers(true);
objectWrapperBuilder.setIterableSupport(true);
configuration.setObjectWrapper(objectWrapperBuilder.build());
configuration.setAPIBuiltinEnabled(true);
configuration.setTemplateLoader(new FurnaceFreeMarkerTemplateLoader());
configuration.setTemplateUpdateDelayMilliseconds(3600);
return configuration;
}
/**
* Converts a FreeMarker {@link SimpleSequence} to a {@link Set}.
*
*/
public static Set<String> simpleSequenceToSet(SimpleSequence simpleSequence)
{
if (simpleSequence == null)
return Collections.emptySet();
Set<String> result = new HashSet<>();
for (int i = 0; i < simpleSequence.size(); i++)
{
try
{
Object sequenceEntry = simpleSequence.get(i);
if (sequenceEntry instanceof SimpleScalar)
{
result.add(((SimpleScalar) sequenceEntry).getAsString());
}
else
{
result.add(simpleSequence.get(i).toString());
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
return result;
}
/**
* Gets freemarker extensions (eg, custom functions) provided by furnace addons
*/
public static Map<String, Object> findFreeMarkerExtensions(Furnace furnace, GraphRewrite event)
{
Imported<WindupFreeMarkerMethod> freeMarkerMethods = furnace.getAddonRegistry().getServices(
WindupFreeMarkerMethod.class);
Map<String, Object> results = new HashMap<>();
for (WindupFreeMarkerMethod freeMarkerMethod : freeMarkerMethods)
{
freeMarkerMethod.setContext(event);
if (results.containsKey(freeMarkerMethod.getMethodName()))
{
throw new WindupException(Util.WINDUP_BRAND_NAME_ACRONYM+" contains two freemarker extension providing the same name: "
+ freeMarkerMethod.getMethodName());
}
results.put(freeMarkerMethod.getMethodName(), freeMarkerMethod);
}
Imported<WindupFreeMarkerTemplateDirective> freeMarkerDirectives = furnace.getAddonRegistry().getServices(
WindupFreeMarkerTemplateDirective.class);
for (WindupFreeMarkerTemplateDirective freeMarkerDirective : freeMarkerDirectives)
{
freeMarkerDirective.setContext(event);
if (results.containsKey(freeMarkerDirective.getDirectiveName()))
{
throw new WindupException(Util.WINDUP_BRAND_NAME_ACRONYM+" contains two freemarker extension providing the same name: "
+ freeMarkerDirective.getDirectiveName());
}
results.put(freeMarkerDirective.getDirectiveName(), freeMarkerDirective);
}
return results;
}
/**
* Finds all variables in the context with the given names, and also attaches all WindupFreeMarkerMethods from all addons into the map.
*
* This allows external addons to extend the capabilities in the freemarker reporting system.
*/
public static Map<String, Object> findFreeMarkerContextVariables(Variables variables, String... varNames)
{
Map<String, Object> results = new HashMap<>();
for (String varName : varNames)
{
WindupVertexFrame payload = null;
try
{
payload = Iteration.getCurrentPayload(variables, null, varName);
}
catch (IllegalStateException | IllegalArgumentException e)
{
// oh well
}
if (payload != null)
{
results.put(varName, payload);
}
else
{
Iterable<? extends WindupVertexFrame> var = variables.findVariable(varName);
if (var != null)
{
results.put(varName, var);
}
}
}
return results;
}
}