KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > impl > DirectoryCodeBase


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.classfile.impl;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileFilter JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31
32 import edu.umd.cs.findbugs.RecursiveFileSearch;
33 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry;
34 import edu.umd.cs.findbugs.classfile.ICodeBaseIterator;
35 import edu.umd.cs.findbugs.classfile.ICodeBaseLocator;
36 import edu.umd.cs.findbugs.classfile.IScannableCodeBase;
37 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
38
39 /**
40  * IScannableCodeBase implementation to read resources
41  * from a filesystem directory.
42  *
43  * @author David Hovemeyer
44  */

45 public class DirectoryCodeBase extends AbstractScannableCodeBase implements IScannableCodeBase {
46     private class DirectoryCodeBaseIterator implements ICodeBaseIterator {
47
48         Iterator JavaDoc<String JavaDoc> fileNameIterator = rfs.fileNameIterator();
49
50         /* (non-Javadoc)
51          * @see edu.umd.cs.findbugs.classfile.ICodeBaseIterator#hasNext()
52          */

53         public boolean hasNext() throws InterruptedException JavaDoc {
54             return fileNameIterator.hasNext();
55         }
56
57         /* (non-Javadoc)
58          * @see edu.umd.cs.findbugs.classfile.ICodeBaseIterator#next()
59          */

60         public ICodeBaseEntry next() throws InterruptedException JavaDoc {
61             final String JavaDoc fileName = fileNameIterator.next();
62             
63             // Make the filename relative to the directory
64
String JavaDoc resourceName = getResourceName(fileName);
65             
66             // Update last modified time
67
File JavaDoc file = new File JavaDoc(fileName);
68             long modTime = file.lastModified();
69             if (modTime > 0 && modTime > getLastModifiedTime()) {
70                 setLastModifiedTime(modTime);
71             }
72             
73             return new DirectoryCodeBaseEntry(DirectoryCodeBase.this, resourceName);
74         }
75     }
76
77     private File JavaDoc directory;
78     private RecursiveFileSearch rfs;
79     private boolean searchPerformed;
80
81     /**
82      * Constructor.
83      *
84      * @param codeBaseLocator the codebase locator for this codebase
85      * @param directory the filesystem directory
86      */

87     public DirectoryCodeBase(ICodeBaseLocator codeBaseLocator, File JavaDoc directory) {
88         super(codeBaseLocator);
89         if (!directory.isDirectory()) throw new IllegalArgumentException JavaDoc();
90         this.directory = directory;
91         this.rfs = new RecursiveFileSearch(directory.getPath(), new FileFilter JavaDoc(){
92             /* (non-Javadoc)
93              * @see java.io.FileFilter#accept(java.io.File)
94              */

95             public boolean accept(File JavaDoc pathname) {
96                 return true;
97             }
98         });
99         this.searchPerformed = false;
100     }
101
102     /* (non-Javadoc)
103      * @see edu.umd.cs.findbugs.classfile.IScannableCodeBase#iterator()
104      */

105     public ICodeBaseIterator iterator() throws InterruptedException JavaDoc {
106         if (!searchPerformed) {
107             rfs.search();
108             searchPerformed = true;
109         }
110         
111         return new DirectoryCodeBaseIterator();
112     }
113     
114     /* (non-Javadoc)
115      * @see edu.umd.cs.findbugs.classfile.ICodeBase#getPathName()
116      */

117     public String JavaDoc getPathName() {
118         return directory.getPath();
119     }
120     
121     /* (non-Javadoc)
122      * @see edu.umd.cs.findbugs.classfile.ICodeBase#close()
123      */

124     public void close() {
125         // Nothing to do
126
}
127
128     /* (non-Javadoc)
129      * @see edu.umd.cs.findbugs.classfile.ICodeBase#lookupResource(java.lang.String)
130      */

131     public ICodeBaseEntry lookupResource(String JavaDoc resourceName) throws ResourceNotFoundException {
132         // Translate resource name, in case a resource name
133
// has been overridden and the resource is being accessed
134
// using the overridden name.
135
resourceName = translateResourceName(resourceName);
136         
137         File JavaDoc file = getFullPathOfResource(resourceName);
138         if (!file.exists()) {
139             throw new ResourceNotFoundException(resourceName);
140         }
141         return new DirectoryCodeBaseEntry(this, resourceName);
142     }
143     
144     InputStream JavaDoc openFile(String JavaDoc resourceName) throws FileNotFoundException JavaDoc, IOException JavaDoc {
145         File JavaDoc path = getFullPathOfResource(resourceName);
146         return new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(path));
147     }
148
149     /**
150      * Get the full path of given resource.
151      *
152      * @param resourceName
153      * @return
154      */

155     File JavaDoc getFullPathOfResource(String JavaDoc resourceName) {
156         return new File JavaDoc(directory, resourceName);
157     }
158     
159     /**
160      * Get the resource name given a full filename.
161      *
162      * @param fileName the full filename (which must be inside the directory)
163      * @return the resource name (i.e., the filename with the directory stripped off)
164      */

165     String JavaDoc getResourceName(String JavaDoc fileName) {
166         // FIXME: there is probably a more robust way to do this
167

168         // Strip off the directory part.
169
String JavaDoc dirPath = directory.getPath();
170         if (!fileName.startsWith(dirPath)) {
171             throw new IllegalStateException JavaDoc("Filename " + fileName + " not inside directory "+ dirPath);
172         }
173         
174         // The problem here is that we need to take the relative part of the filename
175
// and break it into components that we can then reconstruct into
176
// a resource name (using '/' characters to separate the components).
177
// Unfortunately, the File class does not make this task particularly easy.
178

179         String JavaDoc relativeFileName = fileName.substring(dirPath.length());
180         File JavaDoc file = new File JavaDoc(relativeFileName);
181         LinkedList JavaDoc<String JavaDoc> partList = new LinkedList JavaDoc<String JavaDoc>();
182         do {
183             partList.addFirst(file.getName());
184         } while ((file = file.getParentFile()) != null);
185         
186         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
187         for (String JavaDoc part : partList) {
188             if (buf.length() > 0) {
189                 buf.append('/');
190             }
191             buf.append(part);
192         }
193         
194         return buf.toString();
195     }
196     
197     /* (non-Javadoc)
198      * @see java.lang.Object#toString()
199      */

200     @Override JavaDoc
201     public String JavaDoc toString() {
202         return directory.getPath();
203     }
204 }
205
Popular Tags