11

We use a lot of legacy package.html files in our project and we want to convert them to package-info.java files. Doing that manually isn't an option (way too many files). Is there a good way to automate that?

We want to convert them for a couple of reasons:

  • From the javadoc specs: This file is new in JDK 5.0, and is preferred over package.html.

  • To not mix both types of files in the same codebase

  • To avoid that Intellij/Eclipse builds put those *.html files in our classes dirs (and possibly in a release binary jars) so they behave like our other normal html resources.

3 Answers 3

7
+50

You may need to change the directory separator if you're not running windows. Also, the conversion is a bit of a hack, but it should work. Out of curiosity, how many packages do you have that manual isn't an option?

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("\\\\", "."); } } 
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think it's just a case of renaming -- see download.oracle.com/javase/1.5.0/docs/tooldocs/windows/…
Quite true. It should be easy to grab the content from inside the html body tag, put that in indented form into a javadoc comment, and append the FQN for the package. I'll put it down as a project to do over the next week.
This is a good start, tnx :) Just wondering if it covers all corner cases, like {@link ...} {@value } and whatnot. Also, how do xml escape characters come into this? Also, we'll probably do trim() around the <body> stuff, as well fail-fast if it doesn't find <body>. And sometimes <html> and <body> are on the same line.
What corner cases? It doesn't perform any conversion whatsoever. And you are correct that it doesn't perform the parsing properly, but I doubt that the javadoc tool did full HTML parsing on package.html.
0

The IntelliJ guys have made an intention to do this for all files. It's been resolved and will probably be released in the next IntelliJ release.

Comments

0

To do this in batch mode in IDEA:

  • In settings, activate the inspection gadget "'package.html' may be converted to 'package-info.java' inspection"
  • Open a package.html file
  • You see a banner fix the inspection on top the file
  • Click on the settings icon at the right on the banner
  • Select "Run inspection on" >> "Whole project"
  • Click on "Convert to package-info.java" >> OK
  • Optionally remove the inappropriate lines (sed -i "/Put @see and @since/d" `find . -name "package-info.java"`)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.