01: package org.apache.lucene.index;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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: * @author Daniel Naber / Bernhard Messer
28: * @version $rcs = ' $Id: Exp $ ' ;
29: */
30: public class IndexFileNameFilter implements FilenameFilter {
31:
32: static IndexFileNameFilter singleton = new IndexFileNameFilter();
33: private HashSet extensions;
34: private HashSet extensionsInCFS;
35:
36: public IndexFileNameFilter() {
37: extensions = new HashSet();
38: for (int i = 0; i < IndexFileNames.INDEX_EXTENSIONS.length; i++) {
39: extensions.add(IndexFileNames.INDEX_EXTENSIONS[i]);
40: }
41: extensionsInCFS = new HashSet();
42: for (int i = 0; i < IndexFileNames.INDEX_EXTENSIONS_IN_COMPOUND_FILE.length; i++) {
43: extensionsInCFS
44: .add(IndexFileNames.INDEX_EXTENSIONS_IN_COMPOUND_FILE[i]);
45: }
46: }
47:
48: /* (non-Javadoc)
49: * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
50: */
51: public boolean accept(File dir, String name) {
52: int i = name.lastIndexOf('.');
53: if (i != -1) {
54: String extension = name.substring(1 + i);
55: if (extensions.contains(extension)) {
56: return true;
57: } else if (extension.startsWith("f")
58: && extension.matches("f\\d+")) {
59: return true;
60: } else if (extension.startsWith("s")
61: && extension.matches("s\\d+")) {
62: return true;
63: }
64: } else {
65: if (name.equals(IndexFileNames.DELETABLE))
66: return true;
67: else if (name.startsWith(IndexFileNames.SEGMENTS))
68: return true;
69: }
70: return false;
71: }
72:
73: /**
74: * Returns true if this is a file that would be contained
75: * in a CFS file. This function should only be called on
76: * files that pass the above "accept" (ie, are already
77: * known to be a Lucene index file).
78: */
79: public boolean isCFSFile(String name) {
80: int i = name.lastIndexOf('.');
81: if (i != -1) {
82: String extension = name.substring(1 + i);
83: if (extensionsInCFS.contains(extension)) {
84: return true;
85: }
86: if (extension.startsWith("f") && extension.matches("f\\d+")) {
87: return true;
88: }
89: }
90: return false;
91: }
92:
93: public static IndexFileNameFilter getFilter() {
94: return singleton;
95: }
96: }
|