KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Formats text with different color intensity depending on the score of the
20  * term.
21  *
22  * @author maharwood
23  */

24 public class GradientFormatter implements Formatter
25 {
26     private float maxScore;
27
28     int fgRMin;
29     int fgGMin;
30     int fgBMin;
31
32     int fgRMax;
33     int fgGMax;
34     int fgBMax;
35
36     protected boolean highlightForeground;
37
38     int bgRMin;
39     int bgGMin;
40     int bgBMin;
41
42     int bgRMax;
43     int bgGMax;
44     int bgBMax;
45
46     protected boolean highlightBackground;
47
48     /**
49      * Sets the color range for the IDF scores
50      *
51      * @param maxScore
52      * The score (and above) displayed as maxColor (See QueryScorer.getMaxWeight
53      * which can be used to callibrate scoring scale)
54      * @param minForegroundColor
55      * The hex color used for representing IDF scores of zero eg
56      * #FFFFFF (white) or null if no foreground color required
57      * @param maxForegroundColor
58      * The largest hex color used for representing IDF scores eg
59      * #000000 (black) or null if no foreground color required
60      * @param minBackgroundColor
61      * The hex color used for representing IDF scores of zero eg
62      * #FFFFFF (white) or null if no background color required
63      * @param maxBackgroundColor
64      * The largest hex color used for representing IDF scores eg
65      * #000000 (black) or null if no background color required
66      */

67     public GradientFormatter(float maxScore, String JavaDoc minForegroundColor,
68             String JavaDoc maxForegroundColor, String JavaDoc minBackgroundColor,
69             String JavaDoc maxBackgroundColor)
70     {
71         highlightForeground = (minForegroundColor != null)
72                 && (maxForegroundColor != null);
73         if (highlightForeground)
74         {
75             if (minForegroundColor.length() != 7)
76             {
77                 throw new IllegalArgumentException JavaDoc(
78                         "minForegroundColor is not 7 bytes long eg a hex "
79                                 + "RGB value such as #FFFFFF");
80             }
81             if (maxForegroundColor.length() != 7)
82             {
83                 throw new IllegalArgumentException JavaDoc(
84                         "minForegroundColor is not 7 bytes long eg a hex "
85                                 + "RGB value such as #FFFFFF");
86             }
87             fgRMin = hexToInt(minForegroundColor.substring(1, 3));
88             fgGMin = hexToInt(minForegroundColor.substring(3, 5));
89             fgBMin = hexToInt(minForegroundColor.substring(5, 7));
90
91             fgRMax = hexToInt(maxForegroundColor.substring(1, 3));
92             fgGMax = hexToInt(maxForegroundColor.substring(3, 5));
93             fgBMax = hexToInt(maxForegroundColor.substring(5, 7));
94         }
95
96         highlightBackground = (minBackgroundColor != null)
97                 && (maxBackgroundColor != null);
98         if (highlightBackground)
99         {
100             if (minBackgroundColor.length() != 7)
101             {
102                 throw new IllegalArgumentException JavaDoc(
103                         "minBackgroundColor is not 7 bytes long eg a hex "
104                                 + "RGB value such as #FFFFFF");
105             }
106             if (maxBackgroundColor.length() != 7)
107             {
108                 throw new IllegalArgumentException JavaDoc(
109                         "minBackgroundColor is not 7 bytes long eg a hex "
110                                 + "RGB value such as #FFFFFF");
111             }
112             bgRMin = hexToInt(minBackgroundColor.substring(1, 3));
113             bgGMin = hexToInt(minBackgroundColor.substring(3, 5));
114             bgBMin = hexToInt(minBackgroundColor.substring(5, 7));
115
116             bgRMax = hexToInt(maxBackgroundColor.substring(1, 3));
117             bgGMax = hexToInt(maxBackgroundColor.substring(3, 5));
118             bgBMax = hexToInt(maxBackgroundColor.substring(5, 7));
119         }
120         // this.corpusReader = corpusReader;
121
this.maxScore = maxScore;
122         // totalNumDocs = corpusReader.numDocs();
123
}
124
125     public String JavaDoc highlightTerm(String JavaDoc originalText, TokenGroup tokenGroup)
126     {
127         if (tokenGroup.getTotalScore() == 0)
128             return originalText;
129         float score = tokenGroup.getTotalScore();
130         if (score == 0)
131         {
132             return originalText;
133         }
134         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
135         sb.append("<font ");
136         if (highlightForeground)
137         {
138             sb.append("color=\"");
139             sb.append(getForegroundColorString(score));
140             sb.append("\" ");
141         }
142         if (highlightBackground)
143         {
144             sb.append("bgcolor=\"");
145             sb.append(getBackgroundColorString(score));
146             sb.append("\" ");
147         }
148         sb.append(">");
149         sb.append(originalText);
150         sb.append("</font>");
151         return sb.toString();
152     }
153
154     protected String JavaDoc getForegroundColorString(float score)
155     {
156         int rVal = getColorVal(fgRMin, fgRMax, score);
157         int gVal = getColorVal(fgGMin, fgGMax, score);
158         int bVal = getColorVal(fgBMin, fgBMax, score);
159         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
160         sb.append("#");
161         sb.append(intToHex(rVal));
162         sb.append(intToHex(gVal));
163         sb.append(intToHex(bVal));
164         return sb.toString();
165     }
166
167     protected String JavaDoc getBackgroundColorString(float score)
168     {
169         int rVal = getColorVal(bgRMin, bgRMax, score);
170         int gVal = getColorVal(bgGMin, bgGMax, score);
171         int bVal = getColorVal(bgBMin, bgBMax, score);
172         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
173         sb.append("#");
174         sb.append(intToHex(rVal));
175         sb.append(intToHex(gVal));
176         sb.append(intToHex(bVal));
177         return sb.toString();
178     }
179
180     private int getColorVal(int colorMin, int colorMax, float score)
181     {
182         if (colorMin == colorMax)
183         {
184             return colorMin;
185         }
186         float scale = Math.abs(colorMin - colorMax);
187         float relScorePercent = Math.min(maxScore, score) / maxScore;
188         float colScore = scale * relScorePercent;
189         return Math.min(colorMin, colorMax) + (int) colScore;
190     }
191
192     private static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7',
193             '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
194
195     private static String JavaDoc intToHex(int i)
196     {
197         return "" + hexDigits[(i & 0xF0) >> 4] + hexDigits[i & 0x0F];
198     }
199
200     /**
201      * Converts a hex string into an int. Integer.parseInt(hex, 16) assumes the
202      * input is nonnegative unless there is a preceding minus sign. This method
203      * reads the input as twos complement instead, so if the input is 8 bytes
204      * long, it will correctly restore a negative int produced by
205      * Integer.toHexString() but not neccesarily one produced by
206      * Integer.toString(x,16) since that method will produce a string like '-FF'
207      * for negative integer values.
208      *
209      * @param hex
210      * A string in capital or lower case hex, of no more then 16
211      * characters.
212      * @throws NumberFormatException
213      * if the string is more than 16 characters long, or if any
214      * character is not in the set [0-9a-fA-f]
215      */

216     public static final int hexToInt(String JavaDoc hex)
217     {
218         int len = hex.length();
219         if (len > 16)
220             throw new NumberFormatException JavaDoc();
221
222         int l = 0;
223         for (int i = 0; i < len; i++)
224         {
225             l <<= 4;
226             int c = Character.digit(hex.charAt(i), 16);
227             if (c < 0)
228                 throw new NumberFormatException JavaDoc();
229             l |= c;
230         }
231         return l;
232     }
233
234 }
235
236
Popular Tags