geirm       01/04/22 11:11:23

  Modified:    src/java/org/apache/velocity/runtime/resource
                        ContentResource.java Resource.java
                        ResourceManager.java
  Log:
  Medium-term changes to support per-template encoding.  Main motivation is the
  'app pack' (or whatever they call it) in Turbine.  Now that there is a customer
  for it (Turbine) it's good that we do it.
  
  I think an iteration of change might be good, rearchitecting this a little.
  Some rework might be in order to change some aspects of stream management for 
resources.
  
  Resource.java : added encoding as class member, with set/get accessors.
  ContentResource.java : added inclusion of the encoding for reading the
  intput stream.
  ResourceManager.java : need to support encoding in getResource(), as the RM
  returns a Resource read to go...
  
  Revision  Changes    Path
  1.4       +4 -3      
jakarta-velocity/src/java/org/apache/velocity/runtime/resource/ContentResource.java
  
  Index: ContentResource.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/ContentResource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ContentResource.java      2001/03/05 11:46:42     1.3
  +++ ContentResource.java      2001/04/22 18:11:20     1.4
  @@ -66,7 +66,8 @@
    * sources.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
  - * @version $Id: ContentResource.java,v 1.3 2001/03/05 11:46:42 jvanzyl Exp $
  + * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  + * @version $Id: ContentResource.java,v 1.4 2001/04/22 18:11:20 geirm Exp $
    */
   public class ContentResource extends Resource
   {
  @@ -83,7 +84,7 @@
               StringWriter sw = new StringWriter();
               
               BufferedReader reader = new BufferedReader(
  -                new InputStreamReader(resourceLoader.getResourceStream(name)));
  +                new InputStreamReader(resourceLoader.getResourceStream(name), 
encoding));
               
               char buf[] = new char[1024];
               int len = 0;
  @@ -92,7 +93,7 @@
                   sw.write( buf, 0, len );
           
               data = sw.toString();
  -            
  +           
               return true;
           }
           catch ( Exception e ) 
  
  
  
  1.5       +35 -4     
jakarta-velocity/src/java/org/apache/velocity/runtime/resource/Resource.java
  
  Index: Resource.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/Resource.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Resource.java     2001/03/05 11:46:44     1.4
  +++ Resource.java     2001/04/22 18:11:20     1.5
  @@ -72,7 +72,8 @@
    * sources.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
  - * @version $Id: Resource.java,v 1.4 2001/03/05 11:46:44 jvanzyl Exp $
  + * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  + * @version $Id: Resource.java,v 1.5 2001/04/22 18:11:20 geirm Exp $
    */
   public abstract class Resource
   {
  @@ -111,13 +112,24 @@
        */
       protected long nextCheck = 0;
   
  -    /** Resource name */
  +    /**
  +     *  Name of the resource
  +     */
       protected String name;
  +
  +    /**
  +     *  Character encoding of this resource
  +     */
  +    protected String encoding = Runtime.ENCODING_DEFAULT;
   
  -    /** Resource might require ancillary storage of some kind */
  +    /** 
  +     *  Resource might require ancillary storage of some kind 
  +     */
       protected Object data = null;
   
  -    /** Default constructor */
  +    /** 
  +     *  Default constructor 
  +     */
       public Resource()
       {
       }
  @@ -202,6 +214,25 @@
       {
           return name;
       }        
  +
  +    /**
  +     *  set the encoding of this resource
  +     *  for example, "ISO-8859-1"
  +     */
  +    public void setEncoding( String encoding )
  +    {
  +        this.encoding = encoding;
  +    }
  +
  +    /**
  +     *  get the encoding of this resource
  +     *  for example, "ISO-8859-1"
  +     */
  +    public String getEncoding()
  +    {
  +        return encoding;
  +    }
  +
   
       /**
        * Return the lastModifed time of this
  
  
  
  1.24      +29 -3     
jakarta-velocity/src/java/org/apache/velocity/runtime/resource/ResourceManager.java
  
  Index: ResourceManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/ResourceManager.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- ResourceManager.java      2001/04/18 20:29:24     1.23
  +++ ResourceManager.java      2001/04/22 18:11:21     1.24
  @@ -78,7 +78,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Paulo Gaspar</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  - * @version $Id: ResourceManager.java,v 1.23 2001/04/18 20:29:24 geirm Exp $
  + * @version $Id: ResourceManager.java,v 1.24 2001/04/22 18:11:21 geirm Exp $
    */
   public class ResourceManager
   {
  @@ -215,7 +215,7 @@
        *          to syntax (or other) error.
        * @throws Exception if a problem in parse
        */
  -    public static Resource getResource(String resourceName, int resourceType)
  +    public static Resource getResource(String resourceName, int resourceType, 
String encoding )
           throws ResourceNotFoundException, ParseErrorException, Exception
       {        
           /* 
  @@ -247,6 +247,21 @@
   
                   if(  resource.isSourceModified() )
                   {
  +                    /*
  +                     *  now check encoding info.  It's possible that the newly 
declared
  +                     *  encoding is different than the encoding already in the 
resource
  +                     *  this strikes me as bad...
  +                     */
  +                    
  +                    if (!resource.getEncoding().equals( encoding ) )
  +                    {
  +                        Runtime.error("Declared encoding for template '" + 
resourceName 
  +                                      + "' is different on reload.  Old = '" + 
resource.getEncoding()
  +                                      + "'  New = '" + encoding );
  +
  +                        resource.setEncoding( encoding );
  +                    }
  +
                       try
                       {
                           /*
  @@ -303,7 +318,8 @@
               try
               {
                   resource = ResourceFactory.getResource(resourceName, resourceType);
  -                resource.setName(resourceName);
  +                resource.setName( resourceName );
  +                resource.setEncoding( encoding );
                   
                   /* 
                    * Now we have to try to find the appropriate
  @@ -408,4 +424,14 @@
           }
           return resource;
       }
  +
  +    /**
  +     * @deprecated
  +     */
  +    public static Resource getResource(String resourceName, int resourceType  )
  +        throws ResourceNotFoundException, ParseErrorException, Exception
  +    {  
  +        return getResource( resourceName, resourceType, Runtime.ENCODING_DEFAULT);
  +    }
  +
   }
  
  
  

Reply via email to