1 9 10 package org.jboss.portal.format.template; 11 12 import java.io.InputStream ; 13 import java.io.InputStreamReader ; 14 import java.io.Reader ; 15 import java.util.Set ; 16 17 import org.dom4j.Document; 18 import org.jboss.portal.format.util.CLLoader; 19 import org.jboss.portal.format.util.Loader; 20 import org.jboss.portal.common.util.Tools; 21 22 26 public class TemplateLoader extends TemplateRepository 27 { 28 29 private Loader loader; 30 32 public TemplateLoader(Loader loader) 33 { 34 setLoader(loader); 35 } 36 37 public Loader getLoader() 38 { 39 return loader; 40 } 41 42 public void setLoader(Loader loader) 43 { 44 if (loader == null) 45 { 46 throw new IllegalArgumentException ("loader cannot be null"); 47 } 48 this.loader = loader; 49 } 50 51 64 79 86 public void addTemplate(String name, String loadName) throws BuildException 87 { 88 Document doc = loadTemplate(loadName); 89 addTemplate(name, doc); 90 } 91 92 public void addTemplate(String name, String loadName, Set names) throws BuildException 93 { 94 Document doc = loadTemplate(loadName); 95 addTemplate(name, doc, names); 96 } 97 98 104 128 131 public Document loadTemplate(String name) throws BuildException 132 { 133 InputStream tin = null; 134 try 135 { 136 tin = loader.load(name); 137 if (tin != null) 138 { 139 Reader reader = new InputStreamReader (tin); 140 TemplateParser parser = new TemplateParser(reader); 141 return parser.parse(); 142 } 143 else 144 { 145 throw new BuildException("Template " + name + " not found"); 146 } 147 } 148 catch(ParseException e) 149 { 150 throw new BuildException("Cannot parse the template source " + name, e); 151 } 152 finally 153 { 154 Tools.safeClose(tin); 155 } 156 } 157 158 public static TemplateLoader create(ClassLoader cl, String base) 159 { 160 return new TemplateLoader(new CLLoader(cl, base)); 161 } 162 } 163 | Popular Tags |