Class PackageRenamer


  • public class PackageRenamer
    extends java.lang.Object
    This class performs package renaming. It demonstrates the following: a) Reading the properties file to be a reference for changing the package name from your source code. b) Traverse source root directory for creating a corresponding output directory and finding the java source file(s) to be changing the package name. c) Search and replace the old TopLink package name(s) with new one(s) according to the reference. You will be able to see the logging message at the command line window where the PackageRenamer is running.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.lang.String SYSTEM_OUT  
    • Constructor Summary

      Constructors 
      Constructor Description
      PackageRenamer()
      The constructor of a PackageRenamer class.
      PackageRenamer​(java.lang.String propertiesFileName)  
      PackageRenamer​(java.lang.String[] args)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.String bannerText()  
      void binaryCopy​(java.io.File inFile, java.io.File outFile)
      Do a binary copy of the file byte buffer by byte buffer.
      protected boolean bufferContainsNullChar​(byte[] buffer, int bufferLength)  
      java.io.File buildAndCheckDestinationFile​(java.lang.String aDirString)
      INTERNAL Creates a destination directory File object under the path passed, verifying correctness.
      java.io.File buildAndCheckExistingDirFile​(java.lang.String aDirString)  
      java.io.PrintWriter buildAndCheckLogWriter​(java.lang.String logFileString)  
      protected void cleanup()  
      void createDestinationDirectory​(java.io.File aDirectory)
      This method creates an output directory for post-rename file(s).
      static boolean directoryIsSubdirectory​(java.io.File directory1, java.io.File directory2)
      Return true if directory2 is contained within directory1.
      java.io.File existingDirectoryFromPrompt()  
      static java.lang.String getDefaultPropertiesFileName()  
      java.io.BufferedReader getReader()  
      boolean isExtensionSupported​(java.lang.String extension)
      Return true if the PackageRenamer should work on the given file extension.
      void logln​(java.lang.String str)  
      static void main​(java.lang.String[] args)
      Main method to run the PackageRenamer
      java.lang.String parseFileExtension​(java.io.File aFile)
      Returns the extension of the given file.
      protected java.io.File promptForDestinationDirectory()
      INTERNAL Prompt from System.in for an empty or non-existent directory to use as the destination directory.
      java.util.Properties readChangesFile​(java.lang.String filename)
      This readChangesFile() method reads the given properties file to be a reference for renaming TopLink package name.
      static java.lang.String replace​(java.lang.String str, java.lang.String oldChars, java.lang.String newChars)
      Do a search and replace in a string.
      java.lang.String returnNewFileNameIfRequired​(java.lang.String aSourceFileNameWithoutRoot)
      Renames a file based on the properties passed.
      void run()
      This run() method performs, reading the properties file into properties variable to be a reference for changing package name.
      void runSearchAndReplacePackageName​(java.io.File sourceFile)
      This runSearchAndReplacePackageName() reads a pre-rename source file all into string variable and replacing the old package names with the new ones according to the properties file.
      protected java.io.PrintWriter streamForNonExistentFilePrompt()  
      void traverseSourceDirectory​(java.io.File aDirectoryString)
      This traverseSourceDirectory() traverse source-root-directory, creating an corresponding output directory, and calling another method for replacing old TopLink package name.
      static void usage()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PackageRenamer

        public PackageRenamer()
        The constructor of a PackageRenamer class.
      • PackageRenamer

        public PackageRenamer​(java.lang.String propertiesFileName)
      • PackageRenamer

        public PackageRenamer​(java.lang.String[] args)
    • Method Detail

      • bannerText

        protected java.lang.String bannerText()
      • binaryCopy

        public void binaryCopy​(java.io.File inFile,
                               java.io.File outFile)
                        throws java.io.FileNotFoundException,
                               java.io.IOException
        Do a binary copy of the file byte buffer by byte buffer.
        Parameters:
        inFile - The file to copy
        outFile - The destination file
        Throws:
        java.io.FileNotFoundException - if either of the two files does not exist
        java.io.IOException - if any other IO related error occurs
      • bufferContainsNullChar

        protected boolean bufferContainsNullChar​(byte[] buffer,
                                                 int bufferLength)
      • buildAndCheckDestinationFile

        public java.io.File buildAndCheckDestinationFile​(java.lang.String aDirString)
        INTERNAL Creates a destination directory File object under the path passed, verifying correctness.
        Parameters:
        aDirString - The path to the directory File object to create
        Returns:
        The destination directory File object
      • buildAndCheckExistingDirFile

        public java.io.File buildAndCheckExistingDirFile​(java.lang.String aDirString)
      • buildAndCheckLogWriter

        public java.io.PrintWriter buildAndCheckLogWriter​(java.lang.String logFileString)
      • cleanup

        protected void cleanup()
      • createDestinationDirectory

        public void createDestinationDirectory​(java.io.File aDirectory)
        This method creates an output directory for post-rename file(s).
        Parameters:
        aDirectory - The output directory to create
      • directoryIsSubdirectory

        public static boolean directoryIsSubdirectory​(java.io.File directory1,
                                                      java.io.File directory2)
        Return true if directory2 is contained within directory1. Both directories must be absolute.
        Parameters:
        directory1 - The higher level directory
        directory2 - The lower level directory
        Returns:
        TRUE if directory2 is a subdirectory of directory1
      • existingDirectoryFromPrompt

        public java.io.File existingDirectoryFromPrompt()
      • getDefaultPropertiesFileName

        public static java.lang.String getDefaultPropertiesFileName()
      • getReader

        public java.io.BufferedReader getReader()
      • isExtensionSupported

        public boolean isExtensionSupported​(java.lang.String extension)
        Return true if the PackageRenamer should work on the given file extension.
        Parameters:
        extension - The file extension to check for being supported
        Returns:
        TRUE if the extension is supported
      • logln

        public void logln​(java.lang.String str)
        Parameters:
        str - The String to log
      • main

        public static void main​(java.lang.String[] args)
        Main method to run the PackageRenamer
        Parameters:
        args - Command line arguments
      • parseFileExtension

        public java.lang.String parseFileExtension​(java.io.File aFile)
        Returns the extension of the given file.
        Parameters:
        aFile - The file of which to retrieve the extension
        Returns:
        The file extension or an empty string if none was found.
      • promptForDestinationDirectory

        protected java.io.File promptForDestinationDirectory()
        INTERNAL Prompt from System.in for an empty or non-existent directory to use as the destination directory.
        Returns:
        The destination directory File object
      • readChangesFile

        public java.util.Properties readChangesFile​(java.lang.String filename)
        This readChangesFile() method reads the given properties file to be a reference for renaming TopLink package name.
        Parameters:
        filename - The input file to use for the renaming
        Returns:
        The Properties object containing the renaming information
      • run

        public void run()
        This run() method performs, reading the properties file into properties variable to be a reference for changing package name. creating an destination-root-direetory. and, calling traverseSourceDirectory() method.
      • streamForNonExistentFilePrompt

        protected java.io.PrintWriter streamForNonExistentFilePrompt()
      • runSearchAndReplacePackageName

        public void runSearchAndReplacePackageName​(java.io.File sourceFile)
        This runSearchAndReplacePackageName() reads a pre-rename source file all into string variable and replacing the old package names with the new ones according to the properties file.
        Parameters:
        sourceFile - The source file to process
      • replace

        public static java.lang.String replace​(java.lang.String str,
                                               java.lang.String oldChars,
                                               java.lang.String newChars)
        Do a search and replace in a string.
        Parameters:
        str - The original String
        oldChars - The character pattern to replace
        newChars - The character pattern to replace the existing with
        Returns:
        the modified String
      • returnNewFileNameIfRequired

        public java.lang.String returnNewFileNameIfRequired​(java.lang.String aSourceFileNameWithoutRoot)
        Renames a file based on the properties passed.
        Parameters:
        aSourceFileNameWithoutRoot - The original filename
        Returns:
        The new filename, regardless of whether is has been changed
      • traverseSourceDirectory

        public void traverseSourceDirectory​(java.io.File aDirectoryString)
        This traverseSourceDirectory() traverse source-root-directory, creating an corresponding output directory, and calling another method for replacing old TopLink package name.
        Parameters:
        aDirectoryString - The source root directory to traverse
      • usage

        public static void usage()