Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
Analyzer.cs
Go to the documentation of this file.
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 using Lucene.Net.Documents;
20 using Lucene.Net.Store;
21 using Lucene.Net.Util;
22 
23 namespace Lucene.Net.Analysis
24 {
32  public abstract class Analyzer : IDisposable
33  {
38  public abstract TokenStream TokenStream(String fieldName, System.IO.TextReader reader);
39 
47  public virtual TokenStream ReusableTokenStream(String fieldName, System.IO.TextReader reader)
48  {
49  return TokenStream(fieldName, reader);
50  }
51 
53  private bool isDisposed;
54 
59  protected internal virtual object PreviousTokenStream
60  {
61  get
62  {
63  if (tokenStreams == null)
64  {
65  throw new AlreadyClosedException("this Analyzer is closed");
66  }
67  return tokenStreams.Get();
68  }
69  set
70  {
71  if (tokenStreams == null)
72  {
73  throw new AlreadyClosedException("this Analyzer is closed");
74  }
75  tokenStreams.Set(value);
76  }
77  }
78 
79  [Obsolete()]
80  protected internal bool overridesTokenStreamMethod = false;
81 
92  [Obsolete("This is only present to preserve back-compat of classes that subclass a core analyzer and override tokenStream but not reusableTokenStream ")]
93  protected internal virtual void SetOverridesTokenStreamMethod<TClass>()
94  where TClass : Analyzer
95  {
96  try
97  {
98  System.Reflection.MethodInfo m = this.GetType().GetMethod("TokenStream", new[] { typeof(string), typeof(System.IO.TextReader) });
99  overridesTokenStreamMethod = m.DeclaringType != typeof(TClass);
100  }
101  catch (MethodAccessException)
102  {
103  // can't happen, as baseClass is subclass of Analyzer
104  overridesTokenStreamMethod = false;
105  }
106  }
107 
108 
123  public virtual int GetPositionIncrementGap(String fieldName)
124  {
125  return 0;
126  }
127 
140  public virtual int GetOffsetGap(IFieldable field)
141  {
142  return field.IsTokenized ? 1 : 0;
143  }
144 
146  public void Close()
147  {
148  Dispose();
149  }
150 
151  public virtual void Dispose()
152  {
153  Dispose(true);
154  }
155 
156  protected virtual void Dispose(bool disposing)
157  {
158  if (isDisposed) return;
159 
160  if (disposing)
161  {
162  if (tokenStreams != null)
163  {
164  tokenStreams.Close();
165  tokenStreams = null;
166  }
167  }
168  isDisposed = true;
169  }
170  }
171 }