Coverage report

  %line %branch
org.apache.jetspeed.util.DirectoryHelper
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.util;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileFilter;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.FileOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.nio.channels.FileChannel;
 25  
 
 26  
 /**
 27  
  * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
 28  
  * @version $Id: DirectoryHelper.java 516448 2007-03-09 16:25:47Z ate $
 29  
  */
 30  
 public class DirectoryHelper
 31  
     extends
 32  
         AbstractFileSystemHelper
 33  
     implements 
 34  
         FileSystemHelper
 35  
 {
 36  
 
 37  
     protected File directory;
 38  
     /**
 39  
      * 
 40  
      */
 41  
     public DirectoryHelper(File directory)
 42  
     {
 43  0
         super();
 44  0
         if(!directory.exists())
 45  
         {
 46  0
             directory.mkdirs();
 47  
         }
 48  
         
 49  0
         if(!directory.isDirectory())
 50  
         {
 51  0
             throw new IllegalArgumentException("DirectoryHelper(File) requires directory not a file.");
 52  
         }
 53  0
         this.directory = directory;
 54  
         
 55  
 
 56  
         
 57  0
     }
 58  
 
 59  
     /**
 60  
      * <p>
 61  
      * copyFrom
 62  
      * </p>
 63  
      *
 64  
      * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File)
 65  
      * @param directory
 66  
      * @throws IOException
 67  
      */
 68  
     public void copyFrom( File srcDirectory ) throws IOException
 69  
     {
 70  0
         copyFrom(srcDirectory, new FileFilter() {
 71  
             public boolean accept(File pathname)
 72  
             {
 73  
                return true;
 74  
             }
 75  
            });
 76  0
     }
 77  
     
 78  
     /**
 79  
      * <p>
 80  
      * copyFrom
 81  
      * </p>
 82  
      *
 83  
      * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File, java.io.FileFilter)
 84  
      * @param directory
 85  
      * @param fileFilter
 86  
      * @throws IOException
 87  
      */
 88  
     public void copyFrom( File srcDirectory, FileFilter fileFilter ) throws IOException
 89  
     {
 90  0
         if(!srcDirectory.isDirectory())
 91  
         {
 92  0
             throw new IllegalArgumentException("DirectoryHelper.copyFrom(File) requires directory not a file.");
 93  
         }
 94  0
         copyFiles(srcDirectory, directory, fileFilter);        
 95  
 
 96  0
     }
 97  
     /**
 98  
      * 
 99  
      * <p>
 100  
      * copyFiles
 101  
      * </p>
 102  
      *
 103  
      * @param srcDir Source directory to copy from.
 104  
      * @param dstDir Destination directory to copy to.
 105  
      * @throws IOException
 106  
      * @throws FileNotFoundException
 107  
 
 108  
      */
 109  
     protected void copyFiles(File srcDir, File dstDir, FileFilter fileFilter) throws IOException
 110  
     {
 111  0
         FileChannel srcChannel = null;
 112  0
         FileChannel dstChannel = null;
 113  
 
 114  
         try
 115  
         {
 116  0
         File[] children = srcDir.listFiles(fileFilter);
 117  0
         for(int i=0; i<children.length; i++)
 118  
         {
 119  0
             File child = children[i];
 120  0
             if(child.isFile())
 121  
             {
 122  0
                 File toFile = new File(dstDir, child.getName());
 123  0
                 toFile.createNewFile();
 124  0
                 srcChannel = new FileInputStream(child).getChannel();
 125  0
                 dstChannel = new FileOutputStream(toFile).getChannel();
 126  0
                 dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
 127  0
                 srcChannel.close();
 128  0
                 dstChannel.close();
 129  0
             }
 130  
             else
 131  
             {
 132  0
                 File newSubDir = new File(dstDir, child.getName());
 133  0
                 newSubDir.mkdir();
 134  0
                 copyFiles(child, newSubDir, fileFilter);
 135  
             }
 136  
         }
 137  
         }
 138  
         finally
 139  
         {
 140  0
             if ( srcChannel != null && srcChannel.isOpen() )
 141  
             {
 142  
                 try
 143  
                 {
 144  0
                     srcChannel.close();
 145  
                 }
 146  0
                 catch (Exception e)
 147  
                 {
 148  
                     
 149  0
                 }
 150  
             }
 151  0
             if ( dstChannel != null && dstChannel.isOpen() )
 152  
             {
 153  
                 try
 154  
                 {
 155  0
                     dstChannel.close();
 156  
                 }
 157  0
                 catch (Exception e)
 158  
                 {
 159  
                     
 160  0
                 }
 161  
             }
 162  
         }
 163  0
     }
 164  
 
 165  
     /**
 166  
      * <p>
 167  
      * remove
 168  
      * </p>
 169  
      *
 170  
      * @see org.apache.jetspeed.util.FileSystemHelper#remove()
 171  
      * 
 172  
      */
 173  
     public boolean remove()
 174  
     {
 175  0
         return doRemove(directory);
 176  
     }
 177  
     
 178  
     /**
 179  
      * 
 180  
      * <p>
 181  
      * doRemove
 182  
      * </p>
 183  
      *
 184  
      * @param file
 185  
      * @return <code>true</code> if the removal war successful, otherwise returns
 186  
      * <code>false</code>.
 187  
      */
 188  
     protected boolean doRemove(File file)
 189  
     {
 190  0
         if (file.isDirectory())
 191  
 		{
 192  0
 			String[] children = file.list();
 193  0
 			for (int i = 0; i < children.length; i++)
 194  
 			{
 195  0
 				boolean success = doRemove(new File(file, children[i]));
 196  0
 				if (!success)
 197  
 				{
 198  0
 					return false;
 199  
 				}
 200  
 			}
 201  
 		}
 202  
 
 203  
 		// The directory is now empty so delete it OR it is a plain file
 204  0
 		return file.delete();        
 205  
     }
 206  
 
 207  
     /**
 208  
      * <p>
 209  
      * getRootDirectory
 210  
      * </p>
 211  
      *
 212  
      * @see org.apache.jetspeed.util.FileSystemHelper#getRootDirectory()
 213  
      * @return
 214  
      */
 215  
     public File getRootDirectory()
 216  
     {       
 217  0
         return directory;
 218  
     }
 219  
 
 220  
     /**
 221  
      * <p>
 222  
      * close
 223  
      * </p>
 224  
      *
 225  
      * @see org.apache.jetspeed.util.FileSystemHelper#close()
 226  
      * 
 227  
      */
 228  
     public void close()
 229  
     {
 230  
         // TODO Auto-generated method stub
 231  
 
 232  0
     }
 233  
     /**
 234  
      * <p>
 235  
      * getSourcePath
 236  
      * </p>
 237  
      *
 238  
      * @see org.apache.jetspeed.util.FileSystemHelper#getSourcePath()
 239  
      * @return
 240  
      */
 241  
     public String getSourcePath()
 242  
     {       
 243  0
         return getRootDirectory().getAbsolutePath();
 244  
     }
 245  
         
 246  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.