dlr         01/07/23 20:01:13

  Modified:    src/java/org/apache/turbine/services/velocity
                        TurbineVelocityService.java VelocityService.java
  Log:
  Decoupled from Turbine proper (specifically, removed references
  to RunData).  Added methods to interface which accept character set and encoding 
parameters.
  
  Jason, note the TODO item in the TurbineVelocityService implementation that I was 
not sure
  what to do with.  That code probably needs to be moved somewhere, but I do not know 
where.
  
  Revision  Changes    Path
  1.58      +70 -73    
jakarta-turbine/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java
  
  Index: TurbineVelocityService.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java,v
  retrieving revision 1.57
  retrieving revision 1.58
  diff -u -u -r1.57 -r1.58
  --- TurbineVelocityService.java       2001/07/24 01:25:51     1.57
  +++ TurbineVelocityService.java       2001/07/24 03:01:13     1.58
  @@ -65,7 +65,6 @@
   import org.apache.velocity.app.Velocity;
   import org.apache.velocity.context.Context;
   import org.apache.velocity.exception.MethodInvocationException;
  -import org.apache.turbine.RunData;
   import org.apache.turbine.services.ServiceException;
   import org.apache.turbine.services.InitializationException;
   import org.apache.turbine.services.template.TurbineTemplate;
  @@ -89,7 +88,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Sean Legassick</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  - * @version $Id: TurbineVelocityService.java,v 1.57 2001/07/24 01:25:51 dlr Exp $
  + * @version $Id: TurbineVelocityService.java,v 1.58 2001/07/24 03:01:13 dlr Exp $
    */
   public class TurbineVelocityService 
       extends BaseTemplateEngineService
  @@ -102,8 +101,7 @@
         ".resource.loader.path";
     
       /**
  -     * Default character encoding to use if not specified in the
  -     * RunData object.
  +     * Default character encoding to use if not specified by caller.
        */
       private static final String DEFAULT_CHAR_SET = "ISO-8859-1";
        
  @@ -129,32 +127,29 @@
           }
       }
   
  +    /**
  +     * @see org.apache.turbine.services.velocity.VelocityService
  +     */
       public String handleRequest(TemplateContext context, String template)
           throws ServiceException
       {
           return handleRequest(new ContextAdapter(context), template);
       }
   
  -    public void handleRequest(TemplateContext context, 
  -                              String template,
  -                              OutputStream outputStream)
  +    /**
  +     * @see org.apache.turbine.services.velocity.VelocityService
  +     */
  +    public String handleRequest(Context context, String filename)
           throws ServiceException
       {
  -        handleRequest(new ContextAdapter(context), template, outputStream);
  +        return handleRequest(context, filename, null, null);
       }
   
       /**
  -     * Process the request and fill in the template with the values
  -     * you set in the Context.
  -     *
  -     * @param context  The populated context.
  -     * @param filename The file name of the template.
  -     * @return The process template as a String.
  -     *
  -     * @throws ServiceException Any exception trown while processing will be
  -     *         wrapped into a ServiceException and rethrown.
  +     * @see org.apache.turbine.services.velocity.VelocityService
        */
  -    public String handleRequest(Context context, String filename)
  +    public String handleRequest(Context context, String filename,
  +                                String charset, String encoding)
           throws ServiceException
       {
           String results = null;
  @@ -163,10 +158,11 @@
           try
           {
               bytes = new ByteArrayOutputStream();
  -            String charset = decodeRequest(context, filename, bytes);
  +            charset = decodeRequest(context, filename, bytes, charset,
  +                                    encoding);
               results = bytes.toString(charset);
           } 
  -        catch(Exception e) 
  +        catch (Exception e)
           {
               renderingError(filename, e);
           }
  @@ -174,9 +170,12 @@
           {
               try 
               {
  -                if (bytes != null) bytes.close();
  +                if (bytes != null)
  +                {
  +                    bytes.close();
  +                }
               }
  -            catch(IOException ignored)
  +            catch (IOException ignored)
               {
               }
           }
  @@ -184,75 +183,74 @@
       }
   
       /**
  -     * Process the request and fill in the template with the values
  -     * you set in the Context. 
  -     *
  -     * @param context A Context.
  -     * @param filename A String with the filename of the template.
  -     * @param out A OutputStream where we will write the process template as
  -     * a String.
  -     *
  -     * @throws ServiceException Any exception trown while processing will be
  -     *         wrapped into a ServiceException and rethrown.
  +     * @see org.apache.turbine.services.template.TemplateEngineService
        */
  -    public void handleRequest(Context context,
  -                              String filename,
  +    public void handleRequest(TemplateContext context, String template,
  +                              OutputStream outputStream)
  +        throws ServiceException
  +    {
  +        handleRequest(new ContextAdapter(context), template, outputStream);
  +    }
  +
  +    /**
  +     * @see org.apache.turbine.services.velocity.VelocityService
  +     */
  +    public void handleRequest(Context context, String filename,
                                 OutputStream output)
           throws ServiceException
       {
  -        decodeRequest(context, filename, output);
  +        handleRequest(context, filename, output, null, null);
       }
   
       /**
  -     * Process the request and fill in the template with the values
  -     * you set in the Context. Apply the character and template 
  -     * encodings from RunData to the result.
  +     * @see org.apache.turbine.services.velocity.VelocityService
  +     */
  +    public void handleRequest(Context context, String filename,
  +                              OutputStream output, String charset,
  +                              String encoding)
  +        throws ServiceException
  +    {
  +        decodeRequest(context, filename, output, charset, encoding);
  +    }
  +
  +    /**
  +     * Processes the request and fill in the template with the values
  +     * you set in the the supplied Context. Applies the specified
  +     * character and template encodings.
        *
  -     * @param context A Context.
  -     * @param filename A String with the filename of the template.
  -     * @param out A OutputStream where we will write the process template as
  -     * a String.
  -     * @return The character encoding applied to the resulting String. 
  +     * @param context A context to use when evaluating the specified
  +     * template.
  +     * @param filename The file name of the template.
  +     * @param out The stream to which we will write the processed
  +     * template as a String.
  +     * @return The character set applied to the resulting text.
        *
  -     * @throws ServiceException Any exception trown while processing will be
  -     *         wrapped into a ServiceException and rethrown.
  +     * @throws ServiceException Any exception trown while processing
  +     * will be wrapped into a ServiceException and rethrown.
        */
  -    private String decodeRequest(Context context,
  -                                 String filename,
  -                                 OutputStream output)
  +    private String decodeRequest(Context context, String filename,
  +                                 OutputStream output, String charset,
  +                                 String encoding)
           throws ServiceException
       {
  +        // TODO: Push this method of getting character set & encoding
  +        // from RunData back into Turbine.
  +        // charset  = ((RunData) data).getCharSet();
  +        // encoding = ((RunData) data).getTemplateEncoding();
   
  -        // Get the chracter and template encodings from 
  -        // the RunData object. 
  -        String charset;
  -        String encoding;
  -        
  -        Object data = context.get("data");
  -        
  -        if (data != null && data instanceof RunData)
  +        if (charset == null)
           {
  -            charset = ((RunData) data).getCharSet();
  -            if (charset == null)
  -            {
  -                charset = DEFAULT_CHAR_SET;
  -            }
  -            encoding = ((RunData) data).getTemplateEncoding();
  -        }
  -        else
  -        {
               charset = DEFAULT_CHAR_SET;
  -            encoding = null;
           }
  -          
  +
           OutputStreamWriter writer = null;
  -        
  +
           try
           {
               writer = new OutputStreamWriter(output, charset);
               if (encoding != null)
               {
  -                // Request based encoding is supported in Velocity 1.1.
  +                // Request scoped encoding first supported by Velocity 1.1.
                   Velocity.mergeTemplate(filename, encoding, context, writer);
               }
               else
  @@ -260,7 +258,7 @@
                   Velocity.mergeTemplate(filename, context, writer);
               }
           }
  -        catch(Exception e) 
  +        catch (Exception e)
           {
               renderingError(filename, e);
           }
  @@ -277,7 +275,7 @@
               {
               }
           }
  -        
  +
           return charset;
       }
   
  @@ -297,11 +295,10 @@
           getCategory().error(err + ": " + e.getMessage());
           // if the Exception is a MethodInvocationException, the underlying
           // Exception is likely to be more informative, so rewrap that one.
  -        if ( e instanceof MethodInvocationException ) 
  +        if (e instanceof MethodInvocationException)
           {
               e = ((MethodInvocationException)e).getWrappedThrowable();
           }
  -        
   
           throw new ServiceException(err, e);
       }
  
  
  
  1.21      +56 -17    
