KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > FileDataSource


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3  */

4 package net.sourceforge.pmd;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10
11 /**
12  * DataSource implementation to read data from a file.
13  */

14 public class FileDataSource implements DataSource {
15     
16     private static final String JavaDoc fileSeparator = System.getProperty("file.separator");
17     
18     private File JavaDoc file;
19
20     /**
21      * @param file the file to read
22      */

23     public FileDataSource(File JavaDoc file) {
24         this.file = file;
25     }
26
27     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
28         return new FileInputStream JavaDoc(file);
29     }
30
31     public String JavaDoc getNiceFileName(boolean shortNames, String JavaDoc inputFileName) {
32         return glomName(shortNames, inputFileName, file);
33     }
34
35     private String JavaDoc glomName(boolean shortNames, String JavaDoc inputFileName, File JavaDoc file) {
36         if (shortNames && inputFileName.indexOf(',') == -1) {
37             if ((new File JavaDoc(inputFileName)).isDirectory()) {
38                 return trimAnyPathSep(file.getAbsolutePath().substring(inputFileName.length()));
39             } else {
40                 if (inputFileName.indexOf(fileSeparator.charAt(0)) == -1) {
41                     return inputFileName;
42                 }
43                 return trimAnyPathSep(inputFileName.substring(inputFileName.lastIndexOf(System.getProperty("file.separator"))));
44             }
45         }
46
47         return file.getAbsolutePath();
48     }
49
50     private String JavaDoc trimAnyPathSep(String JavaDoc name) {
51
52         return name.startsWith(fileSeparator) ?
53             name.substring(1) :
54             name;
55     }
56 }
57
Popular Tags