001    package hirondelle.web4jtools.util;
002    
003    import java.io.IOException;
004    import javax.servlet.jsp.JspException;
005    import java.util.Scanner;
006    import hirondelle.web4j.ui.tag.TagHelper;
007    import hirondelle.web4j.util.Util;
008    
009    /**
010    * Remove all empty lines in the body of the tag. 
011    * 
012    * <P>In addition, in JSP 2.1, you may remove empty lines <em>associated with directives</em> 
013    * by using the following directive at the top of your JSP :
014    *  
015    * <P><tt>&lt;%@page trimDirectiveWhitespaces="true"%&gt;</tt>
016    */
017    public final class NoEmptyLinesTag extends TagHelper {
018    
019      @Override protected String getEmittedText(String aBody) throws JspException, IOException {
020        StringBuilder result = new StringBuilder();
021        
022        Scanner scanner = new Scanner(aBody);
023        while ( scanner.hasNextLine() ) {
024          addNonEmptyLineToResult(scanner.nextLine(), result);
025        }
026        scanner.close();
027        
028        return result.toString();
029      }
030      
031      // PRIVATE //
032      private void addNonEmptyLineToResult(String aLine, StringBuilder aResult){
033        if( Util.textHasContent(aLine) ) {
034          aResult.append(aLine);
035        }
036      }
037    }