Coverage Report - org.apache.commons.classscan.builtin.DefaultClassPathElementFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultClassPathElementFactory
0%
0/19
0%
0/8
6
 
 1  
 /*
 2  
  * Licensed under the Apache License, Version 2.0 (the "License");
 3  
  * you may not use this file except in compliance with the License.
 4  
  * You may obtain a copy of the License at
 5  
  *
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  *
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package org.apache.commons.classscan.builtin;
 15  
 
 16  
 import java.io.File;
 17  
 import java.io.FileNotFoundException;
 18  
 import java.io.IOException;
 19  
 import java.net.URI;
 20  
 import java.net.URISyntaxException;
 21  
 import java.net.URL;
 22  
 
 23  
 import org.apache.commons.classscan.MetaRegistry;
 24  
 import org.apache.commons.classscan.spi.ClassPathElementFactory;
 25  
 import org.apache.commons.classscan.spi.model.SpiClassPathElement;
 26  
 
 27  0
 public class DefaultClassPathElementFactory implements ClassPathElementFactory {
 28  
 
 29  
         @Override
 30  
         public SpiClassPathElement getClassPathElement(MetaRegistry metaRegistry, URL url) throws IOException {
 31  0
                 String scheme= url.getProtocol();
 32  0
                 if(scheme.equals("file")) {
 33  
                         try {
 34  0
                                 return createFileClassPathElement(url);
 35  0
                         } catch (URISyntaxException e) {
 36  0
                                 FileNotFoundException ex = new FileNotFoundException(url.toExternalForm()+" not found");
 37  0
                                 ex.initCause(e);
 38  0
                                 throw ex;
 39  
                         }
 40  
                 }
 41  0
                 else if(scheme.equals("jar")) {
 42  0
                     return new JarClassPathElement(url);                        
 43  
                 }
 44  
                 
 45  0
                 return null;
 46  
         }
 47  
         
 48  
     private SpiClassPathElement createFileClassPathElement(URL url) throws IOException, URISyntaxException {
 49  0
             File location = new File(url.toURI());
 50  0
             if (!location.canRead()) {
 51  0
                     throw new FileNotFoundException(location + " can not be read");
 52  
             }
 53  
         
 54  0
             if (location.isDirectory()) {
 55  0
                     return new FileClassPathElement(location);
 56  
             }
 57  
 
 58  0
             URI fileUri = location.toURI();
 59  0
                 URI jarURI = new URI("jar:" + fileUri.toString() + "!/");
 60  0
             return new JarClassPathElement(jarURI.toURL());
 61  
     }
 62  
 }