自定义Resource Loader

velocity在1.5中提供了6种资源加载器,已经足够满足大多数的需求,当然,如果你觉得这些都不适用,你也可以自己实现一个。

实现一个资源加载器,实际上很简单,只要继承ResourceLoader,实现它的几个方法就可以了。

下面是一个简单的例子,它展示了如何实现一个最简单的ResourceLoader。

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://sivasoft.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20org.sivasoft.viework.velocity.resource.loader%3B%0A%0Aimport%20java.io.ByteArrayInputStream%3B%0Aimport%20java.io.InputStream%3B%0Aimport%20java.io.UnsupportedEncodingException%3B%0A%0Aimport%20org.apache.commons.collections.ExtendedProperties%3B%0Aimport%20org.apache.commons.logging.Log%3B%0Aimport%20org.apache.commons.logging.LogFactory%3B%0Aimport%20org.apache.velocity.exception.ResourceNotFoundException%3B%0Aimport%20org.apache.velocity.runtime.resource.Resource%3B%0Aimport%20org.apache.velocity.runtime.resource.loader.ResourceLoader%3B%0A%0Apublic%20class%20TestResourceLoader%20extends%20ResourceLoader%20%7B%0A%0A%09private%20static%20Log%20log%20%3D%20LogFactory.getLog(TestResourceLoader.class)%3B%0A%0A%09%2F**%0A%09%20*%20%E5%BE%97%E5%88%B0%E8%B5%84%E6%BA%90%E6%B5%81%E7%9A%84%E6%9C%80%E5%90%8E%E4%BF%AE%E6%94%B9%E6%97%B6%E9%97%B4%0A%09%20*%20%0A%09%20*%20%40param%20resource%0A%09%20*%20%40return%20Time%20in%20millis%20when%20the%20resource%20has%20been%20modified.%0A%09%20*%2F%0A%09%40Override%0A%09public%20long%20getLastModified(Resource%20arg0)%20%7B%0A%09%09return%200%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E5%BE%97%E5%88%B0%E4%B8%80%E4%B8%AA%E8%B5%84%E6%BA%90%E8%BE%93%E5%85%A5%E6%B5%81%E3%80%82%0A%09%20*%20%0A%09%20*%20%40param%20source%0A%09%20*%20%40return%20The%20input%20stream%20for%20the%20requested%20resource.%0A%09%20*%20%40throws%20ResourceNotFoundException%0A%09%20*%2F%0A%09%40Override%0A%09public%20InputStream%20getResourceStream(String%20name)%0A%09%09%09throws%20ResourceNotFoundException%20%7B%0A%09%09if%20(name%20%3D%3D%20null%20%7C%7C%20name.length()%20%3D%3D%200)%20%7B%0A%09%09%09throw%20new%20ResourceNotFoundException(%0A%09%09%09%09%09%22Need%20to%20specify%20a%20template%20name!%22)%3B%0A%09%09%7D%0A%0A%09%09%2F%2F%20%E5%BE%97%E5%88%B0%E4%B8%80%E4%B8%AA%E8%B5%84%E6%BA%90%E6%B5%81%EF%BC%8C%E8%BF%99%E9%87%8C%E4%B8%BA%E4%BA%86%E7%AE%80%E5%8D%95%EF%BC%8C%E4%BD%BF%E7%94%A8%E4%BA%86%E4%B8%80%E4%B8%AA%E5%9B%BA%E5%AE%9A%E7%9A%84String%EF%BC%8C%E5%AE%9E%E9%99%85%E4%B8%8A%E5%B9%B6%E4%B8%8D%E4%BC%9A%E5%A6%82%E6%AD%A4%E4%BD%BF%E7%94%A8%E3%80%82%0A%09%09String%20str%20%3D%20%22Hello%20%24name%22%3B%0A%09%09try%20%7B%0A%09%09%09return%20new%20ByteArrayInputStream(str.getBytes(%22UTF-8%22))%3B%0A%09%09%7D%20catch%20(UnsupportedEncodingException%20e)%20%7B%2F%2F%20%E8%BF%99%E4%B8%AA%E5%BC%82%E5%B8%B8%E5%BA%94%E8%AF%A5%E4%B8%8D%E4%BC%9A%E5%8F%91%E7%94%9F%E3%80%82%0A%09%09%09log.error(e)%3B%0A%09%09%09throw%20new%20RuntimeException(e)%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%B5%84%E6%BA%90%E5%8A%A0%E8%BD%BD%E5%99%A8%20a%20resources%20class.%0A%09%20*%20%0A%09%20*%20%40param%20configuration%0A%09%20*%2F%0A%09%40Override%0A%09public%20void%20init(ExtendedProperties%20arg0)%20%7B%0A%09%09if%20(log.isTraceEnabled())%20%7B%0A%09%09%09log.trace(%22TestResourceLoader%20%3A%20initialization%20complete.%22)%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%A3%80%E6%9F%A5%E8%B5%84%E6%BA%90%E6%98%AF%E5%90%A6%E8%A2%AB%E4%BF%AE%E6%94%B9%E3%80%82%0A%09%20*%20%0A%09%20*%20%40param%20resource%0A%09%20*%20%40return%20True%20if%20the%20resource%20has%20been%20modified.%0A%09%20*%2F%0A%09%40Override%0A%09public%20boolean%20isSourceModified(Resource%20arg0)%20%7B%0A%09%09return%20false%3B%0A%09%7D%0A%0A%7D%0A"></embed>
  1. package org.sivasoft.viework.velocity.resource.loader;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.InputStream;  
  5. import java.io.UnsupportedEncodingException;  
  6.   
  7. import org.apache.commons.collections.ExtendedProperties;  
  8. import org.apache.commons.logging.Log;  
  9. import org.apache.commons.logging.LogFactory;  
  10. import org.apache.velocity.exception.ResourceNotFoundException;  
  11. import org.apache.velocity.runtime.resource.Resource;  
  12. import org.apache.velocity.runtime.resource.loader.ResourceLoader;  
  13.   
  14. public class TestResourceLoader extends ResourceLoader {  
  15.   
  16.     private static Log log = LogFactory.getLog(TestResourceLoader.class);  
  17.   
  18.     /** 
  19.      * 得到资源流的最后修改时间 
  20.      *  
  21.      * @param resource 
  22.      * @return Time in millis when the resource has been modified. 
  23.      */  
  24.     @Override  
  25.     public long getLastModified(Resource arg0) {  
  26.         return 0;  
  27.     }  
  28.   
  29.     /** 
  30.      * 得到一个资源输入流。 
  31.      *  
  32.      * @param source 
  33.      * @return The input stream for the requested resource. 
  34.      * @throws ResourceNotFoundException 
  35.      */  
  36.     @Override  
  37.     public InputStream getResourceStream(String name)  
  38.             throws ResourceNotFoundException {  
  39.         if (name == null || name.length() == 0) {  
  40.             throw new ResourceNotFoundException(  
  41.                     "Need to specify a template name!");  
  42.         }  
  43.   
  44.         // 得到一个资源流,这里为了简单,使用了一个固定的String,实际上并不会如此使用。  
  45.         String str = "Hello $name";  
  46.         try {  
  47.             return new ByteArrayInputStream(str.getBytes("UTF-8"));  
  48.         } catch (UnsupportedEncodingException e) {// 这个异常应该不会发生。  
  49.             log.error(e);  
  50.             throw new RuntimeException(e);  
  51.         }  
  52.     }  
  53.   
  54.     /** 
  55.      * 初始化资源加载器 a resources class. 
  56.      *  
  57.      * @param configuration 
  58.      */  
  59.     @Override  
  60.     public void init(ExtendedProperties arg0) {  
  61.         if (log.isTraceEnabled()) {  
  62.             log.trace("TestResourceLoader : initialization complete.");  
  63.         }  
  64.     }  
  65.   
  66.     /** 
  67.      * 检查资源是否被修改。 
  68.      *  
  69.      * @param resource 
  70.      * @return True if the resource has been modified. 
  71.      */  
  72.     @Override  
  73.     public boolean isSourceModified(Resource arg0) {  
  74.         return false;  
  75.     }  
  76.   
  77. }  

 

