scan Resource Names By File - Java Reflection

Java examples for Reflection:Class Path

Description

scan Resource Names By File

Demo Code

/*//from  w ww . j  a v  a2s .c  o m
 * Copyright Bruce Liang (ldcsaa@gmail.com)
 *
 * Version   : JessMA 3.5.1
 * Author   : Bruce Liang
 * Website   : http://www.jessma.org
 * Project   : http://www.oschina.net/p/portal-basic
 * Blog      : http://www.cnblogs.com/ldcsaa
 * WeiBo   : http://weibo.com/u/1402935851
 * QQ Group   : 75375912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

public class Main{
    private static final char PATH_SEP_CHAR = '/';
    private static final String PATH_SEP_STR = "/";
    
    public static final void scanResourceNamesByFile(String filePath,
            final int packageStartIndex, final boolean recursive,
            Set<String> names) {
        scanResourceNamesByFile(filePath, packageStartIndex, recursive,
                null, names);
    }
    
    public static final void scanResourceNamesByFile(String filePath,
            int packageStartIndex, final boolean recursive,
            final ResourceFilter filter, Set<String> names) {
        if (filePath.endsWith(PATH_SEP_STR))
            filePath = filePath.substring(0, filePath.length() - 1);
        if (packageStartIndex > filePath.length())
            packageStartIndex = filePath.length();

        final String basePackagePath = filePath
                .substring(packageStartIndex);

        File dir = new File(filePath);
        File[] files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                if (file.isDirectory())
                    return recursive;

                if (filter != null) {
                    String fileName = file.getName();
                    return filter.accept(basePackagePath, fileName);
                }

                return true;
            }
        });

        for (File file : files) {
            String name = file.getName();

            if (file.isDirectory()) {
                String subPath = filePath + PATH_SEP_CHAR + name;

                scanResourceNamesByFile(subPath, packageStartIndex,
                        recursive, filter, names);
            } else {
                if (basePackagePath.length() != 0)
                    name = basePackagePath + PATH_SEP_CHAR + name;
                if (name.startsWith(PATH_SEP_STR))
                    name = name.substring(1, name.length());

                names.add(name);
            }
        }
    }
}

Related Tutorials