VelocityTemplateUtils.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.HashMap;
  26. import java.util.Map;
  27. import java.util.Properties;

  28. import org.apache.velocity.Template;
  29. import org.apache.velocity.VelocityContext;
  30. import org.apache.velocity.app.VelocityEngine;
  31. import org.apache.velocity.runtime.RuntimeConstants;
  32. import org.apache.velocity.runtime.RuntimeInstance;
  33. import org.apache.velocity.runtime.parser.ParseException;



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

  42.    
  43.     /** -------- Utility per la creazione delle configurazioni ------------- */
  44.    
  45.     public static VelocityEngine newTemplateEngine() {
  46.         VelocityEngine engine = new VelocityEngine();
  47.         engine.init();
  48.         return engine;
  49.     }
  50.     public static VelocityEngine newTemplateEngine(String propsFileName) {
  51.         VelocityEngine engine = new VelocityEngine();
  52.         engine.init(propsFileName);
  53.         return engine;
  54.     }
  55.     public static VelocityEngine newTemplateEngine(Properties p) {
  56.         VelocityEngine engine = new VelocityEngine();
  57.         engine.init(p);
  58.         return engine;
  59.     }
  60.    
  61.    
  62.    
  63.     /** -------- Utility per la creazione dei template ------------- */
  64.    
  65.     public static Template getTemplate(VelocityEngine engine,String templateName) throws IOException{
  66.         return engine.getTemplate(templateName);
  67.     }
  68.     public static Template getTemplate(VelocityEngine engine,String templateName, String encoding) throws IOException{
  69.         return engine.getTemplate(templateName, encoding);
  70.     }
  71.    
  72.     public static Template buildTemplate(String name,byte[] bytes) throws IOException, ParseException{
  73.         return buildTemplate(name, bytes, Charset.UTF_8.getValue(), null);
  74.     }
  75.     public static Template buildTemplate(String name,byte[] bytes, String encoding) throws IOException, ParseException{
  76.         return buildTemplate(name, bytes, encoding,
  77.                 null);
  78.     }
  79.    
  80.     public static Template buildTemplate(String name,byte[] bytes,
  81.             Map<String, byte[]> templateIncludes) throws IOException, ParseException{
  82.         return buildTemplate(name, bytes, Charset.UTF_8.getValue(),
  83.                 templateIncludes);
  84.     }
  85.     public static Template buildTemplate(String name,byte[] bytes, String encoding,
  86.             Map<String, byte[]> templateIncludes) throws IOException, ParseException{
  87.        
  88.         RuntimeInstance instance = new RuntimeInstance();
  89.        
  90.         String defaultTemplate = "_____DEFAULT_TEMPLATE___";
  91.        
  92.         if(templateIncludes==null) {
  93.             templateIncludes= new HashMap<String, byte[]>();
  94.         }
  95.         VelocityTemplateLoader loader = new VelocityTemplateLoader(templateIncludes);
  96.         templateIncludes.put(defaultTemplate, bytes);
  97.         instance.setProperty(RuntimeConstants.RESOURCE_LOADER, "govway_rl" );
  98.         instance.setProperty("govway_rl."+RuntimeConstants.RESOURCE_LOADER+"."+RuntimeConstants.RESOURCE_LOADER_INSTANCE, loader);

  99.         instance.init();

  100.         Template template = instance.getTemplate(defaultTemplate, encoding);
  101.        
  102.         /*
  103.         Metodo che non consentiva la risoluzione dei template interni
  104.         Template template = new Template();
  105.         template.setName(name);
  106.         template.setEncoding(encoding);
  107.         RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
  108.         StringReader reader = new StringReader(new String(bytes));
  109.         SimpleNode node = runtimeServices.parse(reader, template);
  110.         template.setRuntimeServices(runtimeServices);
  111.         template.setData(node);
  112.         template.initDocument();
  113.         */
  114.         return template;
  115.     }
  116.    
  117.    
  118.    
  119.     /** -------- Trasformazioni dei template ------------- */
  120.        
  121.     public static VelocityContext toVelocityContext(Map<String, Object> map) {
  122.         VelocityContext context = new VelocityContext(map);
  123.         return context;
  124.     }
  125.    
  126.     public static byte[] toByteArray(Template template,Map<String, Object> map) throws IOException {
  127.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  128.         OutputStreamWriter writer = new OutputStreamWriter(bout);
  129.         template.merge( toVelocityContext(map), writer );
  130.         writer.flush();
  131.         writer.close();
  132.         bout.flush();
  133.         bout.close();
  134.         return bout.toByteArray();
  135.     }
  136.     public static String toString(Template template,Map<String, Object> map) throws IOException{
  137.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  138.         OutputStreamWriter writer = new OutputStreamWriter(bout);
  139.         template.merge( toVelocityContext(map), writer );
  140.         writer.flush();
  141.         writer.close();
  142.         bout.flush();
  143.         bout.close();
  144.         return bout.toString();
  145.     }
  146.     public static void writeFile(Template template,Map<String, Object> map,File file,boolean overwrite) throws Exception{
  147.         if(!overwrite){
  148.             if(file.exists()){
  149.                 System.out.println(": WARNING !! File ["+file.getAbsolutePath()+"] is already exist, it is not overwritten !!");
  150.                 return;
  151.             }
  152.         }
  153.         FileSystemUtilities.mkdirParentDirectory(file);
  154.         FileSystemUtilities.writeFile(file, VelocityTemplateUtils.toByteArray(template, map));
  155.     }
  156.    

  157.    
  158. }