Added freemarker template

This commit is contained in:
Luca Frosini 2023-03-29 11:40:38 +02:00
parent 90733d8115
commit ee36dd6fc2
3 changed files with 85 additions and 0 deletions

View File

@ -82,6 +82,12 @@
<artifactId>javax.ws.rs-api</artifactId> <artifactId>javax.ws.rs-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.32</version>
</dependency>
<!-- Added to support Java 11 JDK --> <!-- Added to support Java 11 JDK -->
<dependency> <dependency>
<groupId>javax.xml.ws</groupId> <groupId>javax.xml.ws</groupId>

View File

@ -0,0 +1,62 @@
package org.gcube.grsf.freemarker;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.net.URL;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
public class FreeMarkerTest {
public File getResourcesDirectory() throws Exception {
URL logbackFileURL = this.getClass().getClassLoader().getResource("logback-test.xml");
File logbackFile = new File(logbackFileURL.toURI());
File resourcesDirectory = logbackFile.getParentFile();
return resourcesDirectory;
}
public File getTraceabilityUnitExampleDirectory() throws Exception {
File resourcesDirectory = getResourcesDirectory();
return new File(resourcesDirectory, "examples/AssessmentUnit");
}
public File getFreemarkerDirectory() throws Exception {
File resourcesDirectory = getResourcesDirectory();
return new File(resourcesDirectory, "freemarker");
}
public Configuration getConfiguration() throws Exception {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
cfg.setDirectoryForTemplateLoading(getFreemarkerDirectory());
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
return cfg;
}
@Test
public void testIt() throws Exception {
Configuration cfg = getConfiguration();
Template template = cfg.getTemplate("example.ftl");
File json = new File(getTraceabilityUnitExampleDirectory(), "00a1f849-1b43-3fd1-9255-1d1134c969bb.json");
ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readValue(json, Map.class);
File out = new File(getFreemarkerDirectory(), "output.json");
out.delete();
Writer writer = new FileWriter(out);
template.process(map, writer);
writer.flush();
writer.close();
}
}

View File

@ -0,0 +1,17 @@
{
"title" : "${stock_name}",
"license_id": "${license_id}",
"description":
"Short Name: ${short_name}\nGRSF Semantic Identifier: ${grsf_semantic_identifier}\nRecord URL: https://data.d4science.org/ctlg/GRSF_Admin/${grsf_uuid}",
"tags" : [
{
"name": "${similarities_indicator}"
}
],
"extras": [
{
"key": "Record URL",
"value": "https://data.d4science.org/ctlg/GRSF_Admin/${grsf_uuid}"
}
]
}