KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > taglib > FormatDescriptionTag


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 cowsultants.itracker.web.taglib;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26 import javax.servlet.jsp.*;
27 import javax.servlet.jsp.tagext.*;
28
29 import org.apache.struts.util.*;
30
31 import cowsultants.itracker.ejb.client.resources.*;
32 import cowsultants.itracker.ejb.client.util.*;
33 import cowsultants.itracker.web.util.*;
34
35 public class FormatDescriptionTag extends BodyTagSupport {
36     private String JavaDoc text = null;
37
38     private String JavaDoc truncateKey = "itracker.web.generic.truncated";
39     private int truncateLength = 40;
40
41     public String JavaDoc getTruncateKey() {
42         return truncateKey;
43     }
44
45     public void setTruncateKey(String JavaDoc value) {
46         truncateKey = value;
47     }
48
49     public int getTruncateLength() {
50         return truncateLength;
51     }
52
53     public void setTruncateLength(int value) {
54         truncateLength = value;
55     }
56
57     public int doStartTag() throws JspException {
58         text = null;
59         return EVAL_BODY_BUFFERED;
60     }
61
62     public int doAfterBody() throws JspException {
63         if(bodyContent != null) {
64             String JavaDoc value = bodyContent.getString().trim();
65             if(value.length() > 0) {
66                 text = value;
67             }
68         }
69         return SKIP_BODY;
70     }
71
72     public int doEndTag() throws JspException {
73         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
74         if(text != null) {
75             Locale currLocale = null;
76
77             HttpSession session = pageContext.getSession();
78             if(session != null) {
79                 currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
80             }
81
82             if(text.length() > truncateLength) {
83                 String JavaDoc truncateValue = ITrackerResources.getString(truncateKey, currLocale);
84                 if(truncateValue == null) {
85                     truncateValue = "";
86                 }
87                 results.append(text.substring(0, (truncateLength - truncateValue.length())));
88                 results.append(truncateValue);
89             } else {
90                 results.append(text);
91             }
92         }
93         ResponseUtils.write(pageContext, results.toString());
94         clearState();
95         return (EVAL_PAGE);
96     }
97
98     public void release() {
99         super.release();
100         clearState();
101     }
102
103     private void clearState() {
104         truncateKey = "itracker.web.generic.truncated";
105         truncateLength = 40;
106         text = null;
107     }
108 }
109
Popular Tags