KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > taglib > common > CropTextTag


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.taglib.common;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26
27 import org.infoglue.deliver.taglib.AbstractTag;
28
29 /**
30  * This tag will crop a text sent in
31  */

32
33 public class CropTextTag extends AbstractTag
34 {
35     /**
36      * The universal version identifier.
37      */

38     private static final long serialVersionUID = 8603406098980150888L;
39     
40     /**
41      * The original text.
42      */

43     private String JavaDoc text;
44
45     /**
46      * The position to start substring-method on - default from the first position.
47      */

48     private int startIndex = 0;
49
50     /**
51      * The maxlength of the modified text.
52      */

53     private int maxLength = -1;
54
55     /**
56      * The text indicating that there is more text
57      */

58     private String JavaDoc suffix = "...";
59
60     /**
61      * States whether or not to account for entities like ä etc.
62      */

63     private boolean adjustForEntities = true;
64     
65     /**
66      * Default constructor.
67      */

68     public CropTextTag()
69     {
70         super();
71     }
72     
73     /**
74      * Process the end tag. Crops the text and adds the suffix.
75      *
76      * @return indication of whether to continue evaluating the JSP page.
77      * @throws JspException if an error occurred while processing this tag.
78      */

79     public int doEndTag() throws JspException JavaDoc
80     {
81         if(adjustForEntities)
82         {
83             int startEntity = text.indexOf("&");
84             while(startEntity > -1 && startEntity < maxLength)
85             {
86                 int endEntity = text.indexOf(";", startEntity);
87                 if(endEntity != -1)
88                 {
89                     maxLength = maxLength + (endEntity - startEntity);
90                 }
91                 if(endEntity != -1)
92                     startEntity = text.indexOf("&", endEntity);
93                 else
94                     startEntity = text.indexOf("&", startEntity + 1);
95             }
96         }
97         
98         String JavaDoc modifiedText = text;
99         if(maxLength > -1)
100         {
101             if(text.length() > (startIndex + maxLength))
102                 modifiedText = text.substring(startIndex, maxLength);
103             else
104                 modifiedText = text.substring(startIndex);
105         }
106         else
107         {
108             modifiedText = text.substring(startIndex);
109         }
110         
111         modifiedText += suffix;
112         
113         setResultAttribute(modifiedText);
114         
115         return EVAL_PAGE;
116     }
117
118     public void setMaxLength(String JavaDoc maxLength) throws JspException JavaDoc
119     {
120         this.maxLength = evaluateInteger("cropText", "maxLength", maxLength).intValue();
121     }
122     
123     public void setStartIndex(String JavaDoc startIndex) throws JspException JavaDoc
124     {
125         this.startIndex = evaluateInteger("cropText", "startIndex", startIndex).intValue();
126     }
127     
128     public void setSuffix(String JavaDoc suffix) throws JspException JavaDoc
129     {
130         this.suffix = evaluateString("cropText", "suffix", suffix);
131     }
132     
133     public void setText(String JavaDoc text) throws JspException JavaDoc
134     {
135         this.text = evaluateString("cropText", "text", text);
136     }
137     
138     public void setAdjustForEntities(boolean adjustForEntities)
139     {
140         this.adjustForEntities = adjustForEntities;
141     }
142 }
143
Popular Tags