Implementing solution based on FreeMarker

This commit is contained in:
Luca Frosini 2023-04-11 13:51:56 +02:00
parent 1c67f0fb2e
commit cf89cb3fc1
3 changed files with 115 additions and 62 deletions

View File

@ -0,0 +1,75 @@
package org.gcube.grsf.publisher.freemarker;
import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class FreeMarker {
protected static Configuration configuration;
protected static File freemarkerDirectory;
public static File getResourcesDirectory() throws Exception {
URL logbackFileURL = FreeMarker.class.getClassLoader().getResource("config.properties");
File logbackFile = new File(logbackFileURL.toURI());
File resourcesDirectory = logbackFile.getParentFile();
return resourcesDirectory;
}
public static File getFreemarkerDirectory() throws Exception {
if(freemarkerDirectory==null) {
File resourcesDirectory = getResourcesDirectory();
freemarkerDirectory = new File(resourcesDirectory, "freemarker");
}
return freemarkerDirectory;
}
public static Configuration getConfiguration() throws Exception {
if(configuration ==null) {
configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDirectoryForTemplateLoading(getFreemarkerDirectory());
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
}
return configuration;
}
public Template getTemplate(String templateName) throws Exception {
Configuration configuration = getConfiguration();
Template template = configuration.getTemplate(templateName);
return template;
}
public Map<String, Object> getMap(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> map = mapper.readValue(json, Map.class);
return map;
}
public String generateJson(String templateName, String dataAsJsonString) throws Exception {
Template template = getTemplate(templateName);
Map<String, Object> map = getMap(dataAsJsonString);
StringWriter writer = new StringWriter();
generateJson(template, map, writer);
return writer.toString();
}
public void generateJson(Template template, Map<String, Object> map, Writer writer) throws Exception {
template.process(map, writer);
writer.flush();
writer.close();
}
}

View File

@ -1,62 +0,0 @@
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,40 @@
package org.gcube.grsf.publisher.freemarker;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import freemarker.template.Template;
public class FreeMarkerTest {
public File getTraceabilityUnitExampleDirectory() throws Exception {
File resourcesDirectory = FreeMarker.getResourcesDirectory();
return new File(resourcesDirectory, "examples/AssessmentUnit");
}
@Test
public void testIt() throws Exception {
FreeMarker freeMarker = new FreeMarker();
Template template = freeMarker.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(FreeMarker.getResourcesDirectory(), "output.json");
out.delete();
Writer writer = new FileWriter(out);
template.process(map, writer);
writer.flush();
writer.close();
}
}