001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.repository.file;
021
022import java.io.File;
023import java.io.FilenameFilter;
024
025/**
026 * This filters files based on the extension and is tailored to provide
027 * backwards compatibility of the numbered repositories that Avalon does.
028 */
029public class NumberedRepositoryFileFilter implements FilenameFilter {
030    private final String postfix;
031    private final String prefix;
032
033    /**
034     * Default Constructor
035     * 
036     * @param extension
037     *            the extension for which checked
038     */
039    public NumberedRepositoryFileFilter(String extension) {
040        postfix = extension;
041        prefix = ".Repository";
042    }
043
044    /**
045     * @see java.io.FilenameFilter#accept(File, String)
046     */
047    public boolean accept(File file, String name) {
048        // System.out.println("check: " + name);
049        // System.out.println("post: " + postfix);
050        if (!name.endsWith(postfix)) {
051            return false;
052        }
053        // Look for a couple of digits next
054        int pos = name.length() - postfix.length();
055        // We have to find at least one digit... if not then this isn't what we
056        // want
057        if (!Character.isDigit(name.charAt(pos - 1))) {
058            return false;
059        }
060        pos--;
061        while (pos >= 1 && Character.isDigit(name.charAt(pos - 1))) {
062            // System.out.println("back one");
063            pos--;
064        }
065        // System.out.println("sub: " + name.substring(0, pos));
066        // Now want to check that we match the rest
067        return name.substring(0, pos).endsWith(prefix);
068    }
069}