KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > ConvertNewLineTag


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import java.io.IOException JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import javax.servlet.jsp.JspException JavaDoc;
19 import javax.servlet.jsp.JspTagException JavaDoc;
20 import javax.servlet.jsp.JspWriter JavaDoc;
21 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24
25
26 /**
27  * Converts text in the body of the tag adding %lt;br /> tags at new lines or wrapping lines in paragraphs.
28  * @author Fabrizio Giustina
29  * @version $Revision $ ($Author $)
30  */

31 public class ConvertNewLineTag extends BodyTagSupport JavaDoc {
32
33     /**
34      * Stable serialVersionUID.
35      */

36     private static final long serialVersionUID = 222L;
37
38     /**
39      * Use paragraphs.
40      */

41     private boolean para;
42
43     /**
44      * Setter for the <code>para</code> attribute.
45      * @param paragraphs <code>true</code> is each line should be wrapped in a %lt;p> tag.
46      */

47     public void setPara(boolean paragraphs) {
48         this.para = paragraphs;
49     }
50
51     /**
52      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
53      */

54     public int doEndTag() throws JspException JavaDoc {
55         String JavaDoc bodyText = bodyContent.getString();
56
57         if (StringUtils.isNotEmpty(bodyText)) {
58             StringTokenizer JavaDoc bodyTk = new StringTokenizer JavaDoc(bodyText, "\n", false); //$NON-NLS-1$
59
JspWriter JavaDoc out = pageContext.getOut();
60
61             try {
62                 if (this.para) {
63                     // wrap text in p
64
while (bodyTk.hasMoreTokens()) {
65                         out.write("<p>"); //$NON-NLS-1$
66
out.write(StringUtils.replaceChars(bodyTk.nextToken(), (char) 63, '\''));
67                         out.write("</p>"); //$NON-NLS-1$
68
}
69                 }
70                 else {
71                     // add newlines
72
while (bodyTk.hasMoreTokens()) {
73                         out.write(StringUtils.replaceChars(bodyTk.nextToken(), (char) 63, '\''));
74                         if (bodyTk.hasMoreTokens()) {
75                             out.write("<br/>"); //$NON-NLS-1$
76
}
77                     }
78                 }
79             }
80             catch (IOException JavaDoc e) {
81                 throw new JspTagException JavaDoc(e.getMessage());
82             }
83         }
84         return EVAL_PAGE;
85     }
86
87     /**
88      * @see javax.servlet.jsp.tagext.Tag#release()
89      */

90     public void release() {
91         para = false;
92         super.release();
93     }
94
95 }
Popular Tags