pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / IndexFileNameFilter.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.File;
21 import java.io.FilenameFilter;
22 import java.util.HashSet;
23
24 /**
25  * Filename filter that accept filenames and extensions only created by Lucene.
26  *
27  * @lucene.internal
28  */
29 public class IndexFileNameFilter implements FilenameFilter {
30
31   private static IndexFileNameFilter singleton = new IndexFileNameFilter();
32   private HashSet<String> extensions;
33   private HashSet<String> extensionsInCFS;
34
35   // Prevent instantiation.
36   private IndexFileNameFilter() {
37     extensions = new HashSet<String>();
38     for (String ext : IndexFileNames.INDEX_EXTENSIONS) {
39       extensions.add(ext);
40     }
41     extensionsInCFS = new HashSet<String>();
42     for (String ext : IndexFileNames.INDEX_EXTENSIONS_IN_COMPOUND_FILE) {
43       extensionsInCFS.add(ext);
44     }
45   }
46
47   /* (non-Javadoc)
48    * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
49    */
50   public boolean accept(File dir, String name) {
51     int i = name.lastIndexOf('.');
52     if (i != -1) {
53       String extension = name.substring(1+i);
54       if (extensions.contains(extension)) {
55         return true;
56       } else if (extension.startsWith("f") &&
57                  extension.matches("f\\d+")) {
58         return true;
59       } else if (extension.startsWith("s") &&
60                  extension.matches("s\\d+")) {
61         return true;
62       }
63     } else {
64       if (name.equals(IndexFileNames.DELETABLE)) return true;
65       else if (name.startsWith(IndexFileNames.SEGMENTS)) return true;
66     }
67     return false;
68   }
69
70   /**
71    * Returns true if this is a file that would be contained
72    * in a CFS file.  This function should only be called on
73    * files that pass the above "accept" (ie, are already
74    * known to be a Lucene index file).
75    */
76   public boolean isCFSFile(String name) {
77     int i = name.lastIndexOf('.');
78     if (i != -1) {
79       String extension = name.substring(1+i);
80       if (extensionsInCFS.contains(extension)) {
81         return true;
82       }
83       if (extension.startsWith("f") &&
84           extension.matches("f\\d+")) {
85         return true;
86       }
87     }
88     return false;
89   }
90
91   public static IndexFileNameFilter getFilter() {
92     return singleton;
93   }
94 }