KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import javax.servlet.jsp.JspException JavaDoc;
29
30 import org.infoglue.deliver.taglib.TemplateControllerTag;
31
32 /**
33  * This tag help modifying texts in different ways. An example is to html-encode strings or replace all
34  * whitespaces with &nbsp; or replacing linebreaks with <br/>
35  */

36
37 public class TextTransformTag extends TemplateControllerTag
38 {
39     /**
40      * The universal version identifier.
41      */

42     private static final long serialVersionUID = 8603406098980150888L;
43     
44     /**
45      * The original text.
46      */

47     private String JavaDoc text;
48
49     /**
50      * Should we encode i18n chars
51      */

52     private boolean htmlEncode = false;
53
54     /**
55      * Should we replace linebreaks with something?
56      */

57     private boolean replaceLineBreaks = false;
58
59     /**
60      * The string to replace linebreaks with
61      */

62     private String JavaDoc lineBreakReplacer = "<br/>";
63
64     /**
65      * The linebreak char
66      */

67     private String JavaDoc lineBreakChar = System.getProperty("line.separator");
68
69     /**
70      * What to replace
71      */

72     private String JavaDoc replaceString = null;
73
74     /**
75      * What to replace with
76      */

77     private String JavaDoc replaceWithString = null;
78
79     /**
80      * Default constructor.
81      */

82     public TextTransformTag()
83     {
84         super();
85     }
86     
87     /**
88      * Process the end tag. Modifies the string according to settings made.
89      *
90      * @return indication of whether to continue evaluating the JSP page.
91      * @throws JspException if an error occurred while processing this tag.
92      */

93     public int doEndTag() throws JspException JavaDoc
94     {
95         String JavaDoc modifiedText = text;
96         
97         if(replaceString != null && replaceWithString != null)
98         {
99             Pattern JavaDoc pattern = Pattern.compile(replaceString);
100             Matcher JavaDoc matcher = pattern.matcher(modifiedText);
101             modifiedText = matcher.replaceAll(replaceWithString);
102         }
103         
104         if(replaceLineBreaks)
105             modifiedText.replaceAll(lineBreakChar, lineBreakReplacer);
106         
107         if(htmlEncode)
108             modifiedText = this.getController().getVisualFormatter().escapeHTMLforXMLService(modifiedText);
109             
110         setResultAttribute(modifiedText);
111         
112         return EVAL_PAGE;
113     }
114
115        
116     public void setText(String JavaDoc text) throws JspException JavaDoc
117     {
118         this.text = evaluateString("cropText", "text", text);
119     }
120     
121     public void setHtmlEncode(boolean htmlEncode)
122     {
123         this.htmlEncode = htmlEncode;
124     }
125     
126     public void setReplaceLineBreaks(boolean replaceLineBreaks)
127     {
128         this.replaceLineBreaks = replaceLineBreaks;
129     }
130
131     public void setLineBreakChar(String JavaDoc lineBreakChar) throws JspException JavaDoc
132     {
133         this.lineBreakChar = evaluateString("TextTransform", "lineBreakChar", lineBreakChar);
134     }
135     
136     public void setLineBreakReplacer(String JavaDoc lineBreakReplacer) throws JspException JavaDoc
137     {
138         this.lineBreakReplacer = evaluateString("TextTransform", "lineBreakReplacer", lineBreakReplacer);
139     }
140         
141     public void setReplaceString(String JavaDoc replaceString) throws JspException JavaDoc
142     {
143         this.replaceString = evaluateString("TextTransform", "replaceString", replaceString);
144     }
145     
146     public void setReplaceWithString(String JavaDoc replaceWithString) throws JspException JavaDoc
147     {
148         this.replaceWithString = evaluateString("TextTransform", "replaceWithString", replaceWithString);
149     }
150 }
151
Popular Tags