KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > exomvc > config > VelocityResourceManager


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlet.exomvc.config;
6
7 import java.io.InputStream JavaDoc;
8 import java.util.Properties JavaDoc;
9 import javax.portlet.PortletContext;
10 import org.apache.velocity.Template;
11 import org.apache.velocity.app.Velocity;
12 import org.apache.velocity.app.VelocityEngine;
13 import org.exoplatform.commons.utils.IOUtil;
14
15 /**
16  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
17  * @since Nov 12, 2004
18  * @version $Id$
19  */

20 public class VelocityResourceManager {
21   private String JavaDoc reposistoryPath_ ;
22   private PortletContext context_ ;
23   private VelocityEngine vengine_;
24  
25   public VelocityResourceManager(PortletContext context, String JavaDoc repo ) throws Exception JavaDoc {
26     reposistoryPath_ = repo + "/velocity/" ;
27     context_ = context ;
28     String JavaDoc path = context.getRealPath(repo + "/velocity") ;
29     Properties JavaDoc p = new Properties JavaDoc();
30     p.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH, path);
31     vengine_ = new VelocityEngine();
32     vengine_.init( p );
33   }
34   
35   public String JavaDoc getVelocityReposistory() { return reposistoryPath_ ; }
36   
37   public VelocityEngine getVelocityEngine() { return vengine_ ; }
38   
39   public Template getTemplate(String JavaDoc template) throws Exception JavaDoc {
40     return vengine_.getTemplate(template) ;
41   }
42   
43   public String JavaDoc getResourceAsText(String JavaDoc resource) throws Exception JavaDoc {
44     String JavaDoc path = reposistoryPath_ + resource ;
45     InputStream JavaDoc is = context_.getResourceAsStream(path) ;
46     if(is == null) return "reosurce : " + resource + " is notfound" ;
47     return IOUtil.getStreamContentAsString(is) ;
48   }
49 }
Popular Tags