KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.StringTokenizer JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
24
25 import dlog4j.util.StringUtils;
26
27 /**
28  * 用于高亮显示搜索关键字的标签库
29  * @author Winter Lau
30  */

31 public class SearchHighlightTag extends BodyTagSupport JavaDoc {
32
33     String JavaDoc color = "red";
34     boolean bold = false;
35     String JavaDoc query;
36     
37     /* (non-Javadoc)
38      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
39      */

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

47     public int doEndTag() throws JspException JavaDoc {
48         Reader JavaDoc reader = getBodyContent().getReader();
49         char[] buf = new char[1024];
50         try{
51             StringBuffer JavaDoc content = new StringBuffer JavaDoc(1024);
52             do{
53                 int rc = reader.read(buf);
54                 if(rc>0)
55                     content.append(buf,0,rc);
56                 if(rc<1024)
57                     break;
58             }while(true);
59             //整理content
60
if(query!=null && !StringUtils.isEmpty(query.trim())){
61                 String JavaDoc startTag = "<font color=" + color + ">";
62                 String JavaDoc endTag = "";
63                 if(bold){
64                     startTag += "<b>";
65                     endTag = "</b>";
66                 }
67                 endTag += "</font>";
68                 String JavaDoc replaceContent = content.toString();
69                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(query);
70                 while(st.hasMoreElements()){
71                     String JavaDoc key = st.nextToken();
72                     if(key.length()<key.getBytes().length)
73                         replaceContent = StringUtils.replace(replaceContent, key, startTag + key + endTag);
74                     else{
75                         //使用大小写无关的替换策略
76
replaceContent = StringUtils.replaceIgnoreCase(replaceContent, key, startTag + key + endTag);
77                     }
78                 }
79                 pageContext.getOut().write(replaceContent);
80             }
81             else
82                 pageContext.getOut().write(content.toString());
83         }catch(IOException JavaDoc e0){
84         }
85         return EVAL_PAGE;
86     }
87     
88     public boolean isBold() {
89         return bold;
90     }
91     public void setBold(boolean bold) {
92         this.bold = bold;
93     }
94     public String JavaDoc getColor() {
95         return color;
96     }
97     public void setColor(String JavaDoc color) {
98         this.color = color;
99     }
100     public String JavaDoc getQuery() {
101         return query;
102     }
103     public void setQuery(String JavaDoc query) {
104         this.query = query;
105     }
106 }
107
Popular Tags