1 package org.apache.lucene.search.highlight;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Simple {@link Formatter} implementation to highlight terms with a pre and
24 public class SimpleHTMLFormatter implements Formatter {
26 private static final String DEFAULT_PRE_TAG = "<B>";
27 private static final String DEFAULT_POST_TAG = "</B>";
29 private String preTag;
30 private String postTag;
32 public SimpleHTMLFormatter(String preTag, String postTag) {
34 this.postTag = postTag;
37 /** Default constructor uses HTML: <B> tags to markup terms. */
38 public SimpleHTMLFormatter() {
39 this(DEFAULT_PRE_TAG, DEFAULT_POST_TAG);
43 * @see org.apache.lucene.search.highlight.Formatter#highlightTerm(java.lang.String, org.apache.lucene.search.highlight.TokenGroup)
45 public String highlightTerm(String originalText, TokenGroup tokenGroup) {
46 if (tokenGroup.getTotalScore() <= 0) {
50 // Allocate StringBuilder with the right number of characters from the
51 // beginning, to avoid char[] allocations in the middle of appends.
52 StringBuilder returnBuffer = new StringBuilder(preTag.length() + originalText.length() + postTag.length());
53 returnBuffer.append(preTag);
54 returnBuffer.append(originalText);
55 returnBuffer.append(postTag);
56 return returnBuffer.toString();