KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > www > StringLimitTag


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: StringLimitTag.java,v 1.6 2007/01/07 06:14:08 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21   
22 package org.opensubsystems.core.www;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
26 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
27
28 import org.opensubsystems.core.util.GlobalConstants;
29 import org.opensubsystems.core.util.StringUtils;
30
31 /**
32  * Custom tag to intelligently limit the length of a text to certain length.
33  * If the length of the string needs to be limited, it will be cut if possible
34  * on a word boundary and a ... will be appended to the end.
35  *
36  * @version $Id: StringLimitTag.java,v 1.6 2007/01/07 06:14:08 bastafidli Exp $
37  * @author Miro Halas
38  * @code.reviewer Miro Halas
39  * @code.reviewed 1.3 2006/02/17 06:54:09 bastafidli
40  */

41 public class StringLimitTag extends BodyTagSupport JavaDoc
42 {
43    // Attributes ///////////////////////////////////////////////////////////////
44

45    /**
46     * Generated serial version id for this class.
47     */

48    private static final long serialVersionUID = -6436264560155159257L;
49
50    /**
51     * Number representing the length to limit the text present in the body of
52     * the tag to.
53     */

54    protected String JavaDoc m_strLimit;
55
56    // Constructors /////////////////////////////////////////////////////////////
57

58    /**
59     * Constructor for custom tag.
60     */

61    public StringLimitTag(
62    )
63    {
64       super();
65    }
66    
67    // Business logic ///////////////////////////////////////////////////////////
68

69    /**
70     * {@inheritDoc}
71     */

72    public int doStartTag(
73    ) throws JspException JavaDoc
74    {
75       // Buffer the body since we need to have the whole body before we can
76
// limit it's length
77
return (EVAL_BODY_BUFFERED);
78    }
79
80    /**
81     * {@inheritDoc}
82     */

83    public int doEndTag(
84    ) throws JspException JavaDoc
85    {
86       // Get the buffered body
87
BodyContent JavaDoc content = getBodyContent();
88       
89       if (content != null)
90       {
91          int iLimit = Integer.parseInt(m_strLimit);
92          if (GlobalConstants.ERROR_CHECKING)
93          {
94             assert iLimit > 0 : "Limit has to be positive number.";
95          }
96          
97          TagUtils.write(pageContext,
98                         StringUtils.limitStringLength(iLimit, content.getString()));
99       }
100       
101       return (EVAL_PAGE);
102    }
103    
104    /**
105     * @return String - numeric limit representing length to limit the body text
106     */

107    public String JavaDoc getLimit(
108    )
109    {
110       return m_strLimit;
111    }
112    
113    /**
114     * @param strLimit - numeric limit representing length to limit the body text to
115     */

116    public void setLimit(
117       String JavaDoc strLimit
118    )
119    {
120       m_strLimit = strLimit;
121    }
122 }
123
Popular Tags