很简单,只要做一点小小的开发,你就可以自己管理资源加载。

再来看看如何使用这个加载器:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://sivasoft.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%2F%2F%E4%BB%A3%E7%A0%81%E7%9C%81%E7%95%A5%E6%8E%89%E4%BA%86%E6%89%80%E6%9C%89try%20catch%0AProperties%20p%20%3D%20new%20Properties()%3B%0Ap.setProperty(%22resource.loader%22%2C%20%22test%22)%3B%2F%2F%E7%BB%99%E4%BD%A0%E7%9A%84%E5%8A%A0%E8%BD%BD%E5%99%A8%E9%9A%8F%E4%BE%BF%E5%8F%96%E4%B8%AA%E5%90%8D%E5%AD%97%0Ap.setProperty(%22test.resource.loader.class%22%2C%22org.sivasoft.viework.velocity.resource.loader.TestResourceLoader%22)%3B%2F%2F%E9%85%8D%E7%BD%AE%E4%B8%80%E4%B8%8B%E4%BD%A0%E7%9A%84%E5%8A%A0%E8%BD%BD%E5%99%A8%E5%AE%9E%E7%8E%B0%E7%B1%BB%0Ap.setProperty(%22input.encoding%22%2C%20%22UTF-8%22)%3B%0Ap.setProperty(%22output.encoding%22%2C%20%22UTF-8%22)%3B%0A%0AVelocityEngine%20ve%20%3D%20new%20VelocityEngine()%3B%0Ave.init(p)%3B%0AString%20velocityTemplate%20%3D%20vm%3B%0ATemplate%20template%20%3D%20null%3B%0Atemplate%20%3D%20ve.getTemplate(%22%E9%9A%8F%E4%BE%BF%E4%B8%80%E4%B8%AAString%22%2C%20%22UTF-8%22)%3B%0A%0AVelocityContext%20context%20%3D%20new%20VelocityContext()%3B%0Acontext.put(%22name%22%2C%22Siva%22)%3B%0A%0AFileOutputStream%20fos%20%3D%20new%20FileOutputStream(%22d%3A%5Ctest.txt%22)%3B%0ABufferedWriter%20writer%20%3D%20new%20BufferedWriter(new%20OutputStreamWriter(fos%2C%20%22UTF-8%22))%3B%0Atemplate.merge(context%2C%20writer)%3B%0Awriter.close()%3B%0Afos.close()%3B%0A"></embed>
  1. //代码省略掉了所有try catch  
  2. Properties p = new Properties();  
  3. p.setProperty("resource.loader""test");//给你的加载器随便取个名字  
  4. p.setProperty("test.resource.loader.class","org.sivasoft.viework.velocity.resource.loader.TestResourceLoader");//配置一下你的加载器实现类  
  5. p.setProperty("input.encoding""UTF-8");  
  6. p.setProperty("output.encoding""UTF-8");  
  7.   
  8. VelocityEngine ve = new VelocityEngine();  
  9. ve.init(p);  
  10. String velocityTemplate = vm;  
  11. Template template = null;  
  12. template = ve.getTemplate("随便一个String""UTF-8");  
  13.   
  14. VelocityContext context = new VelocityContext();  
  15. context.put("name","Siva");  
  16.   
  17. FileOutputStream fos = new FileOutputStream("d:\test.txt");  
  18. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));  
  19. template.merge(context, writer);  
  20. writer.close();  
  21. fos.close();  

 

运行代码(需要给它加上异常处理),可以看到D盘根目录下生成了一个文件,打开看看,是不是Hello Siva。

可以看到,实现一个最简单的资源加载器,我们只要继承ResourceLoader并实现getResourceStream()就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值