1 package net.sf.maia.core.view; 2 3 import java.io.InputStream; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.commons.collections.ExtendedProperties; 8 import org.apache.velocity.exception.ResourceNotFoundException; 9 import org.apache.velocity.runtime.resource.Resource; 10 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 11 12 /*** 13 * 14 * 15 * @author Magnus Grimsell 16 */ 17 public class MaiaResourceLoader extends ResourceLoader 18 { 19 private List mMissingTemplates = new ArrayList(); 20 private String mPath; 21 22 /*** 23 * @see ResourceLoader#init(ExtendedProperties) 24 */ 25 public void init(ExtendedProperties props) 26 { 27 mPath = (String)props.getProperty("path"); 28 if(!mPath.endsWith("/")) 29 { 30 mPath = mPath + "/"; 31 } 32 } 33 34 /*** 35 * @see ResourceLoader#getResourceStream(String) 36 */ 37 public InputStream getResourceStream(String name) throws ResourceNotFoundException 38 { 39 InputStream in; 40 if(mMissingTemplates.contains(name)) 41 { 42 name = "deafult"; 43 } 44 ClassLoader cl = MaiaResourceLoader.class.getClassLoader(); 45 String path = mPath + name + ".vm"; 46 in = cl.getResourceAsStream(path); 47 if(in == null) 48 49 { 50 51 mMissingTemplates.add(name); 52 in = cl.getResourceAsStream(mPath + "default.vm"); 53 if(in == null) 54 { 55 throw new ResourceNotFoundException("Can't find default template in " + mPath); 56 } 57 } 58 59 return in; 60 } 61 62 /*** 63 * @see ResourceLoader#isSourceModified(Resource) 64 */ 65 public boolean isSourceModified(Resource r) 66 { 67 return true; 68 } 69 70 /*** 71 * @see ResourceLoader#getLastModified(Resource) 72 */ 73 public long getLastModified(Resource r) 74 { 75 return 0; 76 } 77 78 }