Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IndexFileNameFilter.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 System.Collections.Generic;
20 
21 namespace Lucene.Net.Index
22 {
23 
24  /// <summary>Filename filter that accept filenames and extensions only created by Lucene. </summary>
25  public class IndexFileNameFilter
26  {
27 
28  private static IndexFileNameFilter singleton = new IndexFileNameFilter();
29  private HashSet<String> extensions;
30  private HashSet<String> extensionsInCFS;
31 
32  // Prevent instantiation.
33  private IndexFileNameFilter()
34  {
35  extensions = new HashSet<String>();
36  for (int i = 0; i < IndexFileNames.INDEX_EXTENSIONS.Length; i++)
37  {
38  extensions.Add(IndexFileNames.INDEX_EXTENSIONS[i]);
39  }
40  extensionsInCFS = new HashSet<String>();
41  for (int i = 0; i < IndexFileNames.INDEX_EXTENSIONS_IN_COMPOUND_FILE.Length; i++)
42  {
43  extensionsInCFS.Add(IndexFileNames.INDEX_EXTENSIONS_IN_COMPOUND_FILE[i]);
44  }
45  }
46 
47  /* (non-Javadoc)
48  * <see cref="java.io.FilenameFilter.accept(java.io.File, java.lang.String)"/>
49  */
50  public virtual bool Accept(System.IO.FileInfo dir, System.String name)
51  {
52  int i = name.LastIndexOf((System.Char) '.');
53  if (i != - 1)
54  {
55  System.String extension = name.Substring(1 + i);
56  if (extensions.Contains(extension))
57  {
58  return true;
59  }
60  else if (extension.StartsWith("f") && (new System.Text.RegularExpressions.Regex("f\\d+")).Match(extension).Success)
61  {
62  return true;
63  }
64  else if (extension.StartsWith("s") && (new System.Text.RegularExpressions.Regex("s\\d+")).Match(extension).Success)
65  {
66  return true;
67  }
68  }
69  else
70  {
71  if (name.Equals(IndexFileNames.DELETABLE))
72  return true;
73  else if (name.StartsWith(IndexFileNames.SEGMENTS))
74  return true;
75  }
76  return false;
77  }
78 
79  /// <summary> Returns true if this is a file that would be contained
80  /// in a CFS file. This function should only be called on
81  /// files that pass the above "accept" (ie, are already
82  /// known to be a Lucene index file).
83  /// </summary>
84  public virtual bool IsCFSFile(System.String name)
85  {
86  int i = name.LastIndexOf((System.Char) '.');
87  if (i != - 1)
88  {
89  System.String extension = name.Substring(1 + i);
90  if (extensionsInCFS.Contains(extension))
91  {
92  return true;
93  }
94  if (extension.StartsWith("f") && (new System.Text.RegularExpressions.Regex("f\\d+")).Match(extension).Success)
95  {
96  return true;
97  }
98  }
99  return false;
100  }
101 
102  public static IndexFileNameFilter Filter
103  {
104  get { return singleton; }
105  }
106  }
107 }