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