001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.util.tags;
017:
018: import java.io.IOException;
019: import java.io.Reader;
020: import java.util.StringTokenizer;
021:
022: import javax.servlet.jsp.JspException;
023: import javax.servlet.jsp.tagext.BodyTagSupport;
024:
025: import dlog4j.util.StringUtils;
026:
027: /**
028: * 用于高亮显示搜索关键字的标签库
029: * @author Winter Lau
030: */
031: public class SearchHighlightTag extends BodyTagSupport {
032:
033: String color = "red";
034: boolean bold = false;
035: String query;
036:
037: /* (non-Javadoc)
038: * @see javax.servlet.jsp.tagext.Tag#doStartTag()
039: */
040: public int doStartTag() throws JspException {
041: return EVAL_BODY_BUFFERED;
042: }
043:
044: /* (non-Javadoc)
045: * @see javax.servlet.jsp.tagext.Tag#doStartTag()
046: */
047: public int doEndTag() throws JspException {
048: Reader reader = getBodyContent().getReader();
049: char[] buf = new char[1024];
050: try {
051: StringBuffer content = new StringBuffer(1024);
052: do {
053: int rc = reader.read(buf);
054: if (rc > 0)
055: content.append(buf, 0, rc);
056: if (rc < 1024)
057: break;
058: } while (true);
059: //整理content
060: if (query != null && !StringUtils.isEmpty(query.trim())) {
061: String startTag = "<font color=" + color + ">";
062: String endTag = "";
063: if (bold) {
064: startTag += "<b>";
065: endTag = "</b>";
066: }
067: endTag += "</font>";
068: String replaceContent = content.toString();
069: StringTokenizer st = new StringTokenizer(query);
070: while (st.hasMoreElements()) {
071: String key = st.nextToken();
072: if (key.length() < key.getBytes().length)
073: replaceContent = StringUtils.replace(
074: replaceContent, key, startTag + key
075: + endTag);
076: else {
077: //使用大小写无关的替换策略
078: replaceContent = StringUtils.replaceIgnoreCase(
079: replaceContent, key, startTag + key
080: + endTag);
081: }
082: }
083: pageContext.getOut().write(replaceContent);
084: } else
085: pageContext.getOut().write(content.toString());
086: } catch (IOException e0) {
087: }
088: return EVAL_PAGE;
089: }
090:
091: public boolean isBold() {
092: return bold;
093: }
094:
095: public void setBold(boolean bold) {
096: this .bold = bold;
097: }
098:
099: public String getColor() {
100: return color;
101: }
102:
103: public void setColor(String color) {
104: this .color = color;
105: }
106:
107: public String getQuery() {
108: return query;
109: }
110:
111: public void setQuery(String query) {
112: this.query = query;
113: }
114: }
|