Coverage Report - org.owasp.dependencycheck.data.lucene.SearchFieldAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchFieldAnalyzer
100%
12/12
50%
1/2
1.333
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.lucene;
 19  
 
 20  
 import java.io.Reader;
 21  
 import org.apache.lucene.analysis.Analyzer;
 22  
 import org.apache.lucene.analysis.TokenStream;
 23  
 import org.apache.lucene.analysis.Tokenizer;
 24  
 import org.apache.lucene.analysis.core.LowerCaseFilter;
 25  
 import org.apache.lucene.analysis.core.StopAnalyzer;
 26  
 import org.apache.lucene.analysis.core.StopFilter;
 27  
 import org.apache.lucene.analysis.miscellaneous.WordDelimiterFilter;
 28  
 import org.apache.lucene.util.Version;
 29  
 
 30  
 /**
 31  
  * A Lucene field analyzer used to analyzer queries against the CPE data.
 32  
  *
 33  
  * @author Jeremy Long <jeremy.long@owasp.org>
 34  
  */
 35  
 public class SearchFieldAnalyzer extends Analyzer {
 36  
 
 37  
     /**
 38  
      * The Lucene Version used.
 39  
      */
 40  
     private final Version version;
 41  
     /**
 42  
      * A local reference to the TokenPairConcatenatingFilter so that we can clear any left over state if this analyzer
 43  
      * is re-used.
 44  
      */
 45  
     private TokenPairConcatenatingFilter concatenatingFilter;
 46  
 
 47  
     /**
 48  
      * Constructs a new SearchFieldAnalyzer.
 49  
      *
 50  
      * @param version the Lucene version
 51  
      */
 52  
     public SearchFieldAnalyzer(Version version) {
 53  
         this.version = version;
 54  
     }
 55  
 
 56  
     /**
 57  
      * Creates a the TokenStreamComponents used to analyze the stream.
 58  
      *
 59  
      * @param fieldName the field that this lucene analyzer will process
 60  
      * @param reader a reader containing the tokens
 61  
      * @return the token stream filter chain
 62  
      */
 63  
     @Override
 64  
     protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
 65  24
         final Tokenizer source = new AlphaNumericTokenizer(version, reader);
 66  
 
 67  24
         TokenStream stream = source;
 68  
 
 69  24
         stream = new WordDelimiterFilter(stream,
 70  
                 WordDelimiterFilter.GENERATE_WORD_PARTS
 71  
                 | WordDelimiterFilter.GENERATE_NUMBER_PARTS
 72  
                 | WordDelimiterFilter.PRESERVE_ORIGINAL
 73  
                 | WordDelimiterFilter.SPLIT_ON_CASE_CHANGE
 74  
                 | WordDelimiterFilter.SPLIT_ON_NUMERICS
 75  
                 | WordDelimiterFilter.STEM_ENGLISH_POSSESSIVE, null);
 76  
 
 77  24
         stream = new LowerCaseFilter(version, stream);
 78  24
         stream = new UrlTokenizingFilter(stream);
 79  24
         concatenatingFilter = new TokenPairConcatenatingFilter(stream);
 80  24
         stream = concatenatingFilter;
 81  24
         stream = new StopFilter(version, stream, StopAnalyzer.ENGLISH_STOP_WORDS_SET);
 82  
 
 83  24
         return new TokenStreamComponents(source, stream);
 84  
     }
 85  
 
 86  
     /**
 87  
      * <p>
 88  
      * Resets the analyzer and clears any internal state data that may have been left-over from previous uses of the
 89  
      * analyzer.</p>
 90  
      * <p>
 91  
      * <b>If this analyzer is re-used this method must be called between uses.</b></p>
 92  
      */
 93  
     public void clear() {
 94  2
         if (concatenatingFilter != null) {
 95  2
             concatenatingFilter.clear();
 96  
         }
 97  2
     }
 98  
 }