You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Chuck Riess <cr...@yahoo-inc.com> on 2004/02/05 23:32:29 UTC

Code to remove whitespaces

Hello,

I've written this class to remove leading whitespace and blank lines from the output of a Velocity template.

The class extends Writer, so you can easily modify your rendering code:

     Writer out = new GobbleWriter (
        new BufferedWriter (
          new FileWriter ("outfile"));
      
      Velocity.mergeTemplate (getTemplateName (), getContext (), out);

We use Velocity templates to generate HTML pages for our Sports website and this class has reduced our HTML output by 25% dropping one of our basketball scoreboard pages from 140K to 108K.

On the downside, this class examines every character in the stream and is significantly slower than using BufferedWriter (which uses System.arrayCopy).

Suggestions for improvments or replacements are welcome.

-- Chuck (criess@yahoo-inc.com)

/**
 * This writer class will remove leading whitespaces from each line
 * in the stream provided by the given writer class.  Lines that contain
 * only whitespace are also removed.
 *
 * <pre>
 * Example:
 *
 * Writer out = new GobbleWriter (
 *                new BufferedWriter (
 *                  new FileWriter ("outfile")));
 * </pre>
 *
 * @see Writer
 *
 * @author      Chuck Riess
 */

import java.io.*;

public class GobbleWriter extends Writer {

  private final static int TRIM_STATE = 1;
  private final static int WRITE_STATE = 2;

  int state;
  Writer out;

  public GobbleWriter (Writer out) {
    this.out = out;
    this.state = TRIM_STATE;
  }

  private boolean isWritable (char c) {

    boolean retval;

    // Remove whitespace state

    if (state == TRIM_STATE) {
      if (Character.isWhitespace (c)) {
        retval = false;
        state = TRIM_STATE;
      }
      else {
        retval = true;
        state = WRITE_STATE;
      }
    }

    // Print characters state

    else {
      if (Character.isSpaceChar (c)) {
        retval = true;
        state = WRITE_STATE;
      }
      else if (Character.isWhitespace (c)) {
        retval = true;
        state = TRIM_STATE;
      }
      else {
        retval = true;
        state = WRITE_STATE;
      }
    }

    return retval;

  }

  public void write (int c) throws IOException {

    synchronized (lock) {
      if (isWritable ((char) c)) {
        out.write (c);
      }
    }
  }

  public void write (char cbuf[], int off, int len) throws IOException {

    synchronized (lock) {
      for (int i = off; i < len; i++) {
        char c = cbuf[i];
        if (isWritable (c)) {
          out.write (c);
        }
      }
    }
  }

  public void write (String s, int off, int len) throws IOException {

    synchronized (lock) {
      char cbuf[] = new char[len];
      s.getChars (off, off + len, cbuf, 0);

      for (int i = 0; i < len; i++) {
        char c = cbuf[i];
        if (isWritable (c)) {
          out.write (c);
        }
      }
    }
  }

  public void close () throws IOException {
    out.close ();
  }

  public void flush () throws IOException {
    out.flush ();
  }

}

Re: Code to remove whitespaces

Posted by Andy Lee <ag...@earthlink.net>.
On Feb 6, 2004, at 11:45 AM, J. B. Rainsberger wrote:
> Two words: Pair Programming.
>

I prefer Au Pair Programming, myself, but that's for a different 
mailing list.

--Andy


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Code to remove whitespaces

Posted by "J. B. Rainsberger" <jb...@rogers.com>.
Claude Brisson wrote:

> lol
> 
> "- Hey, dudes, why couldn't we attach big feather structures to our arms so that if we flap them very hard we'd be able to sustain
> ouselves in the air ?
>  - Shut up and fasten your seatbelt, we're gonna take off !"
> 
> (I'm making fun, but it could have happen to me)

Two words: Pair Programming.
-- 
J. B. Rainsberger,
Diaspar Software Services
http://www.diasparsoftware.com :: +1 416 791-8603
Let's write software that people understand

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Code to remove whitespaces

Posted by Claude Brisson <cl...@savoirweb.com>.
lol

"- Hey, dudes, why couldn't we attach big feather structures to our arms so that if we flap them very hard we'd be able to sustain
ouselves in the air ?
 - Shut up and fasten your seatbelt, we're gonna take off !"

(I'm making fun, but it could have happen to me)

CloD

----- Original Message ----- 
From: "Chuck Riess" <cr...@yahoo-inc.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Friday, February 06, 2004 3:16 AM
Subject: Re: Code to remove whitespaces


> J.B.,
>
>
> > A suggestion: why not just serve up the pages using the application/gzip
> > content type, then? If the concern is to send less over the wire, then
> > this does the trick nicely without much extra work.
> >
> > Are the pages really that slow to render? I'm just asking.
>
> Fantastic suggestion.  We already include the gzip content type when we
> serve our pages (which I promptly forgot) so our original scoreboard page
> that was 140K is gzipped down to around 9K.
>
> So, I'm not sure it's worth using the GobbleWriter.
>
> Oh well it was fun to write.
>
> Thanks
>
> -- Chuck
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Code to remove whitespaces

Posted by "J. B. Rainsberger" <jb...@rogers.com>.
Chuck Riess wrote:

> J.B.,
> 
>>A suggestion: why not just serve up the pages using the application/gzip
>>content type, then? If the concern is to send less over the wire, then
>>this does the trick nicely without much extra work.
>>
>>Are the pages really that slow to render? I'm just asking.
> 
> Fantastic suggestion.  We already include the gzip content type when we
> serve our pages (which I promptly forgot) so our original scoreboard page
> that was 140K is gzipped down to around 9K.
> 
> So, I'm not sure it's worth using the GobbleWriter.

Sometimes you just need another pair of eyes.

> Oh well it was fun to write.

That's important. Sometimes we forget that. :)

Take care.
-- 
J. B. Rainsberger,
Diaspar Software Services
http://www.diasparsoftware.com :: +1 416 791-8603
Let's write software that people understand

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Code to remove whitespaces

Posted by Chuck Riess <cr...@yahoo-inc.com>.
J.B.,


> A suggestion: why not just serve up the pages using the application/gzip
> content type, then? If the concern is to send less over the wire, then
> this does the trick nicely without much extra work.
>
> Are the pages really that slow to render? I'm just asking.

Fantastic suggestion.  We already include the gzip content type when we
serve our pages (which I promptly forgot) so our original scoreboard page
that was 140K is gzipped down to around 9K.

So, I'm not sure it's worth using the GobbleWriter.

Oh well it was fun to write.

Thanks

-- Chuck


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Code to remove whitespaces

Posted by "J. B. Rainsberger" <jb...@rogers.com>.
Chuck Riess wrote:

> We use Velocity templates to generate HTML pages for our Sports website and this class has reduced our HTML output by 25% dropping one of our basketball scoreboard pages from 140K to 108K.

A suggestion: why not just serve up the pages using the application/gzip 
content type, then? If the concern is to send less over the wire, then 
this does the trick nicely without much extra work.

Are the pages really that slow to render? I'm just asking.
-- 
J. B. Rainsberger,
Diaspar Software Services
http://www.diasparsoftware.com :: +1 416 791-8603
Let's write software that people understand

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org