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