// *************************************** // *** copyDirectoryContents() *** // *************************************** // copy all files from source to output private boolean copyDirectoryContents(File sourceFile, File outputFile) { // flag for success or not boolean allFilesCopied = true; // get list of all files in source directory String fileList[] = sourceFile.list(); // copy each file for (int i = 0; i < fileList.length; i++) { // get current file name String currItemName = fileList[i]; File currItemFile = new File(sourceFile, currItemName); // if a directory, call directory copy method if (currItemFile.isDirectory()) { // copy this directory (and its contents etc.) String directoryName = currItemName; File sourceDirectory = currItemFile; File desinationDirectory = new File(outputFile, directoryName); trace("copyDirectoryContents", "** directory ** = " + currItemName, 0); // create destination dir if required, and copy all contents // ( RECURSION !) copyDirectory(sourceDirectory, desinationDirectory); } // if else { // must be a file - so use file copy trace("copyDirectoryContents", "fileList = " + currItemName, 0); // get references to source / desintation filepath+name File oldFile = currItemFile; File newFile = new File(outputFile, currItemName); // copy file contents ... boolean isCopiedSuccessfully = true; isCopiedSuccessfully = copyFile(oldFile, newFile); // if error then flag at least one file didn't copy if (!isCopiedSuccessfully) { allFilesCopied = false; } // if } // else } // for return allFilesCopied; } // method
// ************************* // *** renameHTMLtoPHP() *** // ************************* // rename all the HTML files to end in ".php" private boolean renameHTMLtoPHP(File outputPathFile, String[] htmlFileList) { // flag to indicate if all went well boolean allSucessfullyRenamed = true; // loop for each HTML file for (int i = 0; i < htmlFileList.length; i++) { // get current HTML file name String htmlFileName = htmlFileList[i]; // create String for .php filename String phpFileName = htmlFileName.replace(".html", ".php"); trace( "renameHTMLtoPHP", "htmlFileName = '" + htmlFileName + "', phpFileName = '" + phpFileName + "'", 0); // get current FILE object File htmlFile = new File(outputPathFile, htmlFileName); // get reference file with new (.php) name File phpFile = new File(outputPathFile, phpFileName); // rename file boolean isRenamed = renameFile(htmlFile, phpFile); if (!isRenamed) { // flag that at least one file didn't rename sucessfully allSucessfullyRenamed = false; } // if } // for return allSucessfullyRenamed; } // method
// ************************* // *** constructor // *********************** public PreProcess() { // get current working directory File workingDirectory = new File(System.getProperty("user.dir")); // create File objects for source / destination directories File sourcePathFile = new File(workingDirectory, sourcePathName); File outputPathFile = new File(workingDirectory, outputPathName); // get File object for tracking directory File trackingFolderSourcePathFile = new File(workingDirectory, PHP_TRACKING_DIRECTORY_PATH); File trackingFolderDestinationPathFile = new File(workingDirectory, outputPathName + "/" + PHP_TRACKING_DIRECTORY_NAME); System.out.println("in constructor"); trace("PreProcess", "-----------------------------------step (0)", 0); trace("PreProcess", "(0) delete contents of output directory", 0); // (0) delete contents of output directory deleteAllFiles(outputPathFile); trace("PreProcess", "-----------------------------------step (1)", 0); trace( "PreProcess", "(1) copy all contents (including subdirectoies) from src to output directory", 0); // (1) copy all contents (including subdirectoies) from src to output directory copyDirectoryContents(sourcePathFile, outputPathFile); trace("PreProcess", "-----------------------------------step (2)", 0); trace("PreProcess", "(2) get list of '.html' files in this folder", 0); // (2) get list of ".html" files in this folder String[] htmlFileList = findFilesWithSuffix(outputPathFile, SUFFIX_HTML); for (int i = 0; i < htmlFileList.length; i++) { trace("PreProcess", "htmlFileList = " + htmlFileList[i], 0); } trace("PreProcess", "-----------------------------------step (3)", 0); trace("PreProcess", "(3) rename all these files as '.php'", 0); // (3) rename all these files as ".php" renameHTMLtoPHP(outputPathFile, htmlFileList); trace("PreProcess", "-----------------------------------step (4)", 0); trace("PreProcess", "(4) get list of '.php' files in this folder", 0); // (4) get list of ".php" files in this folder String[] phpFileList = findFilesWithSuffix(outputPathFile, SUFFIX_PHP); for (int i = 0; i < phpFileList.length; i++) { trace("PreProcess", "phpFileList = " + phpFileList[i], 0); } trace("PreProcess", "-----------------------------------step (5)", 0); trace("PreProcess", "(5) add PHP INCLUDE line in ALL PHP files in directory", 0); // (5) add PHP INCLUDE line in ALL PHP files in directory addTrackingCode(outputPathFile, phpFileList); trace("PreProcess", "-----------------------------------step (6)", 0); trace( "PreProcess", "(6) replace any references to the old '.html' files to the renamed '.php' files", 0); // (6) replace any references to the old ".html" files to the renamed ".php" files replaceHTMLReferences(outputPathFile, phpFileList, htmlFileList); trace("PreProcess", "-----------------------------------step (7)", 0); trace("PreProcess", "(7) copy user tracking files into output directory", 0); // (7) copy the user tracking files (and their directory) into the output folder copyDirectory(trackingFolderSourcePathFile, trackingFolderDestinationPathFile); trace("PreProcess", "----------------------------------------------------------", 0); trace("PreProcess", "-------------------- end of application ------------------", 0); trace("PreProcess", "----------------------------------------------------------", 0); } // constructor