KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > util > tags > HtmlFormatTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.util.tags;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Reader JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
23
24 import org.apache.commons.lang.StringUtils;
25 /**
26  * @author Liudong
27  * 格式化大文本内容以适应在页面上进行显示
28  */

29 public class HtmlFormatTag extends BodyTagSupport JavaDoc {
30
31     boolean ubb = false;
32     boolean html= false;
33     boolean face= false;
34
35     /* (non-Javadoc)
36      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
37      */

38     public int doStartTag() throws JspException JavaDoc {
39         return EVAL_BODY_BUFFERED;
40     }
41
42     /* (non-Javadoc)
43      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
44      */

45     public int doEndTag() throws JspException JavaDoc {
46         Reader JavaDoc reader = getBodyContent().getReader();
47         char[] buf = new char[1024];
48         try{
49             StringBuffer JavaDoc content = new StringBuffer JavaDoc(1024);
50             do{
51                 int rc = reader.read(buf);
52                 if(rc>0)
53                     content.append(buf,0,rc);
54                 if(rc<1024)
55                     break;
56             }while(true);
57             pageContext.getOut().write(format(content.toString(),ubb,html,face));
58         }catch(IOException JavaDoc e0){
59         }
60         return EVAL_PAGE;
61     }
62     /**
63      * HTML输出内容格式转换
64      * @param content
65      * @param useUbb
66      * @param transHtml
67      * @param useFace
68      * @return
69      */

70     protected String JavaDoc format(String JavaDoc content, boolean useUbb, boolean transHtml, boolean useFace){
71         String JavaDoc html = content;
72         if(transHtml){
73             html = StringUtils.replace(html,"\t","&nbsp;&nbsp;&nbsp;&nbsp;");//替换跳格
74
html = StringUtils.replace(html," ","&nbsp;");//替换空格
75
html = StringUtils.replace(html,"\n","<br>");//替换换行
76
}
77         if(useUbb){
78             
79         }
80         return html;
81     }
82     /* (non-Javadoc)
83      * @see javax.servlet.jsp.tagext.Tag#release()
84      */

85     public void release() {
86         super.release();
87     }
88
89     /**
90      * @return
91      */

92     public boolean isFace() {
93         return face;
94     }
95
96     /**
97      * @return
98      */

99     public boolean isHtml() {
100         return html;
101     }
102
103     /**
104      * @return
105      */

106     public boolean isUbb() {
107         return ubb;
108     }
109
110     /**
111      * @param b
112      */

113     public void setFace(boolean b) {
114         face = b;
115     }
116
117     /**
118      * @param b
119      */

120     public void setHtml(boolean b) {
121         html = b;
122     }
123
124     /**
125      * @param b
126      */

127     public void setUbb(boolean b) {
128         ubb = b;
129     }
130
131 }
132
Popular Tags