`
wangchuanyin
  • 浏览: 32759 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

搜索Jar包中的类或包

 
阅读更多
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class FindClassFromJars
    /**
     * 查询格式:[查询参数] [关键字..]<br/>
     * 查询参数:--help:显示"软件帮助信息";<br/>
     *  查询级别{0,1}(默认为3):
     *  <li>0:完全匹配;</li>
     *  <li>1:忽略大小写的完全匹配;</li>
     *  <li>2:模糊匹配;</li>
     *  <li>3:忽略大小写的模糊匹配.</li>
     * @param keys
     */
    public static void findKey(String[] keys) {//未考虑将参数写在最后
        int length = keys.length;
        int level = 3;
        File file = new File(".");
        if(length == 0)
            showHelp();
        if(length == 1)
            if("--help".equalsIgnoreCase(keys[0]) || "/?".equalsIgnoreCase(keys[0]))
                showHelp();
            else
                findKey(keys, 3, file);
        if (length > 1)
            try {
                level = Integer.parseInt(keys[0]);//转换搜索级别
                findKey(keys, level, file);
            } catch (NumberFormatException e) {//转换异常,没有输入搜索级别时
                if (!"--help".equalsIgnoreCase(keys[0]) && !"/?".equalsIgnoreCase(keys[0]))
                    findKey(keys, level, file);
                else
                    showHelp();
            }
    }
    
    private static void showHelp() {
        System.out.println("查询格式:[查询参数] [关键字..]\n查询参数:--help:显示\"软件帮助信息\";\n查询级别{0,1,2,3}(默认为3):\n0:完全匹配;\n1:忽略大小写的完全匹配;\n2:模糊匹配;\n3:忽略大小写的模糊匹配.");
    }
    
    private static void findKey(String[] keys, int level, File directory) {
        for(File file : directory.listFiles()) {
            String pathName = file.getName();
            if(file.isDirectory())
                findKey(keys, level, file);
            else if(pathName.toLowerCase().endsWith(".jar"))
                try {
                    String path = file.getPath();
                    Enumeration<JarEntry> enu = new JarFile(path).entries();
                    while (enu.hasMoreElements()) {
                        String fullName = enu.nextElement().getName();
                        String[] packages = fullName.split("/");
                        String[] fileName = packages[packages.length - 1].split("\\.");// 类完整名
                        String packageName = fullName.replace("/", ".");
                        for (int i = 1; i < keys.length; i++) {// 多参时分为全字匹配与部分匹配
                            boolean flag = false;
                            switch (level) {
                            case 0:
                                if (fileName[0].equals(keys[i]))// 完全匹配
                                    flag = true;
                                break;
                            case 1:
                                if (fileName[0].equalsIgnoreCase(keys[i]))// 忽略大小写的完全匹配
                                    flag = true;
                                break;
                            case 2:
                                if (packageName.contains(keys[i]))// 模糊匹配
                                    flag = true;
                                break;
                            case 3:
                                if (packageName.toLowerCase().contains(keys[i].toLowerCase()))// 忽略大小写的模糊匹配
                                    flag = true;
                                break;
                            }
                            if (flag)
                                System.out.println("找到文件" + packageName + ",\n\t" + path + "\n");
                        }
                    }
                } catch (IOException e) {
                    System.out.println("error!");
                    //e.printStackTrace();
                    //System.out.println(e.getMessage());
                }
        }
    }

    public static void main(String[] args) {
        findKey(args);
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics