1 16 package dlog4j.util.tags; 17 18 import java.io.IOException ; 19 import java.io.Reader ; 20 import java.util.StringTokenizer ; 21 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.tagext.BodyTagSupport ; 24 25 import dlog4j.util.StringUtils; 26 27 31 public class SearchHighlightTag extends BodyTagSupport { 32 33 String color = "red"; 34 boolean bold = false; 35 String query; 36 37 40 public int doStartTag() throws JspException { 41 return EVAL_BODY_BUFFERED; 42 } 43 44 47 public int doEndTag() throws JspException { 48 Reader reader = getBodyContent().getReader(); 49 char[] buf = new char[1024]; 50 try{ 51 StringBuffer content = new StringBuffer (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 if(query!=null && !StringUtils.isEmpty(query.trim())){ 61 String startTag = "<font color=" + color + ">"; 62 String endTag = ""; 63 if(bold){ 64 startTag += "<b>"; 65 endTag = "</b>"; 66 } 67 endTag += "</font>"; 68 String replaceContent = content.toString(); 69 StringTokenizer st = new StringTokenizer (query); 70 while(st.hasMoreElements()){ 71 String key = st.nextToken(); 72 if(key.length()<key.getBytes().length) 73 replaceContent = StringUtils.replace(replaceContent, key, startTag + key + endTag); 74 else{ 75 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 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 getColor() { 95 return color; 96 } 97 public void setColor(String color) { 98 this.color = color; 99 } 100 public String getQuery() { 101 return query; 102 } 103 public void setQuery(String query) { 104 this.query = query; 105 } 106 } 107 | Popular Tags |