Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » search » highlight » [javadoc | source]
    1   package org.apache.lucene.search.highlight;
    2   
    3   /**
    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
   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    * Simple {@link Formatter} implementation to highlight terms with a pre and
   22    * post tag.
   23    */
   24   public class SimpleHTMLFormatter implements Formatter {
   25     
   26     private static final String DEFAULT_PRE_TAG = "<B>";
   27     private static final String DEFAULT_POST_TAG = "</B>";
   28     
   29   	private String preTag;
   30   	private String postTag;
   31   	
   32   	public SimpleHTMLFormatter(String preTag, String postTag) {
   33   		this.preTag = preTag;
   34   		this.postTag = postTag;
   35   	}
   36   
   37   	/** Default constructor uses HTML: &lt;B&gt; tags to markup terms. */
   38   	public SimpleHTMLFormatter() {
   39   	  this(DEFAULT_PRE_TAG, DEFAULT_POST_TAG);
   40   	}
   41   
   42   	/* (non-Javadoc)
   43   	 * @see org.apache.lucene.search.highlight.Formatter#highlightTerm(java.lang.String, org.apache.lucene.search.highlight.TokenGroup)
   44   	 */
   45   	public String highlightTerm(String originalText, TokenGroup tokenGroup) {
   46   	  if (tokenGroup.getTotalScore() <= 0) {
   47   	    return originalText;
   48   	  }
   49   	  
   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();
   57   	}
   58   	
   59   }

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » search » highlight » [javadoc | source]