Coverage Report - org.apache.commons.classscan.builtin.FileClassPathElement
 
Classes in this File Line Coverage Branch Coverage Complexity
FileClassPathElement
0%
0/11
0%
0/2
2.556
FileClassPathElement$1
0%
0/18
0%
0/12
2.556
FileClassPathElement$FileCursor
0%
0/15
0%
0/4
2.556
 
 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 CODITIONS 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  
 
 15  
 package org.apache.commons.classscan.builtin;
 16  
 
 17  
 import java.io.File;
 18  
 import java.util.Collection;
 19  
 import java.util.Iterator;
 20  
 
 21  
 import org.apache.commons.classscan.ClassFile;
 22  
 import org.apache.commons.classscan.ResourceFile;
 23  
 import org.apache.commons.classscan.spi.model.SpiClassPathElement;
 24  
 import org.apache.commons.classscan.spi.model.SpiMetaRegistry;
 25  
 
 26  0
 public class FileClassPathElement implements SpiClassPathElement {
 27  
 
 28  
         private final File baseLocation;
 29  
         
 30  0
         FileClassPathElement(File baseLocation) {
 31  0
                 this.baseLocation= baseLocation;
 32  0
         }
 33  
         
 34  
         private static class FileCursor {
 35  
                 final private FileCursor prior;
 36  
                 final private File[] files;
 37  
                 private int fileIdx;
 38  
                 private final int priorLength;
 39  
                 private final StringBuilder sb;
 40  
         
 41  0
                 FileCursor(FileCursor prior, File dir, StringBuilder sb) {
 42  0
                         this.prior = prior;
 43  0
                         files= dir.listFiles();
 44  0
                         this.sb = sb;
 45  0
                         if(prior!=null) {
 46  0
                                 sb.append('/');
 47  
                         }
 48  0
                         priorLength = sb.length();
 49  0
                 }
 50  
 
 51  
                 File nextFile() {
 52  0
                         if(fileIdx==files.length) {
 53  0
                                 return null;
 54  
                         }
 55  0
                         File file = files[fileIdx++];
 56  0
                         sb.setLength(priorLength);
 57  0
                         sb.append(file.getName());
 58  0
                         return file;
 59  
                 }
 60  
                 
 61  
                 FileCursor pop() {
 62  0
                         return prior;
 63  
                 }
 64  
         }
 65  
         
 66  
         @Override
 67  
         public Iterator<ClassFile> iterator() {        
 68  
 
 69  0
                 return new ClassFileIterator() {
 70  0
                         StringBuilder sb = new StringBuilder();
 71  0
                         FileCursor cursor= new FileCursor(null, baseLocation, sb);
 72  
                         
 73  
                         ClassFile getNext() {
 74  
                                 for(;;) {
 75  0
                                         File file = cursor.nextFile();
 76  0
                                         if(file == null) {
 77  0
                                                 cursor = cursor.pop();
 78  0
                                                 if(cursor==null) {
 79  0
                                                         return null;
 80  
                                                 }
 81  
                                                 continue;
 82  
                                         }
 83  
                                         
 84  0
                                         String path = sb.toString();
 85  0
                                         if(file.isDirectory()) {
 86  0
                                                 String fileName = file.getName();
 87  0
                                                 if( ClassNameHelper.isValidIdentifier(fileName) ) {
 88  0
                                                         cursor= new FileCursor(cursor, file, sb);
 89  
                                                 }
 90  
                                                 continue;
 91  
                                         }
 92  
                                         
 93  0
                                         if(file.canRead()) {
 94  0
                                                 String className= ClassNameHelper.getClassNameFromFileName(file.getName());
 95  0
                                                 if(className!=null) {
 96  0
                                                         return new FileClassFile(file, path);
 97  
                                                 }
 98  
                                                 continue;
 99  
                                         }
 100  0
                                 }
 101  
                         }
 102  
                 };
 103  
         }
 104  
 
 105  
         @Override
 106  
         public String getLocation() {
 107  0
                 return baseLocation.toURI().toString();
 108  
         }
 109  
 
 110  
         @Override
 111  
         public Collection<SpiClassPathElement> getAdditionalLocations(SpiMetaRegistry registry) {
 112  0
                 return null;
 113  
         }
 114  
 
 115  
         @Override
 116  
         public ResourceFile getResource(String fileName) {
 117  0
                 File file= new File(baseLocation, fileName);
 118  0
                 if(file.isFile()) {
 119  0
                         return new FileResourceFile(file, fileName);
 120  
                 }
 121  0
                 return null;
 122  
         }
 123  
 }