KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > highlight > SpanGradientFormatter


1 package org.apache.lucene.search.highlight;
2 /**
3  * Copyright 2002-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 font tag which
22  * doesn't work in Mozilla, thus this class.
23  *
24  * @see GradientFormatter
25  * @author David Spencer dave@searchmorph.com
26  */

27
28
29 public class SpanGradientFormatter
30     extends GradientFormatter
31 {
32     public SpanGradientFormatter(float maxScore, String JavaDoc minForegroundColor,
33             String JavaDoc maxForegroundColor, String JavaDoc minBackgroundColor,
34             String JavaDoc maxBackgroundColor)
35     {
36         super( maxScore, minForegroundColor,
37                maxForegroundColor, minBackgroundColor,
38                maxBackgroundColor);
39     }
40     
41
42     
43     public String JavaDoc highlightTerm(String JavaDoc originalText, TokenGroup tokenGroup)
44     {
45         if (tokenGroup.getTotalScore() == 0)
46             return originalText;
47         float score = tokenGroup.getTotalScore();
48         if (score == 0)
49         {
50             return originalText;
51         }
52
53         // try to size sb correctly
54
StringBuffer JavaDoc sb = new StringBuffer JavaDoc( originalText.length() + EXTRA);
55         
56         sb.append("<span style=\"");
57         if (highlightForeground)
58         {
59             sb.append("color: ");
60             sb.append(getForegroundColorString(score));
61             sb.append("; ");
62         }
63         if (highlightBackground)
64         {
65             sb.append("background: ");
66             sb.append(getBackgroundColorString(score));
67             sb.append("; ");
68         }
69         sb.append("\">");
70         sb.append(originalText);
71         sb.append("</span>");
72         return sb.toString();
73     }
74
75     // guess how much extra text we'll add to the text we're highlighting to try to avoid a StringBuffer resize
76
private static final String JavaDoc TEMPLATE = "<span style=\"background: #EEEEEE; color: #000000;\">...</span>";
77     private static final int EXTRA = TEMPLATE.length();
78 }
79
Popular Tags