Coverage Report - com.aurea.maven.plugins.sonic.utils.FileUtilities
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtilities
0%
0/80
0%
0/34
4.571
 
 1  
 package com.aurea.maven.plugins.sonic.utils;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.FileOutputStream;
 6  
 import java.io.IOException;
 7  
 import java.util.ArrayList;
 8  
 import java.util.List;
 9  
 
 10  
 import org.codehaus.plexus.util.DirectoryScanner;
 11  
 import org.codehaus.plexus.util.FileUtils;
 12  
 
 13  
 /**
 14  
  *
 15  
  */
 16  0
 public class FileUtilities {
 17  
 
 18  
         /**
 19  
          * cleanup method to delete all files in a basedir, flag to include
 20  
          * directories, optional filter to specify exclusion filters e.g. .svn, .*
 21  
          * etc
 22  
          * 
 23  
          * @param _baseDir
 24  
          *            base directory to start scanning for files
 25  
          * @param _includes
 26  
          *            inclusion filter to select files for deletion (default **\/* )
 27  
          * @param _excludes
 28  
          *            exclusion filter to select files to be prevented from deletion
 29  
          * */
 30  
         public static void deleteFilesInDir(final String _baseDir, final String _includes, final String _excludes) {
 31  
                 //System.out.println(">>> deleteFilesInDir START");
 32  0
                 File dir = new File(_baseDir);
 33  0
                 if (!dir.exists() && !dir.isDirectory()) {
 34  0
                         return;
 35  
                 }
 36  
 
 37  
                 //System.out.println(">>> deleteFilesInDir baseDir = "+_baseDir);
 38  0
                 for (String file : getFileList(_baseDir, _includes, _excludes)) {
 39  
                         // delete selected file
 40  
                         // TODO: add some debug logging to see what files got deleted
 41  0
                         new File(_baseDir, file).delete();
 42  
                         //System.out.println(">>> deleteFilesInDir file = "+file);
 43  
                 }
 44  
                 //System.out.println(">>> deleteFilesInDir END");
 45  0
         }
 46  
 
 47  
         /**
 48  
          * @param _baseDir
 49  
          *            _baseDir
 50  
          * @param _targetBase
 51  
          *            _targetBase
 52  
          * @throws Exception
 53  
          *             Exception
 54  
          */
 55  
         public static void copyFiles(final String _baseDir, final String _targetBase) throws Exception {
 56  
 
 57  0
                 copyFiles(_baseDir, _targetBase, null, null, null, false);
 58  0
         }
 59  
 
 60  
         /**
 61  
          * @param _baseDir
 62  
          *            source directory for the copy process
 63  
          * @param _targetBase
 64  
          *            base target directory directory for the copy process
 65  
          * @param _includes
 66  
          *            files to be included in the copy
 67  
          * @param _excludes
 68  
          *            files to be excluded in the copy
 69  
          * @param _targetDirs
 70  
          *            a list of target directories relative to the _targetBase for
 71  
          *            the copy process
 72  
          * @param _flatten
 73  
          *            flatten the file structure, putting all files in the target
 74  
          *            directory
 75  
          * @throws Exception
 76  
          *             if something goes wrong
 77  
          */
 78  
         public static void copyFiles(final String _baseDir, final String _targetBase, final String _includes,
 79  
                         final String _excludes, final String _targetDirs, final boolean _flatten) throws Exception {
 80  
 
 81  
                 //System.out.println(">>> copyFiles START");
 82  0
                 File dir = new File(_baseDir);
 83  0
                 if (!dir.exists() && !dir.isDirectory()) {
 84  0
                         return;
 85  
                 }
 86  0
                 File targetBaseDir = new File(_targetBase);
 87  
 
 88  0
                 String[] targetDirList = null;
 89  0
                 if (_targetDirs == null) {
 90  0
                         targetDirList = new String[] { "" };
 91  
                 } else {
 92  0
                         targetDirList = _targetDirs.split(",");
 93  
                 }
 94  
 
 95  0
                 File srcFile = null;
 96  0
                 File destFile = null;
 97  
 
 98  
                 //System.out.println(">>>copyFiles basedir = " + _baseDir);
 99  
                 //System.out.println(">>>copyFiles targetDir = " + _targetBase);
 100  
 
 101  0
                 for (String targetDirName : targetDirList) {
 102  0
                         File targetDir = new File(targetBaseDir, targetDirName);
 103  0
                         String[] files = getFileList(_baseDir, _includes, _excludes);
 104  0
                         if (files.length > 0 || targetDir.getPath().contains("classes")) {
 105  
                                 // avoid empty directories
 106  0
                                 targetDir.mkdirs();
 107  
 
 108  0
                                 for (String file : files) {
 109  
                                         //System.out.println(">>>copyFiles file = " + file);
 110  0
                                         srcFile = new File(_baseDir, file);
 111  0
                                         if (_flatten) {
 112  0
                                                 destFile = new File(targetDir, srcFile.getName());
 113  
                                         } else {
 114  0
                                                 destFile = new File(targetDir, file);
 115  0
                                                 destFile.getParentFile().mkdirs();
 116  
                                         }
 117  
                                         try {
 118  0
                                                 FileUtils.copyFile(srcFile, destFile);
 119  0
                                         } catch (IOException ioe) {
 120  0
                                                 throw new Exception("Error copying [" + srcFile + "->" + destFile + "]", ioe);
 121  0
                                         }
 122  
                                 }
 123  
                         }
 124  
                 }
 125  
                 //System.out.println(">>> copyFiles END");
 126  0
         }
 127  
 
 128  
         /**
 129  
          * @param _baseDir
 130  
          *            base directory for the generating the file list
 131  
          * @param _includes
 132  
          *            comma separated list of files to be included in the result
 133  
          *            list
 134  
          * @param _excludes
 135  
          *            comma separated list of files to be excluded from the result
 136  
          *            list
 137  
          * @return a list of matching files. Returns an empty array if _baseDir
 138  
          *         doesn't exist.
 139  
          */
 140  
         public static String[] getFileList(final String _baseDir, final String _includes, final String _excludes) {
 141  
 
 142  0
                 String[] result = new String[] {};
 143  
                 //System.out.println(">> getFileList START");
 144  
                 // if the search location doesn't exist, return nothing
 145  0
                 if (!(new File(_baseDir).exists())) {
 146  0
                         return result;
 147  
                 }
 148  
 
 149  
                 try {
 150  0
                         DirectoryScanner scanner = new DirectoryScanner();
 151  0
                         scanner.setBasedir(_baseDir);
 152  0
                         scanner.addDefaultExcludes();
 153  0
                         if (_includes != null) {
 154  0
                                 scanner.setIncludes(_includes.split(","));
 155  
                         } else {
 156  0
                                 scanner.setIncludes(new String[] { "**/*" });
 157  
                         }
 158  0
                         if (_excludes != null) {
 159  0
                                 List<String> excludeList = new ArrayList<String>();
 160  0
                                 for (String exclude : DirectoryScanner.DEFAULTEXCLUDES) {
 161  0
                                         excludeList.add(exclude);
 162  
                                 }
 163  0
                                 for (String exclude : _excludes.split(",")) {
 164  0
                                         excludeList.add(exclude);
 165  
                                 }
 166  0
                                 scanner.setExcludes(excludeList.toArray(new String[excludeList.size()]));
 167  0
                         } else {
 168  0
                                 scanner.addDefaultExcludes();
 169  
                         }
 170  
 
 171  0
                         scanner.scan();
 172  0
                         result = scanner.getIncludedFiles();
 173  
                         //System.out.println(">> getFileList result = " + result.length);
 174  
                         // String[] debug = scanner.getExcludedDirectories();
 175  
                         // debug = scanner.getExcludedFiles();
 176  
 
 177  0
                 } catch (Exception e) {
 178  0
                         e.printStackTrace();
 179  0
                 }
 180  
                 //System.out.println(">> getFileList END");
 181  0
                 return result;
 182  
         }
 183  
 
 184  
         /**
 185  
          * writes a byte[] array to a file.
 186  
          * 
 187  
          * @param _content
 188  
          *            contents to be written to the file
 189  
          * @param _fileName
 190  
          *            file to write to
 191  
          */
 192  
         public static void writeFile(final byte[] _content, final String _fileName) {
 193  
 
 194  0
                 final File outputFile = new File(_fileName);
 195  0
                 outputFile.getParentFile().mkdirs();
 196  
 
 197  0
                 writeFile(_content, outputFile);
 198  0
         }
 199  
 
 200  
         /**
 201  
          * writes a byte[] array to a file.
 202  
          * 
 203  
          * @param _content
 204  
          *            contents to be written to the file
 205  
          * @param _file
 206  
          *            file to write to
 207  
          */
 208  
         public static void writeFile(final byte[] _content, final File _file) {
 209  
 
 210  0
                 FileOutputStream fos = null;
 211  
 
 212  
                 try {
 213  0
                         fos = new FileOutputStream(_file);
 214  0
                         fos.write(_content);
 215  0
                         fos.close();
 216  
 
 217  0
                 } catch (IOException ioe) {
 218  
                         // TODO: do something here
 219  
                 } finally {
 220  0
                         if (fos != null) {
 221  
                                 try {
 222  0
                                         fos.close();
 223  0
                                 } catch (IOException ioe) {
 224  
                                         // ignore
 225  0
                                 }
 226  
                         }
 227  
                 }
 228  0
         }
 229  
 
 230  
         /**
 231  
          * returns the contents of a file as a byte[] array.
 232  
          * 
 233  
          * @param _fileName
 234  
          *            name of the file to be read
 235  
          * @return contents of the file
 236  
          * @throws IOException
 237  
          *             if the file is unavailable
 238  
          */
 239  
         public static byte[] readFile(final String _fileName) throws IOException {
 240  
 
 241  0
                 FileInputStream fis = null;
 242  
 
 243  0
                 File file = new File(_fileName);
 244  0
                 fis = new FileInputStream(file);
 245  
 
 246  0
                 byte[] fileContents = new byte[(int) file.length()];
 247  
 
 248  0
                 fis.read(fileContents);
 249  0
                 fis.close();
 250  
 
 251  0
                 return fileContents;
 252  
         }
 253  
 }