微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何自动将所有javadoc package.html文件转换为package-info.java文件?

我们在我们的项目中使用了很多遗留的package.html文件,我们希望将它们转换为package-info. java文件.手动执行不是一个选项(文件太多).有没有自动化的好方法

我们想要转换它们有几个原因:

>从javadoc规范:这个文件是JDK 5.0中的新文件,并且优于package.html.
>不要在同一个代码库中混合两种类型的文件
>为了避免Intellij / Eclipse构建将这些* .html文件放在我们的类dirs(并且可能在一个发行版二进制jar)中,所以它们的行为就像我们其他正常的html资源.

解决方法

如果您没有运行Windows,则可能需要更改目录分隔符.此外,转换是一个黑客,但它应该工作.出于好奇,你有多少包这个手册不是一个选择?
public class Converter {

    public static void main(String[] args) {
        File rootDir = new File(".");
        renamePackagetoPackageInfo(rootDir);
    }

    private static void renamePackagetoPackageInfo(File dir) {
        File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir,String name) {
                return "package.html".equals(name);
            }
        });
        for (File file : files) {
            convertFile(file);
        }
        // Now recursively rename all the child directories.
        File[] dirs = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }
        });
        for (File subdir : dirs) {
            renamePackagetoPackageInfo(subdir);
        }
    }

    private static void convertFile(File html) {
        // determine the FQN package name
        String fqpn = getPackageName(html);

        // check if package-info.java already exists
        File packageInfo = new File(html.getParent(),"package-info.java");
        if (packageInfo.exists()) {
            System.out.println("package-info.java already exists for package: "+fqpn);
            return; 
        }

        // create the I/O streams,and start pumping the data
        try {
            PrintWriter out = new PrintWriter(packageInfo);
            BufferedReader in = new BufferedReader(new FileReader(html));
            out.println("/**");

            // skip over the headers
            while (true) {
                String line = in.readLine();
                if (line.equalsIgnoreCase("<BODY>"))
                    break;
            }
            // Now pump the file into the package-info.java file
            while (true) {
                String line = in.readLine();
                if (line.equalsIgnoreCase("</BODY>"))
                    break;
                out.println(" * " + line);
            }

            out.println("*/");
            out.println("package "+fqpn+";");
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printstacktrace();
        } catch (IOException e) {
            e.printstacktrace();
        }

        // queue the package.html file for deletion
        //html.deleteOnExit();
    }

    private static String getPackageName(File file) {
        StringBuilder path = new StringBuilder(file.getParent());
        // trim the first two characters (./ or .\)
        path.delete(0,2);
        // then convert all separators into . (HACK: should use directory separator property)
        return path.toString().replaceAll("\\\\",".");
    }

}

原文地址:https://www.jb51.cc/java/126231.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