View Javadoc

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;
7   import java.io.FileInputStream;
8   import java.io.IOException;
9   import java.io.InputStream;
10  
11  /***
12   * DataSource implementation to read data from a file.
13   */
14  public class FileDataSource implements DataSource {
15  	
16  	private static final String fileSeparator = System.getProperty("file.separator");
17  	
18      private File file;
19  
20      /***
21       * @param file the file to read
22       */
23      public FileDataSource(File file) {
24          this.file = file;
25      }
26  
27      public InputStream getInputStream() throws IOException {
28          return new FileInputStream(file);
29      }
30  
31      public String getNiceFileName(boolean shortNames, String inputFileName) {
32          return glomName(shortNames, inputFileName, file);
33      }
34  
35      private String glomName(boolean shortNames, String inputFileName, File file) {
36          if (shortNames && inputFileName.indexOf(',') == -1) {
37              if ((new File(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 trimAnyPathSep(String name) {
51  
52      	return name.startsWith(fileSeparator) ?
53              name.substring(1) :
54              name;
55      }
56  }