Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Analysis / PerFieldAnalyzerWrapper.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 namespace Mono.Lucene.Net.Analysis
21 {
22         
23         /// <summary> This analyzer is used to facilitate scenarios where different
24         /// fields require different analysis techniques.  Use {@link #addAnalyzer}
25         /// to add a non-default analyzer on a field name basis.
26         /// 
27         /// <p/>Example usage:
28         /// 
29         /// <pre>
30         /// PerFieldAnalyzerWrapper aWrapper =
31         /// new PerFieldAnalyzerWrapper(new StandardAnalyzer());
32         /// aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
33         /// aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
34         /// </pre>
35         /// 
36         /// <p/>In this example, StandardAnalyzer will be used for all fields except "firstname"
37         /// and "lastname", for which KeywordAnalyzer will be used.
38         /// 
39         /// <p/>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
40         /// and query parsing.
41         /// </summary>
42         public class PerFieldAnalyzerWrapper:Analyzer
43         {
44                 private Analyzer defaultAnalyzer;
45                 private System.Collections.IDictionary analyzerMap = new System.Collections.Hashtable();
46                 
47                 
48                 /// <summary> Constructs with default analyzer.
49                 /// 
50                 /// </summary>
51                 /// <param name="defaultAnalyzer">Any fields not specifically
52                 /// defined to use a different analyzer will use the one provided here.
53                 /// </param>
54                 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer):this(defaultAnalyzer, null)
55                 {
56                 }
57                 
58                 /// <summary> Constructs with default analyzer and a map of analyzers to use for 
59                 /// specific fields.
60                 /// 
61                 /// </summary>
62                 /// <param name="defaultAnalyzer">Any fields not specifically
63                 /// defined to use a different analyzer will use the one provided here.
64                 /// </param>
65                 /// <param name="fieldAnalyzers">a Map (String field name to the Analyzer) to be 
66                 /// used for those fields 
67                 /// </param>
68                 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, System.Collections.IDictionary fieldAnalyzers)
69                 {
70                         this.defaultAnalyzer = defaultAnalyzer;
71                         if (fieldAnalyzers != null)
72                         {
73                                 System.Collections.ArrayList keys = new System.Collections.ArrayList(fieldAnalyzers.Keys);
74                                 System.Collections.ArrayList values = new System.Collections.ArrayList(fieldAnalyzers.Values);
75
76                                 for (int i=0; i < keys.Count; i++)
77                                         analyzerMap[keys[i]] = values[i];
78                         }
79                         SetOverridesTokenStreamMethod(typeof(PerFieldAnalyzerWrapper));
80                 }
81                 
82                 
83                 /// <summary> Defines an analyzer to use for the specified field.
84                 /// 
85                 /// </summary>
86                 /// <param name="fieldName">field name requiring a non-default analyzer
87                 /// </param>
88                 /// <param name="analyzer">non-default analyzer to use for field
89                 /// </param>
90                 public virtual void  AddAnalyzer(System.String fieldName, Analyzer analyzer)
91                 {
92                         analyzerMap[fieldName] = analyzer;
93                 }
94                 
95                 public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
96                 {
97                         Analyzer analyzer = (Analyzer) analyzerMap[fieldName];
98                         if (analyzer == null)
99                         {
100                                 analyzer = defaultAnalyzer;
101                         }
102                         
103                         return analyzer.TokenStream(fieldName, reader);
104                 }
105                 
106                 public override TokenStream ReusableTokenStream(System.String fieldName, System.IO.TextReader reader)
107                 {
108                         if (overridesTokenStreamMethod)
109                         {
110                                 // LUCENE-1678: force fallback to tokenStream() if we
111                                 // have been subclassed and that subclass overrides
112                                 // tokenStream but not reusableTokenStream
113                                 return TokenStream(fieldName, reader);
114                         }
115                         Analyzer analyzer = (Analyzer) analyzerMap[fieldName];
116                         if (analyzer == null)
117                                 analyzer = defaultAnalyzer;
118                         
119                         return analyzer.ReusableTokenStream(fieldName, reader);
120                 }
121                 
122                 /// <summary>Return the positionIncrementGap from the analyzer assigned to fieldName </summary>
123                 public override int GetPositionIncrementGap(System.String fieldName)
124                 {
125                         Analyzer analyzer = (Analyzer) analyzerMap[fieldName];
126                         if (analyzer == null)
127                                 analyzer = defaultAnalyzer;
128                         return analyzer.GetPositionIncrementGap(fieldName);
129                 }
130
131         /// <summary> Return the offsetGap from the analyzer assigned to field </summary>
132         public override int GetOffsetGap(Mono.Lucene.Net.Documents.Fieldable field)
133         {
134             Analyzer analyzer = (Analyzer)analyzerMap[field.Name()];
135             if (analyzer == null)
136                 analyzer = defaultAnalyzer;
137             return analyzer.GetOffsetGap(field);
138         }
139                 
140                 public override System.String ToString()
141                 {
142                         // {{Aroush-2.9}} will 'analyzerMap.ToString()' work in the same way as Java's java.util.HashMap.toString()? 
143                         return "PerFieldAnalyzerWrapper(" + analyzerMap.ToString() + ", default=" + defaultAnalyzer + ")";
144                 }
145         }
146 }