1 package org.apache.turbine.pipeline; 2 3 56 57 import java.io.IOException ; 58 59 import java.util.Iterator ; 60 import java.util.HashMap ; 61 62 import javax.servlet.http.HttpServletResponse ; 63 import javax.servlet.ServletOutputStream ; 64 import javax.servlet.ServletException ; 65 66 import org.apache.turbine.TemplateContext; 67 import org.apache.turbine.RunData; 68 import org.apache.turbine.Turbine; 69 import org.apache.turbine.TurbineException; 70 import org.apache.turbine.modules.Module; 71 72 import org.apache.commons.logging.Log; 73 import org.apache.commons.logging.LogFactory; 74 75 import com.iv.flash.util.Util; 76 import com.iv.flash.context.Context; 77 import com.iv.flash.context.BeanContext; 78 import com.iv.flash.util.FlashOutput; 79 import com.iv.flash.util.IVException; 80 import com.iv.flash.api.FlashFile; 81 82 89 public class JGenRenderer 90 { 91 96 98 static 99 { 100 Util.init( Turbine.getRealPath( "/WEB-INF/conf" ) ); 101 } 102 103 private static final Log log = LogFactory.getLog( JGenRenderer.class ); 104 105 private static final String TEMPLATE_PATH = "jgen.template.path"; 106 private static final String TEMPLATE_PATH_DEFAULT = "/templates/swt/"; 107 108 111 protected RunData data = null; 112 113 116 public JGenRenderer(RunData data) 117 { 118 this.data = data; 119 } 120 121 128 public void render( String target ) 129 throws TurbineException, IVException, IOException , ServletException 130 { 131 String templateRoot = 132 Turbine.getConfiguration().getString( TEMPLATE_PATH, 133 TEMPLATE_PATH_DEFAULT ); 134 135 String targetPath = Turbine.getRealPath( templateRoot + target ); 136 137 log.debug( "SWT to render: " + targetPath ); 138 139 if ( targetPath == null ) 140 { 141 throw new TurbineException( "No target path" ); 142 } 143 144 148 HashMap map = new HashMap (); 149 150 TemplateContext tc = Module.getTemplateContext( data ); 151 152 Iterator keyIterator = tc.keySet().iterator(); 153 154 String key; 155 156 while( keyIterator.hasNext() ) 157 { 158 key = (String ) keyIterator.next(); 159 160 log.debug( "Key '" + key + "' added to context." ); 161 162 map.put( key, tc.get( key ) ); 163 } 164 165 BeanContext context = 166 new BeanContext( null, map ); 167 168 170 FlashOutput fob = process( targetPath, context ); 171 172 send( fob, data.getResponse() ); 173 } 174 175 189 protected FlashOutput process( String fileName, Context context ) 190 throws IVException, IOException 191 { 192 FlashFile file = FlashFile.parse( fileName ); 193 194 file.processFile( context ); 195 196 return file.generate(); 197 } 198 199 207 protected void send( FlashOutput fob, HttpServletResponse res ) 208 throws ServletException , IOException 209 { 210 res.setContentLength( fob.getSize() ); 211 res.setContentType( "application/x-shockwave-flash" ); 212 213 ServletOutputStream sos = res.getOutputStream(); 214 215 sos.write( fob.getBuf(), 0, fob.getSize() ); 216 } 217 } 218 | Popular Tags |