freemarker.ext.misc
Class RemoveBlankLines

java.lang.Object
  extended by freemarker.ext.misc.RemoveBlankLines
All Implemented Interfaces:
TemplateModel, TemplateTransformModel2, java.io.Serializable

public final class RemoveBlankLines
extends java.lang.Object
implements TemplateTransformModel2, java.io.Serializable

A transform model that removes all blank lines. In addition, trailing whitespace is also deleted. Thus, for a line containing only whitespace, the entire line will be removed. Finally, the last line will not have a linefeed at the end of it

Newlines are detected by the BufferedReader.readLine() method, and are rewritten using the platform default (using the System.getProperty() method).

Usage:
From java:

 TemplateModelRoot root = new SimpleHash();

 root.put( "removeBlankLines", freemarker.ext.misc.RemoveBlankLines.getInstance() );

 ...
 

From your FM-Classic template:

 The following is has empty lines removed:
 <transform removeBlankLines>
   <ul>
   <foreach item in listItems>
     <li>${item}</li>
   <foreach>
   </ul>
 </transform>

 ...
 

Note:
RemoveBlankLines is a singleton instance. Use the getInstance() method to retrieve instances of this model.

Since:
1.8.1
Version:
$Id: RemoveBlankLines.java 1101 2013-04-01 04:17:32Z run2000 $
Author:
Nicholas Cull
See Also:
Serialized Form

Method Summary
static RemoveBlankLines getInstance()
          Retrieve a singleton instance of this class.
 boolean isEmpty()
          Is the object empty?
 void transform(java.io.Reader source, java.io.Writer output)
          Strips all blank lines from the source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static RemoveBlankLines getInstance()
Retrieve a singleton instance of this class. Since there is no state information held between calls, there is only a need for one instance.

Returns:
an instance of this RemoveBlankLines class

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Is the object empty?

Specified by:
isEmpty in interface TemplateModel
Returns:
false to indicate that this model is non-empty
Throws:
TemplateModelException

transform

public void transform(java.io.Reader source,
                      java.io.Writer output)
               throws java.io.IOException,
                      TemplateModelException
Strips all blank lines from the source. In addition, removes any trailing spaces from each line. As a consequence, if any line consists only of whitespace, then all whitespace will be removed, and the line will be stripped.

Specified by:
transform in interface TemplateTransformModel2
Parameters:
source - the input to be transformed
output - the destination of the transformation
Throws:
java.io.IOException
TemplateModelException