1 48 49 package org.jpublish.view.webmacro; 50 51 import java.io.*; 52 53 import com.anthonyeden.lib.config.Configuration; 54 import com.anthonyeden.lib.config.ConfigurationException; 55 import com.anthonyeden.lib.util.IOUtilities; 56 import org.jpublish.JPublishRuntimeException; 57 import org.jpublish.RequestContext; 58 import org.jpublish.view.AbstractViewRenderer; 59 import org.jpublish.view.ContentSource; 60 import org.jpublish.view.ViewRenderException; 61 import org.webmacro.Context; 62 import org.webmacro.WM; 63 import org.webmacro.WebMacro; 64 import org.webmacro.engine.StreamTemplate; 65 66 71 72 public class WebMacroViewRenderer extends AbstractViewRenderer { 73 74 private WebMacro wm = null; 75 76 79 80 public void init() { 81 try { 82 wm = new WM(); 83 } catch (Exception e) { 84 throw new JPublishRuntimeException("Error initializing WebMacro engine", e); 85 } 86 } 87 88 97 98 public void render(RequestContext context, String path, Writer out) 99 throws IOException, ViewRenderException { 100 Reader in = null; 101 try { 102 ContentSource contentSource = siteContext.getContentSource(path); 103 if (contentSource == null) { 104 throw new ViewRenderException("Content not found: " + path); 105 } 106 107 in = contentSource.getReader(); 108 109 render(context, path, in, out); 110 } catch (ViewRenderException e) { 111 throw e; 112 } catch (IOException e) { 113 throw e; 114 } catch (Exception e) { 115 throw new ViewRenderException(e); 116 } finally { 117 IOUtilities.close(in); 118 } 119 } 120 121 130 131 public void render(RequestContext context, String path, OutputStream out) 132 throws IOException, ViewRenderException { 133 render(context, path, new OutputStreamWriter(out)); 134 } 135 136 146 147 public void render(RequestContext context, String path, Reader in, 148 Writer out) throws IOException, ViewRenderException { 149 try { 150 Context wmContext = wm.getContext(); 151 Object [] keys = keys = context.getKeys(); 152 for (int i = 0; i < keys.length; i++) { 153 wmContext.put(keys[i], context.get(keys[i].toString())); 154 } 155 156 StreamTemplate template = new StreamTemplate(wm.getBroker(), in); 158 template.setName(path); 159 Object result = template.evaluate(wmContext); 160 if (result == null) { 161 throw new Exception ("Error evaluating template."); 162 } else { 163 out.write(result.toString()); 164 } 165 } catch (ViewRenderException e) { 166 throw e; 167 } catch (IOException e) { 168 throw e; 169 } catch (Exception e) { 170 throw new ViewRenderException(e); 171 } 172 } 173 174 184 185 public void render(RequestContext context, String path, InputStream in, 186 OutputStream out) throws IOException, ViewRenderException { 187 render(context, path, new InputStreamReader(in), 188 new OutputStreamWriter(out)); 189 } 190 191 196 197 public void loadConfiguration(Configuration configuration) 198 throws ConfigurationException { 199 200 } 201 202 } 203 | Popular Tags |