View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index.context;
20  
21  import org.apache.lucene.analysis.Analyzer;
22  import org.apache.lucene.analysis.AnalyzerWrapper;
23  import org.apache.lucene.analysis.LowerCaseFilter;
24  import org.apache.lucene.analysis.Tokenizer;
25  import org.apache.lucene.analysis.standard.StandardAnalyzer;
26  import org.apache.lucene.analysis.util.CharTokenizer;
27  import org.apache.maven.index.ArtifactInfo;
28  
29  /**
30   * A Nexus specific <a
31   * href="http://lucene.apache.org/java/2_4_0/api/core/org/apache/lucene/analysis/Analyzer.html">Lucene Analyzer</a> used
32   * to produce legacy index transfer format
33   *
34   * @author Jason van Zyl
35   */
36  public final class NexusLegacyAnalyzer extends AnalyzerWrapper {
37      private static final Analyzer DEFAULT_ANALYZER = new StandardAnalyzer();
38  
39      private static final Analyzer LETTER_OR_DIGIT_ANALYZER = new Analyzer() {
40          @Override
41          protected TokenStreamComponents createComponents(final String fieldName) {
42              final Tokenizer tokenizer = new CharTokenizer() {
43                  @Override
44                  protected boolean isTokenChar(int c) {
45                      return Character.isLetterOrDigit(c);
46                  }
47              };
48  
49              return new TokenStreamComponents(tokenizer, new LowerCaseFilter(tokenizer));
50          }
51      };
52  
53      public NexusLegacyAnalyzer() {
54          super(PER_FIELD_REUSE_STRATEGY);
55      }
56  
57      @Override
58      protected Analyzer getWrappedAnalyzer(String fieldName) {
59          if (!isTextField(fieldName)) {
60              return LETTER_OR_DIGIT_ANALYZER;
61          } else {
62              return DEFAULT_ANALYZER;
63          }
64      }
65  
66      protected boolean isTextField(String field) {
67          return ArtifactInfo.NAME.equals(field)
68                  || ArtifactInfo.DESCRIPTION.equals(field)
69                  || ArtifactInfo.NAMES.equals(field);
70      }
71  }