1 44 package org.jpublish.template; 45 46 import java.io.InputStream ; 47 import java.io.Reader ; 48 import java.io.Writer ; 49 import java.util.ArrayList ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 import com.anthonyeden.lib.config.Configuration; 54 import com.anthonyeden.lib.config.ConfigurationException; 55 import com.anthonyeden.lib.config.ConfigurationFactory; 56 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory; 57 import org.apache.commons.logging.Log; 58 import org.apache.commons.logging.LogFactory; 59 import org.jpublish.RequestContext; 60 import org.jpublish.SiteContext; 61 import org.jpublish.action.Action; 62 import org.jpublish.action.ActionWrapper; 63 import org.jpublish.page.Page; 64 import org.jpublish.util.PathUtilities; 65 import org.jpublish.view.ViewRenderer; 66 67 72 public class Template { 73 74 private Log log = LogFactory.getLog(Template.class); 75 76 private SiteContext siteContext = null; 77 private String path = null; 78 private String templateText = null; 79 private String viewRendererName = null; 80 private long lastModified = -1; 81 private List templateActions = new ArrayList (); 82 private String templateManagerName = null; 83 84 91 public Template(SiteContext siteContext, String path, 92 String templateManagerName) { 93 this.siteContext = siteContext; 94 this.path = path; 95 96 this.templateManagerName = templateManagerName; 101 } 102 103 108 public String getPath() { 109 return path; 110 } 111 112 117 118 public String getText() { 119 log.debug("getText()"); 120 return templateText; 121 } 122 123 128 129 public void setText(String templateText) { 130 this.templateText = templateText; 131 } 132 133 138 139 public long getLastModified() { 140 return lastModified; 141 } 142 143 148 149 public void setLastModified(long lastModified) { 150 this.lastModified = lastModified; 151 } 152 153 158 159 public String getViewRendererName() { 160 return viewRendererName; 161 } 162 163 169 170 public void setViewRendererName(String viewRendererName) { 171 this.viewRendererName = viewRendererName; 172 } 173 174 179 public String getTemplateManagerName() { 180 return templateManagerName; 181 } 182 183 188 189 public List getTemplateActions() { 190 return templateActions; 191 } 192 193 198 199 public void executeActions(RequestContext context) { 200 Iterator templateActions = getTemplateActions().iterator(); 201 while (templateActions.hasNext()) { 202 ((ActionWrapper) templateActions.next()).execute(context); 203 } 204 } 205 206 214 215 public void merge(RequestContext context, Page page, Writer out) 216 throws TemplateMergeException { 217 try { 218 log.debug("Executing template actions"); 219 executeActions(context); 220 221 227 String path = PathUtilities.makeTemplateURI(getPath(), 228 templateManagerName); 229 ViewRenderer renderer = siteContext.getViewRenderer(); 230 String viewRendererName = getViewRendererName(); 231 if (viewRendererName != null) { 232 renderer = siteContext.getViewRenderer(viewRendererName); 233 } 234 renderer.render(context, path, out); 235 236 log.debug("Merge complete."); 237 } catch (Exception e) { 238 throw new TemplateMergeException(e.getMessage(), e); 239 } 240 } 241 242 248 249 public void loadConfiguration(InputStream in) throws ConfigurationException { 250 ConfigurationFactory configFactory = 251 SAXConfigurationFactory.getInstance(); 252 loadConfiguration(configFactory.getConfiguration(path, in)); 253 } 254 255 261 262 public void loadConfiguration(Reader in) throws ConfigurationException { 263 ConfigurationFactory configFactory = 264 SAXConfigurationFactory.getInstance(); 265 loadConfiguration(configFactory.getConfiguration(path, in)); 266 } 267 268 275 276 public void loadConfiguration(Configuration configuration) 277 throws ConfigurationException { 278 templateActions.clear(); 279 280 setViewRendererName(configuration.getChildValue("view-renderer")); 282 283 log.debug("Looping through template-action elements."); 285 Iterator actionElements = 286 configuration.getChildren("template-action").iterator(); 287 while (actionElements.hasNext()) { 288 Configuration actionElement = 289 (Configuration) actionElements.next(); 290 String name = actionElement.getAttribute("name"); 291 if (name == null) { 292 name = actionElement.getValue(); 293 } 294 295 if (name == null) { 296 throw new ConfigurationException("Error configuring page-action."); 297 } 298 299 Action action = siteContext.getActionManager().findAction(name); 300 if (action == null) { 301 throw new ConfigurationException("Action " + name + " not defined"); 302 } 303 304 templateActions.add(new ActionWrapper(action, actionElement)); 305 } 306 } 307 308 } 309 310 | Popular Tags |