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

前端之家收集整理的这篇文章主要介绍了如何自动将所有javadoc package.html文件转换为package-info.java文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们在我们的项目中使用了很多遗留的package.html文件,我们希望将它们转换为package-info. java文件.手动执行不是一个选项(文件太多).有没有自动化的好方法

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

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

解决方法

如果您没有运行Windows,则可能需要更改目录分隔符.此外,转换是一个黑客,但它应该工作.出于好奇,你有多少包这个手册不是一个选择?
  1. public class Converter {
  2.  
  3. public static void main(String[] args) {
  4. File rootDir = new File(".");
  5. renamePackageToPackageInfo(rootDir);
  6. }
  7.  
  8. private static void renamePackageToPackageInfo(File dir) {
  9. File[] files = dir.listFiles(new FilenameFilter() {
  10. @Override
  11. public boolean accept(File dir,String name) {
  12. return "package.html".equals(name);
  13. }
  14. });
  15. for (File file : files) {
  16. convertFile(file);
  17. }
  18. // now recursively rename all the child directories.
  19. File[] dirs = dir.listFiles(new FileFilter() {
  20. @Override
  21. public boolean accept(File pathname) {
  22. return pathname.isDirectory();
  23. }
  24. });
  25. for (File subdir : dirs) {
  26. renamePackageToPackageInfo(subdir);
  27. }
  28. }
  29.  
  30. private static void convertFile(File html) {
  31. // determine the FQN package name
  32. String fqpn = getPackageName(html);
  33.  
  34. // check if package-info.java already exists
  35. File packageInfo = new File(html.getParent(),"package-info.java");
  36. if (packageInfo.exists()) {
  37. System.out.println("package-info.java already exists for package: "+fqpn);
  38. return;
  39. }
  40.  
  41. // create the i/o streams,and start pumping the data
  42. try {
  43. PrintWriter out = new PrintWriter(packageInfo);
  44. BufferedReader in = new BufferedReader(new FileReader(html));
  45. out.println("/**");
  46.  
  47. // skip over the headers
  48. while (true) {
  49. String line = in.readLine();
  50. if (line.equalsIgnoreCase("<BODY>"))
  51. break;
  52. }
  53. // now pump the file into the package-info.java file
  54. while (true) {
  55. String line = in.readLine();
  56. if (line.equalsIgnoreCase("</BODY>"))
  57. break;
  58. out.println(" * " + line);
  59. }
  60.  
  61. out.println("*/");
  62. out.println("package "+fqpn+";");
  63. out.close();
  64. in.close();
  65. } catch (FileNotFoundException e) {
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70.  
  71. // queue the package.html file for deletion
  72. //html.deleteOnExit();
  73. }
  74.  
  75. private static String getPackageName(File file) {
  76. StringBuilder path = new StringBuilder(file.getParent());
  77. // trim the first two characters (./ or .\)
  78. path.delete(0,2);
  79. // then convert all separators into . (HACK: should use directory separator property)
  80. return path.toString().replaceAll("\\\\",".");
  81. }
  82.  
  83. }

猜你在找的Java相关文章