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 java.io.IOException;
23  import java.io.Reader;
24  
25  import org.apache.lucene.analysis.Analyzer;
26  import org.apache.lucene.analysis.CharTokenizer;
27  import org.apache.lucene.analysis.TokenStream;
28  import org.apache.lucene.analysis.standard.StandardAnalyzer;
29  import org.apache.lucene.util.Version;
30  import org.apache.maven.index.ArtifactInfo;
31  
32  /**
33   * A Nexus specific <a
34   * href="http://lucene.apache.org/java/2_4_0/api/core/org/apache/lucene/analysis/Analyzer.html">Lucene Analyzer</a> used
35   * to produce legacy index transfer format
36   * 
37   * @author Jason van Zyl
38   */
39  public final class NexusLegacyAnalyzer
40      extends Analyzer
41  {
42      private static Analyzer DEFAULT_ANALYZER = new StandardAnalyzer( Version.LUCENE_30 );
43  
44      @Override
45      public TokenStream tokenStream( String field, final Reader reader )
46      {
47          if ( !isTextField( field ) )
48          {
49              return new CharTokenizer( reader )
50              {
51                  @Override
52                  protected boolean isTokenChar( char c )
53                  {
54                      return Character.isLetterOrDigit( c );
55                  }
56  
57                  @Override
58                  protected char normalize( char c )
59                  {
60                      return Character.toLowerCase( c );
61                  }
62              };
63          }
64          else
65          {
66              return DEFAULT_ANALYZER.tokenStream( field, reader );
67          }
68      }
69  
70      @Override
71      public TokenStream reusableTokenStream( String field, Reader reader )
72          throws IOException
73      {
74          if ( !isTextField( field ) )
75          {
76              return new CharTokenizer( reader )
77              {
78                  @Override
79                  protected boolean isTokenChar( char c )
80                  {
81                      return Character.isLetterOrDigit( c );
82                  }
83  
84                  @Override
85                  protected char normalize( char c )
86                  {
87                      return Character.toLowerCase( c );
88                  }
89              };
90          }
91          else
92          {
93              return DEFAULT_ANALYZER.reusableTokenStream( field, reader );
94          }
95      }
96  
97      protected boolean isTextField( String field )
98      {
99          return ArtifactInfo.NAME.equals( field ) || ArtifactInfo.DESCRIPTION.equals( field )
100             || ArtifactInfo.NAMES.equals( field );
101 
102     }
103 
104 }