View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.taglib;
20  
21  import org.apache.struts.taglib.TagUtils;
22  import org.itracker.core.resources.ITrackerResources;
23  import org.itracker.web.util.LoginUtilities;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.tagext.BodyTagSupport;
28  
29  
30  /**
31   * Truncate a string if it's longer than truncateLength
32   *
33   * @author ranks@rosa.com
34   */
35  public class FormatDescriptionTag extends BodyTagSupport {
36      /**
37       *
38       */
39      private static final long serialVersionUID = 1L;
40  
41      private String text = null;
42  
43      private String truncateKey = "itracker.web.generic.truncated";
44      private String truncateText = null;
45      private int truncateLength = 40;
46  
47      public String getTruncateKey() {
48          return truncateKey;
49      }
50  
51      public void setTruncateKey(String value) {
52          truncateKey = value;
53      }
54  
55      public int getTruncateLength() {
56          return truncateLength;
57      }
58  
59      public void setTruncateLength(int value) {
60          truncateLength = value;
61      }
62  
63      public int doStartTag() throws JspException {
64          text = null;
65          return EVAL_BODY_BUFFERED;
66      }
67  
68      public int doAfterBody() throws JspException {
69          if (bodyContent != null) {
70              String value = bodyContent.getString().trim();
71              if (value.length() > 0) {
72                  text = value;
73              }
74          }
75          return SKIP_BODY;
76      }
77  
78      public String getTruncateText() {
79          if (null == truncateText) {
80              if (pageContext.getRequest() instanceof HttpServletRequest) {
81                  truncateText = ITrackerResources.getString(truncateKey, LoginUtilities.getCurrentLocale((HttpServletRequest) this.pageContext.getRequest()));
82              } else {
83                  truncateText = ITrackerResources.getString(truncateKey);
84              }
85              if (null != truncateText) {
86                  truncateText = truncateText.trim();
87              } else {
88                  truncateText = "";
89              }
90          }
91          return truncateText;
92      }
93  
94      public int doEndTag() throws JspException {
95          StringBuffer results = new StringBuffer();
96          if (text != null && text.trim().length() > truncateLength - getTruncateText().length()) {
97              results.append(text.trim().substring(0, truncateLength - getTruncateText().length()).trim());
98              results.append(getTruncateText());
99          } else if (null == text) {
100             results.append("");
101         } else {
102             results.append(text.trim());
103         }
104         TagUtils.getInstance().write(pageContext, results.toString());
105         clearState();
106         return (EVAL_PAGE);
107     }
108 
109     public void release() {
110         super.release();
111         clearState();
112     }
113 
114     private void clearState() {
115         truncateKey = "itracker.web.generic.truncated";
116         truncateLength = 40;
117         text = null;
118         truncateText = null;
119     }
120 }