jakarta-turbine/src/java/org/apache/turbine/services/velocity/VelocityService.java
  
  Index: VelocityService.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/velocity/VelocityService.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -u -r1.20 -r1.21
  --- VelocityService.java      2001/07/20 21:32:49     1.20
  +++ VelocityService.java      2001/07/24 03:01:13     1.21
  @@ -56,48 +56,87 @@
   
   import java.io.OutputStream;
   import org.apache.turbine.services.Service;
  -import org.apache.turbine.RunData;
   import org.apache.turbine.services.ServiceException;
   import org.apache.velocity.Template;
   import org.apache.velocity.context.Context;
   
   /**
  - * Implementations of the VelocityService interface.
  + * The Turbine service interface to
  + * <a href="http://jakarta.apache.org/velocity/";>Velocity</a>.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>John D. McNally</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Dave Bryson</a>
  - * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
  - * @version $Id: VelocityService.java,v 1.20 2001/07/20 21:32:49 jon Exp $
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  + * @version $Id: VelocityService.java,v 1.21 2001/07/24 03:01:13 dlr Exp $
    */
   public interface VelocityService extends Service
   {
       public static final String SERVICE_NAME = "VelocityService";
   
       /**
  -     * Process the request and fill in the template with the values
  -     * you set in the Context.
  +     * Process the request and fill in the template using the values
  +     * set in <code>context</code>.
        *
  -     * @param context A Context.
  -     * @param template A String with the filename of the template.
  +     * @param context A context to use when evaluating the specified
  +     * template.
  +     * @param filename The file name of the template.
        * @return The process template as a String.
        * @exception Exception, a generic exception.
        */
  -    public String handleRequest(Context context, String template)
  +    public String handleRequest(Context context, String filename)
           throws Exception;
   
       /**
  -     * Process the request and fill in the template with the values
  -     * you set in the Context. 
  +     * Process the request and fill in the template using the values
  +     * set in <code>context</code>.
        *
  -     * @param context A Context.
  -     * @param filename A String with the filename of the template.
  -     * @param out A OutputStream where we will write the process template as
  -     *        a String.
  +     * @param context A context to use when evaluating the specified
  +     * template.
  +     * @param filename The file name of the template.
  +     * @param charset The character set to use when writing the result.
  +     * @param encoding The encoding to use when merging context and
  +     * template.
  +     * @return The process template as a String.
  +     * @exception Exception, a generic exception.
  +     */
  +    public String handleRequest(Context context, String template,
  +                                String charset, String encoding)
  +        throws Exception;
  +
  +    /**
  +     * Process the request and fill in the template using the values
  +     * set in <code>context</code>.
  +     *
  +     * @param context A context to use when evaluating the specified
  +     * template.
  +     * @param filename The file name of the template.
  +     * @param out The stream to which we will write the processed
  +     * template as a String.
        * @throws ServiceException Any exception trown while processing will be
        *         wrapped into a ServiceException and rethrown.
        */
  -    public void handleRequest(Context context,
  -                              String filename,
  +    public void handleRequest(Context context, String filename,
                                 OutputStream out)
  +        throws ServiceException;
  +
  +    /**
  +     * Process the request and fill in the template using the values
  +     * set in <code>context</code>.
  +     *
  +     * @param context A context to use when evaluating the specified
  +     * template.
  +     * @param filename The file name of the template.
  +     * @param out The stream to which we will write the processed
  +     * template as a String.
  +     * @param charset The character set to use when writing the result.
  +     * @param encoding The encoding to use when merging context and
  +     * template.
  +     * @throws ServiceException Any exception trown while processing will be
  +     *         wrapped into a ServiceException and rethrown.
  +     */
  +    public void handleRequest(Context context, String filename,
  +                              OutputStream out, String charset,
  +                              String encoding)
           throws ServiceException;
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to