001    package fang;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileReader;
006    import java.io.FileWriter;
007    import java.io.PrintWriter;
008    import java.util.regex.Pattern;
009    
010    /**
011     * This class moves and refactors all of the packaged
012     * classes into the single package fang for easy
013     * importing.  The refactoring is very basic.
014     * @author Jam Jenkins
015     */
016    public class FangPackageGenerator
017    {
018        /**
019         * Copies and refactors classes from subpackages
020         * to the primary package fang.  The refactoring
021         * is 1. change the package statement and 
022         * 2. remove all imports of fang subpackages. 
023         * @param args
024         * @throws Exception
025         */
026        public static void main(String[] args)
027        throws Exception
028        {
029            //Note 1: it is important that experimental
030            //is first so that if there are conflicting
031            //filenames, only the stable one makes it.
032            //Note 2: the package fang.resources must
033            //contain the same contents as fang.ui.resources
034            File[] directoriesToCopy =
035                {
036                    new File("fang/experimental"),
037                    new File("fang/attributes"),
038                    new File("fang/core"),
039                    new File("fang/media"),
040                    new File("fang/network"),
041                    new File("fang/sprites"),
042                    new File("fang/transformers"),
043                    new File("fang/ui"),
044                    new File("fang/util"),
045                };
046            for (File directory: directoriesToCopy)
047            {
048                for (File file: directory.listFiles())
049                {
050                    if (file.isFile())
051                    {
052                        if (file.getName().endsWith(".java"))
053                        {
054                            String targetName = "fang/" + file.getName();
055                            FileWriter target = new FileWriter(targetName);
056                            FileReader source = new FileReader(file);
057                            BufferedReader reader = new BufferedReader(source);
058                            PrintWriter writer = new PrintWriter(target);
059                            String line = reader.readLine();
060                            while (line != null)
061                            {
062                                if (line.startsWith("package"))
063                                    line = "package fang;";
064                                if(line.trim().startsWith("import static fang"))
065                                {
066                                    String[] parts=line.split(Pattern.quote("."));
067                                    line="import static fang."+
068                                        parts[parts.length-2]+"."+
069                                        parts[parts.length-1];
070                                }
071                                if (!line.startsWith("import fang"))
072                                    writer.println(line);
073                                line = reader.readLine();
074                            }
075                            reader.close();
076                            writer.close();
077                        }
078                    }
079    
080                }
081            }
082        }
083    }