TemplateUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.resources;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.OutputStreamWriter;
  25. import java.util.Map;

  26. import org.apache.commons.io.input.CharSequenceReader;

  27. import freemarker.cache.TemplateLoader;
  28. import freemarker.template.Configuration;
  29. import freemarker.template.ObjectWrapper;
  30. import freemarker.template.Template;
  31. import freemarker.template.TemplateException;

  32. /**
  33.  *  TemplateUtils
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class TemplateUtils {

  40.    
  41.     /** -------- Utility per la creazione delle configurazioni ------------- */
  42.    
  43.     public static Configuration newTemplateEngine() {
  44.         return TemplateUtils.newTemplateEngine(TemplateUtils.class,"", null,null);
  45.     }
  46.     public static Configuration newTemplateEngine(String prefix) {
  47.         return TemplateUtils.newTemplateEngine(TemplateUtils.class,prefix, null,null);
  48.     }
  49.     public static Configuration newTemplateEngine(Class<?> c) {
  50.         return TemplateUtils.newTemplateEngine(c,"", null,null);
  51.     }
  52.     public static Configuration newTemplateEngine(Class<?> c,String prefix) {
  53.         return TemplateUtils.newTemplateEngine(c, prefix, null,null);
  54.     }
  55.     public static Configuration newTemplateEngine(Class<?> c,String prefix,ObjectWrapper wrapper) {
  56.         return TemplateUtils.newTemplateEngine(c, prefix, wrapper, null);
  57.     }
  58.     public static Configuration newTemplateEngine(Class<?> c,String prefix,ObjectWrapper wrapper,TemplateLoader templateLoader) {
  59.         // Initialize the FreeMarker configuration;
  60.         // - Create a configuration instance
  61.         Configuration cfgFreeMarker = new Configuration(Configuration.VERSION_2_3_23);
  62.        
  63.         cfgFreeMarker.setClassForTemplateLoading(c, prefix);
  64.        
  65.         if(wrapper!=null){
  66.             cfgFreeMarker.setObjectWrapper(wrapper);
  67.         }
  68.        
  69.         if(templateLoader!=null){
  70.             cfgFreeMarker.setTemplateLoader(templateLoader);
  71.         }
  72.        
  73.         return cfgFreeMarker;
  74.     }
  75.    
  76.    
  77.    
  78.     /** -------- Utility per la creazione dei template ------------- */
  79.    
  80.     public static Template getTemplate(Configuration cfg,String templateName) throws IOException{
  81.         return cfg.getTemplate(templateName);
  82.     }
  83.    
  84.     public static Template getTemplate(String templateName) throws IOException{
  85.         Configuration cfg = TemplateUtils.newTemplateEngine();
  86.         return cfg.getTemplate(templateName);
  87.     }
  88.     public static Template getTemplate(String prefix,String templateName) throws IOException{
  89.         Configuration cfg = TemplateUtils.newTemplateEngine(prefix);
  90.         return cfg.getTemplate(templateName);
  91.     }
  92.     public static Template getTemplate(Class<?> c,String prefix,String templateName) throws IOException{
  93.         Configuration cfg = TemplateUtils.newTemplateEngine(c,prefix);
  94.         return cfg.getTemplate(templateName);
  95.     }
  96.     public static Template getTemplate(Class<?> c,String prefix,ObjectWrapper wrapper,String templateName) throws IOException{
  97.         Configuration cfg = TemplateUtils.newTemplateEngine(c,prefix,wrapper);
  98.         return cfg.getTemplate(templateName);
  99.     }
  100.    
  101.     public static Template buildTemplate(String name,byte[] bytes) throws IOException{
  102.         return new Template(name, new CharSequenceReader(new String(bytes)),newTemplateEngine());
  103.     }
  104.     public static Template buildTemplate(Configuration cfg,String name,byte[] bytes) throws IOException{
  105.         return new Template(name, new CharSequenceReader(new String(bytes)),cfg);
  106.     }
  107.    
  108.    
  109.    
  110.     /** -------- Trasformazioni dei template ------------- */
  111.        
  112.     public static byte[] toByteArray(Template template,Map<?, ?> map) throws IOException,TemplateException{
  113.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  114.         OutputStreamWriter writer = new OutputStreamWriter(bout);
  115.         template.process(map, writer);
  116.         writer.flush();
  117.         writer.close();
  118.         bout.flush();
  119.         bout.close();
  120.         return bout.toByteArray();
  121.     }
  122.     public static String toString(Template template,Map<?, ?> map) throws IOException,TemplateException{
  123.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  124.         OutputStreamWriter writer = new OutputStreamWriter(bout);
  125.         template.process(map, writer);
  126.         writer.flush();
  127.         writer.close();
  128.         bout.flush();
  129.         bout.close();
  130.         return bout.toString();
  131.     }
  132.     public static void writeFile(Template template,Map<?, ?> map,File file,boolean overwrite) throws Exception{
  133.         if(!overwrite){
  134.             if(file.exists()){
  135.                 System.out.println(": WARNING !! File ["+file.getAbsolutePath()+"] is already exist, it is not overwritten !!");
  136.                 return;
  137.             }
  138.         }
  139.         FileSystemUtilities.mkdirParentDirectory(file);
  140.         FileSystemUtilities.writeFile(file, TemplateUtils.toByteArray(template, map));
  141.     }
  142.    

  143.    
  144. }