VelocityTemplateLoader.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.Reader;
  22. import java.io.StringReader;
  23. import java.io.UnsupportedEncodingException;
  24. import java.util.Map;

  25. import org.apache.velocity.exception.ResourceNotFoundException;
  26. import org.apache.velocity.runtime.resource.Resource;
  27. import org.apache.velocity.runtime.resource.loader.ResourceLoader;
  28. import org.apache.velocity.util.ExtProperties;

  29. /**
  30.  *  VelocityTemplateLoader
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class VelocityTemplateLoader extends ResourceLoader {
  37.    
  38.     private Map<String, byte[]> templateIncludes;
  39.    
  40.     public VelocityTemplateLoader(Map<String, byte[]> templateIncludes) {
  41.         this.templateIncludes = templateIncludes;
  42.     }

  43.     @Override
  44.     public long getLastModified(Resource arg0) {
  45.         return 0;
  46.     }

  47.     @Override
  48.     public boolean isSourceModified(Resource arg0) {
  49.         return false;
  50.     }

  51.     @Override
  52.     public boolean resourceExists(String name) {
  53.         return this.templateIncludes.containsKey(name);
  54.     }

  55.     @Override
  56.     public Reader getResourceReader(String name, String encoding) throws ResourceNotFoundException {
  57.         if(this.templateIncludes.containsKey(name)) {
  58.             try {
  59.                 return new StringReader(new String(this.templateIncludes.get(name),encoding));
  60.             } catch (UnsupportedEncodingException e) {
  61.                 throw new RuntimeException(e.getMessage(),e);
  62.             }
  63.         }
  64.         return null;
  65.     }

  66.     @Override
  67.     public void init(ExtProperties arg0) {
  68.        
  69.     }
  70. }