KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Simple {@link Formatter} implementation to highlight terms with a pre and post tag
20  * @author MAHarwood
21  *
22  */

23 public class SimpleHTMLFormatter implements Formatter
24 {
25     String JavaDoc preTag;
26     String JavaDoc postTag;
27     
28
29     public SimpleHTMLFormatter(String JavaDoc preTag, String JavaDoc postTag)
30     {
31         this.preTag = preTag;
32         this.postTag = postTag;
33     }
34
35     /**
36      * Default constructor uses HTML: <B> tags to markup terms
37      *
38      **/

39     public SimpleHTMLFormatter()
40     {
41         this.preTag = "<B>";
42         this.postTag = "</B>";
43     }
44
45     /* (non-Javadoc)
46      * @see org.apache.lucene.search.highlight.Formatter#highlightTerm(java.lang.String, org.apache.lucene.search.highlight.TokenGroup)
47      */

48     public String JavaDoc highlightTerm(String JavaDoc originalText, TokenGroup tokenGroup)
49     {
50         StringBuffer JavaDoc returnBuffer;
51         if(tokenGroup.getTotalScore()>0)
52         {
53             returnBuffer=new StringBuffer JavaDoc();
54             returnBuffer.append(preTag);
55             returnBuffer.append(originalText);
56             returnBuffer.append(postTag);
57             return returnBuffer.toString();
58         }
59         return originalText;
60     }
61 }
62
Popular Tags