1 48 49 package org.jpublish.view.pipeline; 50 51 import java.io.*; 52 import java.util.ArrayList ; 53 import java.util.Collection ; 54 import java.util.Iterator ; 55 56 import com.anthonyeden.lib.config.Configuration; 57 import com.anthonyeden.lib.config.ConfigurationException; 58 import org.apache.commons.logging.Log; 59 import org.apache.commons.logging.LogFactory; 60 import org.jpublish.RequestContext; 61 import org.jpublish.view.AbstractViewRenderer; 62 import org.jpublish.view.ViewRenderException; 63 import org.jpublish.view.ViewRenderer; 64 65 71 72 public class PipelineViewRenderer extends AbstractViewRenderer { 73 74 private static Log log = LogFactory.getLog(PipelineViewRenderer.class); 75 76 private Collection pipeline = new ArrayList (); 77 78 87 88 public void render(RequestContext context, String path, Writer out) 89 throws IOException, ViewRenderException { 90 91 log.debug("Executing pipeline"); 92 93 boolean first = true; 94 String buffer = null; 95 Iterator viewRenderers = pipeline.iterator(); 96 while (viewRenderers.hasNext()) { 97 ViewRenderer viewRenderer = (ViewRenderer) viewRenderers.next(); 98 if (log.isDebugEnabled()) { 99 log.debug("Renderer: " + viewRenderer.getClass()); 100 } 101 Writer pipelineOut = new StringWriter(); 102 if (first) { 103 first = false; 104 viewRenderer.render(context, path, pipelineOut); 105 buffer = pipelineOut.toString(); 106 } else { 107 StringReader pipelineIn = new StringReader(buffer); 108 if (viewRenderers.hasNext()) { 109 viewRenderer.render(context, path, pipelineIn, 110 pipelineOut); 111 buffer = pipelineOut.toString(); 112 } else { 113 viewRenderer.render(context, path, new StringReader(buffer), 114 out); 115 } 116 } 117 } 118 } 119 120 129 130 public void render(RequestContext context, String path, OutputStream out) 131 throws IOException, ViewRenderException { 132 render(context, path, new OutputStreamWriter(out)); 133 } 134 135 145 146 public void render(RequestContext context, String path, Reader in, 147 Writer out) throws IOException, ViewRenderException { 148 log.debug("Executing pipeline"); 149 150 boolean first = true; 151 String buffer = null; 152 Iterator viewRenderers = pipeline.iterator(); 153 while (viewRenderers.hasNext()) { 154 ViewRenderer viewRenderer = (ViewRenderer) viewRenderers.next(); 155 if (log.isDebugEnabled()) { 156 log.debug("Renderer: " + viewRenderer.getClass()); 157 } 158 Writer pipelineOut = new StringWriter(); 159 if (first) { 160 first = false; 161 viewRenderer.render(context, path, in, pipelineOut); 162 buffer = pipelineOut.toString(); 163 } else { 164 StringReader pipelineIn = new StringReader(buffer); 165 if (viewRenderers.hasNext()) { 166 viewRenderer.render(context, path, pipelineIn, 167 pipelineOut); 168 buffer = pipelineOut.toString(); 169 } else { 170 viewRenderer.render(context, path, new StringReader(buffer), 171 out); 172 } 173 } 174 } 175 } 176 177 187 188 public void render(RequestContext context, String path, InputStream in, 189 OutputStream out) throws IOException, ViewRenderException { 190 render(context, path, new InputStreamReader(in), 191 new OutputStreamWriter(out)); 192 } 193 194 199 200 public void loadConfiguration(Configuration configuration) 201 throws ConfigurationException { 202 pipeline.clear(); 203 204 Iterator viewRendererElements = 205 configuration.getChildren("view-renderer").iterator(); 206 while (viewRendererElements.hasNext()) { 207 Configuration viewRendererElement = 208 (Configuration) viewRendererElements.next(); 209 String viewRendererName = viewRendererElement.getAttribute("name"); 210 if (viewRendererName == null) { 211 if (log.isDebugEnabled()) { 213 log.debug("Added default view renderer to pipeline"); 214 } 215 pipeline.add(siteContext.getViewRenderer()); 216 } else { 217 if (log.isDebugEnabled()) { 219 log.debug("Added " + viewRendererName + " to pipeline"); 220 } 221 pipeline.add(siteContext.getViewRenderer(viewRendererName)); 222 } 223 } 224 } 225 226 } 227 | Popular Tags |