Re: compress html templates when read from disk

2007-03-20 Thread yoursoft

Dear Developers,

The first version is my class is available.
It is a little performance tweak, because this read from disk only, 
when original template is changed, and make a cached compressed copy of it.
With compressed input, the memory use is lower, velocity variables 
insertion is faster and bandwidth usage is also lower.

Any comments are welcome :-)

Best Regards:
   Ferenc Lutischán

package com.ys.velocity;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* License); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.io.UnicodeInputStream;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.util.StringUtils;

/**
* A loader for templates stored on the file system.  Treats the template
* as relative to the configured root path.  If the root path is empty
* treats the template name as an absolute path.
*
* @author a href=mailto:[EMAIL PROTECTED]Lutischan Ferenc/a
*/
public class HTMLCompressFileResourceLoader extends ResourceLoader {
   /**
* The paths to search for templates.
*/
   private List paths = new ArrayList();
   private static final String ALPHA = 
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$\\;

   private static final String SPECIAL = {};=*/+-%()[]+(char) 10;
   private static final String END_LINE = {};=*/+-%([+(char) 10;

   /**
* Used to map the path that a template was found on
* so that we can properly check the modification
* times of the files. This is synchronizedMap
* instance.
*/
   private Map templatePaths = Collections.synchronizedMap(new HashMap());

   /** Shall we inspect unicode files to see what encoding they 
contain?. */

   private boolean unicode = false;

   /**
* @see 
org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)

*/
   public void init( ExtendedProperties configuration) {
   if (log.isTraceEnabled()) {
   log.trace(HTMLCompressFileResourceLoader : initialization 
starting.);

   }

   paths.addAll( configuration.getVector(path) );

   // unicode files may have a BOM marker at the start, but Java
   // has problems recognizing the UTF-8 bom. Enabling unicode will
   // recognize all unicode boms.
   unicode = configuration.getBoolean(unicode, false);

   if (log.isDebugEnabled()) {
   log.debug(Do unicode file recognition:   + unicode);
   }

   // trim spaces from all paths
   StringUtils.trimStrings(paths);
   if (log.isInfoEnabled()) {
   // this section lets tell people what paths we will be using
   int sz = paths.size();
   for( int i=0; i  sz; i++) {
   log.info(HTMLCompressFileResourceLoader : adding path 
' + (String) paths.get(i) + ');

   }
   log.trace(HTMLCompressFileResourceLoader : initialization 
complete.);

   }
   }

   /**
* Get an InputStream so that the Runtime can build a
* template with it.
*
* @param templateName name of template to get
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found
* in the file template path.
*/
   public InputStream getResourceStream(String templateName)
   throws ResourceNotFoundException {
   /*
* Make sure we have a valid templateName.
*/
   if (org.apache.commons.lang.StringUtils.isEmpty(templateName)) {
   /*
* If we don't get a properly formed templateName then
* there's not much we can do. So we'll forget about
* trying to search any more paths for the template.
*/
   throw new ResourceNotFoundException(
   

Re: compress html templates when read from disk

2007-03-20 Thread yoursoft

Christopher Schultz wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

yoursoft,

yoursoft wrote:
  

Dear Developers,

The first version is my class is available.
It is a little performance tweak, because this read from disk only,
when original template is changed, and make a cached compressed copy of it.



Just to recap... this thing normalizes spaces in a template, right?

  

Yes, it is normalizes white-spaces in the templates.

With compressed input, the memory use is lower, velocity variables
insertion is faster and bandwidth usage is also lower.



Can you quantify these claims? For instance, how much less memory (and
bandwidth) is really used given a relatively standard template?

I wrote that: it is a little performance tweak.
Whitespace removal is depends how to format your templates. In most case 
it is compress your html code to 50%-75%.

Memory usage is more complex thing:
Depends on format of you html code and the count of templates in memory.


 How can
Velocity variable insertion be faster?

  
I think, when Velocity replace a $variable in a template, velocity will 
be faster when a template is smaller.



It also looks like you implemented many methods (such as init,
getResourceStream, isSourceModified, and getLastModified) that might be
better implemented elsewhere. For instance, since your implementation of
ResourceLoader appears to be file-based, why not extend
FileResourceLoader and then just modify the behavior of
getResourceStream like this:

public InputStream getResourceStream(String templateName)
throws ResourceNotFoundException
{
 InputStream in = super.getResourceStream(templateName);

 // now perform your filtering
}

This would make a much cleaner class, IMO.

Yes this is a good idea :-)

It would also make this into
more of a filter, which makes more sense me, personally. Instead of
essentially copying the file and then reading /that/ into memory, why
not just filter the input stream on the fly?

  
In your solution, when you e.g. restart Tomcat, and Velocity reload a 
template, every time need to compress it. In my solution if the 
compressed file date is not smaller than original template date, in this 
case isn't need to recompress it, only reload from compressed file. But 
your solution is an other way, that may be better. My first think was 
same with your think. :-)

You could also write your class such that instead of extending a single
class (such as FileResourceLoader), you could wrap /any/ type of
resource loader, and pass the source loader as an argument to a
constructor. Your no-arg constructor could simply use a
FileResourceLoader as the default.

Then, all your methods would be delegates to the real loader except
for getResourceStream, which would perform the filtering.

  
This is good idea, but is not my target. My target only a file resource 
(html file) loader, that compress my input templates. :-)

- -chris
  

Thanks for suggestions,

Regards,
   Ferenc

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



compress html templates when read from disk

2007-03-09 Thread yoursoft

Dear Developers,

I would like to write a plugin to velocity.
This plugin will compress html templates when reading from disk 
(remove white-spaces,  comments etc.), not when Velocity is write to 
output, because my cache modified check time is one hour. :-)


Can you suggest any entry point where I made it? (In 
ContentResource.java at setData(sw.toString())???)


Best Regards,
   Ferenc Lutischan

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