01: package org.apache.lucene.search.highlight;
02:
03: /**
04: * Copyright 2002-2004 The Apache Software Foundation
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: /**
20: * Formats text with different color intensity depending on the score of the
21: * term using the span tag. GradientFormatter uses a bgcolor argument to the
22: * font tag which doesn't work in Mozilla, thus this class.
23: *
24: * @see GradientFormatter
25: * @author David Spencer dave@searchmorph.com
26: */
27:
28: public class SpanGradientFormatter extends GradientFormatter {
29: public SpanGradientFormatter(float maxScore,
30: String minForegroundColor, String maxForegroundColor,
31: String minBackgroundColor, String maxBackgroundColor) {
32: super (maxScore, minForegroundColor, maxForegroundColor,
33: minBackgroundColor, maxBackgroundColor);
34: }
35:
36: public String highlightTerm(String originalText,
37: TokenGroup tokenGroup) {
38: if (tokenGroup.getTotalScore() == 0)
39: return originalText;
40: float score = tokenGroup.getTotalScore();
41: if (score == 0) {
42: return originalText;
43: }
44:
45: // try to size sb correctly
46: StringBuffer sb = new StringBuffer(originalText.length()
47: + EXTRA);
48:
49: sb.append("<span style=\"");
50: if (highlightForeground) {
51: sb.append("color: ");
52: sb.append(getForegroundColorString(score));
53: sb.append("; ");
54: }
55: if (highlightBackground) {
56: sb.append("background: ");
57: sb.append(getBackgroundColorString(score));
58: sb.append("; ");
59: }
60: sb.append("\">");
61: sb.append(originalText);
62: sb.append("</span>");
63: return sb.toString();
64: }
65:
66: // guess how much extra text we'll add to the text we're highlighting to try
67: // to avoid a StringBuffer resize
68: private static final String TEMPLATE = "<span style=\"background: #EEEEEE; color: #000000;\">...</span>";
69:
70: private static final int EXTRA = TEMPLATE.length();
71: }
|