Coverage Report - org.codehaus.plexus.resource.loader.FileResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
FileResourceLoader
44 %
11/25
50 %
8/16
8,5
 
 1  
 package org.codehaus.plexus.resource.loader;
 2  
 
 3  
 /*
 4  
  * The MIT License
 5  
  *
 6  
  * Copyright (c) 2004, The Codehaus
 7  
  *
 8  
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 9  
  * this software and associated documentation files (the "Software"), to deal in
 10  
  * the Software without restriction, including without limitation the rights to
 11  
  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 12  
  * of the Software, and to permit persons to whom the Software is furnished to do
 13  
  * so, subject to the following conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be included in all
 16  
  * copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  * SOFTWARE.
 25  
  */
 26  
 
 27  
 import java.io.File;
 28  
 import java.io.IOException;
 29  
 import java.util.Iterator;
 30  
 
 31  
 import org.codehaus.plexus.resource.PlexusResource;
 32  
 import org.codehaus.plexus.resource.loader.AbstractResourceLoader;
 33  
 import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
 34  
 import org.codehaus.plexus.util.FileUtils;
 35  
 
 36  
 /**
 37  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 38  
  * @author Jason van Zyl
 39  
  * @version $Id$
 40  
  * @plexus.component role-hint="file" instantiation-strategy="per-lookup"
 41  
  */
 42  9
 public class FileResourceLoader
 43  
     extends AbstractResourceLoader
 44  
 {
 45  
     public static final String ID = "file";
 46  
 
 47  
     // ----------------------------------------------------------------------
 48  
     // ResourceLoader Implementation
 49  
     // ----------------------------------------------------------------------
 50  
 
 51  
     public PlexusResource getResource( String name )
 52  
         throws ResourceNotFoundException
 53  
     {
 54  17
         for ( Iterator it = paths.iterator(); it.hasNext(); )
 55  
         {
 56  17
             String path = (String) it.next();
 57  
 
 58  17
             final File file = new File( path, name );
 59  
 
 60  17
             if ( file.canRead() )
 61  
             {
 62  8
                 return new FilePlexusResource( file );
 63  
             }
 64  9
         }
 65  9
         File file = new File( name );
 66  9
         if ( file.isAbsolute() && file.canRead() )
 67  
         {
 68  2
             return new FilePlexusResource( file );
 69  
         }
 70  7
         throw new ResourceNotFoundException( name );
 71  
     }
 72  
 
 73  
     /**
 74  
      * @deprecated Use {@link org.codehaus.plexus.resource.ResourceManager#getResourceAsFile(PlexusResource )}.
 75  
      */
 76  
     public static File getResourceAsFile( String name, String outputPath, File outputDirectory )
 77  
         throws FileResourceCreationException
 78  
 
 79  
     {
 80  0
         File f = new File( name );
 81  
 
 82  0
         if ( f.exists() )
 83  
         {
 84  0
             if ( outputPath == null )
 85  
             {
 86  0
                 return f;
 87  
             }
 88  
             else
 89  
             {
 90  
                 try
 91  
                 {
 92  
                     File outputFile;
 93  
 
 94  0
                     if ( outputDirectory != null )
 95  
                     {
 96  0
                         outputFile = new File( outputDirectory, outputPath );
 97  
                     }
 98  
                     else
 99  
                     {
 100  0
                         outputFile = new File( outputPath );
 101  
                     }
 102  
 
 103  0
                     if ( !outputFile.getParentFile().exists() )
 104  
                     {
 105  0
                         outputFile.getParentFile().mkdirs();
 106  
                     }
 107  
 
 108  0
                     FileUtils.copyFile( f, outputFile );
 109  
 
 110  0
                     return outputFile;
 111  
                 }
 112  0
                 catch ( IOException e )
 113  
                 {
 114  0
                     throw new FileResourceCreationException( "Cannot create file-based resource.", e );
 115  
                 }
 116  
             }
 117  
         }
 118  
 
 119  0
         return null;
 120  
     }
 121  
 }