KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > tags > RestrictTag


1 package com.sslexplorer.input.tags;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.apache.struts.taglib.TagUtils;
8 import org.apache.struts.taglib.html.BaseFieldTag;
9
10 import com.sslexplorer.boot.Util;
11
12
13 /**
14  * Restrict the width of some plain text, breaking it up into lines
15  * separated by <br> tags.
16  *
17  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
18  */

19 public class RestrictTag extends BaseFieldTag {
20
21     final static Log log = LogFactory.getLog(FrameTag.class);
22
23     // Protected instance variables
24

25     protected int width;
26
27
28     /**
29      * Set the width to restrict to
30      *
31      * @param width width
32      */

33     public void setWidth(int width) {
34         this.width = width;
35     }
36
37     /*
38      * (non-Javadoc)
39      *
40      * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
41      */

42     public int doEndTag() throws JspException JavaDoc {
43         TagUtils.getInstance().write(pageContext, generateFragment());
44         return (EVAL_PAGE);
45     }
46
47     /*
48      * (non-Javadoc)
49      *
50      * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
51      */

52     public void release() {
53         super.release();
54         width = 40;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
61      */

62     public int doStartTag() throws JspException JavaDoc {
63         return EVAL_BODY_BUFFERED;
64     }
65
66     String JavaDoc generateFragment() throws JspException JavaDoc {
67         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
68         String JavaDoc content = Util.trimBoth(bodyContent.getString());
69         while(true) {
70             // If content left is less than width then just append and finish
71
if(content.length() <= width) {
72                 buf.append(content);
73                 break;
74             }
75             else {
76                 // Find the last break character
77

78                 char[] chars = { '\n', ' ', '-', '.', ',' };
79                 int sidx = -1;
80                 for(int i = 0 ; i < chars.length ; i++) {
81                     sidx = Math.max(content.lastIndexOf(chars[i], width - 1), sidx);
82                 }
83                 
84                 // If no break char then just break at the full width
85
if(sidx == -1) {
86                     buf.append(content.substring(0, width));
87                     buf.append("<br/>");
88                     content = content.substring(width);
89                 }
90                 else {
91                     // Other break at the break character
92
buf.append(content.substring(0, sidx));
93                     buf.append("<br/>");
94                     content = Util.trimBoth(content.substring(sidx));
95                 }
96             }
97         }
98         return buf.toString();
99     }
100 }
101
Popular Tags