View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.mailrepository.filepair;
23  
24  import java.io.File;
25  import java.io.FilenameFilter;
26  
27  /**
28   * This filters files based on the extension and is tailored to provide
29   * backwards compatibility of the numbered repositories that Avalon does.
30   *
31   */
32  public class NumberedRepositoryFileFilter implements FilenameFilter {
33      private String postfix;
34      private String prefix;
35  
36      /**
37       * Default Constructor
38       * 
39       * @param extension the extension for which checked
40       */
41      public NumberedRepositoryFileFilter(final String extension) {
42          postfix = extension;
43          prefix = ".Repository";
44      }
45  
46      /**
47       * @see java.io.FilenameFilter#accept(File, String)
48       */
49      public boolean accept(final File file, final String name) {
50          //System.out.println("check: " + name);
51          //System.out.println("post: " + postfix);
52          if (!name.endsWith(postfix)) {
53              return false;
54          }
55          //Look for a couple of digits next
56          int pos = name.length() - postfix.length();
57          //We have to find at least one digit... if not then this isn't what we want
58          if (!Character.isDigit(name.charAt(pos - 1))) {
59              return false;
60          }
61          pos--;
62          while (pos >= 1 && Character.isDigit(name.charAt(pos - 1))) {
63              //System.out.println("back one");
64              pos--;
65          }
66          //System.out.println("sub: " + name.substring(0, pos));
67          //Now want to check that we match the rest
68          return name.substring(0, pos).endsWith(prefix);
69      }
70  }
71  
72