Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically convert all javadoc package.html files into package-info.java files?

Tags:

java

javadoc

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.

like image 730
Geoffrey De Smet Avatar asked Jan 28 '11 10:01

Geoffrey De Smet


People also ask

How to generate package-info java?

Creating a package-info file is fairly simple: we can create it manually or seek IDE help for generating the same. In IntelliJ IDEA, we can right-click on the package and select New-> package-info. java: Eclipse's New Java Package option allows us to generate a package-info.

What is package-info java in JAXB?

package-info. java is a way to apply java annotations at the package level. In this case Jaxb is using package-level annotations to indicate the namespace, and to specify namespace qualification for attributes (source).

What is a package-info Java file?

The package-info. java is a Java file that can be added to any Java source package. Its purpose is to provide a home for package level documentation and package level annotations. Simply create the package-info. java file and add the package declaration that it relates to in the file.


1 Answers

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("\\\\", ".");
    }

}
like image 152
SEK Avatar answered Sep 20 '22 06:09

SEK