KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > pipeline > JGenRenderer


1 package org.apache.turbine.pipeline;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.IOException JavaDoc;
58
59 import java.util.Iterator JavaDoc;
60 import java.util.HashMap JavaDoc;
61
62 import javax.servlet.http.HttpServletResponse JavaDoc;
63 import javax.servlet.ServletOutputStream JavaDoc;
64 import javax.servlet.ServletException JavaDoc;
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 /**
83  * Renderer that use JGenerator to merge a SWT template with the template
84  * context. The template context is wrapped in a JGenerator BeanContext.
85  *
86  * @author <a HREF="mailto:james@jamestaylor.org">James Taylor</a>
87  * @version $Id: JGenRenderer.java,v 1.5 2002/06/04 12:17:07 jtaylor Exp $
88  */

89 public class JGenRenderer
90 {
91     // JGenerator expects to be initialized with the directory containing
92
// its 'iv.properties' file. In the future it is expected JGen will
93
// support more methods of configuration to support embedded use more
94
// cleanly.
95

96     // FIXME: Need a better means to do one time initialization of JGenerator
97

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 JavaDoc TEMPLATE_PATH = "jgen.template.path";
106     private static final String JavaDoc TEMPLATE_PATH_DEFAULT = "/templates/swt/";
107
108     /**
109      * RunData of the request this Renderer is for.
110      */

111     protected RunData data = null;
112
113     /**
114      * Construct a renderer for the given RunData.
115      */

116     public JGenRenderer(RunData data)
117     {
118         this.data = data;
119     }
120     
121     /**
122      * Process the request
123      *
124      * @param target the filename of the template.
125      * @throws TurbineException Any exception trown while processing will be
126      * wrapped into a TurbineException and rethrown.
127      */

128     public void render( String JavaDoc target )
129         throws TurbineException, IVException, IOException JavaDoc, ServletException JavaDoc
130     {
131         String JavaDoc templateRoot =
132             Turbine.getConfiguration().getString( TEMPLATE_PATH,
133                                                   TEMPLATE_PATH_DEFAULT );
134
135         String JavaDoc 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         // Build a BeanContext from the TemplateContext. Really we should
145
// be able to just make the TemplateContext the root object, but
146
// it seems JXPath is dumber than I thought.
147

148         HashMap JavaDoc map = new HashMap JavaDoc();
149         
150         TemplateContext tc = Module.getTemplateContext( data );
151         
152         Iterator JavaDoc keyIterator = tc.keySet().iterator();
153
154         String JavaDoc key;
155         
156         while( keyIterator.hasNext() )
157         {
158             key = (String JavaDoc) 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         // Process the SWT with the context into a FlashOutput buffer
169

170         FlashOutput fob = process( targetPath, context );
171         
172         send( fob, data.getResponse() );
173     }
174
175     /**
176      * Process template<BR>
177      * <UL>
178      * <LI>parse template
179      * <LI>process (perform substitutions and generator commands)
180      * <LI>generate movie
181      * </UL>
182      *
183      * @param fileName template file name
184      * @param context generator context
185      * @return generated flash content
186      * @exception IVException
187      * @exception IOException
188      */

189     protected FlashOutput process( String JavaDoc fileName, Context context )
190         throws IVException, IOException JavaDoc
191     {
192         FlashFile file = FlashFile.parse( fileName );
193
194         file.processFile( context );
195
196         return file.generate();
197     }
198     
199     /**
200      * Send generator output buffer to the client
201      *
202      * @param fob flash data to send
203      * @param res response to send to
204      * @exception ServletException
205      * @exception IOException
206      */

207     protected void send( FlashOutput fob, HttpServletResponse JavaDoc res )
208         throws ServletException JavaDoc, IOException JavaDoc
209     {
210         res.setContentLength( fob.getSize() );
211         res.setContentType( "application/x-shockwave-flash" );
212
213         ServletOutputStream JavaDoc sos = res.getOutputStream();
214
215         sos.write( fob.getBuf(), 0, fob.getSize() );
216     }
217 }
218
Popular Tags