001    package hirondelle.web4jtools.util;
002    
003    import java.io.IOException;
004    import javax.servlet.jsp.JspException;
005    import hirondelle.web4j.ui.tag.TagHelper;
006    import hirondelle.web4j.util.Util;
007    import static hirondelle.web4j.util.Consts.NOT_FOUND;
008    
009    /**
010    * Trim the final comma appearing in text, along with any trailing characters appearing after 
011    * the final comma.
012    * 
013    * <P>This tag is used in code generation of lists of items separated by a comma.
014    * It is almost always necessary to remove the final comma from such lists. 
015    */
016    public final class LastCommaTag extends TagHelper {
017    
018      @Override protected String getEmittedText(String aText) throws JspException, IOException {
019        String result = aText;
020        if (  Util.textHasContent(aText) ) {
021          int finalCommaIdx = aText.lastIndexOf(COMMA);
022          if ( isFound(finalCommaIdx) ){
023            result = removeFinalComma(aText, finalCommaIdx);
024          }
025        }
026        return result;
027      }
028      
029      // PRIVATE //
030      private static final String COMMA = ",";
031      
032      private boolean isFound(int aFinalComma){
033        return aFinalComma != NOT_FOUND;
034      }
035      
036      private String removeFinalComma(String aText, int aFinalCommaIdx){
037        return aText.substring(0, aFinalCommaIdx);
038      }
039    }