1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.qa.glnm.maven.freemarker.util;
21
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import freemarker.template.Configuration;
32 import freemarker.template.Template;
33 import freemarker.template.TemplateException;
34 import freemarker.template.TemplateExceptionHandler;
35 import io.wcm.qa.glnm.configuration.GaleniumConfiguration;
36 import io.wcm.qa.glnm.exceptions.GaleniumException;
37 import io.wcm.qa.glnm.interaction.Element;
38 import io.wcm.qa.glnm.interaction.FormElement;
39 import io.wcm.qa.glnm.maven.freemarker.methods.ClassNameFromSelectorMethod;
40 import io.wcm.qa.glnm.maven.freemarker.methods.ClassNameFromSpecMethod;
41 import io.wcm.qa.glnm.maven.freemarker.methods.ClassNameFromStringMethod;
42 import io.wcm.qa.glnm.maven.freemarker.methods.ConstantNameMethod;
43 import io.wcm.qa.glnm.maven.freemarker.methods.EscapeJavaDocMethod;
44 import io.wcm.qa.glnm.maven.freemarker.methods.EscapeJavaMethod;
45 import io.wcm.qa.glnm.maven.freemarker.methods.FullSelectorClassNameFromSelectorMethod;
46 import io.wcm.qa.glnm.maven.freemarker.methods.FullWebElementClassNameFromSelectorMethod;
47 import io.wcm.qa.glnm.maven.freemarker.methods.PackageNameMethod;
48 import io.wcm.qa.glnm.maven.freemarker.pojo.InteractionPojo;
49 import io.wcm.qa.glnm.maven.freemarker.pojo.SpecPojo;
50 import io.wcm.qa.glnm.selectors.base.NestedSelector;
51
52
53
54
55
56
57 public final class FreemarkerUtil {
58
59 private static final Configuration CONFIGURATION = generateConfiguration();
60
61 private static final Logger LOG = LoggerFactory.getLogger(FreemarkerUtil.class);
62
63 private FreemarkerUtil() {
64
65 }
66
67
68
69
70
71
72
73
74
75 public static Map<String, Object> getDataModelForInteractiveSelector(String packageRoot, String interfaceName, String className) {
76 Map<String, Object> model = getCommonDataModel();
77 model.put("elementInteraction", new InteractionPojo(Element.class));
78 model.put("formElementInteraction", new InteractionPojo(FormElement.class));
79 model.put("interactiveSelectorPackage", packageRoot);
80 model.put("interactiveSelectorBaseClassName", className);
81 model.put("interactiveSelectorInterfaceClassName", interfaceName);
82
83 return model;
84 }
85
86
87
88
89
90
91
92
93
94
95
96 public static Map<String, Object> getDataModelForSelector(
97 NestedSelector selector,
98 SpecPojo spec,
99 String packageName,
100 String interactiveClassName,
101 String interactiveInterfaceName) {
102 Map<String, Object> model = getDataModelForInteractiveSelector(packageName, interactiveInterfaceName, interactiveClassName);
103 model.put("className", new ClassNameFromSelectorMethod());
104 model.put("spec", spec);
105 model.put("this", selector);
106
107 return model;
108 }
109
110
111
112
113
114
115
116
117 public static Map<String, Object> getDataModelForSpec(SpecPojo spec, String packagePrefixSpecs) {
118 Map<String, Object> model = getCommonDataModel();
119 model.put("className", new ClassNameFromSpecMethod());
120 model.put("classNameFromString", new ClassNameFromStringMethod());
121 model.put("specRootPackageName", packagePrefixSpecs);
122 model.put("spec", spec);
123 return model;
124 }
125
126
127
128
129
130
131
132
133
134
135
136 public static Map<String, Object> getDataModelForWebElement(
137 NestedSelector selector,
138 SpecPojo spec,
139 String packageName,
140 String interactiveClassName,
141 String interactiveInterfaceName) {
142 Map<String, Object> model = getDataModelForSelector(selector, spec, packageName, interactiveClassName, interactiveInterfaceName);
143 model.put("gweClassName", new FullWebElementClassNameFromSelectorMethod());
144 model.put("selectorClassName", new FullSelectorClassNameFromSelectorMethod());
145 return model;
146 }
147
148
149
150
151
152
153
154
155
156 public static File getOutputFile(File outputDir, String outputPackage, String outputClassName) {
157 File outputDirectoryPackage = new File(outputDir, outputPackage.replaceAll("\\.", "/"));
158 outputDirectoryPackage.mkdirs();
159 File outputFile = new File(outputDirectoryPackage, outputClassName + ".java");
160 return outputFile;
161 }
162
163
164
165
166
167
168
169
170 public static Template getTemplate(File directory, String name) {
171 try {
172 CONFIGURATION.setDirectoryForTemplateLoading(directory);
173 Template template = CONFIGURATION.getTemplate(name);
174 return template;
175 }
176 catch (IOException ex) {
177 throw new GaleniumException("exception when getting template.", ex);
178 }
179 }
180
181
182
183
184
185
186
187
188 public static void process(Template template, Map<String, Object> dataModel, File outputFile) {
189 FileWriter out = null;
190 try {
191 out = new FileWriter(outputFile);
192 LOG.debug("generating '" + outputFile.getPath() + "'");
193 template.process(dataModel, out);
194 LOG.info("generated '" + outputFile.getPath() + "'");
195 }
196 catch (IOException | TemplateException ex) {
197 throw new GaleniumException("template exception", ex);
198 }
199 finally {
200 if (out != null) {
201 try {
202 out.close();
203 }
204 catch (IOException ex) {
205 throw new GaleniumException("could not close writer when processing Freemarker template.", ex);
206 }
207 }
208 }
209 }
210
211 private static Configuration generateConfiguration() {
212 Configuration cfg = new Configuration();
213 cfg.setDefaultEncoding("UTF-8");
214 cfg.setTemplateExceptionHandler(getExceptionHandler());
215 return cfg;
216 }
217
218 private static Map<String, Object> getCommonDataModel() {
219 Map<String, Object> model = new HashMap<>();
220 model.put("escapeXml", new EscapeJavaDocMethod());
221 model.put("escapeJava", new EscapeJavaMethod());
222 model.put("constantName", new ConstantNameMethod());
223 model.put("packageName", new PackageNameMethod());
224 return model;
225 }
226
227 private static TemplateExceptionHandler getExceptionHandler() {
228 if (GaleniumConfiguration.isSparseReporting()) {
229 return TemplateExceptionHandler.RETHROW_HANDLER;
230 }
231 else {
232 return TemplateExceptionHandler.DEBUG_HANDLER;
233 }
234 }
235
236 }